Author: chris.laprun(a)jboss.com
Date: 2009-01-10 08:21:11 -0500 (Sat, 10 Jan 2009)
New Revision: 12461
Modified:
modules/portlet/trunk/controller/src/main/java/org/jboss/portal/portlet/controller/impl/ControllerRequestFactory.java
modules/portlet/trunk/controller/src/main/java/org/jboss/portal/portlet/controller/state/PortletPageNavigationalStateSerialization.java
modules/portlet/trunk/portlet/src/main/java/org/jboss/portal/portlet/OpaqueStateString.java
modules/portlet/trunk/portlet/src/main/java/org/jboss/portal/portlet/ParametersStateString.java
modules/portlet/trunk/portlet/src/main/java/org/jboss/portal/portlet/StateString.java
modules/portlet/trunk/portlet/src/test/java/org/jboss/portal/test/portlet/StateStringTestCase.java
modules/portlet/trunk/portlet/src/test/java/org/jboss/portal/test/portlet/navigation/StateStringTestCase.java
Log:
- Extracted factory methods from ParameterStateString to StateString.
Modified:
modules/portlet/trunk/controller/src/main/java/org/jboss/portal/portlet/controller/impl/ControllerRequestFactory.java
===================================================================
---
modules/portlet/trunk/controller/src/main/java/org/jboss/portal/portlet/controller/impl/ControllerRequestFactory.java 2009-01-09
21:05:23 UTC (rev 12460)
+++
modules/portlet/trunk/controller/src/main/java/org/jboss/portal/portlet/controller/impl/ControllerRequestFactory.java 2009-01-10
13:21:11 UTC (rev 12461)
@@ -22,31 +22,30 @@
******************************************************************************/
package org.jboss.portal.portlet.controller.impl;
-import org.jboss.portal.portlet.controller.state.PortletPageNavigationalState;
-import org.jboss.portal.portlet.controller.state.PortletWindowNavigationalState;
+import org.jboss.portal.Mode;
+import org.jboss.portal.WindowState;
+import org.jboss.portal.common.io.IOTools;
+import org.jboss.portal.common.io.Serialization;
+import org.jboss.portal.common.io.SerializationFilter;
+import org.jboss.portal.common.util.Base64;
+import org.jboss.portal.common.util.MapAdapters;
+import org.jboss.portal.portlet.StateString;
+import org.jboss.portal.portlet.cache.CacheLevel;
import org.jboss.portal.portlet.controller.request.ControllerRequest;
import org.jboss.portal.portlet.controller.request.PortletActionRequest;
import org.jboss.portal.portlet.controller.request.PortletRenderRequest;
import org.jboss.portal.portlet.controller.request.PortletResourceRequest;
-import org.jboss.portal.portlet.StateString;
-import org.jboss.portal.portlet.ParametersStateString;
-import org.jboss.portal.portlet.cache.CacheLevel;
-import org.jboss.portal.common.util.Base64;
-import org.jboss.portal.common.util.MapAdapters;
-import org.jboss.portal.common.io.IOTools;
-import org.jboss.portal.common.io.SerializationFilter;
-import org.jboss.portal.common.io.Serialization;
-import org.jboss.portal.Mode;
-import org.jboss.portal.WindowState;
-import org.jboss.portal.web.WebRequest;
+import org.jboss.portal.portlet.controller.state.PortletPageNavigationalState;
+import org.jboss.portal.portlet.controller.state.PortletWindowNavigationalState;
import org.jboss.portal.web.Body;
+import org.jboss.portal.web.WebRequest;
import java.util.Map;
/**
- * A factory that provides a way to create ControllerRequest. This factory is just a
default implementation
- * and is not an authority which means that any client of the controller framework is
free to determine how
- * a controller request is created.
+ * A factory that provides a way to create ControllerRequest. This factory is just a
default implementation and is not
+ * an authority which means that any client of the controller framework is free to
determine how a controller request is
+ * created.
*
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
* @version $Revision: 630 $
@@ -77,7 +76,7 @@
* Create a controller request.
*
* @param queryParameters the query parameters
- * @param body the body
+ * @param body the body
* @return the decoded controller request
*/
public ControllerRequest decode(Map<String, String[]> queryParameters, Body
body)
@@ -98,7 +97,7 @@
* Create a controller request.
*
* @param queryParameterMap the query parameter map
- * @param formParameterMap the optional form parameter map
+ * @param formParameterMap the optional form parameter map
* @return the decoded controller request
*/
public ControllerRequest decode(Map<String, String[]> queryParameterMap,
Map<String, String[]> formParameterMap)
@@ -137,7 +136,7 @@
StateString navigationalState = null;
if (navigationalStateString != null)
{
- navigationalState = ParametersStateString.create(navigationalStateString);
+ navigationalState = StateString.create(navigationalStateString);
}
//
@@ -147,7 +146,7 @@
String phase =
queryParameters.get(ControllerRequestParameterNames.LIFECYCLE_PHASE);
if (ControllerRequestParameterNames.RESOURCE_PHASE.equals(phase))
{
- StateString resourceState =
ParametersStateString.create(queryParameters.get(ControllerRequestParameterNames.RESOURCE_STATE));
+ StateString resourceState =
StateString.create(queryParameters.get(ControllerRequestParameterNames.RESOURCE_STATE));
String resourceId =
queryParameters.get(ControllerRequestParameterNames.RESOURCE_ID);
//
@@ -182,7 +181,7 @@
{
if (ControllerRequestParameterNames.ACTION_PHASE.equals(phase))
{
- StateString interactionState =
ParametersStateString.create(queryParameters.get(ControllerRequestParameterNames.INTERACTION_STATE));
+ StateString interactionState =
StateString.create(queryParameters.get(ControllerRequestParameterNames.INTERACTION_STATE));
//
return new PortletActionRequest(
Modified:
modules/portlet/trunk/controller/src/main/java/org/jboss/portal/portlet/controller/state/PortletPageNavigationalStateSerialization.java
===================================================================
---
modules/portlet/trunk/controller/src/main/java/org/jboss/portal/portlet/controller/state/PortletPageNavigationalStateSerialization.java 2009-01-09
21:05:23 UTC (rev 12460)
+++
modules/portlet/trunk/controller/src/main/java/org/jboss/portal/portlet/controller/state/PortletPageNavigationalStateSerialization.java 2009-01-10
13:21:11 UTC (rev 12461)
@@ -22,20 +22,19 @@
******************************************************************************/
package org.jboss.portal.portlet.controller.state;
-import org.jboss.portal.common.io.Serialization;
-import org.jboss.portal.WindowState;
import org.jboss.portal.Mode;
+import org.jboss.portal.WindowState;
+import org.jboss.portal.common.io.Serialization;
import org.jboss.portal.portlet.StateString;
-import org.jboss.portal.portlet.ParametersStateString;
import javax.xml.namespace.QName;
-import java.io.OutputStream;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
-import java.io.DataOutputStream;
-import java.io.DataInputStream;
-import java.util.Map;
+import java.io.OutputStream;
import java.util.HashMap;
+import java.util.Map;
import java.util.Set;
/**
@@ -182,7 +181,7 @@
StateString portletNS = null;
if ((header & 1) != 0)
{
- portletNS = ParametersStateString.create(data);
+ portletNS = StateString.create(data);
}
WindowState windowState = null;
int windowStateHeader = (header & (7 << 1)) >> 1;
@@ -221,7 +220,7 @@
QName name = new QName(namespaceURI, localName);
int length = data.readInt();
String[] values = new String[length];
- for (int i = 0;i < length;i++)
+ for (int i = 0; i < length; i++)
{
values[i] = data.readUTF();
}
Modified:
modules/portlet/trunk/portlet/src/main/java/org/jboss/portal/portlet/OpaqueStateString.java
===================================================================
---
modules/portlet/trunk/portlet/src/main/java/org/jboss/portal/portlet/OpaqueStateString.java 2009-01-09
21:05:23 UTC (rev 12460)
+++
modules/portlet/trunk/portlet/src/main/java/org/jboss/portal/portlet/OpaqueStateString.java 2009-01-10
13:21:11 UTC (rev 12461)
@@ -53,7 +53,7 @@
public void writeTo(DataOutputStream out) throws IOException
{
- out.writeByte(2);
+ out.writeByte(StateString.OPAQUE);
out.writeUTF(value);
}
Modified:
modules/portlet/trunk/portlet/src/main/java/org/jboss/portal/portlet/ParametersStateString.java
===================================================================
---
modules/portlet/trunk/portlet/src/main/java/org/jboss/portal/portlet/ParametersStateString.java 2009-01-09
21:05:23 UTC (rev 12460)
+++
modules/portlet/trunk/portlet/src/main/java/org/jboss/portal/portlet/ParametersStateString.java 2009-01-10
13:21:11 UTC (rev 12461)
@@ -22,19 +22,13 @@
******************************************************************************/
package org.jboss.portal.portlet;
-import org.jboss.portal.common.util.Base64;
-import org.jboss.portal.common.util.ParameterMap;
import org.jboss.portal.common.io.Serialization;
+import org.jboss.portal.common.util.ParameterMap;
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
import java.io.IOException;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
import java.io.Serializable;
-import java.io.DataOutputStream;
-import java.io.DataInputStream;
-import java.util.HashMap;
import java.util.Map;
/**
@@ -46,15 +40,9 @@
public class ParametersStateString extends StateString implements Serializable
{
- /** . */
- public static final String JBPNS_PREFIX = "JBPNS_";
-
/** The serialVersionUID */
private static final long serialVersionUID = -8529807471117491810L;
- /** . */
- private static final String EOF = "__EOF__";
-
public static ParametersStateString create()
{
return new ParametersStateString();
@@ -66,58 +54,6 @@
}
/**
- * Factory method that will create the most appropriate form from the byte
representation.
- *
- * @param in the inputstream to read from
- * @return a new state string
- * @throws IOException any IOException
- */
- public static StateString create(DataInputStream in) throws IOException
- {
- if (in == null)
- {
- throw new IllegalArgumentException();
- }
-
- //
- byte b = in.readByte();
- switch (b)
- {
- case 0:
- return new ParametersStateString();
- case 1:
- return new ParametersStateString(in);
- case 2:
- return new OpaqueStateString(in.readUTF());
- }
-
- //
- throw new IllegalArgumentException("Wrong format unrecognized header " +
b);
- }
-
- /**
- * Factory method that will create the most appropriate form from the string
representation.
- *
- * @param opaqueValue the opaque value
- * @return a new state string
- */
- public static StateString create(String opaqueValue)
- {
- if (opaqueValue == null)
- {
- throw new IllegalArgumentException();
- }
- if (opaqueValue.startsWith(JBPNS_PREFIX))
- {
- return new ParametersStateString(opaqueValue);
- }
- else
- {
- return new OpaqueStateString(opaqueValue);
- }
- }
-
- /**
* Create a parameters state string. It assumes that the argument is either an
instance of
* <code>ParametersStateString</code> or that it is the string encoded
value of a
* <code>ParametersStateString</code>.
@@ -149,7 +85,7 @@
/** The underlying map that does not have a copy read/write access mode. */
private ParameterMap parameters;
- private ParametersStateString(DataInputStream in) throws IOException
+ protected ParametersStateString(DataInputStream in) throws IOException
{
Map<String, String[]> tmp = Serialization.PARAMETER_MAP.unserialize(in);
@@ -157,57 +93,13 @@
parameters = ParameterMap.wrap(tmp);
}
- private ParametersStateString(String opaqueValue)
+ protected ParametersStateString(String opaqueValue)
{
- if (!opaqueValue.startsWith(JBPNS_PREFIX))
+ Map<String, String[]> params = StateString.decodeOpaqueValue(opaqueValue);
+ if (!params.isEmpty())
{
- throw new IllegalArgumentException("Bad format");
+ parameters = ParameterMap.wrap(params);
}
-
- //
- opaqueValue = opaqueValue.substring(JBPNS_PREFIX.length());
- if (opaqueValue.length() > 0)
- {
- try
- {
- byte[] bytes = Base64.decode(opaqueValue, true);
- ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
- ObjectInputStream ois = new ObjectInputStream(bais);
- Map<String, String[]> params = new HashMap<String, String[]>();
-
- //
- String[] values;
-
- // read the first String which should be a param name
- String current = ois.readUTF();
-
- // keep reading until we haven't reached the EOF marker
- while (!EOF.equals(current))
- {
- // next is the size of the value array
- int length = ois.readInt();
- values = new String[length];
-
- // read as many Strings as are supposed to be in the array
- for (int i = 0; i < length; i++)
- {
- values[i] = ois.readUTF();
- }
-
- // we're done for this param, add it to the param map
- params.put(current, values);
-
- // read the next string to loop
- current = ois.readUTF();
- }
-
- parameters = ParameterMap.wrap(params);
- }
- catch (Exception e)
- {
- throw new Error(e);
- }
- }
else
{
parameters = new ParameterMap();
@@ -215,7 +107,7 @@
}
/** Creates an empty parameter set. */
- private ParametersStateString()
+ protected ParametersStateString()
{
this.parameters = new ParameterMap();
}
@@ -334,49 +226,18 @@
*/
public String getStringValue()
{
- if (parameters != null && parameters.size() != 0)
- {
- try
- {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- ObjectOutputStream oos = new ObjectOutputStream(baos);
- for (Map.Entry entry : parameters.entrySet())
- {
- String name = (String)entry.getKey();
- oos.writeUTF(name);
- String[] values = (String[])entry.getValue();
- int length = values.length;
- oos.writeInt(length);
- for (String value : values)
- {
- oos.writeUTF(value);
- }
- }
- oos.writeUTF(EOF);
- oos.close();
- byte[] bytes = baos.toByteArray();
- return JBPNS_PREFIX + Base64.encodeBytes(bytes, true);
- }
- catch (IOException e)
- {
- throw new RuntimeException(e);
- }
- }
- else
- {
- return JBPNS_PREFIX;
- }
+ return StateString.encodeAsOpaqueValue(parameters);
}
public void writeTo(DataOutputStream out) throws IOException
{
if (parameters.isEmpty())
{
- out.writeByte(0);
+ out.writeByte(StateString.EMPTY);
}
else
{
- out.writeByte(1);
+ out.writeByte(StateString.SERIALIZED);
Serialization.PARAMETER_MAP.serialize(parameters, out);
}
}
@@ -390,7 +251,7 @@
{
if (obj == this)
{
- return true;
+ return true;
}
if (obj instanceof ParametersStateString)
{
Modified:
modules/portlet/trunk/portlet/src/main/java/org/jboss/portal/portlet/StateString.java
===================================================================
---
modules/portlet/trunk/portlet/src/main/java/org/jboss/portal/portlet/StateString.java 2009-01-09
21:05:23 UTC (rev 12460)
+++
modules/portlet/trunk/portlet/src/main/java/org/jboss/portal/portlet/StateString.java 2009-01-10
13:21:11 UTC (rev 12461)
@@ -22,11 +22,19 @@
******************************************************************************/
package org.jboss.portal.portlet;
-import java.io.Serializable;
+import org.jboss.portal.common.util.Base64;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
-import java.io.ByteArrayOutputStream;
-import java.util.zip.GZIPOutputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
/**
* Encapsulate state as a string.
@@ -36,6 +44,14 @@
*/
public abstract class StateString implements Serializable
{
+ /** . */
+ public static final String JBPNS_PREFIX = "JBPNS_";
+ protected static final int EMPTY = 0;
+ protected static final int SERIALIZED = 1;
+ protected static final int OPAQUE = 2;
+ /** . */
+ private static final String EOF = "__EOF__";
+
/**
* Return the value of the navigational state of the portlet.
*
@@ -45,4 +61,149 @@
public abstract void writeTo(DataOutputStream out) throws IOException;
+ /**
+ * Factory method that will create the most appropriate form from the byte
representation.
+ *
+ * @param in the inputstream to read from
+ * @return a new state string
+ * @throws java.io.IOException any IOException
+ */
+ public static StateString create(DataInputStream in) throws IOException
+ {
+ if (in == null)
+ {
+ throw new IllegalArgumentException();
+ }
+
+ //
+ byte b = in.readByte();
+ switch (b)
+ {
+ case EMPTY:
+ return new ParametersStateString();
+ case SERIALIZED:
+ return new ParametersStateString(in);
+ case OPAQUE:
+ return new OpaqueStateString(in.readUTF());
+ }
+
+ //
+ throw new IllegalArgumentException("Wrong format unrecognized header " +
b);
+ }
+
+ /**
+ * Factory method that will create the most appropriate form from the string
representation.
+ *
+ * @param opaqueValue the opaque value
+ * @return a new state string
+ */
+ public static StateString create(String opaqueValue)
+ {
+ if (opaqueValue == null)
+ {
+ throw new IllegalArgumentException();
+ }
+ if (opaqueValue.startsWith(JBPNS_PREFIX))
+ {
+ return new ParametersStateString(opaqueValue);
+ }
+ else
+ {
+ return new OpaqueStateString(opaqueValue);
+ }
+ }
+
+ public static Map<String, String[]> decodeOpaqueValue(String opaqueValue)
+ {
+ if (!opaqueValue.startsWith(JBPNS_PREFIX))
+ {
+ throw new IllegalArgumentException("Bad format: [" + opaqueValue
+ + "] was not encoded by JBoss Portal and thus cannot be
decoded.");
+ }
+
+ //
+ opaqueValue = opaqueValue.substring(JBPNS_PREFIX.length());
+ if (opaqueValue.length() > 0)
+ {
+ try
+ {
+ byte[] bytes = Base64.decode(opaqueValue, true);
+ ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
+ ObjectInputStream ois = new ObjectInputStream(bais);
+ Map<String, String[]> params = new HashMap<String, String[]>();
+
+ //
+ String[] values;
+
+ // read the first String which should be a param name
+ String current = ois.readUTF();
+
+ // keep reading until we haven't reached the EOF marker
+ while (!EOF.equals(current))
+ {
+ // next is the size of the value array
+ int length = ois.readInt();
+ values = new String[length];
+
+ // read as many Strings as are supposed to be in the array
+ for (int i = 0; i < length; i++)
+ {
+ values[i] = ois.readUTF();
+ }
+
+ // we're done for this param, add it to the param map
+ params.put(current, values);
+
+ // read the next string to loop
+ current = ois.readUTF();
+ }
+
+ return params;
+ }
+ catch (Exception e)
+ {
+ throw new Error(e);
+ }
+ }
+ else
+ {
+ return Collections.emptyMap();
+ }
+ }
+
+ public static String encodeAsOpaqueValue(Map<String, String[]> parameters)
+ {
+ if (parameters != null && parameters.size() != 0)
+ {
+ try
+ {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ ObjectOutputStream oos = new ObjectOutputStream(baos);
+ for (Map.Entry entry : parameters.entrySet())
+ {
+ String name = (String)entry.getKey();
+ oos.writeUTF(name);
+ String[] values = (String[])entry.getValue();
+ int length = values.length;
+ oos.writeInt(length);
+ for (String value : values)
+ {
+ oos.writeUTF(value);
+ }
+ }
+ oos.writeUTF(EOF);
+ oos.close();
+ byte[] bytes = baos.toByteArray();
+ return JBPNS_PREFIX + Base64.encodeBytes(bytes, true);
+ }
+ catch (IOException e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+ else
+ {
+ return JBPNS_PREFIX;
+ }
+ }
}
Modified:
modules/portlet/trunk/portlet/src/test/java/org/jboss/portal/test/portlet/StateStringTestCase.java
===================================================================
---
modules/portlet/trunk/portlet/src/test/java/org/jboss/portal/test/portlet/StateStringTestCase.java 2009-01-09
21:05:23 UTC (rev 12460)
+++
modules/portlet/trunk/portlet/src/test/java/org/jboss/portal/test/portlet/StateStringTestCase.java 2009-01-10
13:21:11 UTC (rev 12461)
@@ -22,19 +22,18 @@
******************************************************************************/
package org.jboss.portal.test.portlet;
-import org.jboss.unit.api.pojo.annotations.Test;
+import org.jboss.portal.common.util.MapBuilder;
+import org.jboss.portal.portlet.OpaqueStateString;
import org.jboss.portal.portlet.ParametersStateString;
import org.jboss.portal.portlet.StateString;
-import org.jboss.portal.portlet.OpaqueStateString;
-import org.jboss.portal.common.util.MapBuilder;
-import static org.jboss.unit.api.Assert.*;
+import static org.jboss.unit.api.Assert.assertEquals;
+import org.jboss.unit.api.pojo.annotations.Test;
+import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
+import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
-import java.io.ByteArrayInputStream;
-import java.io.DataInputStream;
-import java.util.zip.GZIPOutputStream;
import java.util.Map;
/**
@@ -68,7 +67,7 @@
byte[] bytes = baos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
DataInputStream in = new DataInputStream(bais);
- StateString copy = ParametersStateString.create(in);
+ StateString copy = StateString.create(in);
assertEquals(parameters, copy);
}
}
Modified:
modules/portlet/trunk/portlet/src/test/java/org/jboss/portal/test/portlet/navigation/StateStringTestCase.java
===================================================================
---
modules/portlet/trunk/portlet/src/test/java/org/jboss/portal/test/portlet/navigation/StateStringTestCase.java 2009-01-09
21:05:23 UTC (rev 12460)
+++
modules/portlet/trunk/portlet/src/test/java/org/jboss/portal/test/portlet/navigation/StateStringTestCase.java 2009-01-10
13:21:11 UTC (rev 12461)
@@ -24,10 +24,11 @@
package org.jboss.portal.test.portlet.navigation;
import org.jboss.portal.portlet.ParametersStateString;
+import org.jboss.portal.portlet.StateString;
+import static org.jboss.unit.api.Assert.assertEquals;
+import static org.jboss.unit.api.Assert.assertNotNull;
import org.jboss.unit.api.pojo.annotations.Test;
-import static org.jboss.unit.api.Assert.*;
-
/**
* @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
* @version $Revision: 5976 $
@@ -51,7 +52,7 @@
System.out.println("opaqueValue = " + opaqueValue);
assertNotNull(opaqueValue);
- ns = (ParametersStateString)ParametersStateString.create(opaqueValue);
+ ns = (ParametersStateString)StateString.create(opaqueValue);
assertEquals(VALUE1, ns.getValue(NAME1));
}
}