Author: julien(a)jboss.com
Date: 2008-02-04 22:17:08 -0500 (Mon, 04 Feb 2008)
New Revision: 9760
Added:
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller2/PortletURLRenderer.java
Removed:
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/PortletURLRenderer.java
Modified:
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/TestPortletInvocationContext.java
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/PortletControllerContext.java
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/PortletRequestHandler.java
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/PortletResourceRequestHandler.java
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller2/PageUpdateRenderer.java
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller2/PortletControllerContextImpl.java
Log:
- moved url creation to PortletControllerContext interface
- moved the PortletURLRenderer class to controller2 package (along with the
PortletControllerContextImpl since those 2 classes are associated)
Deleted:
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/PortletURLRenderer.java
===================================================================
---
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/PortletURLRenderer.java 2008-02-05
02:56:52 UTC (rev 9759)
+++
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/PortletURLRenderer.java 2008-02-05
03:17:08 UTC (rev 9760)
@@ -1,245 +0,0 @@
-/******************************************************************************
- * JBoss, a division of Red Hat *
- * Copyright 2006, 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.jboss.portal.portlet.test;
-
-import org.jboss.portal.common.io.IOTools;
-import org.jboss.portal.common.text.CharBuffer;
-import org.jboss.portal.common.text.FastURLEncoder;
-import org.jboss.portal.common.util.Tools;
-import org.jboss.portal.portlet.ActionURL;
-import org.jboss.portal.portlet.ContainerURL;
-import org.jboss.portal.portlet.Portlet;
-import org.jboss.portal.portlet.PortletURL;
-import org.jboss.portal.portlet.RenderURL;
-import org.jboss.portal.portlet.ResourceURL;
-import org.jboss.portal.portlet.StateString;
-import org.jboss.portal.portlet.info.PortletInfo;
-import org.jboss.portal.portlet.cache.CacheLevel;
-import static org.jboss.portal.portlet.test.URLParameterConstants.*;
-import org.jboss.portal.portlet.test.controller.PageNavigationalState;
-
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import java.io.IOException;
-import java.util.HashMap;
-import java.util.Map;
-
-/**
- * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
- * @version $Revision: 1.1 $
- */
-public class PortletURLRenderer
-{
-
- /** . */
- final String windowId;
-
- /** . */
- final HttpServletRequest clientReq;
-
- /** . */
- final HttpServletResponse clientResp;
-
- /** . */
- final PageNavigationalState pageNS;
-
- public PortletURLRenderer(
- PageNavigationalState pageNS,
- String windowId,
- HttpServletRequest clientReq,
- HttpServletResponse clientResp)
- {
- this.pageNS = pageNS;
- this.windowId = windowId;
- this.clientReq = clientReq;
- this.clientResp = clientResp;
- }
-
- String renderURL(ContainerURL containerURL, Boolean wantSecure, Boolean
wantAuthenticated, boolean relative)
- {
- CharBuffer buffer = new CharBuffer();
- buffer.append(clientReq.getScheme());
- buffer.append("://");
- buffer.append(clientReq.getServerName());
- buffer.append(':');
- buffer.append(Integer.toString(clientReq.getServerPort()));
- buffer.append(clientReq.getContextPath());
- buffer.append('/');
- buffer.append(StringCodec.encode(windowId));
-
- //
- Map<String, String> parameters = new HashMap<String, String>();
-
- //
- String type;
- if (containerURL instanceof ActionURL)
- {
- type = ACTION_LIFECYCLE;
- }
- else if (containerURL instanceof RenderURL)
- {
- type = RENDER_LIFECYCLE;
- }
- else if (containerURL instanceof ResourceURL)
- {
- type = RESOURCE_LIFECYCLE;
- }
- else
- {
- throw new Error();
- }
- parameters.put(LIFECYCLE_TYPE, type);
-
- //
- String pageState;
- try
- {
- byte[] bytes = PageNavigationalState.serialize(pageNS);
- pageState = Tools.toHexString(bytes);
- }
- catch (IOException e)
- {
- throw new Error(e); //
- }
-
- //
- if (containerURL instanceof PortletURL)
- {
- PortletURL portletURL = (PortletURL)containerURL;
-
- if (portletURL.getMode() != null)
- {
- parameters.put(MODE, portletURL.getMode().toString());
- }
-
- //
- if (portletURL.getWindowState() != null)
- {
- parameters.put(WINDOW_STATE, portletURL.getWindowState().toString());
- }
-
- //
- parameters.put(PAGE_STATE, pageState);
-
- //
- if (containerURL instanceof ActionURL)
- {
- ActionURL actionURL = (ActionURL)containerURL;
-
- //
- if (actionURL.getNavigationalState() != null)
- {
- parameters.put(NAVIGATIONAL_STATE,
actionURL.getNavigationalState().getStringValue());
- }
-
- //
- StateString interactionState = actionURL.getInteractionState();
- parameters.put(INTERACTION_STATE, interactionState.getStringValue());
- }
- else
- {
- RenderURL renderURL = (RenderURL)containerURL;
-
- //
- try
- {
- Map<String, String[]> changes =
renderURL.getPublicNavigationalStateChanges();
- HashMap<String, String[]> serialiableChanges = new
HashMap<String, String[]>(changes);
- byte[] bytes = IOTools.serialize(serialiableChanges);
- String ns = Tools.toHexString(bytes);
- parameters.put(PUBLIC_NAVIGATIONAL_STATE_CHANGES, ns);
- }
- catch (IOException e)
- {
- throw new Error(e);
- }
-
- //
- StateString navigationalState = renderURL.getNavigationalState();
- parameters.put(NAVIGATIONAL_STATE, navigationalState.getStringValue());
- }
- }
- else
- {
- ResourceURL resourceURL = (ResourceURL)containerURL;
-
- //
- StateString resourceState = resourceURL.getResourceState();
- parameters.put(RESOURCE_STATE, resourceState.getStringValue());
-
- //
- String resourceId = resourceURL.getResourceId();
- if (resourceId != null)
- {
- parameters.put(RESOURCE_ID, resourceId);
- }
-
- //
- CacheLevel cacheability = resourceURL.getCacheability();
- parameters.put(RESOURCE_CACHEABILITY, cacheability.name());
-
- //
- if (cacheability != CacheLevel.FULL)
- {
- if (resourceURL.getNavigationalState() != null)
- {
- parameters.put(NAVIGATIONAL_STATE,
resourceURL.getNavigationalState().getStringValue());
- }
-
- //
- if (resourceURL.getMode() != null)
- {
- parameters.put(MODE, resourceURL.getMode().toString());
- }
-
- //
- if (resourceURL.getWindowState() != null)
- {
- parameters.put(WINDOW_STATE, resourceURL.getWindowState().toString());
- }
-
- if (cacheability == CacheLevel.PAGE)
- {
- parameters.put(PAGE_STATE, pageState);
- }
- }
- }
-
- //
- boolean first = true;
- for (Map.Entry<String, String> entry : parameters.entrySet())
- {
- String name = entry.getKey();
- String value = entry.getValue();
- buffer.append(first ? '?' : '&');
- buffer.append(name, FastURLEncoder.getUTF8Instance());
- buffer.append('=');
- buffer.append(value, FastURLEncoder.getUTF8Instance());
- first = false;
- }
-
- //
- String url = buffer.asString();
- return clientResp.encodeURL(url);
- }
-}
Modified:
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/TestPortletInvocationContext.java
===================================================================
---
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/TestPortletInvocationContext.java 2008-02-05
02:56:52 UTC (rev 9759)
+++
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/TestPortletInvocationContext.java 2008-02-05
03:17:08 UTC (rev 9760)
@@ -23,6 +23,8 @@
package org.jboss.portal.portlet.test;
import org.jboss.portal.portlet.ContainerURL;
+import org.jboss.portal.portlet.test.controller.PortletControllerContext;
+import org.jboss.portal.portlet.test.controller.PageNavigationalState;
import org.jboss.portal.portlet.invocation.PortletInvocation;
import org.jboss.portal.portlet.impl.spi.AbstractPortletInvocationContext;
import org.jboss.portal.common.util.MarkupInfo;
@@ -40,17 +42,29 @@
{
/** . */
- private final PortletURLRenderer urlRenderer;
+ private final PortletControllerContext controllerContext;
- public TestPortletInvocationContext(PortletURLRenderer urlRenderer, MarkupInfo
markupInfo)
+ /** . */
+ private final String windowId;
+
+ /** . */
+ private final PageNavigationalState pageState;
+
+ public TestPortletInvocationContext(
+ PortletControllerContext controllerContext,
+ String windowId,
+ PageNavigationalState pageState,
+ MarkupInfo markupInfo)
{
super(markupInfo);
//
- this.urlRenderer = urlRenderer;
+ this.windowId = windowId;
+ this.pageState = pageState;
+ this.controllerContext = controllerContext;
//
- addResolver(PortletInvocation.PRINCIPAL_SCOPE, new
PrincipalAttributeResolver(urlRenderer.clientReq));
+ addResolver(PortletInvocation.PRINCIPAL_SCOPE, new
PrincipalAttributeResolver(controllerContext.getClientRequest()));
addResolver(PortletInvocation.INVOCATION_SCOPE, new MapAttributeResolver());
addResolver(PortletInvocation.REQUEST_PROPERTIES_SCOPE, new
MapAttributeResolver());
addResolver(PortletInvocation.RESPONSE_PROPERTIES_SCOPE, new
MapAttributeResolver());
@@ -59,16 +73,16 @@
public HttpServletRequest getClientRequest() throws IllegalStateException
{
- return urlRenderer.clientReq;
+ return controllerContext.getClientRequest();
}
public HttpServletResponse getClientResponse() throws IllegalStateException
{
- return urlRenderer.clientResp;
+ return controllerContext.getClientResponse();
}
public String renderURL(ContainerURL containerURL, Boolean wantSecure, Boolean
wantAuthenticated, boolean relative)
{
- return urlRenderer.renderURL(containerURL, wantSecure, wantAuthenticated,
relative);
+ return controllerContext.renderURL(windowId, pageState, containerURL, wantSecure,
wantAuthenticated, relative);
}
}
Modified:
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/PortletControllerContext.java
===================================================================
---
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/PortletControllerContext.java 2008-02-05
02:56:52 UTC (rev 9759)
+++
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/PortletControllerContext.java 2008-02-05
03:17:08 UTC (rev 9760)
@@ -24,6 +24,7 @@
import org.jboss.portal.portlet.Portlet;
import org.jboss.portal.portlet.PortletInvokerException;
+import org.jboss.portal.portlet.ContainerURL;
import org.jboss.portal.portlet.info.PortletInfo;
import org.jboss.portal.portlet.invocation.PortletInvocation;
import org.jboss.portal.portlet.invocation.ActionInvocation;
@@ -54,9 +55,6 @@
PortletInfo getPortletInfo(String windowId) throws PortletInvokerException;
- // To be removed
- PortletInvocationResponse invoke(PortletInvocation invocation) throws
PortletInvokerException;
-
PortletInvocationResponse invoke(String windowId, ActionInvocation actionInvocation)
throws PortletInvokerException;
PortletInvocationResponse invoke(String windowId, List<Cookie> requestCookies,
EventInvocation eventInvocation) throws PortletInvokerException;
@@ -65,4 +63,15 @@
EventControllerContext getEventControllerContext();
+ String renderURL(
+ String windowId,
+ PageNavigationalState pageState,
+ ContainerURL containerURL,
+ Boolean wantSecure,
+ Boolean wantAuthenticated,
+ boolean relative);
+
+
+ // To be removed, now only used in controller2
+ PortletInvocationResponse invoke(PortletInvocation invocation) throws
PortletInvokerException;
}
Modified:
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/PortletRequestHandler.java
===================================================================
---
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/PortletRequestHandler.java 2008-02-05
02:56:52 UTC (rev 9759)
+++
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/PortletRequestHandler.java 2008-02-05
03:17:08 UTC (rev 9760)
@@ -19,7 +19,6 @@
import org.jboss.portal.portlet.invocation.response.PortletInvocationResponse;
import org.jboss.portal.portlet.invocation.response.ResponseProperties;
import org.jboss.portal.portlet.invocation.response.UpdateNavigationalStateResponse;
-import org.jboss.portal.portlet.test.PortletURLRenderer;
import org.jboss.portal.portlet.test.TestPortletInvocationContext;
import static org.jboss.portal.portlet.test.controller.PortletController.*;
import org.jboss.portal.portlet.test.controller.event.Event;
@@ -184,7 +183,7 @@
PortletActionRequest portletActionRequest =
(PortletActionRequest)portletRequest;
//
- PageNavigationalState pageNS = portletActionRequest.getPageState();
+ PageNavigationalState pageState = portletActionRequest.getPageState();
//
Mode mode = portletActionRequest.getWindowNavigationalState().getMode();
@@ -201,9 +200,12 @@
}
//
- ParameterMap publicNS = pageNS.getPublicNavigationalState(portlet);
- PortletURLRenderer renderer = new PortletURLRenderer(pageNS,
portletRequest.getWindowId(), req, resp);
- TestPortletInvocationContext portletInvocationContext = new
TestPortletInvocationContext(renderer, MARKUP_INFO);
+ ParameterMap publicNS = pageState.getPublicNavigationalState(portlet);
+ TestPortletInvocationContext portletInvocationContext = new
TestPortletInvocationContext(
+ context,
+ portletRequest.getWindowId(),
+ pageState,
+ MARKUP_INFO);
ActionInvocation actionInvocation = new
ActionInvocation(portletInvocationContext);
//
@@ -220,14 +222,13 @@
}
private PortletInvocationResponse deliverEvent(PortletControllerContext context, Event
event,
- PageNavigationalState pageNS,
List<Cookie> requestCookies)
+ PageNavigationalState pageState,
List<Cookie> requestCookies)
throws PortletInvokerException
{
PortletInfo portlet = context.getPortletInfo(event.getWindowId());
HttpServletRequest req = context.getClientRequest();
HttpServletResponse resp = context.getClientResponse();
- WindowNavigationalState windowNS =
pageNS.getWindowNavigationalState(event.getWindowId());
- PortletURLRenderer renderer = new PortletURLRenderer(pageNS, event.getWindowId(),
req, resp);
+ WindowNavigationalState windowNS =
pageState.getWindowNavigationalState(event.getWindowId());
//
if (windowNS == null)
@@ -236,10 +237,14 @@
}
//
- ParameterMap publicNS = pageNS.getPublicNavigationalState(portlet);
+ ParameterMap publicNS = pageState.getPublicNavigationalState(portlet);
//
- TestPortletInvocationContext actionContext = new
TestPortletInvocationContext(renderer, MARKUP_INFO);
+ TestPortletInvocationContext actionContext = new TestPortletInvocationContext(
+ context,
+ event.getWindowId(),
+ pageState,
+ MARKUP_INFO);
EventInvocation eventInvocation = new EventInvocation(actionContext);
//
Modified:
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/PortletResourceRequestHandler.java
===================================================================
---
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/PortletResourceRequestHandler.java 2008-02-05
02:56:52 UTC (rev 9759)
+++
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller/PortletResourceRequestHandler.java 2008-02-05
03:17:08 UTC (rev 9760)
@@ -26,7 +26,6 @@
import org.jboss.portal.Mode;
import org.jboss.portal.WindowState;
import org.jboss.portal.common.util.ParameterMap;
-import org.jboss.portal.portlet.Portlet;
import org.jboss.portal.portlet.PortletInvokerException;
import org.jboss.portal.portlet.StateString;
import org.jboss.portal.portlet.info.PortletInfo;
@@ -35,7 +34,6 @@
import org.jboss.portal.portlet.invocation.response.ErrorResponse;
import org.jboss.portal.portlet.invocation.response.FragmentResponse;
import org.jboss.portal.portlet.invocation.response.PortletInvocationResponse;
-import org.jboss.portal.portlet.test.PortletURLRenderer;
import org.jboss.portal.portlet.test.TestPortletInvocationContext;
import static org.jboss.portal.portlet.test.controller.PortletController.*;
import org.jboss.portal.portlet.test.controller.request.ControllerRequest;
@@ -133,10 +131,11 @@
}
//
- PortletURLRenderer renderer = new PortletURLRenderer(pageNS,
portletResourceRequest.getWindowId(), req, resp);
-
- //
- TestPortletInvocationContext resourceInvocationContext = new
TestPortletInvocationContext(renderer, MARKUP_INFO);
+ TestPortletInvocationContext resourceInvocationContext = new
TestPortletInvocationContext(
+ context,
+ portletResourceRequest.getWindowId(),
+ pageNS,
+ MARKUP_INFO);
ResourceInvocation resourceInvocation = new
ResourceInvocation(resourceInvocationContext);
//
Modified:
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller2/PageUpdateRenderer.java
===================================================================
---
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller2/PageUpdateRenderer.java 2008-02-05
02:56:52 UTC (rev 9759)
+++
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller2/PageUpdateRenderer.java 2008-02-05
03:17:08 UTC (rev 9760)
@@ -41,7 +41,7 @@
import org.jboss.portal.portlet.invocation.response.FragmentResponse;
import org.jboss.portal.portlet.invocation.response.PortletInvocationResponse;
import org.jboss.portal.portlet.invocation.response.ResponseProperties;
-import org.jboss.portal.portlet.test.PortletURLRenderer;
+import org.jboss.portal.portlet.test.controller2.PortletURLRenderer;
import org.jboss.portal.portlet.test.TestInstanceContext;
import org.jboss.portal.portlet.test.controller.WindowNavigationalState;
import org.jboss.portal.portlet.test.TestPortalContext;
@@ -228,11 +228,13 @@
HttpServletResponse resp = context.getClientResponse();
//
- PortletURLRenderer renderer = new PortletURLRenderer(pageNS,
portlet.getContext().getId(), req, resp);
- //
TestInstanceContext instanceContext = new TestInstanceContext(req,
portlet.getContext(), false);
- TestPortletInvocationContext renderContext = new
TestPortletInvocationContext(renderer, PortletController.MARKUP_INFO);
+ TestPortletInvocationContext renderContext = new TestPortletInvocationContext(
+ context,
+ portlet.getContext().getId(),
+ pageNS,
+ PortletController.MARKUP_INFO);
//
RenderInvocation render = new RenderInvocation(renderContext);
Modified:
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller2/PortletControllerContextImpl.java
===================================================================
---
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller2/PortletControllerContextImpl.java 2008-02-05
02:56:52 UTC (rev 9759)
+++
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller2/PortletControllerContextImpl.java 2008-02-05
03:17:08 UTC (rev 9760)
@@ -33,6 +33,7 @@
import org.jboss.portal.portlet.PortletInvoker;
import org.jboss.portal.portlet.PortletInvokerException;
import org.jboss.portal.portlet.StateString;
+import org.jboss.portal.portlet.ContainerURL;
import org.jboss.portal.portlet.info.PortletInfo;
import org.jboss.portal.portlet.impl.spi.AbstractClientContext;
import org.jboss.portal.portlet.impl.spi.AbstractServerContext;
@@ -49,6 +50,7 @@
import org.jboss.portal.portlet.test.StringCodec;
import org.jboss.portal.portlet.test.TestInstanceContext;
import org.jboss.portal.portlet.test.TestPortalContext;
+import org.jboss.portal.portlet.test.controller2.PortletURLRenderer;
import static org.jboss.portal.portlet.test.URLParameterConstants.*;
import org.jboss.portal.portlet.test.controller.WindowNavigationalState;
import org.jboss.portal.portlet.test.controller.PortletControllerContext;
@@ -302,6 +304,14 @@
return eventControllerContext;
}
+ public String renderURL(String windowId, PageNavigationalState pageState, ContainerURL
containerURL, Boolean wantSecure, Boolean wantAuthenticated, boolean relative)
+ {
+ PortletURLRenderer renderer = new PortletURLRenderer(pageState, windowId, req,
resp);
+
+ //
+ return renderer.renderURL(containerURL, wantSecure, wantAuthenticated, relative);
+ }
+
public PortletInvocationResponse invoke(String windowId, ActionInvocation
actionInvocation) throws PortletInvokerException
{
Copied:
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller2/PortletURLRenderer.java
(from rev 9758,
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/PortletURLRenderer.java)
===================================================================
---
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller2/PortletURLRenderer.java
(rev 0)
+++
modules/portlet/trunk/test/src/main/java/org/jboss/portal/portlet/test/controller2/PortletURLRenderer.java 2008-02-05
03:17:08 UTC (rev 9760)
@@ -0,0 +1,246 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, 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.jboss.portal.portlet.test.controller2;
+
+import org.jboss.portal.common.io.IOTools;
+import org.jboss.portal.common.text.CharBuffer;
+import org.jboss.portal.common.text.FastURLEncoder;
+import org.jboss.portal.common.util.Tools;
+import org.jboss.portal.portlet.ActionURL;
+import org.jboss.portal.portlet.ContainerURL;
+import org.jboss.portal.portlet.Portlet;
+import org.jboss.portal.portlet.PortletURL;
+import org.jboss.portal.portlet.RenderURL;
+import org.jboss.portal.portlet.ResourceURL;
+import org.jboss.portal.portlet.StateString;
+import org.jboss.portal.portlet.info.PortletInfo;
+import org.jboss.portal.portlet.cache.CacheLevel;
+import static org.jboss.portal.portlet.test.URLParameterConstants.*;
+import org.jboss.portal.portlet.test.controller.PageNavigationalState;
+import org.jboss.portal.portlet.test.StringCodec;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class PortletURLRenderer
+{
+
+ /** . */
+ final String windowId;
+
+ /** . */
+ final HttpServletRequest clientReq;
+
+ /** . */
+ final HttpServletResponse clientResp;
+
+ /** . */
+ final PageNavigationalState pageNS;
+
+ public PortletURLRenderer(
+ PageNavigationalState pageNS,
+ String windowId,
+ HttpServletRequest clientReq,
+ HttpServletResponse clientResp)
+ {
+ this.pageNS = pageNS;
+ this.windowId = windowId;
+ this.clientReq = clientReq;
+ this.clientResp = clientResp;
+ }
+
+ public String renderURL(ContainerURL containerURL, Boolean wantSecure, Boolean
wantAuthenticated, boolean relative)
+ {
+ CharBuffer buffer = new CharBuffer();
+ buffer.append(clientReq.getScheme());
+ buffer.append("://");
+ buffer.append(clientReq.getServerName());
+ buffer.append(':');
+ buffer.append(Integer.toString(clientReq.getServerPort()));
+ buffer.append(clientReq.getContextPath());
+ buffer.append('/');
+ buffer.append(StringCodec.encode(windowId));
+
+ //
+ Map<String, String> parameters = new HashMap<String, String>();
+
+ //
+ String type;
+ if (containerURL instanceof ActionURL)
+ {
+ type = ACTION_LIFECYCLE;
+ }
+ else if (containerURL instanceof RenderURL)
+ {
+ type = RENDER_LIFECYCLE;
+ }
+ else if (containerURL instanceof ResourceURL)
+ {
+ type = RESOURCE_LIFECYCLE;
+ }
+ else
+ {
+ throw new Error();
+ }
+ parameters.put(LIFECYCLE_TYPE, type);
+
+ //
+ String pageState;
+ try
+ {
+ byte[] bytes = PageNavigationalState.serialize(pageNS);
+ pageState = Tools.toHexString(bytes);
+ }
+ catch (IOException e)
+ {
+ throw new Error(e); //
+ }
+
+ //
+ if (containerURL instanceof PortletURL)
+ {
+ PortletURL portletURL = (PortletURL)containerURL;
+
+ if (portletURL.getMode() != null)
+ {
+ parameters.put(MODE, portletURL.getMode().toString());
+ }
+
+ //
+ if (portletURL.getWindowState() != null)
+ {
+ parameters.put(WINDOW_STATE, portletURL.getWindowState().toString());
+ }
+
+ //
+ parameters.put(PAGE_STATE, pageState);
+
+ //
+ if (containerURL instanceof ActionURL)
+ {
+ ActionURL actionURL = (ActionURL)containerURL;
+
+ //
+ if (actionURL.getNavigationalState() != null)
+ {
+ parameters.put(NAVIGATIONAL_STATE,
actionURL.getNavigationalState().getStringValue());
+ }
+
+ //
+ StateString interactionState = actionURL.getInteractionState();
+ parameters.put(INTERACTION_STATE, interactionState.getStringValue());
+ }
+ else
+ {
+ RenderURL renderURL = (RenderURL)containerURL;
+
+ //
+ try
+ {
+ Map<String, String[]> changes =
renderURL.getPublicNavigationalStateChanges();
+ HashMap<String, String[]> serialiableChanges = new
HashMap<String, String[]>(changes);
+ byte[] bytes = IOTools.serialize(serialiableChanges);
+ String ns = Tools.toHexString(bytes);
+ parameters.put(PUBLIC_NAVIGATIONAL_STATE_CHANGES, ns);
+ }
+ catch (IOException e)
+ {
+ throw new Error(e);
+ }
+
+ //
+ StateString navigationalState = renderURL.getNavigationalState();
+ parameters.put(NAVIGATIONAL_STATE, navigationalState.getStringValue());
+ }
+ }
+ else
+ {
+ ResourceURL resourceURL = (ResourceURL)containerURL;
+
+ //
+ StateString resourceState = resourceURL.getResourceState();
+ parameters.put(RESOURCE_STATE, resourceState.getStringValue());
+
+ //
+ String resourceId = resourceURL.getResourceId();
+ if (resourceId != null)
+ {
+ parameters.put(RESOURCE_ID, resourceId);
+ }
+
+ //
+ CacheLevel cacheability = resourceURL.getCacheability();
+ parameters.put(RESOURCE_CACHEABILITY, cacheability.name());
+
+ //
+ if (cacheability != CacheLevel.FULL)
+ {
+ if (resourceURL.getNavigationalState() != null)
+ {
+ parameters.put(NAVIGATIONAL_STATE,
resourceURL.getNavigationalState().getStringValue());
+ }
+
+ //
+ if (resourceURL.getMode() != null)
+ {
+ parameters.put(MODE, resourceURL.getMode().toString());
+ }
+
+ //
+ if (resourceURL.getWindowState() != null)
+ {
+ parameters.put(WINDOW_STATE, resourceURL.getWindowState().toString());
+ }
+
+ if (cacheability == CacheLevel.PAGE)
+ {
+ parameters.put(PAGE_STATE, pageState);
+ }
+ }
+ }
+
+ //
+ boolean first = true;
+ for (Map.Entry<String, String> entry : parameters.entrySet())
+ {
+ String name = entry.getKey();
+ String value = entry.getValue();
+ buffer.append(first ? '?' : '&');
+ buffer.append(name, FastURLEncoder.getUTF8Instance());
+ buffer.append('=');
+ buffer.append(value, FastURLEncoder.getUTF8Instance());
+ first = false;
+ }
+
+ //
+ String url = buffer.asString();
+ return clientResp.encodeURL(url);
+ }
+}