karaf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jbono...@apache.org
Subject [karaf-cave] branch master updated: [KARAF-5297] Allow cave proxy and populate repository for URLs with authorization
Date Tue, 14 Nov 2017 14:21:19 GMT
This is an automated email from the ASF dual-hosted git repository.

jbonofre pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/karaf-cave.git


The following commit(s) were added to refs/heads/master by this push:
     new 65373a1  [KARAF-5297] Allow cave proxy and populate repository for URLs with authorization
     new a678915  [KARAF-5297] This closes #5
65373a1 is described below

commit 65373a101e8b0e246615b669177fef7cc9c4cf10
Author: dangiankit <dangiankit@gmail.com>
AuthorDate: Thu Aug 24 17:54:40 2017 -0400

    [KARAF-5297] Allow cave proxy and populate repository for URLs with authorization
---
 assembly/src/main/resources/features.xml           |   1 +
 .../asciidoc/user-guide/populate-repository.adoc   |  15 +++
 .../main/asciidoc/user-guide/proxy-repository.adoc |  15 +++
 pom.xml                                            |   2 +
 .../karaf/cave/server/api/CaveRepository.java      |  24 +++++
 .../server/command/RepositoryPopulateCommand.java  |   7 +-
 .../server/command/RepositoryProxyCommand.java     |   6 +-
 .../server/management/CaveRepositoryMBean.java     |   4 +-
 .../internal/CaveRepositoryMBeanImpl.java          |  28 +++++-
 server/storage/pom.xml                             |  10 ++
 .../cave/server/storage/CaveRepositoryImpl.java    |  71 ++++++++++---
 .../apache/karaf/cave/server/storage/Utils.java    | 112 +++++++++++++++++++++
 12 files changed, 273 insertions(+), 22 deletions(-)

diff --git a/assembly/src/main/resources/features.xml b/assembly/src/main/resources/features.xml
index 18622b7..978f59f 100644
--- a/assembly/src/main/resources/features.xml
+++ b/assembly/src/main/resources/features.xml
@@ -32,6 +32,7 @@
             mvn:org.apache.karaf.cave/apache-karaf-cave/${project.version}/cfg/filesystem
         </configfile>
         <bundle>mvn:org.jsoup/jsoup/${jsoup.version}</bundle>
+        <bundle>mvn:commons-codec/commons-codec/${commons-codec.version}</bundle>
         <bundle>mvn:org.apache.karaf.cave.server/org.apache.karaf.cave.server.api/${project.version}</bundle>
         <bundle>mvn:org.apache.karaf.cave.server/org.apache.karaf.cave.server.storage/${project.version}</bundle>
         <conditional>
diff --git a/manual/src/main/asciidoc/user-guide/populate-repository.adoc b/manual/src/main/asciidoc/user-guide/populate-repository.adoc
index 67d42f7..5663966 100644
--- a/manual/src/main/asciidoc/user-guide/populate-repository.adoc
+++ b/manual/src/main/asciidoc/user-guide/populate-repository.adoc
@@ -64,3 +64,18 @@ a regex option for the filter. For instance, to pick up only joda-time
version 2
 ----
 karaf@root()> cave:repository-populate --filter .*joda-time-2.* my-repository http://repo2.maven.org/maven2/joda-time/joda-time
 ----
