jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r1154947 - in /jackrabbit/trunk: jackrabbit-core/src/main/java/org/apache/jackrabbit/core/util/db/ jackrabbit-jcr-commons/ jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/ jackrabbit-jcr-commons/src/test/java/org/apache/jack...
Date Mon, 08 Aug 2011 13:04:56 GMT
Author: jukka
Date: Mon Aug  8 13:04:56 2011
New Revision: 1154947

URL: http://svn.apache.org/viewvc?rev=1154947&view=rev
Log:
JCR-2697: Add support for encrpted db password in repository.xml

Extend the Base64 class to support string arguments and return values and to work on the command
line.

Use the extended class to allow passwords to be specified in base64 encoding, when prefixed
with {base64}.

Modified:
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/util/db/ConnectionFactory.java
    jackrabbit/trunk/jackrabbit-jcr-commons/pom.xml
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/Base64.java
    jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/util/Base64Test.java

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/util/db/ConnectionFactory.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/util/db/ConnectionFactory.java?rev=1154947&r1=1154946&r2=1154947&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/util/db/ConnectionFactory.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/util/db/ConnectionFactory.java
Mon Aug  8 13:04:56 2011
@@ -32,6 +32,7 @@ import org.apache.commons.dbcp.BasicData
 import org.apache.commons.dbcp.DelegatingConnection;
 import org.apache.jackrabbit.core.config.DataSourceConfig;
 import org.apache.jackrabbit.core.config.DataSourceConfig.DataSourceDefinition;
