karaf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [karaf-cave] Diff for: [GitHub] jbonofre merged pull request #21: [KARAF-5978] Add Maven repository browsing
Date Tue, 15 Jan 2019 17:15:37 GMT
diff --git a/server/maven/src/main/java/org/apache/karaf/cave/server/maven/Activator.java b/server/maven/src/main/java/org/apache/karaf/cave/server/maven/Activator.java
index 8d73d64..11c64ac 100644
--- a/server/maven/src/main/java/org/apache/karaf/cave/server/maven/Activator.java
+++ b/server/maven/src/main/java/org/apache/karaf/cave/server/maven/Activator.java
@@ -65,7 +65,7 @@ protected void doStart() throws Exception {
         }
         this.resolver = MavenResolvers.createMavenResolver(config, pid);
         this.alias = alias;
-        this.servlet = new CaveMavenServlet(this.resolver, poolSize, realm, downloadRole,
uploadRole);
+        this.servlet = new CaveMavenServlet(this.resolver, null, null, poolSize, realm, downloadRole,
uploadRole);
         this.httpService.registerServlet(this.alias, this.servlet, config, null);
 
         CaveMavenRepositoryListenerImpl repositoryListener = new CaveMavenRepositoryListenerImpl(httpService,
@@ -78,7 +78,6 @@ protected void doStop() {
         if (httpService != null) {
             try {
                 httpService.unregister(alias);
-                httpService.unregister("/cave/maven/repositories/test");
             } catch (Throwable t) {
                 logger.debug("Exception caught while stopping", t);
             } finally {
diff --git a/server/maven/src/main/java/org/apache/karaf/cave/server/maven/CaveMavenRepositoryListenerImpl.java
b/server/maven/src/main/java/org/apache/karaf/cave/server/maven/CaveMavenRepositoryListenerImpl.java
index d7af7e3..b8a8edf 100644
--- a/server/maven/src/main/java/org/apache/karaf/cave/server/maven/CaveMavenRepositoryListenerImpl.java
+++ b/server/maven/src/main/java/org/apache/karaf/cave/server/maven/CaveMavenRepositoryListenerImpl.java
@@ -48,7 +48,7 @@ public void addRepository(String name, String location) throws Exception
{
         config.put("localRepository", location);
         config.put("repositories", "file:" + location + "@id=" + name + "@snapshots");
         MavenResolver resolver = MavenResolvers.createMavenResolver(config, null);
-        CaveMavenServlet servlet = new CaveMavenServlet(resolver, poolSize, realm, downloadRole,
uploadRole);
+        CaveMavenServlet servlet = new CaveMavenServlet(resolver, name, location, poolSize,
realm, downloadRole, uploadRole);
         httpService.registerServlet("/cave/maven/repositories/" + name, servlet, null, null);
     }
 
diff --git a/server/maven/src/main/java/org/apache/karaf/cave/server/maven/CaveMavenServlet.java
b/server/maven/src/main/java/org/apache/karaf/cave/server/maven/CaveMavenServlet.java
index 7e56614..71cee8f 100644
--- a/server/maven/src/main/java/org/apache/karaf/cave/server/maven/CaveMavenServlet.java
+++ b/server/maven/src/main/java/org/apache/karaf/cave/server/maven/CaveMavenServlet.java
@@ -16,11 +16,16 @@
  */
 package org.apache.karaf.cave.server.maven;
 
+import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileReader;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.InputStreamReader;
 import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.NoSuchFileException;
@@ -109,12 +114,17 @@
 
     final MavenResolver resolver;
 
-    public CaveMavenServlet(MavenResolver resolver, int threadMaximumPoolSize, String realm,
String downloadRole, String uploadRole) {
+    final String name;
+    final String location;
+
+    public CaveMavenServlet(MavenResolver resolver, String name, String location, int threadMaximumPoolSize,
String realm, String downloadRole, String uploadRole) {
         this.resolver = resolver;
         this.threadMaximumPoolSize = threadMaximumPoolSize;
         this.realm = realm;
         this.downloadRole = downloadRole;
         this.uploadRole = uploadRole;
+        this.name = name;
+        this.location = location;
     }
 
     //
@@ -324,7 +334,37 @@ public void operationComplete(ArtifactDownloadFuture future) {
                         resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                     }
                 } else {
-                    resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
+                    // browsing
+                    try {
+                        File requested = new File(location, path);
+                        if (requested.exists()) {
+                            if (requested.isDirectory()) {
+                                Writer writer = new OutputStreamWriter(resp.getOutputStream());
+                                writer.write("<html>");
+                                writer.write("<head><title>Cave Repository "
+ name + ": " + path + "</title></head>");
+                                writer.write("<body>");
+                                writer.write("<header><h1>" + path + "</h1></header>");
+                                writer.write("<hr/>");
+                                writer.write("<main><pre id=\"contents\">");
+                                if (!path.isEmpty()) {
+                                    writer.write("<a href=\"" + req.getRequestURI() +
"../\">..</a><br/>");
+                                }
+                                for (File child : requested.listFiles()) {
+                                    writer.write("<a href=\"" + req.getRequestURI() +
child.getName() + "/\" title=\"" + child.getName() + "\">" + child.getName() + "</a><br/>");
+                                }
+                                writer.write("</pre><hr/></main>");
+                                writer.write("</body></html>");
+                                writer.flush();
+                            } else {
+                                StreamUtils.copy(new FileInputStream(requested), resp.getOutputStream());
+                            }
+                        } else {
+                            resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
+                        }
+                    } catch (Exception e) {
+                        LOGGER.warn("", e);
+                        resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+                    }
                 }
                 future.release();
                 try {
diff --git a/server/maven/src/test/java/org/apache/karaf/cave/server/maven/MavenProxyServletTest.java
b/server/maven/src/test/java/org/apache/karaf/cave/server/maven/MavenProxyServletTest.java
index d58a9b2..93e648e 100644
--- a/server/maven/src/test/java/org/apache/karaf/cave/server/maven/MavenProxyServletTest.java
+++ b/server/maven/src/test/java/org/apache/karaf/cave/server/maven/MavenProxyServletTest.java
@@ -143,13 +143,13 @@ public void testRepoRegex() {
 
     @Test(expected = InvalidMavenArtifactRequest.class)
     public void testConvertNullPath() throws InvalidMavenArtifactRequest {
-        CaveMavenServlet servlet = new CaveMavenServlet(createResolver(), 5, null, null,
null);
+        CaveMavenServlet servlet = new CaveMavenServlet(createResolver(), null, null, 5,
null, null, null);
         servlet.convertArtifactPathToCoord(null);
     }
 
     @Test
     public void testConvertNormalPath() throws InvalidMavenArtifactRequest {
-        CaveMavenServlet servlet = new CaveMavenServlet(createResolver(), 5, null, null,
null);
+        CaveMavenServlet servlet = new CaveMavenServlet(createResolver(), null, null, 5,
null, null, null);
 
         assertEquals("groupId:artifactId:extension:version",servlet.convertArtifactPathToCoord("groupId/artifactId/version/artifactId-version.extension").toString());
         assertEquals("group.id:artifactId:extension:version",servlet.convertArtifactPathToCoord("group/id/artifactId/version/artifactId-version.extension").toString());
@@ -166,7 +166,7 @@ public void testConvertNormalPath() throws InvalidMavenArtifactRequest
{
 
     @Test
     public void testConvertNormalPathWithClassifier() throws InvalidMavenArtifactRequest
{
-        CaveMavenServlet servlet = new CaveMavenServlet(createResolver(), 5, null, null,
null);
+        CaveMavenServlet servlet = new CaveMavenServlet(createResolver(), null, null, 5,
null, null, null);
 
         assertEquals("groupId:artifactId:extension:classifier:version",servlet.convertArtifactPathToCoord("groupId/artifactId/version/artifactId-version-classifier.extension").toString());
         assertEquals("group.id:artifactId:extension:classifier:version",servlet.convertArtifactPathToCoord("group/id/artifactId/version/artifactId-version-classifier.extension").toString());
@@ -190,7 +190,7 @@ public void testStartServlet() throws Exception {
         System.setProperty("karaf.data", new File("target").getCanonicalPath());
         try {
             MavenResolver resolver = createResolver();
-            CaveMavenServlet servlet = new CaveMavenServlet(resolver, 5, null, null, null);
+            CaveMavenServlet servlet = new CaveMavenServlet(resolver, null, null, 5, null,
null, null);
             servlet.init();
         } finally {
             if (old != null) {
@@ -370,7 +370,7 @@ public void handle(String target, Request baseRequest, HttpServletRequest
reques
             int localPort = ((NetworkConnector) server.getConnectors()[0]).getLocalPort();
             // TODO: local repo should point to target/tmp
             MavenResolver resolver = createResolver("target/tmp", "http://relevant.not/repo1@id=repo1,http://relevant.not/repo2@id=repo2",
"http", "localhost", localPort, "fuse", "fuse", null);
-            CaveMavenServlet servlet = new CaveMavenServlet(resolver, 5, null, null, null);
+            CaveMavenServlet servlet = new CaveMavenServlet(resolver, null, null, 5, null,
null, null);
 
             AsyncContext context = EasyMock.createMock(AsyncContext.class);
 
@@ -463,7 +463,7 @@ private void testDownload(Handler serverHandler) throws Exception {
             int localPort = ((NetworkConnector) server.getConnectors()[0]).getLocalPort();
             // TODO: local repo should point to target/tmp
             MavenResolver resolver = createResolver("target/tmp", "http://relevant.not/maven2@id=central",
"http", "localhost", localPort, "fuse", "fuse", null);
-            CaveMavenServlet servlet = new CaveMavenServlet(resolver, 5, null, null, null);
+            CaveMavenServlet servlet = new CaveMavenServlet(resolver, null, null, 5, null,
null, null);
 
             AsyncContext context = EasyMock.createMock(AsyncContext.class);
 
@@ -667,7 +667,7 @@ public void handle(String target, Request baseRequest, HttpServletRequest
reques
         try {
             int localPort = ((NetworkConnector) server.getConnectors()[0]).getLocalPort();
             MavenResolver resolver = createResolver("target/tmp", "http://relevant.not/maven2@id=central",
"http", "localhost", localPort, "fuse", "fuse", null);
-            CaveMavenServlet servlet = new CaveMavenServlet(resolver, 5, null, null, null);
+            CaveMavenServlet servlet = new CaveMavenServlet(resolver, null, null,5, null,
null, null);
 
             HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class);
             EasyMock.expect(request.getPathInfo()).andReturn(path);


With regards,
Apache Git Services

Mime
View raw message