Author: chris.laprun(a)jboss.com
Date: 2011-04-07 08:30:30 -0400 (Thu, 07 Apr 2011)
New Revision: 6171
Modified:
components/pc/trunk/api/src/main/java/org/gatein/pc/api/PortletContext.java
components/pc/trunk/api/src/test/java/org/gatein/pc/api/PortletContextTestCase.java
Log:
- GTNPC-58: Provided validation of currently known PortletContexts format. Now validates
input by default. Added inner PortletContextComponents class to record components
extracted during validation so that they can be used if needed.
Modified: components/pc/trunk/api/src/main/java/org/gatein/pc/api/PortletContext.java
===================================================================
--- components/pc/trunk/api/src/main/java/org/gatein/pc/api/PortletContext.java 2011-04-07
08:34:16 UTC (rev 6170)
+++ components/pc/trunk/api/src/main/java/org/gatein/pc/api/PortletContext.java 2011-04-07
12:30:30 UTC (rev 6171)
@@ -35,104 +35,115 @@
public class PortletContext implements Serializable
{
- /** . */
- protected final String id;
- private final String applicationName;
- private final String portletName;
private static final String PREFIX = "/";
private static final char SEPARATOR = '.';
- PortletContext(String id) throws IllegalArgumentException
+ /* TODO: remove from ProducerPortletInvoker so that we can use these constants in
GateIn
+ public static final String CONSUMER_CLONE_ID = "_dumbvalue";
+ public static final String PRODUCER_CLONE_ID_PREFIX = "_";
+ public final static PortletContext CONSUMER_CLONE =
PortletContext.createPortletContext(PortletInvoker.LOCAL_PORTLET_INVOKER_ID + SEPARATOR +
CONSUMER_CLONE_ID);*/
+
+ protected final String id;
+ private final PortletContextComponents components;
+
+ protected PortletContext(String id) throws IllegalArgumentException
{
+ this(id, true);
+ }
+
+ protected PortletContext(String id, boolean interpret)
+ {
ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(id, "portlet
id", "PortletContext");
+ PortletContextComponents components = null;
+ boolean isSimpleAppPortlet = false;
+ boolean isOpaquePortlet = false;
+ boolean isCompoundAppPortlet = false;
+
// components
- String trimmedId = id.trim();
- if (trimmedId.startsWith(PREFIX)) // only consider components if the id starts with
'/'
+ if (interpret)
{
- int separator = trimmedId.indexOf(SEPARATOR); // find first separator, other
separator are considered part of the portlet name
- if (separator != -1)
- {
- portletName = trimmedId.substring(separator + 1).trim();
- applicationName = PREFIX + trimmedId.substring(1, separator).trim();
- }
- else
- {
- portletName = null;
- applicationName = null;
- }
- }
- else
- {
- // check if we have the case: invokerId./application.portlet
- int prefix = trimmedId.indexOf(PREFIX);
- int invoker = trimmedId.indexOf(SEPARATOR);
+ ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(id, "portlet
id", "PortletContext");
- // find first separator, check if it could be an invoker id
- if (invoker > 0 && invoker < prefix)
+ String trimmedId = id.trim();
+
+ try
{
- // check if we have a second separator, which would indicate a portlet
context with invoker id
- int separator = trimmedId.indexOf(SEPARATOR, prefix);
- if (separator != -1)
+ if (trimmedId.startsWith(PREFIX))
{
- String invokerId = trimmedId.substring(0, invoker).trim();
- portletName = trimmedId.substring(separator + 1).trim();
- trimmedId = trimmedId.substring(invoker + 1).trim();
- applicationName = PREFIX + trimmedId.substring(1,
trimmedId.indexOf(SEPARATOR)).trim();
- this.id = invokerId + SEPARATOR + buildIdFrom(applicationName,
portletName); // recreate id with invoker
- return;
+ // check the case: /application.portlet
+ int separator = trimmedId.indexOf(SEPARATOR); // find first separator,
other separator are considered part of the portlet name
+ String portletName = trimmedId.substring(separator + 1).trim();
+ String appName = trimmedId.substring(1, separator).trim();
+ isSimpleAppPortlet = separator != -1 && appName.length() > 0
&& portletName.length() > 0;
+ if (isSimpleAppPortlet)
+ {
+ components = new PortletContextComponents(null, appName, portletName);
+ }
}
else
{
- portletName = null;
- applicationName = null;
+ int invoker = trimmedId.indexOf(SEPARATOR);
+ int prefix = trimmedId.indexOf(PREFIX);
+
+ if (prefix != -1)
+ {
+ // check if we have the case: invokerId./something
+ if (invoker > 0 && invoker < prefix)
+ {
+ String invokerId = trimmedId.substring(0, invoker).trim();
+
+ int separator = trimmedId.indexOf(SEPARATOR, prefix);
+ // check the case: invokerId./application.portlet
+ if (separator != -1)
+ {
+ String portletName = trimmedId.substring(separator + 1).trim();
+ trimmedId = trimmedId.substring(invoker + 1).trim();
+ String applicationName = trimmedId.substring(1,
trimmedId.indexOf(SEPARATOR)).trim();
+ isCompoundAppPortlet = invokerId.length() > 0 &&
applicationName.length() > 0 && portletName.length() > 0;
+ if (isCompoundAppPortlet)
+ {
+ components = new PortletContextComponents(invokerId,
applicationName, portletName);
+ }
+ }
+ }
+ }
+
+ // check if we have the case: invokerId.portletId
+ if (!isCompoundAppPortlet && invoker > 0)
+ {
+ String invokerId = trimmedId.substring(0, invoker).trim();
+ String portletName = trimmedId.substring(invoker + 1).trim();
+ isOpaquePortlet = invokerId.length() > 0 &&
portletName.length() > 0;
+ if (isOpaquePortlet)
+ {
+ components = new PortletContextComponents(invokerId, null,
portletName);
+ }
+ }
}
}
- else
+ catch (Exception e)
{
- portletName = null;
- applicationName = null;
+ throw new IllegalArgumentException("Couldn't interpret id
'" + id + "'", e);
}
}
- if (portletName == null || applicationName == null)
+ if (interpret && !(isSimpleAppPortlet || isCompoundAppPortlet ||
isOpaquePortlet))
{
- this.id = trimmedId;
+ throw new IllegalArgumentException("Couldn't interpret id '" +
id + "'");
}
- else
- {
- this.id = buildIdFrom(applicationName, portletName);
- }
- }
- private PortletContext(String applicationName, String portletName, boolean
formatApplicationName)
- {
- ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(applicationName,
"portlet application id", "PortletContext");
- ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(portletName,
"container id", "PortletContext");
-
- if (!applicationName.startsWith(PREFIX))
- {
- if (formatApplicationName)
- {
- applicationName = PREFIX + applicationName;
- }
- else
- {
- throw new IllegalArgumentException("Application name must start with
'" + PREFIX + "'. Was: " + applicationName);
- }
- }
-
- this.applicationName = applicationName;
- this.portletName = portletName;
- this.id = buildIdFrom(applicationName, portletName);
+ this.components = components;
+ this.id = components != null ? components.getId() : id;
}
- private String buildIdFrom(final String applicationName, final String portletName)
+ protected PortletContext(PortletContextComponents components)
{
- return applicationName + SEPARATOR + portletName;
+ ParameterValidation.throwIllegalArgExceptionIfNull(components, "portlet
context components");
+ this.components = components;
+ this.id = components.getId();
}
-
public boolean equals(Object o)
{
if (this == o)
@@ -172,14 +183,7 @@
@Deprecated()
public static PortletContext createPortletContext(String id, byte[] state)
{
- if (state != null && state.length > 0)
- {
- return new StatefulPortletContext<byte[]>(id, PortletStateType.OPAQUE,
state);
- }
- else
- {
- return new PortletContext(id);
- }
+ return createPortletContext(id, state, true);
}
/**
@@ -199,19 +203,21 @@
public static PortletContext createPortletContext(String portletId)
{
- return createPortletContext(portletId, (byte[])null);
+ return createPortletContext(portletId, null, true);
}
- public String getApplicationName()
+ public static PortletContext createPortletContext(String portletId, byte[] state,
boolean interpret)
{
- return applicationName;
+ if (state != null && state.length > 0)
+ {
+ return new StatefulPortletContext<byte[]>(portletId,
PortletStateType.OPAQUE, state);
+ }
+ else
+ {
+ return new PortletContext(portletId, interpret);
+ }
}
- public String getPortletName()
- {
- return portletName;
- }
-
/**
* Creates a new PortletContext referencing the specified portlet in the specified
application (usually a web
* application).
@@ -224,23 +230,55 @@
*/
public static PortletContext createPortletContext(String applicationName, String
portletName)
{
- return createPortletContext(applicationName, portletName, false);
+ ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(applicationName,
"portlet application id", "PortletContext");
+ ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(portletName,
"container id", "PortletContext");
+
+ if (applicationName.startsWith(PREFIX))
+ {
+ applicationName = applicationName.substring(1);
+ }
+
+ return new PortletContext(new PortletContextComponents(null, applicationName,
portletName));
}
- /**
- * Creates a new PortletContext referencing the specified portlet in the specified
application (usually a web
- * application).
- *
- * @param applicationName the application name (usually a web application
context path)
- * @param portletName the portlet name
- * @param formatApplicationName <code>true</code> if the application name
should be formatted before attempting to
- * create the PortletContext,
<code>false</code> otherwise.
- * @return a newly created PortletContext referencing the specified portlet in the
specified application.
- * @throws IllegalArgumentException if the specified arguments are null or empty and
if the application name is not
- * properly formatted.
- */
- public static PortletContext createPortletContext(String applicationName, String
portletName, boolean formatApplicationName)
+ public PortletContextComponents getComponents()
{
- return new PortletContext(applicationName, portletName, formatApplicationName);
+ return components;
}
+
+ public static class PortletContextComponents
+ {
+ private final String applicationName;
+ private final String portletName;
+ private final String invokerName;
+
+ public PortletContextComponents(String invokerName, String applicationName, String
portletName)
+ {
+ this.applicationName = applicationName;
+ this.portletName = portletName;
+ this.invokerName = invokerName;
+ }
+
+ public String getApplicationName()
+ {
+ return applicationName;
+ }
+
+ public String getPortletName()
+ {
+ return portletName;
+ }
+
+ public String getInvokerName()
+ {
+ return invokerName;
+ }
+
+ public String getId()
+ {
+ return (invokerName == null ? "" : invokerName + SEPARATOR)
+ + (applicationName == null ? "" : PREFIX + applicationName +
SEPARATOR)
+ + (portletName == null ? "" : portletName);
+ }
+ }
}
Modified:
components/pc/trunk/api/src/test/java/org/gatein/pc/api/PortletContextTestCase.java
===================================================================
---
components/pc/trunk/api/src/test/java/org/gatein/pc/api/PortletContextTestCase.java 2011-04-07
08:34:16 UTC (rev 6170)
+++
components/pc/trunk/api/src/test/java/org/gatein/pc/api/PortletContextTestCase.java 2011-04-07
12:30:30 UTC (rev 6171)
@@ -33,89 +33,126 @@
public void testGetComponents()
{
PortletContext context =
PortletContext.createPortletContext("/applicationName.portletName");
- assertEquals("/applicationName", context.getApplicationName());
- assertEquals("portletName", context.getPortletName());
assertEquals("/applicationName.portletName", context.getId());
+ PortletContext.PortletContextComponents components = context.getComponents();
+ assertNotNull(components);
+ assertNull(components.getInvokerName());
+ assertEquals("applicationName", components.getApplicationName());
+ assertEquals("portletName", components.getPortletName());
context = PortletContext.createPortletContext("\t\t\n
/applicationName.portletName \t");
- assertEquals("/applicationName", context.getApplicationName());
- assertEquals("portletName", context.getPortletName());
assertEquals("/applicationName.portletName", context.getId());
+ components = context.getComponents();
+ assertNotNull(components);
+ assertNull(components.getInvokerName());
+ assertEquals("applicationName", components.getApplicationName());
+ assertEquals("portletName", components.getPortletName());
- context = PortletContext.createPortletContext("/");
- assertNull(context.getApplicationName());
- assertNull(context.getPortletName());
- assertEquals("/", context.getId());
+ try
+ {
+ PortletContext.createPortletContext("/");
+ fail("invalid");
+ }
+ catch (IllegalArgumentException e)
+ {
+ // expected
+ }
context =
PortletContext.createPortletContext("applicationName.portletName");
- assertNull(context.getApplicationName());
- assertNull(context.getPortletName());
assertEquals("applicationName.portletName", context.getId());
+ components = context.getComponents();
+ assertNotNull(components);
+ assertEquals("applicationName", components.getInvokerName());
+ assertNull(components.getApplicationName());
+ assertEquals("portletName", components.getPortletName());
context =
PortletContext.createPortletContext("/applicationName.portlet.Name");
- assertEquals("/applicationName", context.getApplicationName());
- assertEquals("portlet.Name", context.getPortletName());
assertEquals("/applicationName.portlet.Name", context.getId());
+ components = context.getComponents();
+ assertNotNull(components);
+ assertNull(components.getInvokerName());
+ assertEquals("applicationName", components.getApplicationName());
+ assertEquals("portlet.Name", components.getPortletName());
- context = PortletContext.createPortletContext("/.");
- assertEquals("/", context.getApplicationName());
- assertEquals("", context.getPortletName());
- assertEquals("/.", context.getId());
+ try
+ {
+ PortletContext.createPortletContext("/.");
+ fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ // expected
+ }
context = PortletContext.createPortletContext("/ applicationName\t. portlet
Name");
- assertEquals("/applicationName", context.getApplicationName());
- assertEquals("portlet Name", context.getPortletName());
assertEquals("/applicationName.portlet Name", context.getId());
+ components = context.getComponents();
+ assertNotNull(components);
+ assertNull(components.getInvokerName());
+ assertEquals("applicationName", components.getApplicationName());
+ assertEquals("portlet Name", components.getPortletName());
}
public void testPortletContextWithInvokerId()
{
PortletContext context =
PortletContext.createPortletContext("local./foo.bar");
- assertEquals("/foo", context.getApplicationName());
- assertEquals("bar", context.getPortletName());
assertEquals("local./foo.bar", context.getId());
+ PortletContext.PortletContextComponents components = context.getComponents();
+ assertNotNull(components);
+ assertEquals("local", components.getInvokerName());
+ assertEquals("foo", components.getApplicationName());
+ assertEquals("bar", components.getPortletName());
context = PortletContext.createPortletContext(" local\t . / foo \t. \t\n
bar");
- assertEquals("/foo", context.getApplicationName());
- assertEquals("bar", context.getPortletName());
assertEquals("local./foo.bar", context.getId());
+ components = context.getComponents();
+ assertNotNull(components);
+ assertEquals("local", components.getInvokerName());
+ assertEquals("foo", components.getApplicationName());
+ assertEquals("bar", components.getPortletName());
context = PortletContext.createPortletContext("local.foo.bar");
- assertNull(context.getApplicationName());
- assertNull(context.getPortletName());
assertEquals("local.foo.bar", context.getId());
+ components = context.getComponents();
+ assertNotNull(components);
+ assertEquals("local", components.getInvokerName());
+ assertNull(components.getApplicationName());
+ assertEquals("foo.bar", components.getPortletName());
context = PortletContext.createPortletContext("local./foo");
- assertNull(context.getApplicationName());
- assertNull(context.getPortletName());
assertEquals("local./foo", context.getId());
+ components = context.getComponents();
+ assertNotNull(components);
+ assertEquals("local", components.getInvokerName());
+ assertNull(components.getApplicationName());
+ assertEquals("/foo", components.getPortletName());
}
public void testCreateFromComponents()
{
- PortletContext context;
- try
- {
- context = PortletContext.createPortletContext("applicationName",
"portletName");
- fail("'applicationName' is not a properly formatted application
name");
- }
- catch (IllegalArgumentException e)
- {
- // expected
- }
-
PortletContext fromId =
PortletContext.createPortletContext("/applicationName.portletName");
- context = PortletContext.createPortletContext("applicationName",
"portletName", true);
- assertEquals("/applicationName", context.getApplicationName());
- assertEquals("portletName", context.getPortletName());
+ PortletContext context =
PortletContext.createPortletContext("applicationName",
"portletName");
assertEquals("/applicationName.portletName", context.getId());
+ PortletContext.PortletContextComponents components = context.getComponents();
+ assertNotNull(components);
+ assertNull(components.getInvokerName());
+ assertEquals("applicationName", components.getApplicationName());
+ assertEquals("portletName", components.getPortletName());
assertEquals(context, fromId);
+ }
- context = PortletContext.createPortletContext("/applicationName",
"portletName");
- assertEquals("/applicationName", context.getApplicationName());
- assertEquals("portletName", context.getPortletName());
+ public void testShouldProperlyHandleApplicationNameStartingWithSlash()
+ {
+ PortletContext fromId =
PortletContext.createPortletContext("/applicationName.portletName");
+
+ PortletContext context =
PortletContext.createPortletContext("/applicationName",
"portletName");
assertEquals("/applicationName.portletName", context.getId());
+ PortletContext.PortletContextComponents components = context.getComponents();
+ assertNotNull(components);
+ assertNull(components.getInvokerName());
+ assertEquals("applicationName", components.getApplicationName());
+ assertEquals("portletName", components.getPortletName());
assertEquals(context, fromId);
}