You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jspwiki.apache.org by aj...@apache.org on 2008/05/23 06:05:26 UTC
svn commit: r659408 -
/incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/util/Serializer.java
Author: ajaquith
Date: Thu May 22 21:05:25 2008
New Revision: 659408
URL: http://svn.apache.org/viewvc?rev=659408&view=rev
Log:
Checked in three new features for UserProfile and the UserDatabase classes. First, profiles can now store arbitrary Serializable objects via a new method in UserProfile, getAttributes, that returns a Map<Serializable,Serializable> that can be directly manipulated. Arbitrary attributes such as user preferences can be added to the profile and be guaranteed to be persisted on save. Second, the UserProfile now has two methods setLockExpiry(Date)/getLockExpiry that allow callers to disable user profiles. These are NOT enforced in AuthenticationManager yet. Third, user profile now have a 'uid' field that stores a long value for uniquely identifying users. Existing profiles without UIDs are automatically upgraded when they are loaded by a findBy___() method. The default XML/JDBC UserDatabase implementations have been enhanced to support all of these new features. If you have custom UserDatabase implementations, you should take a look at the new code.
Added:
incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/util/Serializer.java
Added: incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/util/Serializer.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/util/Serializer.java?rev=659408&view=auto
==============================================================================
--- incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/util/Serializer.java (added)
+++ incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/util/Serializer.java Thu May 22 21:05:25 2008
@@ -0,0 +1,68 @@
+package com.ecyrd.jspwiki.util;
+
+import java.io.*;
+import java.sql.SQLException;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.commons.codec.binary.Base64;
+
+public class Serializer
+{
+
+ /**
+ * Deserializes a Base64-encoded String into a HashMap. Both the keys and values
+ * must implement {@link java.io.Serializable}.
+ * @param rawString the String contents containing the map to be deserialized
+ * @return the attributes, parsed into a Properties object
+ * @throws SQLException if the contents cannot be parsed for any reason
+ */
+ @SuppressWarnings("unchecked")
+ public static Map<? extends Serializable,? extends Serializable> deserializeFromBase64( String rawString ) throws IOException
+ {
+ // Decode from Base64-encoded String to byte array
+ byte[] decodedBytes = Base64.decodeBase64( rawString.getBytes() );
+
+ // Deserialize from the input stream to the Map
+ InputStream bytesIn = new ByteArrayInputStream( decodedBytes );
+ ObjectInputStream in = new ObjectInputStream( bytesIn );
+ HashMap<Serializable,Serializable> attributes;
+ try
+ {
+ attributes = (HashMap<Serializable,Serializable>)in.readObject();
+ }
+ catch ( ClassNotFoundException e )
+ {
+ throw new IOException( "Could not deserialiaze user profile attributes. Reason: " + e.getMessage() );
+ }
+ finally
+ {
+ in.close();
+ }
+ return attributes;
+ }
+
+ /**
+ * Serializes a Map and formats it into a Base64-encoded String. For ease of serialization, the Map contents
+ * are first copied into a HashMap, then serialized into a byte array that is encoded as a Base64 String.
+ * @param the Map to serialize
+ * @return a String representing the serialized form of the Map
+ */
+ public static String serializeToBase64( Map<Serializable,Serializable> map ) throws IOException
+ {
+ // Load the Map contents into a defensive HashMap
+ HashMap<Serializable,Serializable> serialMap = new HashMap<Serializable,Serializable>();
+ serialMap.putAll( map );
+
+ // Serialize the Map to an output stream
+ ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
+ ObjectOutputStream out = new ObjectOutputStream( bytesOut );
+ out.writeObject( serialMap );
+ out.close();
+
+ // Transform to Base64-encoded String
+ byte[] result = Base64.encodeBase64( bytesOut.toByteArray() );
+ return new String( result ) ;
+ }
+
+}