gump-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r760395 - in /gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepoproxy: Constants.java HostResolver.java Main.java resources/Checksum.java resources/GumpArtifact.java resources/Jar.java restlets/Proxy.java
Date Tue, 31 Mar 2009 11:03:25 GMT
Author: bodewig
Date: Tue Mar 31 11:03:19 2009
New Revision: 760395

URL: http://svn.apache.org/viewvc?rev=760395&view=rev
Log:
Make multi-proxy support work

Added:
    gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepoproxy/HostResolver.java   (with props)
Modified:
    gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepoproxy/Constants.java
    gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepoproxy/Main.java
    gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepoproxy/resources/Checksum.java
    gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepoproxy/resources/GumpArtifact.java
    gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepoproxy/resources/Jar.java
    gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepoproxy/restlets/Proxy.java

Modified: gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepoproxy/Constants.java
URL: http://svn.apache.org/viewvc/gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepoproxy/Constants.java?rev=760395&r1=760394&r2=760395&view=diff
==============================================================================
--- gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepoproxy/Constants.java (original)
+++ gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepoproxy/Constants.java Tue Mar 31 11:03:19
2009
@@ -34,10 +34,6 @@
      * parameter representing the artifact id
      */
     String ARTIFACT_ID = "artifactId";
-    /**
-     * URL-Prefix
-     */
-    String PREFIX = "prefix";
 
     /**
      * URL template that matches requests for jars by mvn
@@ -71,26 +67,4 @@
      */
     Variant CHECKSUM_VARIANT = new Variant(MediaType.TEXT_PLAIN);
 
-    /**
-     * URL prefix used by the central repository.
-     */
-    String CENTRAL_PREFIX = "maven2";
-
-    /**
-     * URL prefix used by the Apache Snapshot repository.
-     */
-    String APACHE_SNAPSHOT_PREFIX = "repo/m2-snapshot-repository";
-
-    /**
-     * Real central Maven repository in the format http://hostname with no
-     * trailing slash.
-     */
-    String CENTRAL_MVN_REPO_HOST = "http://repo1.maven.org";
-
-    /**
-     * Real Apache snapshots repository in the format http://hostname with no
-     * trailing slash.
-     */
-    String APACHE_SNAPSHOT_MVN_REPO_HOST = "http://people.apache.org";
-
 }
\ No newline at end of file

Added: gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepoproxy/HostResolver.java
URL: http://svn.apache.org/viewvc/gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepoproxy/HostResolver.java?rev=760395&view=auto
==============================================================================
--- gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepoproxy/HostResolver.java (added)
+++ gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepoproxy/HostResolver.java Tue Mar 31
11:03:19 2009
@@ -0,0 +1,75 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+
+package org.apache.gump.mvnrepoproxy;
+
+import java.util.Collection;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+/**
+ * Support for proxying more than one repository by guessing the
+ * correct repository from a set of known URL prefixes.
+ *
+ * yet another static class, but we really only need a static registry
+ * for now.
+ */
+public class HostResolver {
+
+    /**
+     * Real central Maven repository in the format http://hostname with no
+     * trailing slash.
+     */
+    private static final String CENTRAL_MVN_REPO_HOST =
+        "http://repo1.maven.org";
+
+    private static final Map<String, String> PREFIX_2_HOST =
+        new LinkedHashMap<String, String>();
+
+    static {
+        PREFIX_2_HOST.put("/maven2", CENTRAL_MVN_REPO_HOST);
+        PREFIX_2_HOST.put("/repo/m2-snapshot-repository",
+                          "http://people.apache.org");
+        PREFIX_2_HOST.put("/maven/2",
+                          "http://download.java.net");
+        // old and doesn't work if proxied
+        PREFIX_2_HOST.put("/nonav/repository",
+                          "https://maven2-repository.dev.java.net");
+    }
+
+    /**
+     * Find the host that should serve the request, fall back to
+     * central repo.
+     */
+    public static String getHostForPath(String path) {
+        for (Map.Entry<String, String> entry : PREFIX_2_HOST.entrySet()) {
+            if (path.startsWith(entry.getKey())) {
+                return entry.getValue();
+            }
+        }
+        // fall back to central
+        return CENTRAL_MVN_REPO_HOST;
+    }
+
+    /**
+     * All supported prefixes.
+     */
+    public static Collection<String> getKnownPrefixes() {
+        return PREFIX_2_HOST.keySet();
+    }
+}
\ No newline at end of file

