You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ke...@apache.org on 2012/03/23 14:46:36 UTC

svn commit: r1304344 - in /incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src: main/java/org/apache/isis/runtimes/dflt/objectstores/sql/jdbc/ main/java/org/apache/isis/runtimes/dflt/objectstores/sql/jdbc/helpers/ test/java/org/a...

Author: kevin
Date: Fri Mar 23 13:46:36 2012
New Revision: 1304344

URL: http://svn.apache.org/viewvc?rev=1304344&view=rev
Log:
ISIS-219: Extracted SimplePasswordEncoderDecoder class

Added:
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/jdbc/helpers/
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/jdbc/helpers/SimplePasswordEncoderDecoder.java
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/test/java/org/apache/isis/runtimes/dflt/objectstores/sql/jdbc/helpers/
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/test/java/org/apache/isis/runtimes/dflt/objectstores/sql/jdbc/helpers/SimplePasswordEncoderDecoderTest.java
Removed:
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/test/java/org/apache/isis/runtimes/dflt/objectstores/sql/jdbc/JdbcPasswordValueMapperTest.java
Modified:
    incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/jdbc/JdbcPasswordValueMapper.java

Modified: incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/jdbc/JdbcPasswordValueMapper.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/jdbc/JdbcPasswordValueMapper.java?rev=1304344&r1=1304343&r2=1304344&view=diff
==============================================================================
--- incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/jdbc/JdbcPasswordValueMapper.java (original)
+++ incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/jdbc/JdbcPasswordValueMapper.java Fri Mar 23 13:46:36 2012
@@ -23,6 +23,7 @@ import org.apache.isis.core.metamodel.sp
 import org.apache.isis.core.metamodel.spec.feature.ObjectAssociation;
 import org.apache.isis.runtimes.dflt.objectstores.sql.AbstractFieldMappingFactory;
 import org.apache.isis.runtimes.dflt.objectstores.sql.Results;
+import org.apache.isis.runtimes.dflt.objectstores.sql.jdbc.helpers.SimplePasswordEncoderDecoder;
 import org.apache.isis.runtimes.dflt.objectstores.sql.mapping.FieldMapping;
 
 /**
@@ -52,22 +53,15 @@ public class JdbcPasswordValueMapper ext
     }
 
     private final String type;
-    private final String passwordSeed;
-    private final int seedLength;
-    private final Integer valueOfZero = Integer.valueOf('0');
-    private final int dbLength;
+
+    private final SimplePasswordEncoderDecoder simplePasswordEncoderDecoder;
 
     public JdbcPasswordValueMapper(final ObjectAssociation field, final String type, final String passwordSeed,
         final Integer encLength) {
         super(field);
         this.type = type;
-        this.passwordSeed = passwordSeed;
-        if (passwordSeed == null) {
-            seedLength = 0;
-        } else {
-            seedLength = passwordSeed.length();
-        }
-        dbLength = encLength;
+
+        simplePasswordEncoderDecoder = new SimplePasswordEncoderDecoder(passwordSeed, encLength);
     }
 
     /*
@@ -90,7 +84,7 @@ public class JdbcPasswordValueMapper ext
         }
         final Object o = value.getObject();
         final String rawPassword = ((Password) o).getPassword();
-        return encodeRawValueIntoEncodedString(rawPassword);
+        return simplePasswordEncoderDecoder.encodeRawValueIntoEncodedString(rawPassword);
     }
 
     /*
@@ -106,82 +100,7 @@ public class JdbcPasswordValueMapper ext
             return null;
         }
         final EncodableFacet facet = field.getSpecification().getFacet(EncodableFacet.class);
-        return facet.fromEncodedString(decodeEncodedValueIntoRawString(encodedValue));
-    }
-
-    /**
-     * Use a simple algorithm to encode the given value into an encoded String
-     * 
-     * @param String
-     *            raw value
-     * @return encoded String
-     */
-    protected final String encodeRawValueIntoEncodedString(final String value) {
-        if (passwordSeed == null) {
-            return value;
-        }
-        final int rawLength = value.length();
-        String length = Integer.toHexString(rawLength);
-        if (length.length() == 1) {
-            length = "0" + length;
-        }
-        String encodePart1 = length + value;
-        String encoded = "";
-        for (int i = 0; i < rawLength + 2; i++) {
-            int thisSeed = passwordSeed.charAt(i % seedLength);
-            int thisPassword = encodePart1.charAt(i);
-            int nextValue = (thisSeed + thisPassword) % 255;
-            encoded = encoded.concat(String.format("%2h", nextValue));
-
-        }
-        for (int i = rawLength; i < (dbLength / 2) - 2; i++) {
-            int thisSeed = passwordSeed.charAt(i % seedLength);
-            int thisPassword = passwordSeed.charAt((i - 2) % seedLength);
-            int nextValue = (thisSeed + thisPassword + i) % 255;
-            encoded = encoded.concat(String.format("%2h", nextValue));
-        }
-
-        return encoded;
-    }
-
-    /**
-     * Use a simple algorithm to decode the given encoded String into a raw String
-     * 
-     * @param String
-     *            encoded value
-     * @return decoded raw String
-     */
-    protected final String decodeEncodedValueIntoRawString(final String encodedValue) {
-        if (passwordSeed == null) {
-            return encodedValue;
-        }
-        int passwordLength = extractIndexedValueAsInt(encodedValue, 0);
-
-        String decodedValue = "";
-        for (int i = 0; i < passwordLength; i++) {
-            char extracted = extractIndexedValueAsChar(encodedValue, i + 2);
-            decodedValue = decodedValue + (extracted);
-        }
-        return decodedValue;
-    }
-
-    private int extractIndexedValueAsInt(final String encodedValue, int index) {
-        int value1 = decodeIndexedValue(encodedValue, index) - valueOfZero;
-        int value2 = decodeIndexedValue(encodedValue, index + 1) - valueOfZero;
-        return value1 * 16 + value2;
-    }
-
-    private char extractIndexedValueAsChar(final String encodedValue, int index) {
-        int value1 = decodeIndexedValue(encodedValue, index);
-        return (char) value1;
-    }
-
-    private int decodeIndexedValue(final String encodedValue, int index) {
-        String s = encodedValue.substring((index) * 2, (index) * 2 + 2);
-        int hex = Integer.valueOf(s, 16);
-        int thisSeed = passwordSeed.charAt(index % seedLength);
-        int passwordValue = hex - thisSeed;
-        return passwordValue;
+        return facet.fromEncodedString(simplePasswordEncoderDecoder.decodeEncodedValueIntoRawString(encodedValue));
     }
 
 }

