You are viewing a plain text version of this content. The canonical link for it is here.
Posted to pluto-scm@portals.apache.org by cz...@apache.org on 2009/02/25 14:44:13 UTC
svn commit: r747775 - in /portals/pluto/trunk:
pluto-container/src/main/java/org/apache/pluto/util/
pluto-container/src/test/java/org/apache/pluto/util/
pluto-portal-driver-impl/src/main/java/org/apache/pluto/driver/url/impl/
Author: cziegeler
Date: Wed Feb 25 13:44:12 2009
New Revision: 747775
URL: http://svn.apache.org/viewvc?rev=747775&view=rev
Log:
Clean up string utils - the method is only used in the driver.
Modified:
portals/pluto/trunk/pluto-container/src/main/java/org/apache/pluto/util/StringUtils.java
portals/pluto/trunk/pluto-container/src/test/java/org/apache/pluto/util/StringUtilsTest.java
portals/pluto/trunk/pluto-portal-driver-impl/src/main/java/org/apache/pluto/driver/url/impl/PortalURLParserImpl.java
Modified: portals/pluto/trunk/pluto-container/src/main/java/org/apache/pluto/util/StringUtils.java
URL: http://svn.apache.org/viewvc/portals/pluto/trunk/pluto-container/src/main/java/org/apache/pluto/util/StringUtils.java?rev=747775&r1=747774&r2=747775&view=diff
==============================================================================
--- portals/pluto/trunk/pluto-container/src/main/java/org/apache/pluto/util/StringUtils.java (original)
+++ portals/pluto/trunk/pluto-container/src/main/java/org/apache/pluto/util/StringUtils.java Wed Feb 25 13:44:12 2009
@@ -38,43 +38,6 @@
// Static Utility Methods --------------------------------------------------
/**
- * Replaces all occurrences of a pattern within a string by a replacement.
- * @param source the string that should be searched.
- * @param pattern the pattern that should be replaced.
- * @param replace the replacement that should be inserted instead of the
- * pattern.
- * @return The updated source string.
- */
- public static String replace(String source, String pattern, String replace) {
- if (source == null || source.length() == 0
- || pattern == null || pattern.length() == 0) {
- return source;
- }
-
- int k = source.indexOf(pattern);
-
- if (k == -1) {
- return source;
- }
-
- StringBuffer out = new StringBuffer();
- int i = 0, l = pattern.length();
-
- while (k != -1) {
- out.append(source.substring(i, k));
-
- if (replace != null) {
- out.append(replace);
- }
-
- i = k + l;
- k = source.indexOf(pattern, i);
- }
- out.append(source.substring(i));
- return out.toString();
- }
-
- /**
* TODO: can't we just use String[].clone()?
* @param source
* @return
@@ -131,18 +94,4 @@
}
return strippedType.trim();
}
-
- public static void encode(StringBuffer url){
- bla(url,"|","%7C");
- bla(url,"\"","%22");
- }
- private static void bla(StringBuffer url, String character, String change){
- boolean contains = url.toString().contains(character);
- while (contains){
- int index = url.indexOf(character);
- url.deleteCharAt(index);
- url.insert(index, change, 0, 3);
- contains = url.toString().contains(character);
- }
- }
}
Modified: portals/pluto/trunk/pluto-container/src/test/java/org/apache/pluto/util/StringUtilsTest.java
URL: http://svn.apache.org/viewvc/portals/pluto/trunk/pluto-container/src/test/java/org/apache/pluto/util/StringUtilsTest.java?rev=747775&r1=747774&r2=747775&view=diff
==============================================================================
--- portals/pluto/trunk/pluto-container/src/test/java/org/apache/pluto/util/StringUtilsTest.java (original)
+++ portals/pluto/trunk/pluto-container/src/test/java/org/apache/pluto/util/StringUtilsTest.java Wed Feb 25 13:44:12 2009
@@ -27,22 +27,6 @@
*/
public class StringUtilsTest extends PlutoTestCase {
- public void testReplaceAtBegin() {
- assertEquals("ReplacedValue", StringUtils.replace("___lacedValue", "___", "Rep"));
- }
-
- public void testReplaceAtEnd() {
- assertEquals("ReplacedValue", StringUtils.replace("ReplacedVa***", "***", "lue"));
- }
-
- public void testReplaceInMiddle() {
- assertEquals("ReplacedValue", StringUtils.replace("Rep(###)Value", "(###)", "laced"));
- }
-
- public void testReplaceMultiples() {
- assertEquals("ReplacedValueReplacedValue", StringUtils.replace("Rep(###)ValueRep(###)Value", "(###)", "laced"));
- }
-
public void testCopy() {
String[] original = new String[] {"one", "two", "three", "four", "five"};
String[] results = StringUtils.copy(original);
Modified: portals/pluto/trunk/pluto-portal-driver-impl/src/main/java/org/apache/pluto/driver/url/impl/PortalURLParserImpl.java
URL: http://svn.apache.org/viewvc/portals/pluto/trunk/pluto-portal-driver-impl/src/main/java/org/apache/pluto/driver/url/impl/PortalURLParserImpl.java?rev=747775&r1=747774&r2=747775&view=diff
==============================================================================
--- portals/pluto/trunk/pluto-portal-driver-impl/src/main/java/org/apache/pluto/driver/url/impl/PortalURLParserImpl.java (original)
+++ portals/pluto/trunk/pluto-portal-driver-impl/src/main/java/org/apache/pluto/driver/url/impl/PortalURLParserImpl.java Wed Feb 25 13:44:12 2009
@@ -32,7 +32,6 @@
import org.apache.pluto.driver.url.PortalURL;
import org.apache.pluto.driver.url.PortalURLParameter;
import org.apache.pluto.driver.url.PortalURLParser;
-import org.apache.pluto.util.StringUtils;
/**
* @version 1.0
@@ -48,7 +47,7 @@
// Constants used for Encoding/Decoding ------------------------------------
-
+
private static final String PREFIX = "__";
private static final String DELIM = "_";
private static final String PORTLET_ID = "pd";
@@ -76,9 +75,9 @@
new String[] { "\\", "0xb" },
new String[] { "%", "0xc" },
};
-
+
// Constructor -------------------------------------------------------------
-
+
/**
* Private constructor that prevents external instantiation.
*/
@@ -93,24 +92,24 @@
public static PortalURLParser getParser() {
return PARSER;
}
-
-
+
+
// Public Methods ----------------------------------------------------------
-
+
/**
* Parse a servlet request to a portal URL.
* @param request the servlet request to parse.
* @return the portal URL.
*/
public PortalURL parse(HttpServletRequest request) {
-
+
if (LOG.isDebugEnabled()) {
LOG.debug("Parsing URL: " + request.getRequestURI());
}
String contextPath = request.getContextPath();
String servletName = request.getServletPath();
-
+
// Construct portal URL using info retrieved from servlet request.
PortalURL portalURL = new RelativePortalURLImpl(contextPath, servletName, this);
@@ -126,7 +125,7 @@
return portalURL;
}
}
-
+
if (LOG.isDebugEnabled()) {
LOG.debug("Parsing request pathInfo: " + pathInfo);
}
@@ -134,9 +133,9 @@
StringBuffer renderPath = new StringBuffer();
StringTokenizer st = new StringTokenizer(pathInfo, "/", false);
while (st.hasMoreTokens()) {
-
+
String token = st.nextToken();
-
+
// Part of the render path: append to renderPath.
if (!token.startsWith(PREFIX)) {
// renderPath.append(token);
@@ -167,11 +166,11 @@
if (st.hasMoreTokens()) {
value = st.nextToken();
}
- //set the
+ //set the
PortalURLParameter param = decodeParameter(token, value);
portalURL.addParameter(param);
-
-
+
+
}
else{ // besser if PREFIX + PUBLIC_PARAM
String value = null;
@@ -184,19 +183,19 @@
//set public parameter in portalURL
portalURL.addPublicParameterCurrent(param.getName(), param.getValues());
}
-
-
+
+
}
}
if (renderPath.length() > 0) {
portalURL.setRenderPath(renderPath.toString());
}
-
+
// Return the portal URL.
return portalURL;
}
-
-
+
+
/**
* Converts a portal URL to a URL string.
* @param portalURL the portal URL to convert.
@@ -205,7 +204,7 @@
public String toString(PortalURL portalURL) {
StringBuffer buffer = new StringBuffer();
-
+
// Append the server URI and the servlet path.
buffer.append(portalURL.getServletPath().startsWith("/")?"":"/")
.append(portalURL.getServletPath());
@@ -214,7 +213,7 @@
if (portalURL.getRenderPath() != null) {
buffer.append("/").append(portalURL.getRenderPath());
}
- //Append the resource window definition, if it exists.
+ //Append the resource window definition, if it exists.
if (portalURL.getResourceWindow() != null){
buffer.append("/");
buffer.append(PREFIX).append(RESOURCE)
@@ -226,7 +225,7 @@
buffer.append(PREFIX).append(ACTION)
.append(encodeCharacters(portalURL.getActionWindow()));
}
-
+
// Append portlet mode definitions.
for (Iterator it = portalURL.getPortletModes().entrySet().iterator();
it.hasNext(); ) {
@@ -235,7 +234,7 @@
encodeControlParameter(PORTLET_MODE, entry.getKey().toString(),
entry.getValue().toString()));
}
-
+
// Append window state definitions.
for (Iterator it = portalURL.getWindowStates().entrySet().iterator();
it.hasNext(); ) {
@@ -244,15 +243,15 @@
encodeControlParameter(WINDOW_STATE, entry.getKey().toString(),
entry.getValue().toString()));
}
-
+
// Append action and render parameters.
StringBuffer query = new StringBuffer("?");
boolean firstParam = true;
for (Iterator it = portalURL.getParameters().iterator();
it.hasNext(); ) {
-
+
PortalURLParameter param = (PortalURLParameter) it.next();
-
+
// Encode action params in the query appended at the end of the URL.
if (portalURL.getActionWindow() != null
&& portalURL.getActionWindow().equals(param.getWindowId())
@@ -269,7 +268,7 @@
.append(encodeQueryParam(param.getValues()[i]));
}
}
-
+
// Encode render params as a part of the URL.
else if (param.getValues() != null
&& param.getValues().length > 0) {
@@ -283,14 +282,14 @@
}
}
- StringUtils.encode(buffer);
-
-
+ encode(buffer);
+
+
Map<String, String[]> publicParamList = portalURL.getPublicParameters();
if (publicParamList!=null){
for (Iterator iter = publicParamList.keySet().iterator();iter.hasNext();){
String paramname = (String)iter.next();
- String[] tmp = (String[])publicParamList.get(paramname);
+ String[] tmp = publicParamList.get(paramname);
String valueString = encodeMultiValues(tmp);
if (valueString.length()>0){
buffer.append("/").append(encodePublicParamname(PUBLIC_RENDER_PARAM, paramname));
@@ -298,17 +297,31 @@
}
}
}
-
+
// Construct the string representing the portal URL.
// Fix for PLUTO-247 - check if query string contains parameters
if ( query.length() > 1 ) {
return buffer.append(query).toString();
}
-
+
// Construct the string representing the portal URL.
return buffer.append(query).toString();
}
+ public static void encode(StringBuffer url){
+ replaceChar(url,"|","%7C");
+ replaceChar(url,"\"","%22");
+ }
+ private static void replaceChar(StringBuffer url, String character, String change){
+ boolean contains = url.toString().contains(character);
+ while (contains){
+ int index = url.indexOf(character);
+ url.deleteCharAt(index);
+ url.insert(index, change, 0, 3);
+ contains = url.toString().contains(character);
+ }
+ }
+
private String encodeQueryParam(String param) {
try {
return URLEncoder.encode(param, "UTF-8");
@@ -320,7 +333,7 @@
}
// Private Encoding/Decoding Methods ---------------------------------------
-
+
/**
* Encode a control parameter.
* @param type the type of the control parameter, which may be:
@@ -337,14 +350,14 @@
.append(DELIM).append(name);
return buffer.toString();
}
-
+
private String encodePublicParamname(String type, String name){
StringBuffer buffer = new StringBuffer();
buffer.append(PREFIX).append(type)
.append(DELIM).append(name);
return buffer.toString();
}
-
+
/**
* Encode a string array containing multiple values into a single string.
* This method is used to encode multiple render parameter values.
@@ -369,7 +382,7 @@
}
return encodeCharacters(buffer.toString());
}
-
+
/**
* Encode special characters contained in the string value.
* @param string the string value to encode.
@@ -377,9 +390,7 @@
*/
private String encodeCharacters(String string) {
for (int i = 0; i < ENCODINGS.length; i++) {
- string = StringUtils.replace(string,
- ENCODINGS[i][0],
- ENCODINGS[i][1]);
+ string = string.replace(ENCODINGS[i][0], ENCODINGS[i][1]);
}
return string;
}
@@ -416,23 +427,23 @@
* @return the decoded portal URL parameter.
*/
private PortalURLParameter decodeParameter(String name, String value) {
-
+
if (LOG.isDebugEnabled()) {
LOG.debug("Decoding parameter: name=" + name
+ ", value=" + value);
}
-
+
// Decode the name into window ID and parameter name.
String noPrefix = name.substring((PREFIX + PORTLET_ID).length());
String windowId = noPrefix.substring(0, noPrefix.indexOf(DELIM));
String paramName = noPrefix.substring(noPrefix.indexOf(DELIM) + 1);
-
+
// Decode special characters in window ID and parameter value.
windowId = decodeCharacters(windowId);
if (value != null) {
value = decodeCharacters(value);
}
-
+
// Split multiple values into a value array.
String[] paramValues = value.split(VALUE_DELIM);
for (int i = 0; i < paramValues.length;i++){
@@ -445,31 +456,31 @@
// Construct portal URL parameter and return.
return new PortalURLParameter(windowId, paramName, paramValues);
}
-
+
private PortalURLParameter decodePublicParameter(String name, String value) {
-
+
if (LOG.isDebugEnabled()) {
LOG.debug("Decoding parameter: name=" + name
+ ", value=" + value);
}
-
+
// // Decode the name into window ID and parameter name.
String noPrefix = name.substring((PREFIX + PORTLET_ID).length());
String paramName = noPrefix.substring(noPrefix.indexOf(DELIM) + 1);
-
+
// Decode special characters in parameter value.
if (value != null) {
value = decodeCharacters(value);
}
-
+
// Split multiple values into a value array.
String[] paramValues = value.split(VALUE_DELIM);
-
+
// Construct portal URL parameter and return.
return new PortalURLParameter(null, paramName, paramValues);
}
-
+
/**
* Decode special characters contained in the string value.
* @param string the string value to decode.
@@ -477,9 +488,7 @@
*/
private String decodeCharacters(String string) {
for (int i = 0; i < ENCODINGS.length; i++) {
- string = StringUtils.replace(string,
- ENCODINGS[i][1],
- ENCODINGS[i][0]);
+ string = string.replace(ENCODINGS[i][1], ENCODINGS[i][0]);
}
return string;
}