incubator-amber-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From simonetrip...@apache.org
Subject svn commit: r963019 - /incubator/amber/trunk/signature-api/src/main/java/org/apache/amber/signature/rsa/AbstractRsaSha1Key.java
Date Sun, 11 Jul 2010 09:06:07 GMT
Author: simonetripodi
Date: Sun Jul 11 09:06:06 2010
New Revision: 963019

URL: http://svn.apache.org/viewvc?rev=963019&view=rev
Log:
added methods to load a key from classpath, file or URL

Modified:
    incubator/amber/trunk/signature-api/src/main/java/org/apache/amber/signature/rsa/AbstractRsaSha1Key.java

Modified: incubator/amber/trunk/signature-api/src/main/java/org/apache/amber/signature/rsa/AbstractRsaSha1Key.java
URL: http://svn.apache.org/viewvc/incubator/amber/trunk/signature-api/src/main/java/org/apache/amber/signature/rsa/AbstractRsaSha1Key.java?rev=963019&r1=963018&r2=963019&view=diff
==============================================================================
--- incubator/amber/trunk/signature-api/src/main/java/org/apache/amber/signature/rsa/AbstractRsaSha1Key.java
(original)
+++ incubator/amber/trunk/signature-api/src/main/java/org/apache/amber/signature/rsa/AbstractRsaSha1Key.java
Sun Jul 11 09:06:06 2010
@@ -16,9 +16,11 @@
  */
 package org.apache.amber.signature.rsa;
 
+import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLConnection;
 
@@ -36,15 +38,74 @@ abstract class AbstractRsaSha1Key implem
 
     private final byte[] byteValue;
 
+    /**
+     * Loads a key from a certificate located in the classpath.
+     *
+     * @param certificateClasspathLocation
+     * @throws SignatureException
+     */
     public AbstractRsaSha1Key(String certificateClasspathLocation) throws SignatureException
{
-        this(Thread.currentThread().getContextClassLoader().getResource(certificateClasspathLocation));
+        if (certificateClasspathLocation == null) {
+            throw new SignatureException("parameter 'certificateClasspathLocation' must not
be null");
+        }
+        if ('/' == certificateClasspathLocation.charAt(0)) {
+            certificateClasspathLocation = certificateClasspathLocation.substring(1);
+        }
+
+        URL certificateURL = this.getClass().getClassLoader().getResource(certificateClasspathLocation);
+        if (certificateURL == null) {
+            certificateURL = Thread.currentThread().getContextClassLoader().getResource(certificateClasspathLocation);
+        }
+
+        if (certificateURL == null) {
+            throw new SignatureException("Classpath resource '"
+                    + certificateClasspathLocation
+                    + "' not found, please make sure it exists in the classpath");
+        }
+
+        this.byteValue = this.readCertificate(certificateURL);
     }
 
+    /**
+     * Loads a key from a certificate located in a file.
+     *
+     * @param certificateFileLocation
+     * @throws SignatureException
+     */
+    public AbstractRsaSha1Key(File certificateFileLocation) throws SignatureException {
+        if (certificateFileLocation == null) {
+            throw new SignatureException("parameter 'certificateFileLocation' must not be
null");
+        }
+        if (certificateFileLocation.exists() || certificateFileLocation.isDirectory()) {
+            throw new SignatureException("Impossible to load the certificate, '"
+                    + certificateFileLocation
+                    + "' doesn't exist or is a directory");
+        }
+
+        try {
+            this.byteValue = this.readCertificate(certificateFileLocation.toURI().toURL());
+        } catch (MalformedURLException e) {
+            throw new SignatureException("Impossible to read the certificate from '"
+                    + certificateFileLocation
+                    + "' file", e);
+        }
+    }
+
+    /**
+     * Loads a key from a certificate located in an URL.
+     *
+     * @param certificateURL
+     * @throws SignatureException
+     */
     public AbstractRsaSha1Key(URL certificateURL) throws SignatureException {
         if (certificateURL == null) {
             throw new SignatureException("parameter 'certificateURL' must not be null");
         }
 
+        this.byteValue = this.readCertificate(certificateURL);
+    }
+
+    private byte[] readCertificate(URL certificateURL) throws SignatureException {
         URLConnection urlConnection = null;
         InputStream input = null;
 
@@ -52,7 +113,7 @@ abstract class AbstractRsaSha1Key implem
             urlConnection = certificateURL.openConnection();
             input = urlConnection.getInputStream();
 
-            this.byteValue = this.readCertificate(input);
+            return this.readCertificate(input);
         } catch (Exception e) {
             throw new SignatureException("Impossible to read the certificate from '"
                     + certificateURL



Mime
View raw message