You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@gump.apache.org by bo...@apache.org on 2007/08/23 13:54:32 UTC

svn commit: r568945 - in /gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepo: Constants.java Main.java resources/Checksum.java resources/GumpArtifact.java resources/Jar.java

Author: bodewig
Date: Thu Aug 23 04:54:32 2007
New Revision: 568945

URL: http://svn.apache.org/viewvc?rev=568945&view=rev
Log:
Add checksum creation

Added:
    gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepo/resources/Checksum.java
      - copied, changed from r568932, gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepo/resources/Jar.java
    gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepo/resources/GumpArtifact.java
      - copied, changed from r568932, gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepo/resources/Jar.java
Modified:
    gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepo/Constants.java
    gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepo/Main.java
    gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepo/resources/Jar.java

Modified: gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepo/Constants.java
URL: http://svn.apache.org/viewvc/gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepo/Constants.java?rev=568945&r1=568944&r2=568945&view=diff
==============================================================================
--- gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepo/Constants.java (original)
+++ gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepo/Constants.java Thu Aug 23 04:54:32 2007
@@ -38,12 +38,23 @@
     /**
      * URL template that matches requests for jars by mvn
      */
-    String MVN_ARTIFACT_TEMPLATE
+    String JAR_ARTIFACT_TEMPLATE
         = "/maven2/{" + GROUP_ID + "}/{" + ARTIFACT_ID
         + "}/{version}/{jarname}.jar";
 
 
     /**
+     * parameter representing the checksum algorithm
+     */
+    String CS_ALGORITHM = "algorithm";
+
+    /**
+     * URL template that matches requests for checksums by mvn
+     */
+    String CHECKSUM_ARTIFACT_TEMPLATE
+        = JAR_ARTIFACT_TEMPLATE + ".{" + CS_ALGORITHM + "}";
+
+    /**
      * parameter representing the filename
      */
     String FILE_NAME = "file";
@@ -52,6 +63,11 @@
      * Variant used for JAR files
      */
     Variant JAR_VARIANT = new Variant(MediaType.APPLICATION_JAVA_ARCHIVE);
+
+    /**
+     * Variant used for JAR files
+     */
+    Variant CHECKSUM_VARIANT = new Variant(MediaType.TEXT_PLAIN);
 
     /**
      * Real Maven repository in the format http://hostname with no

Modified: gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepo/Main.java
URL: http://svn.apache.org/viewvc/gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepo/Main.java?rev=568945&r1=568944&r2=568945&view=diff
==============================================================================
--- gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepo/Main.java (original)
+++ gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepo/Main.java Thu Aug 23 04:54:32 2007
@@ -18,6 +18,7 @@
 
 package org.apache.gump.mvnrepo;
 
+import org.apache.gump.mvnrepo.resources.Checksum;
 import org.apache.gump.mvnrepo.resources.Jar;
 import org.apache.gump.mvnrepo.restlets.ArtifactAdder;
 import org.apache.gump.mvnrepo.restlets.ArtifactsForm;
@@ -63,8 +64,10 @@
                     Router router = new Router(getContext());
 
                     // known artifacts or proxy requests for jars
-                    router.attach(Constants.MVN_ARTIFACT_TEMPLATE,
+                    router.attach(Constants.JAR_ARTIFACT_TEMPLATE,
                                   Jar.class);
+                    router.attach(Constants.CHECKSUM_ARTIFACT_TEMPLATE,
+                                  Checksum.class);
 
                     // simple HTML form to add new artifacts
                     router.attach("/addartifact.html", new ArtifactsForm());

Copied: gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepo/resources/Checksum.java (from r568932, gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepo/resources/Jar.java)
URL: http://svn.apache.org/viewvc/gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepo/resources/Checksum.java?p2=gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepo/resources/Checksum.java&p1=gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepo/resources/Jar.java&r1=568932&r2=568945&rev=568945&view=diff
==============================================================================
--- gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepo/resources/Jar.java (original)
+++ gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepo/resources/Checksum.java Thu Aug 23 04:54:32 2007
@@ -18,61 +18,79 @@
 
 package org.apache.gump.mvnrepo.resources;
 
+import java.io.FileInputStream;
+import java.security.DigestInputStream;
+import java.security.MessageDigest;
+
 import org.apache.gump.mvnrepo.Constants;
-import org.apache.gump.mvnrepo.Registry;
-import org.apache.gump.mvnrepo.restlets.Proxy;
 
 import org.restlet.Context;
-import org.restlet.data.MediaType;
 import org.restlet.data.Request;
 import org.restlet.data.Response;
-import org.restlet.resource.FileRepresentation;
-import org.restlet.resource.Resource;
 import org.restlet.resource.Representation;
-import org.restlet.resource.Variant;
+import org.restlet.resource.StringRepresentation;
 
 /**
- * A (jar) artifact served from the local file system if registered by
- * Gump or proxied from a real mvn repository.
+ * A checksum (sha1 or md5) artifact served from the local file system
+ * if registered by Gump or proxied from a real mvn repository.
  */
