Author: chris.laprun(a)jboss.com
Date: 2010-03-22 08:02:51 -0400 (Mon, 22 Mar 2010)
New Revision: 2339
Added:
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/MarkupProcessor.java
Modified:
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/RenderHandler.java
components/wsrp/trunk/pom.xml
Log:
- GTNWSRP-11: Temporarily move new TextTools.replaceBoundedString implementation to a new
MarkupProcessor
class to avoid having to release a new version of the common module.
Added:
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/MarkupProcessor.java
===================================================================
---
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/MarkupProcessor.java
(rev 0)
+++
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/MarkupProcessor.java 2010-03-22
12:02:51 UTC (rev 2339)
@@ -0,0 +1,291 @@
+/*
+ * 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.consumer;
+
+import org.gatein.common.net.URLTools;
+import org.gatein.common.util.ParameterValidation;
+import org.gatein.pc.api.URLFormat;
+import org.gatein.pc.api.spi.PortletInvocationContext;
+import org.gatein.wsrp.WSRPPortletURL;
+import org.gatein.wsrp.WSRPResourceURL;
+import org.gatein.wsrp.WSRPRewritingConstants;
+
+import java.util.Set;
+
+/**
+ * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
+ * @version $Revision$
+ */
+public class MarkupProcessor
+{
+ private final PortletInvocationContext context;
+ private final URLFormat format;
+ private final Set<String> supportedCustomModes;
+ private final Set<String> supportedCustomWindowStates;
+ private final String serverAddress;
+ private final String portletApplicationName;
+ private final String namespace;
+ public static final int URL_DELIMITER_LENGTH =
WSRPRewritingConstants.RESOURCE_URL_DELIMITER.length();
+
+ protected MarkupProcessor(String namespace, PortletInvocationContext context,
org.gatein.pc.api.PortletContext target, URLFormat format, ProducerInfo info)
+ {
+ this.namespace = namespace;
+ this.context = context;
+ this.format = format;
+ supportedCustomModes = info.getSupportedCustomModes();
+ supportedCustomWindowStates = info.getSupportedCustomWindowStates();
+ serverAddress = info.getEndpointConfigurationInfo().getRemoteHostAddress();
+ portletApplicationName = target.getApplicationName();
+ }
+
+ public String getReplacementFor(String match, String prefix, String suffix)
+ {
+ if (match.startsWith(WSRPRewritingConstants.RESOURCE_URL_DELIMITER))
+ {
+ // we have a resource URL coming from a template so extract URL
+ int index = match.lastIndexOf(WSRPRewritingConstants.RESOURCE_URL_DELIMITER);
+
+/*
+ // todo: right now, no need to extract value of require rewrite..
+ String requireRewriteStr = match.substring(index + URL_DELIMITER_LENGTH);
+ boolean requireRewrite = Boolean.valueOf(requireRewriteStr);
+ if (requireRewrite)
+ {
+ // FIX-ME: do something
+ log.debug("Required re-writing but this is not yet
implemented...");
+ }*/
+
+ match = match.substring(URL_DELIMITER_LENGTH, index);
+ return URLTools.decodeXWWWFormURL(match);
+ }
+ else if (prefix.equals(match))
+ {
+ return namespace;
+ }
+ else if (match.startsWith(WSRPRewritingConstants.BEGIN_WSRP_REWRITE_END))
+ {
+ // remove end of rewrite token
+ match =
match.substring(WSRPRewritingConstants.BEGIN_WSRP_REWRITE_END.length());
+
+ WSRPPortletURL portletURL = WSRPPortletURL.create(match, supportedCustomModes,
supportedCustomWindowStates, true);
+ if (portletURL instanceof WSRPResourceURL)
+ {
+ WSRPResourceURL resource = (WSRPResourceURL)portletURL;
+ String replacement = getResourceURL(match, resource);
+
+ // if the URL starts with /, prepend the remote host address and the portlet
application name so that we
+ // can attempt to create a remotely available URL
+ if (replacement.startsWith(URLTools.SLASH))
+ {
+ replacement = WSRPResourceURL.createAbsoluteURLFrom(replacement,
serverAddress, portletApplicationName);
+ }
+
+ return replacement;
+
+/*
+ todo: use this code to reactivate primitive use of resources
+ // get the parsed URL and add marker to it so that the consumer can know
it needs to be intercepted
+ URL url = resource.getResourceURL();
+ String query = url.getQuery();
+ if (ParameterValidation.isNullOrEmpty(query))
+ {
+ query = WSRPRewritingConstants.GTNRESOURCE;
+ }
+ else
+ {
+ query = "+" + WSRPRewritingConstants.GTNRESOURCE;
+ }
+
+ try
+ {
+ URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(),
url.getPort(),
+ url.getPath(), query, url.getRef());
+
+ // set the resulting URI as the new resource ID, must be encoded as it
will be used in URLs
+ String s = URLTools.encodeXWWWFormURL(uri.toString());
+ resource.setResourceId(s);
+ }
+ catch (Exception e)
+ {
+ throw new IllegalArgumentException("Cannot parse specified
Resource as a URI: " + url);
+ }*/
+
+ }
+
+ return context.renderURL(portletURL, format);
+ }
+ else
+ {
+ // match is not something we know how to process
+ return match;
+ }
+ }
+
+
+ static String getResourceURL(String urlAsString, WSRPResourceURL resource)
+ {
+ String resourceURL = resource.getResourceURL().toExternalForm();
+ if (InvocationHandler.log.isDebugEnabled())
+ {
+ InvocationHandler.log.debug("URL '" + urlAsString + "'
refers to a resource which are not currently well supported. " +
+ "Attempting to craft a URL that we might be able to work with:
'" + resourceURL + "'");
+ }
+
+ // right now the resourceURL should be output as is, because it will be used
directly but it really should be encoded
+ return resourceURL;
+ }
+
+ /**
+ * TODO: This is a copy of the TextTools.replaceBoundedString method found in common
module, copied here to avoid
+ * having to release a new version of the module TODO: REMOVE when a new version of
common is released.
+ *
+ * @param initial
+ * @param prefix
+ * @param suffix
+ * @param generator
+ * @param replaceIfBoundedStringEmpty
+ * @param keepBoundaries
+ * @param suffixIsOptional
+ * @return
+ */
+ public static String replaceBoundedString(final String initial, final String prefix,
final String suffix, final MarkupProcessor generator,
+ final boolean replaceIfBoundedStringEmpty,
final boolean keepBoundaries, final boolean suffixIsOptional)
+ {
+ if (ParameterValidation.isNullOrEmpty(initial))
+ {
+ return initial;
+ }
+
+ ParameterValidation.throwIllegalArgExceptionIfNull(generator,
"StringReplacementGenerator");
+
+ ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(prefix,
"prefix", "Tools.replaceBoundedString");
+
+ StringBuilder tmp = new StringBuilder(initial);
+ int prefixIndex = tmp.indexOf(prefix);
+ final int prefixLength = prefix.length();
+ boolean suffixAbsent = suffix == null;
+
+ // nothing to do if didn't ask for an optional suffix and we have one and
it's not present in our string
+ if (!suffixIsOptional && suffix != null && tmp.indexOf(suffix) ==
-1)
+ {
+ return initial;
+ }
+
+ // loop as long as we can find an instance of prefix in the String
+ while (prefixIndex != -1)
+ {
+ int suffixIndex;
+ if (suffixAbsent)
+ {
+ // replace prefix with replacement
+ if (keepBoundaries)
+ {
+ // just insert replacement for prefix
+ tmp.insert(prefixIndex + prefixLength, generator.getReplacementFor(prefix,
prefix, suffix));
+ }
+ else
+ {
+ // delete prefix then insert remplacement instead
+ tmp.delete(prefixIndex, prefixIndex + prefixLength);
+ tmp.insert(prefixIndex, generator.getReplacementFor(prefix, prefix,
suffix));
+ }
+
+ // new lookup starting position
+ prefixIndex = tmp.indexOf(prefix, prefixIndex + prefixLength);
+ }
+ else
+ {
+ // look for suffix
+ suffixIndex = tmp.indexOf(suffix, prefixIndex);
+
+ if (suffixIndex == -1)
+ {
+ // we haven't found suffix in the rest of the String so don't look
for it again
+ suffixAbsent = true;
+ continue;
+ }
+ else
+ {
+ if (suffixIsOptional)
+ {
+ // if suffix is optional, look for potential next prefix instance that
we'd need to replace
+ int nextPrefixIndex = tmp.indexOf(prefix, prefixIndex + prefixLength);
+
+ if (nextPrefixIndex != -1 && nextPrefixIndex <=
suffixIndex)
+ {
+ // we've found an in-between prefix, use it as the suffix for
the current match
+ // delete prefix then insert remplacement instead
+ tmp.delete(prefixIndex, prefixIndex + prefixLength);
+ String replacement = generator.getReplacementFor(prefix, prefix,
suffix);
+ tmp.insert(prefixIndex, replacement);
+
+ prefixIndex = nextPrefixIndex - prefixLength +
replacement.length();
+ continue;
+ }
+ }
+
+ // we don't care about empty bounded strings or prefix and suffix
don't delimit an empty String => replace!
+ if (replaceIfBoundedStringEmpty || suffixIndex != prefixIndex +
prefixLength)
+ {
+ String match = tmp.substring(prefixIndex + prefixLength, suffixIndex);
+ if (keepBoundaries)
+ {
+ if (suffix != null)
+ {
+ // delete only match
+ tmp.delete(prefixIndex + prefixLength, suffixIndex);
+ }
+ else
+ {
+ // delete nothing
+ }
+ tmp.insert(prefixIndex + prefixLength,
generator.getReplacementFor(match, prefix, suffix));
+ }
+ else
+ {
+ int suffixLength = suffix != null ? suffix.length() : 0;
+
+ if (suffix != null)
+ {
+ // if we have a suffix, delete everything between start of prefix
and end of suffix
+ tmp.delete(prefixIndex, suffixIndex + suffixLength);
+ }
+ else
+ {
+ // only delete prefix
+ tmp.delete(prefixIndex, prefixIndex + prefixLength);
+ }
+ tmp.insert(prefixIndex, generator.getReplacementFor(match, prefix,
suffix));
+ }
+ }
+ }
+
+ prefixIndex = tmp.indexOf(prefix, prefixIndex + prefixLength);
+
+ }
+ }
+
+ return tmp.toString();
+ }
+}
Modified:
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/RenderHandler.java
===================================================================
---
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/RenderHandler.java 2010-03-22
11:48:01 UTC (rev 2338)
+++
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/RenderHandler.java 2010-03-22
12:02:51 UTC (rev 2339)
@@ -23,8 +23,6 @@
package org.gatein.wsrp.consumer;
-import org.gatein.common.net.URLTools;
-import org.gatein.common.text.TextTools;
import org.gatein.pc.api.PortletInvokerException;
import org.gatein.pc.api.URLFormat;
import org.gatein.pc.api.cache.CacheScope;
@@ -36,8 +34,6 @@
import org.gatein.pc.api.spi.PortletInvocationContext;
import org.gatein.wsrp.WSRPConstants;
import org.gatein.wsrp.WSRPConsumer;
-import org.gatein.wsrp.WSRPPortletURL;
-import org.gatein.wsrp.WSRPResourceURL;
import org.gatein.wsrp.WSRPRewritingConstants;
import org.gatein.wsrp.WSRPTypeFactory;
import org.oasis.wsrp.v1.CacheControl;
@@ -52,7 +48,6 @@
import javax.xml.ws.Holder;
import java.util.List;
-import java.util.Set;
/**
* @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
@@ -194,11 +189,11 @@
{
// fix-me: how to deal with fragment header? => interceptor?
- markup = TextTools.replaceBoundedString(
+ markup = MarkupProcessor.replaceBoundedString(
markup,
WSRPRewritingConstants.WSRP_REWRITE,
WSRPRewritingConstants.END_WSRP_REWRITE,
- new ResourceURLStringReplacementGenerator(namespace, context, target, format,
consumer.getProducerInfo()),
+ new MarkupProcessor(namespace, context, target, format,
consumer.getProducerInfo()),
true,
false,
true
@@ -242,123 +237,4 @@
return result;
}
-
- static class ResourceURLStringReplacementGenerator implements
TextTools.StringReplacementGenerator
- {
- private final PortletInvocationContext context;
- private final URLFormat format;
- private final Set<String> supportedCustomModes;
- private final Set<String> supportedCustomWindowStates;
- private final String serverAddress;
- private final String portletApplicationName;
- private final String namespace;
- public static final int URL_DELIMITER_LENGTH =
WSRPRewritingConstants.RESOURCE_URL_DELIMITER.length();
-
- private ResourceURLStringReplacementGenerator(String namespace,
PortletInvocationContext context, org.gatein.pc.api.PortletContext target, URLFormat
format, ProducerInfo info)
- {
- this.namespace = namespace;
- this.context = context;
- this.format = format;
- supportedCustomModes = info.getSupportedCustomModes();
- supportedCustomWindowStates = info.getSupportedCustomWindowStates();
- serverAddress = info.getEndpointConfigurationInfo().getRemoteHostAddress();
- portletApplicationName = target.getApplicationName();
- }
-
- public String getReplacementFor(String match, String prefix, String suffix)
- {
- if (match.startsWith(WSRPRewritingConstants.RESOURCE_URL_DELIMITER))
- {
- // we have a resource URL coming from a template so extract URL
- int index =
match.lastIndexOf(WSRPRewritingConstants.RESOURCE_URL_DELIMITER);
-
-/*
- // todo: right now, no need to extract value of require rewrite..
- String requireRewriteStr = match.substring(index + URL_DELIMITER_LENGTH);
- boolean requireRewrite = Boolean.valueOf(requireRewriteStr);
- if (requireRewrite)
- {
- // FIX-ME: do something
- log.debug("Required re-writing but this is not yet
implemented...");
- }*/
-
- match = match.substring(URL_DELIMITER_LENGTH, index);
- return URLTools.decodeXWWWFormURL(match);
- }
- else if (prefix.equals(match))
- {
- return namespace;
- }
- else if (match.startsWith(WSRPRewritingConstants.BEGIN_WSRP_REWRITE_END))
- {
- // remove end of rewrite token
- match =
match.substring(WSRPRewritingConstants.BEGIN_WSRP_REWRITE_END.length());
-
- WSRPPortletURL portletURL = WSRPPortletURL.create(match,
supportedCustomModes, supportedCustomWindowStates, true);
- if (portletURL instanceof WSRPResourceURL)
- {
- WSRPResourceURL resource = (WSRPResourceURL)portletURL;
- String replacement = getResourceURL(match, resource);
-
- // if the URL starts with /, prepend the remote host address and the
portlet application name so that we
- // can attempt to create a remotely available URL
- if (replacement.startsWith(URLTools.SLASH))
- {
- replacement = WSRPResourceURL.createAbsoluteURLFrom(replacement,
serverAddress, portletApplicationName);
- }
-
- return replacement;
-
-/*
- todo: use this code to reactivate primitive use of resources
- // get the parsed URL and add marker to it so that the consumer can know
it needs to be intercepted
- URL url = resource.getResourceURL();
- String query = url.getQuery();
- if (ParameterValidation.isNullOrEmpty(query))
- {
- query = WSRPRewritingConstants.GTNRESOURCE;
- }
- else
- {
- query = "+" + WSRPRewritingConstants.GTNRESOURCE;
- }
-
- try
- {
- URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(),
url.getPort(),
- url.getPath(), query, url.getRef());
-
- // set the resulting URI as the new resource ID, must be encoded as it
will be used in URLs
- String s = URLTools.encodeXWWWFormURL(uri.toString());
- resource.setResourceId(s);
- }
- catch (Exception e)
- {
- throw new IllegalArgumentException("Cannot parse specified
Resource as a URI: " + url);
- }*/
-
- }
-
- return context.renderURL(portletURL, format);
- }
- else
- {
- // match is not something we know how to process
- return match;
- }
- }
- }
-
- private static String getResourceURL(String urlAsString, WSRPResourceURL resource)
- {
- String resourceURL = resource.getResourceURL().toExternalForm();
- if (log.isDebugEnabled())
- {
- log.debug("URL '" + urlAsString + "' refers to a resource
which are not currently well supported. " +
- "Attempting to craft a URL that we might be able to work with:
'" + resourceURL + "'");
- }
-
- // right now the resourceURL should be output as is, because it will be used
directly but it really should be encoded
- return resourceURL;
- }
}
Modified: components/wsrp/trunk/pom.xml
===================================================================
--- components/wsrp/trunk/pom.xml 2010-03-22 11:48:01 UTC (rev 2338)
+++ components/wsrp/trunk/pom.xml 2010-03-22 12:02:51 UTC (rev 2339)
@@ -48,7 +48,7 @@
<properties>
<version.gatein.pc>2.1.0-GA</version.gatein.pc>
- <version.gatein.common>2.0.1-GA-SNAPSHOT</version.gatein.common>
+ <version.gatein.common>2.0.0-GA</version.gatein.common>
<version.gatein.wci>2.0.0-GA</version.gatein.wci>
<version.jsf>1.2_12</version.jsf>