Propchange: gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepoproxy/HostResolver.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepoproxy/Main.java
URL: http://svn.apache.org/viewvc/gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepoproxy/Main.java?rev=760395&r1=760394&r2=760395&view=diff
==============================================================================
--- gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepoproxy/Main.java (original)
+++ gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepoproxy/Main.java Tue Mar 31 11:03:19
2009
@@ -98,8 +98,9 @@
                                                .createChildContext());
 
                     // known artifacts or proxy requests for jars
-                    attachResources(router, Constants.APACHE_SNAPSHOT_PREFIX);
-                    attachResources(router, Constants.CENTRAL_PREFIX);
+                    for (String prefix : HostResolver.getKnownPrefixes()) {
+                        attachResources(router, prefix);
+                    }
 
                     // other resources
                     router.attach("/proxyLog.html", LogFile.class);
@@ -122,20 +123,10 @@
                                                     .createChildContext(),
                                                     component));
 
-                    // Proxy for Apache Snapshot Repo
-                    router.attach("/" + Constants.APACHE_SNAPSHOT_PREFIX + "/",
-                                  new Proxy(getContext()
-                                            .createChildContext(),
-                                            Constants.APACHE_SNAPSHOT_MVN_REPO_HOST
-                                            + "/"
-                                            + Constants.APACHE_SNAPSHOT_PREFIX
-                                            + "/{rr}"));
-
                     // catch all - proxy to real repository
-                    router.attach("/", new Proxy(getContext()
-                                                 .createChildContext(),
-                                                 Constants.CENTRAL_MVN_REPO_HOST
-                                                 + "/{rr}"));
+                    router.attach("/",
+                                  new Proxy(getContext().createChildContext(),
+                                            "/{rr}"));
 
                     return router;
                 }
@@ -154,11 +145,11 @@
 
     private static void attachResource(Router router, String prefix,
                                        String uriTemplate, Class clazz) {
-        Route r = router.attach("/" + prefix + uriTemplate, clazz);
-        prepareVariables(r, prefix);
+        Route r = router.attach(prefix + uriTemplate, clazz);
+        prepareVariables(r);
     }
 