-public class Jar extends Resource {
+public class Checksum extends GumpArtifact {
 
-    private final String fileName;
+    private final String algorithm;
 
-    public Jar(Context ctx, Request request, Response response) {
-        super(ctx, request, response);
-        fileName = Registry.getArtifactPath((String) request.getAttributes()
-					    .get(Constants.GROUP_ID),
-					    (String) request.getAttributes()
-					    .get(Constants.ARTIFACT_ID));
-        getVariants().add(Constants.JAR_VARIANT);
+    public Checksum(Context ctx, Request request, Response response) {
+        super(ctx, request, response, Constants.CHECKSUM_VARIANT);
+        this.algorithm =
+            (String) request.getAttributes().get(Constants.CS_ALGORITHM);
     }
 
-    /**
-     * The local file or a proxied request
-     */
     @Override
-    public Representation getRepresentation(Variant v) {
-        if (fileName != null) {
-            log("serving " + fileName + " as");
-            return new FileRepresentation(fileName, v.getMediaType(), 300);
-        } else {
-            log("proxying");
-            return new Proxy(getContext(),
-                             Constants.MVN_REPO_HOST
-                             + Constants.MVN_ARTIFACT_TEMPLATE)
-                .serve(getRequest(), getResponse());
+    protected Representation getRepresentation(String fileName) {
+        FileInputStream fis = null;
+        DigestInputStream dis = null;
+        try {
+            MessageDigest d = MessageDigest.getInstance(algorithm);
+            fis = new FileInputStream(fileName);
+            dis = new DigestInputStream(fis, d);
+            byte[] buf = new byte[1024 * 1024];
+            while (dis.read(buf, 0, 1024 * 1024) != -1) {
+                // Empty statement
+            }
+            byte[] fileDigest = d.digest();
+            StringBuffer checksumSb = new StringBuffer();
+            for (int i = 0; i < fileDigest.length; i++) {
+                String hexStr = Integer.toHexString(0x00ff & fileDigest[i]);
+                if (hexStr.length() < 2) {
+                    checksumSb.append("0");
+                }
+                checksumSb.append(hexStr);
+            }
+            checksumSb.append("\t").append(fileName);
+            return new StringRepresentation(checksumSb,
+                                            Constants.CHECKSUM_VARIANT
+                                            .getMediaType());
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        } finally {
+            try {
+                try {
+                    if (dis != null) {
+                        dis.close();
+                    }
+                } finally {
+                    if (fis != null) {
+                        fis.close();
+                    }
+                }
+            } catch (Exception e) {
+                // swallow
+            }
         }
     }
 
-    private void log(String start) {
-        getLogger().info(start + " artifact for groupId '"
-                          + getRequest().getAttributes()
-                          .get(Constants.GROUP_ID)
-                          + "' and artifactId '"
-                          + getRequest().getAttributes()
-                          .get(Constants.ARTIFACT_ID)
-                          + "'");
+    @Override
+    protected String getArtifactType() {
+        return algorithm + " checksum";
     }
     
 }

Copied: gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepo/resources/GumpArtifact.java (from r568932, gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepo/resources/Jar.java)
URL: http://svn.apache.org/viewvc/gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepo/resources/GumpArtifact.java?p2=gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepo/resources/GumpArtifact.java&p1=gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepo/resources/Jar.java&r1=568932&r2=568945&rev=568945&view=diff
==============================================================================
--- gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepo/resources/Jar.java (original)
+++ gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepo/resources/GumpArtifact.java Thu Aug 23 04:54:32 2007
@@ -23,31 +23,33 @@
 import org.apache.gump.mvnrepo.restlets.Proxy;
 
 import org.restlet.Context;
-import org.restlet.data.MediaType;
 import org.restlet.data.Request;
 import org.restlet.data.Response;
-import org.restlet.resource.FileRepresentation;
 import org.restlet.resource.Resource;
 import org.restlet.resource.Representation;
 import org.restlet.resource.Variant;
 
 /**
- * A (jar) artifact served from the local file system if registered by
- * Gump or proxied from a real mvn repository.
+ * A artifact served from the local file system if registered by Gump
+ * or proxied from a real mvn repository.
  */
-public class Jar extends Resource {
+public abstract class GumpArtifact extends Resource {
 
     private final String fileName;
 
-    public Jar(Context ctx, Request request, Response response) {
+    public GumpArtifact(Context ctx, Request request, Response response,
+                        Variant type) {
         super(ctx, request, response);
         fileName = Registry.getArtifactPath((String) request.getAttributes()
-					    .get(Constants.GROUP_ID),
-					    (String) request.getAttributes()
-					    .get(Constants.ARTIFACT_ID));
-        getVariants().add(Constants.JAR_VARIANT);
+                                            .get(Constants.GROUP_ID),
+                                            (String) request.getAttributes()
+                                            .get(Constants.ARTIFACT_ID));
+        getVariants().add(type);
     }
 
+    protected abstract Representation getRepresentation(String fileName);
+    protected abstract String getArtifactType();
+
     /**
      * The local file or a proxied request
      */
@@ -55,7 +57,7 @@
     public Representation getRepresentation(Variant v) {
         if (fileName != null) {
             log("serving " + fileName + " as");
-            return new FileRepresentation(fileName, v.getMediaType(), 300);
+            return getRepresentation(fileName);
         } else {
             log("proxying");
             return new Proxy(getContext(),
@@ -66,13 +68,14 @@
     }
 
     private void log(String start) {
-        getLogger().info(start + " artifact for groupId '"
-                          + getRequest().getAttributes()
-                          .get(Constants.GROUP_ID)
-                          + "' and artifactId '"
-                          + getRequest().getAttributes()
-                          .get(Constants.ARTIFACT_ID)
-                          + "'");
+        getLogger().info(start + " " + getArtifactType()
+                         + " artifact for groupId '"
+                         + getRequest().getAttributes()
+                         .get(Constants.GROUP_ID)
+                         + "' and artifactId '"
+                         + getRequest().getAttributes()
+                         .get(Constants.ARTIFACT_ID)
+                         + "'");
     }
     
 }

Modified: gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepo/resources/Jar.java
URL: http://svn.apache.org/viewvc/gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepo/resources/Jar.java?rev=568945&r1=568944&r2=568945&view=diff
==============================================================================
--- gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepo/resources/Jar.java (original)
+++ gump/mvnrepo/trunk/src/java/org/apache/gump/mvnrepo/resources/Jar.java Thu Aug 23 04:54:32 2007
@@ -19,60 +19,33 @@
 package org.apache.gump.mvnrepo.resources;
 
 import org.apache.gump.mvnrepo.Constants;
-import org.apache.gump.mvnrepo.Registry;
-import org.apache.gump.mvnrepo.restlets.Proxy;
 
 import org.restlet.Context;
-import org.restlet.data.MediaType;
 import org.restlet.data.Request;
 import org.restlet.data.Response;
 import org.restlet.resource.FileRepresentation;
-import org.restlet.resource.Resource;
 import org.restlet.resource.Representation;
-import org.restlet.resource.Variant;
 
 /**
- * A (jar) artifact served from the local file system if registered by
+ * A jar artifact served from the local file system if registered by
  * Gump or proxied from a real mvn repository.
  */
-public class Jar extends Resource {
-
-    private final String fileName;
+public class Jar extends GumpArtifact {
 
     public Jar(Context ctx, Request request, Response response) {
-        super(ctx, request, response);
-        fileName = Registry.getArtifactPath((String) request.getAttributes()
-					    .get(Constants.GROUP_ID),
-					    (String) request.getAttributes()
-					    .get(Constants.ARTIFACT_ID));
-        getVariants().add(Constants.JAR_VARIANT);
+        super(ctx, request, response, Constants.JAR_VARIANT);
     }
 
-    /**
-     * The local file or a proxied request
-     */
     @Override
-    public Representation getRepresentation(Variant v) {
-        if (fileName != null) {
-            log("serving " + fileName + " as");
-            return new FileRepresentation(fileName, v.getMediaType(), 300);
-        } else {
-            log("proxying");
-            return new Proxy(getContext(),
-                             Constants.MVN_REPO_HOST
-                             + Constants.MVN_ARTIFACT_TEMPLATE)
-                .serve(getRequest(), getResponse());
-        }
+    protected Representation getRepresentation(String fileName) {
+        return new FileRepresentation(fileName,
+                                      Constants.JAR_VARIANT.getMediaType(),
+                                      300);
     }
 
-    private void log(String start) {
-        getLogger().info(start + " artifact for groupId '"
-                          + getRequest().getAttributes()
-                          .get(Constants.GROUP_ID)
-                          + "' and artifactId '"
-                          + getRequest().getAttributes()
-                          .get(Constants.ARTIFACT_ID)
-                          + "'");
+    @Override
+    protected String getArtifactType() {
+        return "jar";
     }
     
 }