Revision: 665
          http://stripes.svn.sourceforge.net/stripes/?rev=665&view=rev
Author:   bengunter
Date:     2007-12-11 10:26:47 -0800 (Tue, 11 Dec 2007)

Log Message:
-----------
STS-452: Allow encryption and decryption of ActionBean properties. If 
@Validate(encrypted=true) is set on an ActionBean property then that property's 
value will be encrypted when written to a page and *must* be encrypted for 
binding to succeed when it is submitted.

Currently encryption is only supported for form population and repopulation. 
Encryption support will be added to ParamTag next.

Added Paths:
-----------
    
trunk/stripes/src/net/sourceforge/stripes/format/EncryptedValueFormatter.java
    trunk/stripes/src/net/sourceforge/stripes/tag/EncryptedValue.java

Added: 
trunk/stripes/src/net/sourceforge/stripes/format/EncryptedValueFormatter.java
===================================================================
--- 
trunk/stripes/src/net/sourceforge/stripes/format/EncryptedValueFormatter.java   
                            (rev 0)
+++ 
trunk/stripes/src/net/sourceforge/stripes/format/EncryptedValueFormatter.java   
    2007-12-11 18:26:47 UTC (rev 665)
@@ -0,0 +1,76 @@
+/* Copyright 2007 Ben Gunter
+ *
+ * Licensed 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 net.sourceforge.stripes.format;
+
+import java.util.Locale;
+
+import net.sourceforge.stripes.controller.StripesFilter;
+import net.sourceforge.stripes.tag.EncryptedValue;
+import net.sourceforge.stripes.util.CryptoUtil;
+
+/**
+ * Finds the appropriate formatter for the value of an [EMAIL PROTECTED] 
EncryptedValue}, formats it, and
+ * encrypts the result.
+ * 
+ * @author Ben Gunter
+ */
+public class EncryptedValueFormatter implements Formatter<EncryptedValue> {
+    private String formatPattern, formatType;
+    private Locale locale;
+
+    public void init() {
+    }
+
+    public void setFormatPattern(String formatPattern) {
+        this.formatPattern = formatPattern;
+    }
+
+    public void setFormatType(String formatType) {
+        this.formatType = formatType;
+    }
+
+    public void setLocale(Locale locale) {
+        this.locale = locale;
+    }
+
+    @SuppressWarnings("unchecked")
+    public String format(EncryptedValue input) {
+        // null check
+        Object object;
+        if (input == null || (object = input.getValue()) == null) {
+            return "";
+        }
+
+        // look up the type converter
+        FormatterFactory factory = 
StripesFilter.getConfiguration().getFormatterFactory();
+        Formatter formatter = factory.getFormatter(object.getClass(), locale, 
formatType,
+                formatPattern);
+
+        // format the value to plain text
+        String value;
+        if (formatter != null)
+            value = formatter.format(object);
+        else
+            value = String.valueOf(object);
+
+        // encrypt the value
+        if (value == null)
+            value = "";
+        else
+            value = CryptoUtil.encrypt(value, input.getRequest());
+
+        return value;
+    }
+}

Added: trunk/stripes/src/net/sourceforge/stripes/tag/EncryptedValue.java
===================================================================
--- trunk/stripes/src/net/sourceforge/stripes/tag/EncryptedValue.java           
                (rev 0)
+++ trunk/stripes/src/net/sourceforge/stripes/tag/EncryptedValue.java   
2007-12-11 18:26:47 UTC (rev 665)
@@ -0,0 +1,46 @@
+/* Copyright 2007 Ben Gunter
+ *
+ * Licensed 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 net.sourceforge.stripes.tag;
+
+import javax.servlet.http.HttpServletRequest;
+
+/**
+ * A simple class that wraps an object that is intended to be encrypted before 
it is written into a
+ * JSP page. This class, coupled with
+ * 
+ * @author Ben Gunter
+ */
+public class EncryptedValue {
+    private Object value;
+    private HttpServletRequest request;
+
+    /**
+     * Create a new instance that wraps the given [EMAIL PROTECTED] value}. 
The encryption routine requires
+     * access to the [EMAIL PROTECTED] request} to get the encryption key.
+     */
+    public EncryptedValue(Object value, HttpServletRequest request) {
+        this.value = value;
+        this.request = request;
+    }
+
+    /** Get the actual value that is to be encrypted. */
+    public Object getValue() {
+        return value;
+    }
+
+    public HttpServletRequest getRequest() {
+        return request;
+    }
+}


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

-------------------------------------------------------------------------
SF.Net email is sponsored by: 
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
Stripes-development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/stripes-development

Reply via email to