Added: incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/jdbc/helpers/SimplePasswordEncoderDecoder.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/jdbc/helpers/SimplePasswordEncoderDecoder.java?rev=1304344&view=auto
==============================================================================
--- incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/jdbc/helpers/SimplePasswordEncoderDecoder.java (added)
+++ incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/main/java/org/apache/isis/runtimes/dflt/objectstores/sql/jdbc/helpers/SimplePasswordEncoderDecoder.java Fri Mar 23 13:46:36 2012
@@ -0,0 +1,115 @@
+/**
+ *  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.isis.runtimes.dflt.objectstores.sql.jdbc.helpers;
+
+/**
+ * 
+ * 
+ * @version $Rev$ $Date$
+ */
+public class SimplePasswordEncoderDecoder {
+    private final String passwordSeed;
+    private final int seedLength;
+    private final Integer valueOfZero = Integer.valueOf('0');
+    private final int dbLength;
+
+    public SimplePasswordEncoderDecoder(String passwordSeed, Integer encLength) {
+        this.passwordSeed = passwordSeed;
+        if (passwordSeed == null) {
+            seedLength = 0;
+        } else {
+            seedLength = passwordSeed.length();
+        }
+        dbLength = encLength;
+    }
+
+    /**
+     * Use a simple algorithm to encode the given value into an encoded String
+     * 
+     * @param String
+     *            raw value
+     * @return encoded String
+     */
+    public final String encodeRawValueIntoEncodedString(final String value) {
+        if (passwordSeed == null) {
+            return value;
+        }
+        final int rawLength = value.length();
+        String length = Integer.toHexString(rawLength);
+        if (length.length() == 1) {
+            length = "0" + length;
+        }
+        String encodePart1 = length + value;
+        String encoded = "";
+        for (int i = 0; i < rawLength + 2; i++) {
+            int thisSeed = passwordSeed.charAt(i % seedLength);
+            int thisPassword = encodePart1.charAt(i);
+            int nextValue = (thisSeed + thisPassword) % 255;
+            encoded = encoded.concat(String.format("%2h", nextValue));
+
+        }
+        for (int i = rawLength; i < (dbLength / 2) - 2; i++) {
+            int thisSeed = passwordSeed.charAt(i % seedLength);
+            int thisPassword = passwordSeed.charAt((i - 2) % seedLength);
+            int nextValue = (thisSeed + thisPassword + i) % 255;
+            encoded = encoded.concat(String.format("%2h", nextValue));
+        }
+
+        return encoded;
+    }
+
+    /**
+     * Use a simple algorithm to decode the given encoded String into a raw String
+     * 
+     * @param String
+     *            encoded value
+     * @return decoded raw String
+     */
+    public final String decodeEncodedValueIntoRawString(final String encodedValue) {
+        if (passwordSeed == null) {
+            return encodedValue;
+        }
+        int passwordLength = extractIndexedValueAsInt(encodedValue, 0);
+
+        String decodedValue = "";
+        for (int i = 0; i < passwordLength; i++) {
+            char extracted = extractIndexedValueAsChar(encodedValue, i + 2);
+            decodedValue = decodedValue + (extracted);
+        }
+        return decodedValue;
+    }
+
+    private int extractIndexedValueAsInt(final String encodedValue, int index) {
+        int value1 = decodeIndexedValue(encodedValue, index) - valueOfZero;
+        int value2 = decodeIndexedValue(encodedValue, index + 1) - valueOfZero;
+        return value1 * 16 + value2;
+    }
+
+    private char extractIndexedValueAsChar(final String encodedValue, int index) {
+        int value1 = decodeIndexedValue(encodedValue, index);
+        return (char) value1;
+    }
+
+    private int decodeIndexedValue(final String encodedValue, int index) {
+        String s = encodedValue.substring((index) * 2, (index) * 2 + 2);
+        int hex = Integer.valueOf(s, 16);
+        int thisSeed = passwordSeed.charAt(index % seedLength);
+        int passwordValue = hex - thisSeed;
+        return passwordValue;
+    }
+
+}