-    private static void prepareVariables(Route r, String prefix) {
+    private static void prepareVariables(Route r) {
         Map<String, Variable> vars = r.getTemplate().getVariables();
         vars.put(Constants.GROUP_ID,
                  new Variable(Variable.TYPE_URI_PATH));

Modified: gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepoproxy/resources/Checksum.java
URL: http://svn.apache.org/viewvc/gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepoproxy/resources/Checksum.java?rev=760395&r1=760394&r2=760395&view=diff
==============================================================================
--- gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepoproxy/resources/Checksum.java (original)
+++ gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepoproxy/resources/Checksum.java Tue Mar
31 11:03:19 2009
@@ -92,9 +92,4 @@
     protected String getArtifactType() {
         return algorithm + " checksum";
     }
-    
-    @Override
-    protected String getURITemplate() {
-        return Constants.CHECKSUM_ARTIFACT_TEMPLATE;
-    }
 }
\ No newline at end of file

Modified: gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepoproxy/resources/GumpArtifact.java
URL: http://svn.apache.org/viewvc/gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepoproxy/resources/GumpArtifact.java?rev=760395&r1=760394&r2=760395&view=diff
==============================================================================
--- gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepoproxy/resources/GumpArtifact.java (original)
+++ gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepoproxy/resources/GumpArtifact.java Tue
Mar 31 11:03:19 2009
@@ -21,6 +21,7 @@
 import java.util.ArrayList;
 
 import org.apache.gump.mvnrepoproxy.Constants;
+import org.apache.gump.mvnrepoproxy.HostResolver;
 import org.apache.gump.mvnrepoproxy.Registry;
 import org.apache.gump.mvnrepoproxy.restlets.Proxy;
 
@@ -51,7 +52,6 @@
 
     protected abstract Representation getRepresentation(String fileName);
     protected abstract String getArtifactType();
-    protected abstract String getURITemplate();
 
     /**
      * The local file or a proxied request
@@ -66,15 +66,10 @@
             log("serving " + fileName + " as");
             return getRepresentation(fileName);
         } else {
-            String uriTemplate = getURITemplate();
-            log("template is " + uriTemplate);
-            String host =
-                uriTemplate.startsWith("/" + Constants.APACHE_SNAPSHOT_PREFIX)
-                ? Constants.APACHE_SNAPSHOT_MVN_REPO_HOST
-                : Constants.CENTRAL_MVN_REPO_HOST;
-            log("proxying for " + host + ":");
-            return new Proxy(getContext(), host + uriTemplate)
-                .serve(getRequest(), getResponse());
+            log("proxying");
+            Request request = getRequest();
+            return new Proxy(getContext(), request.getResourceRef().getPath())
+                .serve(request, getResponse());
         }
     }
 

Modified: gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepoproxy/resources/Jar.java
URL: http://svn.apache.org/viewvc/gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepoproxy/resources/Jar.java?rev=760395&r1=760394&r2=760395&view=diff
==============================================================================
--- gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepoproxy/resources/Jar.java (original)
+++ gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepoproxy/resources/Jar.java Tue Mar 31
11:03:19 2009
@@ -47,10 +47,4 @@
     protected String getArtifactType() {
         return "jar";
     }
-    
-    @Override
-    protected String getURITemplate() {
-        return Constants.JAR_ARTIFACT_TEMPLATE;
-    }
-     
 }
\ No newline at end of file

Modified: gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepoproxy/restlets/Proxy.java
URL: http://svn.apache.org/viewvc/gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepoproxy/restlets/Proxy.java?rev=760395&r1=760394&r2=760395&view=diff
==============================================================================
--- gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepoproxy/restlets/Proxy.java (original)
+++ gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepoproxy/restlets/Proxy.java Tue Mar 31
11:03:19 2009
@@ -18,6 +18,8 @@
 
 package org.apache.gump.mvnrepoproxy.restlets;
 
+import org.apache.gump.mvnrepoproxy.HostResolver;
+
 import org.restlet.Context;
 import org.restlet.Redirector;
 import org.restlet.Restlet;
@@ -32,18 +34,18 @@
  * <p>This is a very thin layer on top of Redirector.</p>
  */
 public class Proxy extends Restlet {
-    private final Redirector redirector;
+    private final String template;
 
     public Proxy(Context ctx, String template) {
         super(ctx);
-        redirector = new Redirector(ctx, template, Redirector.MODE_DISPATCHER);
+        this.template = template;
     }
 
     /**
      * Used when invoked via ArtifactResource
      */
     public Representation serve(Request request, Response response) {
-        Response r = redirector.handle(request);
+        Response r = getRedirector(request).handle(request);
         response.setStatus(r.getStatus());
         return r.getEntity();
     }
@@ -52,6 +54,15 @@
      * Used when invoked as fallback
      */
     public void handle(Request request, Response response) {
-        redirector.handle(request, response);
+        getRedirector(request).handle(request, response);
+    }
+
+    private Redirector getRedirector(Request request) {
+        String host = HostResolver.getHostForPath(request.getResourceRef()
+                                                  .getPath());
+        Redirector redirector =
+            new Redirector(getContext(), host + template,
+                           Redirector.MODE_DISPATCHER);
+        return redirector;
     }
 }
\ No newline at end of file



Mime
View raw message