From do-not-reply at jboss.org Mon Aug 9 11:54:06 2010 Content-Type: multipart/mixed; boundary="===============7993864521643519848==" MIME-Version: 1.0 From: do-not-reply at jboss.org To: gatein-commits at lists.jboss.org Subject: [gatein-commits] gatein SVN: r3781 - in components/wsrp/trunk/common/src: test/java/org/gatein/wsrp and 1 other directory. Date: Mon, 09 Aug 2010 11:54:06 -0400 Message-ID: <201008091554.o79Fs6c7014924@svn01.web.mwc.hst.phx2.redhat.com> --===============7993864521643519848== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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/WSRPRenderURL= TestCase.java Modified: components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPPortletUR= L.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 prop= erly when URLs affecting the public NS are generated. Modified: components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPPo= rtletURL.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPPortletU= RL.java 2010-08-09 13:44:54 UTC (rev 3780) +++ components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPPortletU= RL.java 2010-08-09 15:54:05 UTC (rev 3781) @@ -51,10 +51,10 @@ { private static final Logger log =3D LoggerFactory.getLogger(WSRPPortlet= URL.class); = - private static final String EQUALS =3D "=3D"; + protected static final String EQUALS =3D "=3D"; + protected static final String AMPERSAND =3D "&"; = private static final String ENCODED_AMPERSAND =3D "&"; - private static final String AMPERSAND =3D "&"; private static final String AMP_AMP =3D "&amp;"; = private static final String PARAM_SEPARATOR =3D "|"; Modified: components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPRe= nderURL.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPRenderUR= L.java 2010-08-09 13:44:54 UTC (rev 3780) +++ components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPRenderUR= L.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 Julien Viet @@ -55,12 +60,12 @@ { super.dealWithSpecificParams(params, originalURL); = - /*String paramValue =3D getRawParameterValueFor(params, WSRP2Rewriti= ngConstants.NAVIGATIONAL_VALUES); + String paramValue =3D getRawParameterValueFor(params, WSRP2Rewriting= Constants.NAVIGATIONAL_VALUES); if (paramValue !=3D null) { publicNSChanges =3D 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= -navigationalValues - if(publicNSChanges !=3D null) + if (publicNSChanges !=3D null) { createURLParameter(sb, WSRP2RewritingConstants.NAVIGATIONAL_VALUE= S, encodePublicNS(publicNSChanges)); - }*/ + } } + + /** + * Encodes the public NS according to the rules found at + * http://docs.oasis-open.org/wsrp/v2/wsrp-2.0-spec-os-01.html#_wsrp-na= vigationalValues + * + * @param publicNSChanges + * @return + */ + protected static String encodePublicNS(Map publicNSCh= anges) + { + if (ParameterValidation.existsAndIsNotEmpty(publicNSChanges)) + { + StringBuilder sb =3D new StringBuilder(128); + + Set> entries =3D publicNSChanges.entr= ySet(); + int entryNb =3D entries.size(); + int currentEntry =3D 0; + for (Map.Entry entry : entries) + { + String name =3D entry.getKey(); + String[] values =3D entry.getValue(); + + if (ParameterValidation.existsAndIsNotEmpty(values)) + { + int valueNb =3D values.length; + int currentValueIndex =3D 0; + for (String value : values) + { + sb.append(name).append("=3D").append(value); + if (currentValueIndex++ !=3D valueNb - 1) + { + sb.append("&"); + } + } + } + else + { + sb.append(name); + } + + if (currentEntry++ !=3D entryNb - 1) + { + sb.append("&"); + } + } + + return URLTools.encodeXWWWFormURL(sb.toString()); + } + else + { + return null; + } + } + + protected static Map decodePublicNS(String paramValue) + { + if (!ParameterValidation.isNullOrEmpty(paramValue)) + { + String encodedURL =3D URLTools.decodeXWWWFormURL(paramValue); + Map publicNS =3D new HashMap(= 7); + + boolean finished =3D false; + while (encodedURL.length() > 0 && !finished) + { + int endParamIndex =3D encodedURL.indexOf(AMPERSAND); + String param; + if (endParamIndex < 0) + { + // no param left: try the remainder of the String + param =3D encodedURL; + finished =3D true; + } + else + { + param =3D encodedURL.substring(0, endParamIndex); + } + + int equalsIndex =3D param.indexOf(EQUALS); + if (equalsIndex < 0) + { + publicNS.put(param, null); + } + else + { + // extract param name + String name =3D param.substring(0, equalsIndex); + // extract param value + String value =3D param.substring(equalsIndex + EQUALS.lengt= h(), param.length()); + + WSRPUtils.addMultiValuedValueTo(publicNS, name, value); + } + encodedURL =3D encodedURL.substring(endParamIndex + AMPERSAND.= length()); + } + + return publicNS; + } + else + { + return null; + } + } } Modified: components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPUt= ils.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPUtils.ja= va 2010-08-09 13:44:54 UTC (rev 3780) +++ components/wsrp/trunk/common/src/main/java/org/gatein/wsrp/WSRPUtils.ja= va 2010-08-09 15:54:05 UTC (rev 3781) @@ -545,24 +545,29 @@ for (NamedString publicParam : publicParams) { String paramName =3D publicParam.getName(); - String[] values =3D publicNS.get(paramName); - if (ParameterValidation.existsAndIsNotEmpty(values)) - { - int valuesNb =3D values.length; - String[] newValues =3D new String[valuesNb + 1]; - System.arraycopy(values, 0, newValues, 0, valuesNb); - newValues[valuesNb] =3D publicParam.getValue(); - publicNS.put(paramName, newValues); - } - else - { - values =3D new String[]{publicParam.getValue()}; - publicNS.put(paramName, values); - } + addMultiValuedValueTo(publicNS, paramName, publicParam.getValue()= ); } return publicNS; } = + public static void addMultiValuedValueTo(Map paramMap= , String paramName, String paramValue) + { + String[] values =3D paramMap.get(paramName); + if (ParameterValidation.existsAndIsNotEmpty(values)) + { + int valuesNb =3D values.length; + String[] newValues =3D new String[valuesNb + 1]; + System.arraycopy(values, 0, newValues, 0, valuesNb); + newValues[valuesNb] =3D paramValue; + paramMap.put(paramName, newValues); + } + else + { + values =3D new String[]{paramValue}; + paramMap.put(paramName, values); + } + } + /** * @author Chris Laprun * @version $Revision$ Added: components/wsrp/trunk/common/src/test/java/org/gatein/wsrp/WSRPRende= rURLTestCase.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- components/wsrp/trunk/common/src/test/java/org/gatein/wsrp/WSRPRenderUR= LTestCase.java (rev 0) +++ components/wsrp/trunk/common/src/test/java/org/gatein/wsrp/WSRPRenderUR= LTestCase.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 Chris Laprun + * @version $Revision$ + */ +public class WSRPRenderURLTestCase extends TestCase +{ + public void testNullPublicNavigationalState() + { + WSRPRenderURL url =3D new WSRPRenderURL(null, null, false, null, nul= l); + assertFalse(url.toString().contains(WSRP2RewritingConstants.NAVIGATI= ONAL_VALUES)); + } + + public void testEmptyPublicNavigationalState() + { + WSRPRenderURL url =3D new WSRPRenderURL(null, null, false, null, Col= lections.emptyMap()); + assertFalse(url.toString().contains(WSRP2RewritingConstants.NAVIGATI= ONAL_VALUES)); + } + + public void testPublicNavigationalStateEncoding() + { + //use a TreeMap here to guarantee order of parameters to facilitate = testing + Map publicNS =3D new TreeMap(); + publicNS.put("p1", new String[]{"value1", "value2"}); + publicNS.put("p2", null); + + String actual =3D WSRPRenderURL.encodePublicNS(publicNS); + assertEquals("p1%3Dvalue1%26p1%3Dvalue2%26p2", actual); + } + + public void testPublicNavigationalStateDecoding() + { + Map publicNS =3D 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"}, public= NS.get("p1")); + } +} --===============7993864521643519848==--