Added: incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/test/java/org/apache/isis/runtimes/dflt/objectstores/sql/jdbc/helpers/SimplePasswordEncoderDecoderTest.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/test/java/org/apache/isis/runtimes/dflt/objectstores/sql/jdbc/helpers/SimplePasswordEncoderDecoderTest.java?rev=1304344&view=auto
==============================================================================
--- incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/test/java/org/apache/isis/runtimes/dflt/objectstores/sql/jdbc/helpers/SimplePasswordEncoderDecoderTest.java (added)
+++ incubator/isis/trunk/framework/runtimes/dflt/objectstores/sql/sql-impl/src/test/java/org/apache/isis/runtimes/dflt/objectstores/sql/jdbc/helpers/SimplePasswordEncoderDecoderTest.java Fri Mar 23 13:46:36 2012
@@ -0,0 +1,66 @@
+/**
+ *  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.isis.runtimes.dflt.objectstores.sql.jdbc.helpers;
+
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.not;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * 
+ * 
+ * @version $Rev$ $Date$
+ */
+public class SimplePasswordEncoderDecoderTest {
+
+    private static final String testSeed = "randomString12345";
+    private static final Integer encLength = 120;
+
+    @Test
+    public void testEncodingValueFromString() {
+        SimplePasswordEncoderDecoder encdec = new SimplePasswordEncoderDecoder(testSeed, encLength);
+        String input = "password";
+        String encoded = encdec.encodeRawValueIntoEncodedString(input);
+        Assert.assertThat(encoded, is(not(input)));
+        Assert.assertThat(encoded.length(), is(120));
+    }
+
+    @Test
+    public void testDecodingEncodedValue() {
+        SimplePasswordEncoderDecoder encdec = new SimplePasswordEncoderDecoder(testSeed, encLength);
+        String input = "password";
+        String encoded = encdec.encodeRawValueIntoEncodedString(input);
+        Assert.assertThat(encoded, is(not(input)));
+
+        String decoded = encdec.decodeEncodedValueIntoRawString(encoded);
+        Assert.assertThat(decoded, is(input));
+    }
+
+    @Test
+    public void testNoSeedDoesNothing() {
+        SimplePasswordEncoderDecoder encdec = new SimplePasswordEncoderDecoder(null, encLength);
+        String input = "password";
+        String encoded = encdec.encodeRawValueIntoEncodedString(input);
+        Assert.assertThat(encoded, is(input));
+
+        String decoded = encdec.decodeEncodedValueIntoRawString(encoded);
+        Assert.assertThat(decoded, is(input));
+    }
+
+}