+import org.apache.jackrabbit.util.Base64;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -163,7 +164,8 @@ public final class ConnectionFactory {
             sanityCheck();
             DataSource ds = keyToDataSource.get(key);
             if (ds == null) {
-                ds = createDataSource(driver, url, user, password);
+                ds = createDataSource(
+                        driver, url, user, Base64.decodeIfEncoded(password));
                 keyToDataSource.put(key, ds);
             }
             return ds;

Modified: jackrabbit/trunk/jackrabbit-jcr-commons/pom.xml
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/pom.xml?rev=1154947&r1=1154946&r2=1154947&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/pom.xml (original)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/pom.xml Mon Aug  8 13:04:56 2011
@@ -42,6 +42,9 @@
         <extensions>true</extensions>
         <configuration>
           <instructions>
+            <Main-Class>
+              org.apache.jackrabbit.util.Base64
+            </Main-Class>
             <Export-Package>
               org.apache.jackrabbit.*;version=${project.version}
             </Export-Package>

Modified: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/Base64.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/Base64.java?rev=1154947&r1=1154946&r2=1154947&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/Base64.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/Base64.java
Mon Aug  8 13:04:56 2011
@@ -16,12 +16,14 @@
  */
 package org.apache.jackrabbit.util;
 
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.io.Reader;
+import java.io.StringWriter;
 import java.io.Writer;
 import java.io.BufferedWriter;
 
@@ -29,6 +31,7 @@ import java.io.BufferedWriter;
  * <code>Base64</code> provides Base64 encoding/decoding of strings and streams.
  */
 public class Base64 {
+
     // charset used for base64 encoded data (7-bit ASCII)
     private static final String CHARSET = "US-ASCII";
 
@@ -61,6 +64,55 @@ public class Base64 {
     }
 
     /**
+     * Base64-decodes or -encodes (see {@link #decodeOrEncode(String)}
+     * all the given arguments and prints the results on separate lines
+     * in standard output.
+     *
+     * @since Apache Jackrabbit 2.3
+     * @param args command line arguments to be decoded or encoded
+     */
+    public static void main(String[] args) {
+        for (String arg : args) {
+            System.out.println(decodeOrEncode(arg));
+        }
+    }
+
+    /**
+     * Base64-decodes or -encodes the given string, depending on whether
+     * or not it contains a "{base64}" prefix. If the string gets encoded,
+     * the "{base64}" prefix is added to it.
+     *
+     * @since Apache Jackrabbit 2.3
+     * @param data string to be decoded or encoded
+     * @return decoded or encoded string
+     */
+    public static String decodeOrEncode(String data) {
+        if (data.startsWith("{base64}")) {
+            return decode(data.substring("{base64}".length()));
+        } else {
+            return "{base64}" + encode(data);
+        }
+    }
+
+    /**
+     * Decodes a base64-encoded string marked by a "{base64}" prefix.
+     * If the prefix is not found, then the string is returned as-is.
+     * If the given string is <code>null</code>, then <code>null</code>
+     * is returned.
+     *
+     * @since Apache Jackrabbit 2.3
+     * @param data string to be decoded, can be <code>null</code>
+     * @return the given string, possibly decoded
+     */
+    public static String decodeIfEncoded(String data) {
+        if (data != null && data.startsWith("{base64}")) {
+            return decode(data.substring("{base64}".length()));
+        } else {
+            return data;
+        }
+    }
+
+    /**
      * Calculates the size (i.e. number of bytes) of the base64 encoded output
      * given the length (i.e. number of bytes) of the data to be encoded.
      *
@@ -178,6 +230,47 @@ public class Base64 {
     }
 
     /**
+     * Returns the base64 representation of UTF-8 encoded string.
+     *
+     * @since Apache Jackrabbit 2.3
+     * @param data the string to be encoded
+     * @param base64-encoding of the string
+     */
+    public static String encode(String data) {
+        try {
+            StringWriter buffer = new StringWriter();
+            byte[] b = data.getBytes("UTF-8");
+            encode(b, 0, b.length, buffer);
+            return buffer.toString();
+        } catch (IOException e) { // should never happen
+            throw new RuntimeException(
+                    "Unable to encode base64 data: " + data, e);
+        }
+    }
+
+    /**
+     * Decodes a base64-encoded string using the UTF-8 character encoding.
+     * The given string is returned as-is if it doesn't contain a valid
+     * base64 encoding.
+     *
+     * @since Apache Jackrabbit 2.3
+     * @param data the base64-encoded data to be decoded
+     * @return decoded string
+     */
+    public static String decode(String data) {
+        try {
+            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+            decode(data, buffer);
+            return new String(buffer.toByteArray(), "UTF-8");
+        } catch (IllegalArgumentException e) {
+            return data;
+        } catch (IOException e) { // should never happen
+            throw new RuntimeException(
+                    "Unable to decode base64 data: " + data, e);
+        }
+    }
+
+    /**
      * Decode base64 encoded data.
      *
      * @param reader reader for the base64 encoded data to be decoded

Modified: jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/util/Base64Test.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/util/Base64Test.java?rev=1154947&r1=1154946&r2=1154947&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/util/Base64Test.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/util/Base64Test.java
Mon Aug  8 13:04:56 2011
@@ -18,6 +18,7 @@ package org.apache.jackrabbit.util;
 
 import junit.framework.TestCase;
 
+import java.io.IOException;
 import java.io.StringWriter;
 import java.io.ByteArrayOutputStream;
 import java.io.ByteArrayInputStream;
@@ -96,4 +97,27 @@ public class Base64Test extends TestCase
             assertEquals(ba[i], ba2[i]);
         }
     }
+
+    public void testDecodeOrEncode() throws IOException {
+        assertEquals("", Base64.decodeOrEncode(Base64.decodeOrEncode("")));
+        assertEquals("test", Base64.decodeOrEncode(Base64.decodeOrEncode("test")));
+        assertEquals("{base64}dGVzdA==", Base64.decodeOrEncode("test"));
+        assertEquals("test", Base64.decodeOrEncode("{base64}dGVzdA=="));
+    }
+
+    public void testDecodeIfEncoded() throws IOException {
+        assertEquals(null, Base64.decodeIfEncoded(null));
+        assertEquals("", Base64.decodeIfEncoded(""));
+        assertEquals("", Base64.decodeIfEncoded("{base64}"));
+        assertEquals("test", Base64.decodeIfEncoded("test"));
+        assertEquals("test", Base64.decodeIfEncoded("{base64}dGVzdA=="));
+    }
+
+    public void testStringEncodeDecode() throws IOException {
+        assertEquals("", Base64.decode(Base64.encode("")));
+        assertEquals("test", Base64.decode(Base64.encode("test")));
+        assertEquals("dGVzdA==", Base64.encode("test"));
+        assertEquals("test", Base64.decode("dGVzdA=="));
+    }
+
 }
\ No newline at end of file



Mime
View raw message