Author: chris.laprun(a)jboss.com
Date: 2010-08-09 11:54:05 -0400 (Mon, 09 Aug 2010)
New Revision: 3781
Added:
components/wsrp/trunk/common/src/test/java/org/gatein/wsrp/WSRPRenderURLTestCase.java
Modified:
components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPPortletURL.java
components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPRenderURL.java
components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPUtils.java
Log:
- GTNWSRP-38: Fixed missing URL encoding and decoding. Should now work properly when URLs
affecting the public NS are generated.
Modified: components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPPortletURL.java
===================================================================
---
components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPPortletURL.java 2010-08-09
13:44:54 UTC (rev 3780)
+++
components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPPortletURL.java 2010-08-09
15:54:05 UTC (rev 3781)
@@ -51,10 +51,10 @@
{
private static final Logger log = LoggerFactory.getLogger(WSRPPortletURL.class);
- private static final String EQUALS = "=";
+ protected static final String EQUALS = "=";
+ protected static final String AMPERSAND = "&";
private static final String ENCODED_AMPERSAND = "&";
- private static final String AMPERSAND = "&";
private static final String AMP_AMP = "&";
private static final String PARAM_SEPARATOR = "|";
Modified: components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPRenderURL.java
===================================================================
---
components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPRenderURL.java 2010-08-09
13:44:54 UTC (rev 3780)
+++
components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPRenderURL.java 2010-08-09
15:54:05 UTC (rev 3781)
@@ -23,12 +23,17 @@
package org.gatein.wsrp;
+import org.gatein.common.net.URLTools;
+import org.gatein.common.util.ParameterValidation;
import org.gatein.pc.api.Mode;
import org.gatein.pc.api.RenderURL;
import org.gatein.pc.api.StateString;
import org.gatein.pc.api.WindowState;
+import org.gatein.wsrp.spec.v2.WSRP2RewritingConstants;
+import java.util.HashMap;
import java.util.Map;
+import java.util.Set;
/**
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
@@ -55,12 +60,12 @@
{
super.dealWithSpecificParams(params, originalURL);
- /*String paramValue = getRawParameterValueFor(params,
WSRP2RewritingConstants.NAVIGATIONAL_VALUES);
+ String paramValue = getRawParameterValueFor(params,
WSRP2RewritingConstants.NAVIGATIONAL_VALUES);
if (paramValue != null)
{
publicNSChanges = decodePublicNS(paramValue);
params.remove(WSRP2RewritingConstants.NAVIGATIONAL_VALUES);
- }*/
+ }
}
protected String getURLType()
@@ -75,12 +80,110 @@
protected void appendEnd(StringBuffer sb)
{
- /*
- // todo: publicNS must be encoded according to rules found at:
- //
http://docs.oasis-open.org/wsrp/v2/wsrp-2.0-spec-os-01.html#_wsrp-navigat...
- if(publicNSChanges != null)
+ if (publicNSChanges != null)
{
createURLParameter(sb, WSRP2RewritingConstants.NAVIGATIONAL_VALUES,
encodePublicNS(publicNSChanges));
- }*/
+ }
}
+
+ /**
+ * Encodes the public NS according to the rules found at <a
href='http://docs.oasis-open.org/wsrp/v2/wsrp-2.0-spec-os-01.html#_ws...
+ *
http://docs.oasis-open.org/wsrp/v2/wsrp-2.0-spec-os-01.html#_wsrp-navigat...
+ *
+ * @param publicNSChanges
+ * @return
+ */
+ protected static String encodePublicNS(Map<String, String[]> publicNSChanges)
+ {
+ if (ParameterValidation.existsAndIsNotEmpty(publicNSChanges))
+ {
+ StringBuilder sb = new StringBuilder(128);
+
+ Set<Map.Entry<String, String[]>> entries =
publicNSChanges.entrySet();
+ int entryNb = entries.size();
+ int currentEntry = 0;
+ for (Map.Entry<String, String[]> entry : entries)
+ {
+ String name = entry.getKey();
+ String[] values = entry.getValue();
+
+ if (ParameterValidation.existsAndIsNotEmpty(values))
+ {
+ int valueNb = values.length;
+ int currentValueIndex = 0;
+ for (String value : values)
+ {
+ sb.append(name).append("=").append(value);
+ if (currentValueIndex++ != valueNb - 1)
+ {
+ sb.append("&");
+ }
+ }
+ }
+ else
+ {
+ sb.append(name);
+ }
+
+ if (currentEntry++ != entryNb - 1)
+ {
+ sb.append("&");
+ }
+ }
+
+ return URLTools.encodeXWWWFormURL(sb.toString());
+ }
+ else
+ {
+ return null;
+ }
+ }
+
+ protected static Map<String, String[]> decodePublicNS(String paramValue)
+ {
+ if (!ParameterValidation.isNullOrEmpty(paramValue))
+ {
+ String encodedURL = URLTools.decodeXWWWFormURL(paramValue);
+ Map<String, String[]> publicNS = new HashMap<String, String[]>(7);
+
+ boolean finished = false;
+ while (encodedURL.length() > 0 && !finished)
+ {
+ int endParamIndex = encodedURL.indexOf(AMPERSAND);
+ String param;
+ if (endParamIndex < 0)
+ {
+ // no param left: try the remainder of the String
+ param = encodedURL;
+ finished = true;
+ }
+ else
+ {
+ param = encodedURL.substring(0, endParamIndex);
+ }
+
+ int equalsIndex = param.indexOf(EQUALS);
+ if (equalsIndex < 0)
+ {
+ publicNS.put(param, null);
+ }
+ else
+ {
+ // extract param name
+ String name = param.substring(0, equalsIndex);
+ // extract param value
+ String value = param.substring(equalsIndex + EQUALS.length(),
param.length());
+
+ WSRPUtils.addMultiValuedValueTo(publicNS, name, value);
+ }
+ encodedURL = encodedURL.substring(endParamIndex + AMPERSAND.length());
+ }
+
+ return publicNS;
+ }
+ else
+ {
+ return null;
+ }
+ }
}
Modified: components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPUtils.java
===================================================================
--- components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPUtils.java 2010-08-09
13:44:54 UTC (rev 3780)
+++ components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPUtils.java 2010-08-09
15:54:05 UTC (rev 3781)
@@ -545,24 +545,29 @@
for (NamedString publicParam : publicParams)
{
String paramName = publicParam.getName();
- String[] values = publicNS.get(paramName);
- if (ParameterValidation.existsAndIsNotEmpty(values))
- {
- int valuesNb = values.length;
- String[] newValues = new String[valuesNb + 1];
- System.arraycopy(values, 0, newValues, 0, valuesNb);
- newValues[valuesNb] = publicParam.getValue();
- publicNS.put(paramName, newValues);
- }
- else
- {
- values = new String[]{publicParam.getValue()};
- publicNS.put(paramName, values);
- }
+ addMultiValuedValueTo(publicNS, paramName, publicParam.getValue());
}
return publicNS;
}
+ public static void addMultiValuedValueTo(Map<String, String[]> paramMap, String
paramName, String paramValue)
+ {
+ String[] values = paramMap.get(paramName);
+ if (ParameterValidation.existsAndIsNotEmpty(values))
+ {
+ int valuesNb = values.length;
+ String[] newValues = new String[valuesNb + 1];
+ System.arraycopy(values, 0, newValues, 0, valuesNb);
+ newValues[valuesNb] = paramValue;
+ paramMap.put(paramName, newValues);
+ }
+ else
+ {
+ values = new String[]{paramValue};
+ paramMap.put(paramName, values);
+ }
+ }
+
/**
* @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
* @version $Revision$
Added:
components/wsrp/trunk/common/src/test/java/org/gatein/wsrp/WSRPRenderURLTestCase.java
===================================================================
--- components/wsrp/trunk/common/src/test/java/org/gatein/wsrp/WSRPRenderURLTestCase.java
(rev 0)
+++
components/wsrp/trunk/common/src/test/java/org/gatein/wsrp/WSRPRenderURLTestCase.java 2010-08-09
15:54:05 UTC (rev 3781)
@@ -0,0 +1,72 @@
+/*
+ * JBoss, a division of Red Hat
+ * Copyright 2010, Red Hat Middleware, LLC, and individual
+ * contributors as indicated by the @authors tag. See the
+ * copyright.txt in the distribution for a full listing of
+ * individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.gatein.wsrp;
+
+import junit.framework.TestCase;
+import org.gatein.wsrp.spec.v2.WSRP2RewritingConstants;
+import org.gatein.wsrp.test.ExtendedAssert;
+
+import java.util.Collections;
+import java.util.Map;
+import java.util.TreeMap;
+
+/**
+ * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
+ * @version $Revision$
+ */
+public class WSRPRenderURLTestCase extends TestCase
+{
+ public void testNullPublicNavigationalState()
+ {
+ WSRPRenderURL url = new WSRPRenderURL(null, null, false, null, null);
+ assertFalse(url.toString().contains(WSRP2RewritingConstants.NAVIGATIONAL_VALUES));
+ }
+
+ public void testEmptyPublicNavigationalState()
+ {
+ WSRPRenderURL url = new WSRPRenderURL(null, null, false, null,
Collections.<String, String[]>emptyMap());
+ assertFalse(url.toString().contains(WSRP2RewritingConstants.NAVIGATIONAL_VALUES));
+ }
+
+ public void testPublicNavigationalStateEncoding()
+ {
+ //use a TreeMap here to guarantee order of parameters to facilitate testing
+ Map<String, String[]> publicNS = new TreeMap<String, String[]>();
+ publicNS.put("p1", new String[]{"value1",
"value2"});
+ publicNS.put("p2", null);
+
+ String actual = WSRPRenderURL.encodePublicNS(publicNS);
+ assertEquals("p1%3Dvalue1%26p1%3Dvalue2%26p2", actual);
+ }
+
+ public void testPublicNavigationalStateDecoding()
+ {
+ Map<String, String[]> publicNS =
WSRPRenderURL.decodePublicNS("p1%3Dvalue1%26p1%3Dvalue2%26p2");
+
+ assertEquals(2, publicNS.size());
+ assertTrue(publicNS.containsKey("p2"));
+ assertEquals(null, publicNS.get("p2"));
+ ExtendedAssert.assertEquals(new String[]{"value1", "value2"},
publicNS.get("p1"));
+ }
+}