From 9cb495dc14f101a98f9c0c17803cb4cf44615057 Mon Sep 17 00:00:00 2001 From: Don Corley Date: Tue, 5 Jun 2012 15:02:45 -0700 Subject: [PATCH 2/2] Add repository filter --- .../karaf/cave/server/api/CaveRepository.java | 6 +- .../server/command/PopulateRepositoryCommand.java | 5 +- .../server/command/ProxyRepositoryCommand.java | 5 +- .../cave/server/storage/CaveRepositoryImpl.java | 112 +++++++++++--------- 4 files changed, 75 insertions(+), 53 deletions(-) diff --git a/server/api/src/main/java/org/apache/karaf/cave/server/api/CaveRepository.java b/server/api/src/main/java/org/apache/karaf/cave/server/api/CaveRepository.java index ce68dd1..65b5415 100644 --- a/server/api/src/main/java/org/apache/karaf/cave/server/api/CaveRepository.java +++ b/server/api/src/main/java/org/apache/karaf/cave/server/api/CaveRepository.java @@ -85,18 +85,20 @@ public abstract class CaveRepository { * Proxy an URL (for instance a Maven repository) and add OBR information. * * @param url the URL to proxy. + * @param filter regex URL include filter. * @throws Exception */ - public abstract void proxy(URL url) throws Exception; + public abstract void proxy(URL url, String filter) throws Exception; /** * Populate from a remote URL (for instance a Maven repository), and eventually update the OBR information. * * @param url the URL to copy. + * @param filter regex URL include filter. * @param update if true the OBR information is updated, false else. * @throws Exception in case of copy failure. */ - public abstract void populate(URL url, boolean update) throws Exception; + public abstract void populate(URL url, String filter, boolean update) throws Exception; /** * Return the output stream of the resource at the given URI. diff --git a/server/command/src/main/java/org/apache/karaf/cave/server/command/PopulateRepositoryCommand.java b/server/command/src/main/java/org/apache/karaf/cave/server/command/PopulateRepositoryCommand.java index 8651b5f..1dbeeee 100644 --- a/server/command/src/main/java/org/apache/karaf/cave/server/command/PopulateRepositoryCommand.java +++ b/server/command/src/main/java/org/apache/karaf/cave/server/command/PopulateRepositoryCommand.java @@ -32,6 +32,9 @@ public class PopulateRepositoryCommand extends CaveRepositoryCommandSupport { @Option(name = "-nu", aliases = { "--no-update" }, description = "Not update the OBR metadata", required = false, multiValued = false) boolean noUpdate = false; + @Option(name = "-f", aliases = {"--filter"}, description = "Regex URL include filter", required = false, multiValued = false) + String filter; + @Argument(index = 0, name = "name", description = "The name of the Karaf Cave repository", required = true, multiValued = false) String name = null; @@ -40,7 +43,7 @@ public class PopulateRepositoryCommand extends CaveRepositoryCommandSupport { protected Object doExecute() throws Exception { CaveRepository repository = getExistingRepository(name); - repository.populate(new URL(url), !noUpdate); + repository.populate(new URL(url), filter, !noUpdate); if (!noUpdate) { getCaveRepositoryService().register(name); } diff --git a/server/command/src/main/java/org/apache/karaf/cave/server/command/ProxyRepositoryCommand.java b/server/command/src/main/java/org/apache/karaf/cave/server/command/ProxyRepositoryCommand.java index 66cfc6b..0a1b94a 100644 --- a/server/command/src/main/java/org/apache/karaf/cave/server/command/ProxyRepositoryCommand.java +++ b/server/command/src/main/java/org/apache/karaf/cave/server/command/ProxyRepositoryCommand.java @@ -38,9 +38,12 @@ public class ProxyRepositoryCommand extends CaveRepositoryCommandSupport { @Option(name = "-nu", aliases = { "--no-update", "--no-refresh", "--no-register" }, description = "No refresh of the OBR URLs", required = false, multiValued = false) boolean noUpdate = false; + @Option(name = "-f", aliases = {"--filter"}, description = "Regex URL include filter", required = false, multiValued = false) + String filter; + protected Object doExecute() throws Exception { CaveRepository repository = getExistingRepository(name); - repository.proxy(new URL(url)); + repository.proxy(new URL(url), filter); if (!noUpdate) { getCaveRepositoryService().register(name); } diff --git a/server/storage/src/main/java/org/apache/karaf/cave/server/storage/CaveRepositoryImpl.java b/server/storage/src/main/java/org/apache/karaf/cave/server/storage/CaveRepositoryImpl.java index 3398c23..38a4df2 100644 --- a/server/storage/src/main/java/org/apache/karaf/cave/server/storage/CaveRepositoryImpl.java +++ b/server/storage/src/main/java/org/apache/karaf/cave/server/storage/CaveRepositoryImpl.java @@ -181,17 +181,18 @@ public class CaveRepositoryImpl extends CaveRepository { * Proxy an URL (by adding repository.xml OBR information) in the Karaf Cave repository. * * @param url the URL to proxyFilesystem. the URL to proxyFilesystem. + * @param filter regex URL include filter. * @throws Exception */ - public void proxy(URL url) throws Exception { + public void proxy(URL url, String filter) throws Exception { if (url.getProtocol().equals("file")) { // filesystem proxyFilesystem (to another folder) File proxyFolder = new File(url.toURI()); - this.proxyFilesystem(proxyFolder); + this.proxyFilesystem(proxyFolder, filter); } if (url.getProtocol().equals("http")) { // HTTP proxyFilesystem - this.proxyHttp(url.toExternalForm()); + this.proxyHttp(url.toExternalForm(), filter); } this.generateRepositoryXml(); } @@ -199,21 +200,24 @@ public class CaveRepositoryImpl extends CaveRepository { /** * Proxy a local filesystem (folder). * @param entry the filesystem to proxyFilesystem. + * @param filter regex URL include filter. * @throws Exception in case of proxyFilesystem failure */ - private void proxyFilesystem(File entry) throws Exception { + private void proxyFilesystem(File entry, String filter) throws Exception { LOGGER.debug("Proxying filesystem {}", entry.getAbsolutePath()); if (entry.isDirectory()) { File[] children = entry.listFiles(); for (int i = 0; i < children.length; i++) { - proxyFilesystem(children[i]); + proxyFilesystem(children[i], filter); } } else { try { - Resource resource = new DataModelHelperImpl().createResource(entry.toURI().toURL()); - if (resource != null) { - obrRepository.addResource(resource); - obrRepository.setLastModified(System.currentTimeMillis()); + if ((filter == null) || (entry.toURI().toURL().toString().matches(filter))) { + Resource resource = new DataModelHelperImpl().createResource(entry.toURI().toURL()); + if (resource != null) { + obrRepository.addResource(resource); + obrRepository.setLastModified(System.currentTimeMillis()); + } } } catch (IllegalArgumentException e) { LOGGER.warn(e.getMessage()); @@ -225,9 +229,10 @@ public class CaveRepositoryImpl extends CaveRepository { * Proxy a HTTP URL locally. * * @param url the HTTP URL to proxy. + * @param filter regex URL include filter. * @throws Exception in case of proxy failure. */ - private void proxyHttp(String url) throws Exception { + private void proxyHttp(String url, String filter) throws Exception { LOGGER.debug("Proxying HTTP URL {}", url); HttpClient httpClient = new DefaultHttpClient(); @@ -240,10 +245,12 @@ public class CaveRepositoryImpl extends CaveRepository { || entity.getContentType().getValue().equals("application/octet-stream")) { // I have a jar/binary, potentially a resource try { - Resource resource = new DataModelHelperImpl().createResource(new URL(url)); - if (resource != null) { - obrRepository.addResource(resource); - obrRepository.setLastModified(System.currentTimeMillis()); + if ((filter == null) || (url.matches(filter))) { + Resource resource = new DataModelHelperImpl().createResource(new URL(url)); + if (resource != null) { + obrRepository.addResource(resource); + obrRepository.setLastModified(System.currentTimeMillis()); + } } } catch (IllegalArgumentException e) { LOGGER.warn(e.getMessage()); @@ -257,7 +264,7 @@ public class CaveRepositoryImpl extends CaveRepository { for (int i = 1; i < links.size(); i++) { Element link = links.get(i); String absoluteHref = link.attr("abs:href"); - this.proxyHttp(absoluteHref); + this.proxyHttp(absoluteHref, filter); } } } @@ -268,18 +275,19 @@ public class CaveRepositoryImpl extends CaveRepository { * Populate an URL into the Karaf Cave repository, and eventually update the OBR information. * * @param url the URL to copy. + * @param filter regex URL include filter. * @param update if true the OBR information is updated, false else. * @throws Exception in case of populate failure. */ - public void populate(URL url, boolean update) throws Exception { + public void populate(URL url, String filter, boolean update) throws Exception { if (url.getProtocol().equals("file")) { // populate the Karaf Cave repository from a filesystem folder File populateFolder = new File(url.toURI()); - this.populateFromFilesystem(populateFolder, update); + this.populateFromFilesystem(populateFolder, filter, update); } if (url.getProtocol().equals("http")) { // populate the Karaf Cave repository from a HTTP URL - this.populateFromHttp(url.toExternalForm(), update); + this.populateFromHttp(url.toExternalForm(), filter, update); } if (update) { this.generateRepositoryXml(); @@ -291,28 +299,31 @@ public class CaveRepositoryImpl extends CaveRepository { * * @param filesystem the "source" directory. * @param update if true, the resources are added into the OBR metadata, false else. + * @param filter regex URL include filter * @throws Exception in case of populate failure. */ - private void populateFromFilesystem(File filesystem, boolean update) throws Exception { + private void populateFromFilesystem(File filesystem, String filter, boolean update) throws Exception { LOGGER.debug("Populating from filesystem {}", filesystem.getAbsolutePath()); if (filesystem.isDirectory()) { File[] children = filesystem.listFiles(); for (int i = 0; i < children.length; i++) { - populateFromFilesystem(children[i], update); + populateFromFilesystem(children[i], filter, update); } } else { try { - ResourceImpl resource = (ResourceImpl) new DataModelHelperImpl().createResource(filesystem.toURI().toURL()); - if (resource != null) { - // copy the resource - File destination = new File(new File(this.getLocation()), filesystem.getName()); - LOGGER.debug("Copy from {} to {}", filesystem.getAbsolutePath(), destination.getAbsolutePath()); - FileUtils.copyFile(filesystem, destination); - if (update) { - resource = (ResourceImpl) new DataModelHelperImpl().createResource(destination.toURI().toURL()); - LOGGER.debug("Update the OBR metadata with {}", resource.getId()); - this.addResource(resource); - } + if ((filter == null) || (filesystem.toURI().toURL().toString().matches(filter))) { + ResourceImpl resource = (ResourceImpl) new DataModelHelperImpl().createResource(filesystem.toURI().toURL()); + if (resource != null) { + // copy the resource + File destination = new File(new File(this.getLocation()), filesystem.getName()); + LOGGER.debug("Copy from {} to {}", filesystem.getAbsolutePath(), destination.getAbsolutePath()); + FileUtils.copyFile(filesystem, destination); + if (update) { + resource = (ResourceImpl) new DataModelHelperImpl().createResource(destination.toURI().toURL()); + LOGGER.debug("Update the OBR metadata with {}", resource.getId()); + this.addResource(resource); + } + } } } catch (IllegalArgumentException e) { LOGGER.warn(e.getMessage()); @@ -324,10 +335,11 @@ public class CaveRepositoryImpl extends CaveRepository { * Populate the Karaf Cave repository using the given URL. * * @param url the "source" HTTP URL. + * @param filter regex URL include filter * @param update true if the OBR metadata should be updated, false else. * @throws Exception in case of populate failure. */ - private void populateFromHttp(String url, boolean update) throws Exception { + private void populateFromHttp(String url, String filter, boolean update) throws Exception { LOGGER.debug("Populating from HTTP URL {}", url); HttpClient httpClient = new DefaultHttpClient(); @@ -340,22 +352,24 @@ public class CaveRepositoryImpl extends CaveRepository { || entity.getContentType().getValue().equals("application/octet-stream")) { // I have a jar/binary, potentially a resource try { - ResourceImpl resource = (ResourceImpl) new DataModelHelperImpl().createResource(new URL(url)); - if (resource != null) { - LOGGER.debug("Copy {} into the Karaf Cave repository storage", url); - int index = url.lastIndexOf("/"); - if (index > 0) { - url = url.substring(index); - } - File destination = new File(new File(this.getLocation()), url); - FileOutputStream outputStream = new FileOutputStream(destination); - entity.writeTo(outputStream); - outputStream.flush(); - outputStream.close(); - if (update) { - resource = (ResourceImpl) new DataModelHelperImpl().createResource(destination.toURI().toURL()); - LOGGER.debug("Update OBR metadata with {}", resource.getId()); - this.addResource(resource); + if ((filter == null) || (url.matches(filter))) { + ResourceImpl resource = (ResourceImpl) new DataModelHelperImpl().createResource(new URL(url)); + if (resource != null) { + LOGGER.debug("Copy {} into the Karaf Cave repository storage", url); + int index = url.lastIndexOf("/"); + if (index > 0) { + url = url.substring(index); + } + File destination = new File(new File(this.getLocation()), url); + FileOutputStream outputStream = new FileOutputStream(destination); + entity.writeTo(outputStream); + outputStream.flush(); + outputStream.close(); + if (update) { + resource = (ResourceImpl) new DataModelHelperImpl().createResource(destination.toURI().toURL()); + LOGGER.debug("Update OBR metadata with {}", resource.getId()); + this.addResource(resource); + } } } } catch (IllegalArgumentException e) { @@ -370,7 +384,7 @@ public class CaveRepositoryImpl extends CaveRepository { for (int i = 1; i < links.size(); i++) { Element link = links.get(i); String absoluteHref = link.attr("abs:href"); - this.populateFromHttp(absoluteHref, update); + this.populateFromHttp(absoluteHref, filter, update); } } } -- 1.7.7