+
+When the remote repository requires HTTP authorization parameters, those can be specified
in a Properties file:
+
+----
+karaf@root()> cave:repository-populate --properties <properties-file-path> my-repository
<remote-repository-url>
+----
+
+where, `remote-repository-url` is the link to the external remote repository and `<properties-file-path>`
is the path to a .properties file containing the following keys:
+
+----
+# Authorization parameters for remote repository
+cave.storage.http.username=
+cave.storage.http.password=
+----
+
diff --git a/manual/src/main/asciidoc/user-guide/proxy-repository.adoc b/manual/src/main/asciidoc/user-guide/proxy-repository.adoc
index b55b713..4fa1a71 100644
--- a/manual/src/main/asciidoc/user-guide/proxy-repository.adoc
+++ b/manual/src/main/asciidoc/user-guide/proxy-repository.adoc
@@ -39,3 +39,18 @@ The `cave:repository-proxy` command accepts the filter option, as the `cave:repo
 ----
 karaf@root()> cave:repository-proxy --filter .*joda-time-2.* my-repository http://repo2.maven.org/maven2/joda-time/joda-time
 ----
+
+The `cave:repository-proxy` command accepts the properties option, as the `cave:repository-populate`
command:
+
+----
+karaf@root()> cave:repository-proxy --properties <properties-file-path> my-repository
<remote-repository-url>
+----
+
+where, `remote-repository-url` is the link to the external remote repository and `<properties-file-path>`
is the path to the .properties file containing the following keys:
+
+----
+# Authorization parameters for remote repository
+cave.storage.http.username=
+cave.storage.http.password=
+----
+
diff --git a/pom.xml b/pom.xml
index 5b93497..83dc8ab 100644
--- a/pom.xml
+++ b/pom.xml
@@ -41,6 +41,7 @@
         <jsoup.version>1.7.3</jsoup.version>
         <karaf.version>4.1.3</karaf.version>
         <osgi.version>6.0.0</osgi.version>
+        <commons-codec.version>1.10</commons-codec.version>
         <wagon.version>1.0</wagon.version>
 
         <servlet.spec.groupId>javax.servlet</servlet.spec.groupId>
@@ -391,6 +392,7 @@
                     <plugin>
                         <groupId>org.codehaus.mojo</groupId>
                         <artifactId>build-helper-maven-plugin</artifactId>
+                        <version>1.5</version>
                         <executions>
                             <execution>
                                 <id>attach-assemblies</id>
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 0ec9196..e4ccb9d 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
@@ -17,6 +17,7 @@
 package org.apache.karaf.cave.server.api;
 
 import java.net.URL;
+import java.util.Properties;
 
 /**
  * Cave repository is a storage area where to upload artifacts.
@@ -79,6 +80,17 @@ public interface CaveRepository {
     public void proxy(URL url, String filter) throws Exception;
 
     /**
+     * Proxy an URL (for instance a Maven repository), eventually filtering some artifacts,
+     * provide a Properties object containing URL authorization parameters and add repository
metadata..
+     *
+     * @param url the URL to proxy.
+     * @param filter regex filter on the artifacts URL.
+     * @param properties a Properties object containing URL authorization parameters.
+     * @throws Exception
+     */
+    public void proxy(URL url, String filter, Properties properties) throws Exception;
+
+    /**
      * Populate from a remote URL (for instance a Maven repository), and eventually update
the repository metadata.
      *
      * @param url the URL to copy.
@@ -98,6 +110,18 @@ public interface CaveRepository {
     public void populate(URL url, String filter, boolean update) throws Exception;
 
     /**
+     * Populate from a remote URL (for instance a Maven repository), eventually filtering
artifacts,
+     * provide a Properties object containing URL authorization parameters and eventually
update the repository metadata.
+     *
+     * @param url the URL to copy.
+     * @param filter regex filter on the artifacts URL.
+     * @param properties a Properties object containing URL authorization parameters.
+     * @param update if true the repository metadata is updated, false else.
+     * @throws Exception
+     */
+    public void populate(URL url, String filter, Properties properties, boolean update) throws
Exception;
+
+    /**
      * Return an URL for the resource at the given URI.
      *
      * @param uri the resource URI.
diff --git a/server/command/src/main/java/org/apache/karaf/cave/server/command/RepositoryPopulateCommand.java
b/server/command/src/main/java/org/apache/karaf/cave/server/command/RepositoryPopulateCommand.java
index c864775..6c42031 100644
--- a/server/command/src/main/java/org/apache/karaf/cave/server/command/RepositoryPopulateCommand.java
+++ b/server/command/src/main/java/org/apache/karaf/cave/server/command/RepositoryPopulateCommand.java
@@ -18,6 +18,7 @@ package org.apache.karaf.cave.server.command;
 
 import java.net.URL;
 
+import java.util.Properties;
 import org.apache.karaf.cave.server.api.CaveRepository;
 import org.apache.karaf.cave.server.command.completers.RepositoryCompleter;
 import org.apache.karaf.shell.api.action.Argument;
@@ -46,17 +47,19 @@ public class RepositoryPopulateCommand extends CaveRepositoryCommandSupport
{
     @Argument(index = 1, name = "url", description = "The source URL to use", required =
true, multiValued = false)
     String url = null;
 
+    @Option(name="-prop", aliases = { "--properties" }, description = "Path to Properties
file containing URL authorization parameters", required = false, multiValued = false)
+    Properties properties;
+
     protected Object doExecute() throws Exception {
         if (getCaveRepositoryService().getRepository(name) == null) {
             System.err.println("Cave repository " + name + " doesn't exist");
             return null;
         }
         CaveRepository repository = getCaveRepositoryService().getRepository(name);
-        repository.populate(new URL(url), filter, !noUpdate);
+        repository.populate(new URL(url), filter, properties, !noUpdate);
         if (!noUpdate) {
             getCaveRepositoryService().install(name);
         }
         return null;
     }
-
 }
diff --git a/server/command/src/main/java/org/apache/karaf/cave/server/command/RepositoryProxyCommand.java
b/server/command/src/main/java/org/apache/karaf/cave/server/command/RepositoryProxyCommand.java
index f38f49f..bf4bfce 100644
--- a/server/command/src/main/java/org/apache/karaf/cave/server/command/RepositoryProxyCommand.java
+++ b/server/command/src/main/java/org/apache/karaf/cave/server/command/RepositoryProxyCommand.java
@@ -18,6 +18,7 @@ package org.apache.karaf.cave.server.command;
 
 import java.net.URL;
 
+import java.util.Properties;
 import org.apache.karaf.cave.server.api.CaveRepository;
 import org.apache.karaf.cave.server.command.completers.RepositoryCompleter;
 import org.apache.karaf.shell.api.action.Argument;
@@ -46,13 +47,16 @@ public class RepositoryProxyCommand extends CaveRepositoryCommandSupport
{
     @Option(name = "-f", aliases = { "--filter" }, description = "Regex filter on the artifacts
URL", required = false, multiValued = false)
     String filter;
 
+    @Option(name="-prop", aliases = { "--properties" }, description = "Path to Properties
file containing URL authorization parameters", required = false, multiValued = false)
+    Properties properties;
+
     protected Object doExecute() throws Exception {
         if (getCaveRepositoryService().getRepository(name) == null) {
             System.err.println("Cave repository " + name + " doesn't exist");
             return null;
         }
         CaveRepository repository = getCaveRepositoryService().getRepository(name);
-        repository.proxy(new URL(url), filter);
+        repository.proxy(new URL(url), filter, properties);
         if (!noUpdate) {
             getCaveRepositoryService().install(name);
         }
diff --git a/server/management/src/main/java/org/apache/karaf/cave/server/management/CaveRepositoryMBean.java
b/server/management/src/main/java/org/apache/karaf/cave/server/management/CaveRepositoryMBean.java
index 50069c3..ec47daa 100644
--- a/server/management/src/main/java/org/apache/karaf/cave/server/management/CaveRepositoryMBean.java
+++ b/server/management/src/main/java/org/apache/karaf/cave/server/management/CaveRepositoryMBean.java
@@ -26,8 +26,8 @@ public interface CaveRepositoryMBean {
     void destroyRepository(String name) throws Exception;
     void installRepository(String name) throws Exception;
     void uninstallRepository(String name) throws Exception;
-    void populateRepository(String name, String url, boolean generate, String filter) throws
Exception;
-    void proxyRepository(String name, String url, boolean generate, String filter) throws
Exception;
+    void populateRepository(String name, String url, boolean generate, String filter, String
properties) throws Exception;
+    void proxyRepository(String name, String url, boolean generate, String filter, String
properties) throws Exception;
     void updateRepository(String name) throws Exception;
     void uploadArtifact(String repository, String artifactUrl, boolean generate) throws Exception;
 
diff --git a/server/management/src/main/java/org/apache/karaf/cave/server/management/internal/CaveRepositoryMBeanImpl.java
b/server/management/src/main/java/org/apache/karaf/cave/server/management/internal/CaveRepositoryMBeanImpl.java
index f0ab7f0..39052cf 100644
--- a/server/management/src/main/java/org/apache/karaf/cave/server/management/internal/CaveRepositoryMBeanImpl.java
+++ b/server/management/src/main/java/org/apache/karaf/cave/server/management/internal/CaveRepositoryMBeanImpl.java
@@ -14,6 +14,9 @@
 
 package org.apache.karaf.cave.server.management.internal;
 
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.util.Properties;
 import org.apache.karaf.cave.server.api.CaveRepository;
 import org.apache.karaf.cave.server.api.CaveRepositoryService;
 import org.apache.karaf.cave.server.management.CaveRepositoryMBean;
@@ -104,23 +107,23 @@ public class CaveRepositoryMBeanImpl extends StandardMBean implements
CaveReposi
         caveRepositoryService.uninstall(name);
     }
 
-    public void populateRepository(String name, String url, boolean generate, String filter)
throws Exception {
+    public void populateRepository(String name, String url, boolean generate, String filter,
String properties) throws Exception {
         if (getCaveRepositoryService().getRepository(name) == null) {
             throw new IllegalArgumentException("Cave repository " + name + " doesn't exist");
         }
         CaveRepository repository = getCaveRepositoryService().getRepository(name);
-        repository.populate(new URL(url), filter, generate);
+        repository.populate(new URL(url), filter, Utils.loadProperties(properties), generate);
         if (generate) {
             getCaveRepositoryService().install(name);
         }
     }
 
-    public void proxyRepository(String name, String url, boolean generate, String filter)
throws Exception {
+    public void proxyRepository(String name, String url, boolean generate, String filter,
String properties) throws Exception {
         if (getCaveRepositoryService().getRepository(name) == null) {
             throw new IllegalArgumentException("Cave repository " + name + " doesn't exist");
         }
         CaveRepository repository = getCaveRepositoryService().getRepository(name);
-        repository.proxy(new URL(url), filter);
+        repository.proxy(new URL(url), filter, Utils.loadProperties(properties));
         if (generate) {
             getCaveRepositoryService().install(name);
         }
@@ -145,4 +148,21 @@ public class CaveRepositoryMBeanImpl extends StandardMBean implements
CaveReposi
         }
     }
 
+    static class Utils {
+
+        /**
+         * Returns the <code>Properties</code> object as represented by the
+         * property list (key and element pairs) in the file path
+         * at the given <code<>propertiesFile</code>.
+         *
+         * @param   propertiesFile a Properties file containing key-element pairs.
+         * @return  a <code>Properties</code> object containing the properties
read from the given file.
+         * @throws  IOException if an error occurred when reading from the input stream.
+         */
+        public static Properties loadProperties (String propertiesFile) throws IOException
{
+            Properties properties = new Properties();
+            properties.load(new FileInputStream(propertiesFile));
+            return properties;
+        }
+    }
 }
diff --git a/server/storage/pom.xml b/server/storage/pom.xml
index f675a4d..1de5d22 100644
--- a/server/storage/pom.xml
+++ b/server/storage/pom.xml
@@ -58,6 +58,16 @@
             <groupId>org.apache.karaf</groupId>
             <artifactId>org.apache.karaf.util</artifactId>
         </dependency>
+        <dependency>
+            <groupId>commons-codec</groupId>
+            <artifactId>commons-codec</artifactId>
+            <version>1.10</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.httpcomponents</groupId>
+            <artifactId>httpcore</artifactId>
+            <version>4.4.4</version>
+        </dependency>
     </dependencies>
 
     <build>
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 6572e30..600d10b 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
@@ -16,6 +16,8 @@
  */
 package org.apache.karaf.cave.server.storage;
 
+import java.net.URLConnection;
+import java.util.Properties;
 import javax.xml.stream.XMLStreamException;
 import java.io.File;
 import java.io.FilterInputStream;
@@ -47,6 +49,7 @@ import org.apache.karaf.features.internal.resolver.ResolverUtil;
 import org.apache.karaf.features.internal.resolver.ResourceBuilder;
 import org.apache.karaf.features.internal.resolver.ResourceImpl;
 import org.apache.karaf.features.internal.resolver.ResourceUtils;
+import org.jsoup.Connection;
 import org.jsoup.Jsoup;
 import org.jsoup.UnsupportedMimeTypeException;
 import org.jsoup.nodes.Document;
@@ -241,6 +244,19 @@ public class CaveRepositoryImpl implements CaveRepository {
      * @throws Exception
      */
     public void proxy(URL url, String filter) throws Exception {
+        proxy(url, filter, null);
+    }
+
+    /**
+     * Proxy an URL (by adding repository.xml repository metadata) in the Cave repository.
+     *
+     * @param url    the URL to proxyFilesystem. the URL to proxyFilesystem.
+     * @param filter regex filter. Only artifacts URL matching the filter will be considered.
+     * @param properties a Properties object containing URL authorization parameters
+     *                   to access URLs that require authorization.
+     * @throws Exception
+     */
+    public void proxy(URL url, String filter, Properties properties) throws Exception {
         if (url.getProtocol().equals("file")) {
             // filesystem proxyFilesystem (to another folder)
             File proxyFolder = new File(url.toURI());
@@ -250,7 +266,7 @@ public class CaveRepositoryImpl implements CaveRepository {
         } else if (url.getProtocol().equals("http")) {
             // HTTP proxyFilesystem
             List<Resource> resources = new ArrayList<>();
-            proxyHttp(url.toExternalForm(), filter, resources);
+            proxyHttp(url.toExternalForm(), filter, properties, resources);
             addResources(resources);
         }
     }
@@ -294,15 +310,19 @@ public class CaveRepositoryImpl implements CaveRepository {
     }
 
     private ResourceImpl createResource(URL url) throws BundleException, IOException, NoSuchAlgorithmException
{
-        return createResource(url, url.toExternalForm(), true);
+        return createResource(url.openConnection());
     }
 
-    private ResourceImpl createResource(URL url, String uri, boolean readFully) throws BundleException,
IOException, NoSuchAlgorithmException {
+    private ResourceImpl createResource(URLConnection urlConnection) throws BundleException,
IOException, NoSuchAlgorithmException {
+        return createResource(urlConnection, urlConnection.getURL().toExternalForm(), true);
+    }
+
+    private ResourceImpl createResource(URLConnection urlConnection, String uri, boolean
readFully) throws BundleException, IOException, NoSuchAlgorithmException {
         Map<String, String> headers = null;
         String digest = null;
         long size = -1;
         // Find headers, compute length and checksum
-        try (ContentInputStream is = new ContentInputStream(url.openStream())) {
+        try (ContentInputStream is = new ContentInputStream(urlConnection.getInputStream()))
{
             ZipInputStream zis = new ZipInputStream(is);
             ZipEntry entry;
             while ((entry = zis.getNextEntry()) != null) {
@@ -323,7 +343,7 @@ public class CaveRepositoryImpl implements CaveRepository {
             }
         }
         if (headers == null) {
-            throw new BundleException("Resource " + url + " does not contain a manifest");
+            throw new BundleException("Resource " + urlConnection.getURL() + " does not contain
a manifest");
         }
         // Fix the content directive
         try {
@@ -401,12 +421,15 @@ public class CaveRepositoryImpl implements CaveRepository {
      *
      * @param url    the HTTP URL to proxy.
      * @param filter regex filter. Only artifacts URL matching the filter will be considered.
+     * @param properties a Properties object containing URL authorization parameters
+     *                   to access URLs that require authorization.
      * @throws Exception in case of proxy failure.
      */
-    private void proxyHttp(String url, String filter, List<Resource> resources) throws
Exception {
+    private void proxyHttp(String url, String filter, Properties properties, List<Resource>
resources) throws Exception {
         LOGGER.debug("Proxying HTTP URL {}", url);
 
         HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
+        conn = (new Utils.Authorizer(properties)).authorize(conn);
         try (InputStream is = conn.getInputStream()) {
             String type = conn.getContentType();
             if ("application/java-archive".equals(type)
@@ -425,11 +448,12 @@ public class CaveRepositoryImpl implements CaveRepository {
             } else {
                 // try to find link to "browse"
                 try {
-                    Document document = Jsoup.connect(url).get();
+                    Connection jConn = Jsoup.connect(url);
+                    Document document = (new Utils.Authorizer(properties)).authorize(jConn).get();
                     for (Element link : document.select("a")) {
                         String absoluteHref = link.attr("abs:href");
                         if (absoluteHref.startsWith(url)) {
-                            proxyHttp(absoluteHref, filter, resources);
+                            proxyHttp(absoluteHref, filter, properties, resources);
                         }
                     }
                 } catch (UnsupportedMimeTypeException e) {
@@ -444,10 +468,12 @@ public class CaveRepositoryImpl implements CaveRepository {
      *
      * @param url    the URL to copy.
      * @param filter regex filter. Only artifacts URL matching the filter will be considered.
+     * @param properties a Properties object containing URL authorization parameters
+     *                   to access URLs that require authorization.
      * @param update if true the repository metadata is updated, false else.
      * @throws Exception in case of populate failure.
      */
-    public void populate(URL url, String filter, boolean update) throws Exception {
+    public void populate(URL url, String filter, Properties properties, boolean update) throws
Exception {
         if (url.getProtocol().equals("file")) {
             // populate the Cave repository from a filesystem folder
             File populateFolder = new File(url.toURI());
@@ -458,7 +484,7 @@ public class CaveRepositoryImpl implements CaveRepository {
         if (url.getProtocol().equals("http")) {
             // populate the Cave repository from a HTTP URL
             List<Resource> resources = new ArrayList<>();
-            populateFromHttp(url.toExternalForm(), filter, update, resources);
+            populateFromHttp(url.toExternalForm(), filter, properties, update, resources);
             addResources(resources);
         }
     }
@@ -467,6 +493,18 @@ public class CaveRepositoryImpl implements CaveRepository {
      * Populate an URL into the Cave repository, and eventually update the repository metadata.
      *
      * @param url    the URL to copy.
+     * @param filter regex filter. Only artifacts URL matching the filter will be considered.
+     * @param update if true the repository metadata is updated, false else.
+     * @throws Exception in case of populate failure.
+     */
+    public void populate(URL url, String filter, boolean update) throws Exception {
+        populate(url, filter, null, update);
+    }
+
+    /**
+     * Populate an URL into the Cave repository, and eventually update the repository metadata.
+     *
+     * @param url    the URL to copy.
      * @param update if true the repository metadata is updated, false else.
      * @throws Exception
      */
@@ -516,13 +554,18 @@ public class CaveRepositoryImpl implements CaveRepository {
      *
      * @param url    the "source" HTTP URL.
      * @param filter regex filter. Only artifacts URL matching the filter will be considered.
+     * @param properties a Properties object containing URL authorization parameters
+     *                   to access URLs that require authorization.
      * @param update true if the repository metadata should be updated, false else.
      * @throws Exception in case of populate failure.
      */
-    private void populateFromHttp(String url, String filter, boolean update, List<Resource>
resources) throws Exception {
+    private void populateFromHttp(String url, String filter, Properties properties,
+                                  boolean update, List<Resource> resources) throws
Exception {
+
         LOGGER.debug("Populating from HTTP URL {}", url);
 
         HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
+        conn = (new Utils.Authorizer(properties)).authorize(conn);
         try (InputStream is = conn.getInputStream()) {
             String type = conn.getContentType();
             if ("application/java-archive".equals(type)
@@ -532,7 +575,9 @@ public class CaveRepositoryImpl implements CaveRepository {
                 try {
                     if ((filter == null) || (url.matches(filter))) {
                         // Make sure this is a valid bundle
-                        ResourceImpl resource = createResource(new URL(url));
+                        URLConnection urlConnection = (URLConnection) new URL(url).openConnection();
+                        urlConnection = (new Utils.Authorizer(properties)).authorize(urlConnection);
+                        ResourceImpl resource = createResource(urlConnection);
                         LOGGER.debug("Copy {} into the Cave repository storage", url);
                         int index = url.lastIndexOf("/");
                         if (index > 0) {
@@ -555,7 +600,7 @@ public class CaveRepositoryImpl implements CaveRepository {
                 for (Element link : document.select("a")) {
                     String absoluteHref = link.attr("abs:href");
                     if (absoluteHref.startsWith(url)) {
-                        populateFromHttp(absoluteHref, filter, update, resources);
+                        populateFromHttp(absoluteHref, filter, properties, update, resources);
                     }
                 }
             }
diff --git a/server/storage/src/main/java/org/apache/karaf/cave/server/storage/Utils.java
b/server/storage/src/main/java/org/apache/karaf/cave/server/storage/Utils.java
index 8210266..0d9ae36 100644
--- a/server/storage/src/main/java/org/apache/karaf/cave/server/storage/Utils.java
+++ b/server/storage/src/main/java/org/apache/karaf/cave/server/storage/Utils.java
@@ -16,12 +16,19 @@
  */
 package org.apache.karaf.cave.server.storage;
 
+import java.io.FileInputStream;
 import java.io.IOException;
+import java.net.HttpURLConnection;
+import java.net.URLConnection;
 import java.nio.file.FileVisitResult;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.SimpleFileVisitor;
 import java.nio.file.attribute.BasicFileAttributes;
+import java.util.Properties;
+import org.apache.commons.codec.binary.Base64;
+import org.apache.http.HttpHeaders;
+import org.jsoup.Connection;
 
 public class Utils {
 
@@ -42,4 +49,109 @@ public class Utils {
         }
     }
 
+    /**
+     * An instance of the <code>Authorizer</code> class
+     * sets the <code>HttpHeaders.AUTHORIZATION</code> request.
+     */
+    public static class Authorizer {
+
+        // Property keys expected in the Properties file.
+        private static final String HTTP_USERNAME = "cave.storage.http.username";
+        private static final String HTTP_PASSWORD = "cave.storage.http.password";
+
+        // a Properties object to store the authorization parameters.
+        private Properties properties;
+
+        /**
+         * Instantiates an object of the <code>Authorizer</code> class
+         * with the given <code>properties</code>.
+         *
+         * @param   properties a Properties object containing key-element pairs.
+         */
+        public Authorizer (Properties properties) {
+            this.properties = properties;
+        }
+
+        /**
+         * Returns the given <code>HttpURLConnection</code> object
+         * modified by setting the <code>HttpHeaders.AUTHORIZATION</code> header
+         * depending on whether or not the authorization keys have been set.
+         *
+         * @param   connection an instance of <code>HttpURLConnection</code>.
+         * @return  a modified <code>HttpURLConnection</code> object.
+         */
+        public HttpURLConnection authorize(HttpURLConnection connection) {
+            if (containsAuthorizationKeys()) {
+                connection.setRequestProperty(HttpHeaders.AUTHORIZATION, getAuthorizationHeader());
+            }
+            return connection;
+        }
+
+        /**
+         * Returns the given <code>URLConnection</code> object
+         * modified by setting the <code>HttpHeaders.AUTHORIZATION</code> header
+         * depending on whether or not the authorization keys have been set.
+         *
+         * @param   connection an instance of <code>URLConnection</code>.
+         * @return  a modified <code>URLConnection</code> object.
+         */
+        public URLConnection authorize(URLConnection connection) {
+            if (containsAuthorizationKeys()) {
+                connection.setRequestProperty(HttpHeaders.AUTHORIZATION, getAuthorizationHeader());
+            }
+            return connection;
+        }
+
+        /**
+         * Returns the given <code>Connection</code> object
+         * modified by setting the <code>HttpHeaders.AUTHORIZATION</code> header
+         * depending on whether or not the authorization keys have been set.
+         *
+         * @param   connection an instance of <code>Connection</code>.
+         * @return  a modified <code>Connection</code> object.
+         */
+        public Connection authorize(Connection connection) {
+            if (containsAuthorizationKeys()) {
+                connection.header(HttpHeaders.AUTHORIZATION, getAuthorizationHeader());
+            }
+            return connection;
+        }
+
+        /**
+         * Returns <code>true</code>, iff the <code>Properties</code>
file
+         * contains the required Authorization keys.
+         *
+         * @return  <code>true</code> iff the Authorization keys are present.
+         */
+        private boolean containsAuthorizationKeys() {
+            return properties.containsKey(HTTP_USERNAME) && properties.containsKey(HTTP_PASSWORD);
+        }
+
+        /**
+         * Returns the <code>String</code> representation
+         * of the Authorization header.
+         *
+         * @return  the Authorization header.
+         */
+        private String getAuthorizationHeader () {
+            return getAuthorizationHeader(
+                    properties.getProperty(HTTP_USERNAME),
+                    properties.getProperty(HTTP_PASSWORD)
+            );
+        }
+
+        /**
+         * Returns the encoded <code>String</code> representation
+         * of the Authorization header for the given username and password.
+         *
+         * @param   username the username for the <code>HttpHeaders.AUTHORIZATION</code>
header.
+         * @param   password the password for the <code>HttpHeaders.AUTHORIZATION</code>
header.
+         * @return  an encoded <code>String</code> representation for Authorization.
+         */
+        private String getAuthorizationHeader (String username, String password) {
+            String auth = username + ":" + password;
+            byte[] encodedAuth = Base64.encodeBase64(auth.getBytes());
+            return "Basic " + new String(encodedAuth);
+        }
+    }
 }

-- 
To stop receiving notification emails like this one, please contact
['"commits@karaf.apache.org" <commits@karaf.apache.org>'].

Mime
View raw message