struts-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nil...@apache.org
Subject svn commit: r724182 - in /struts/struts2/trunk/plugins/portlet/src: main/java/org/apache/struts2/components/PortletUrlRenderer.java test/java/org/apache/struts2/components/PortletUrlRendererTest.java
Date Sun, 07 Dec 2008 19:32:46 GMT
Author: nilsga
Date: Sun Dec  7 11:32:45 2008
New Revision: 724182

URL: http://svn.apache.org/viewvc?rev=724182&view=rev
Log:
WW-2862 Added pass through to servlet renderer when specified or not in a portlet context.

Modified:
    struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/components/PortletUrlRenderer.java
    struts/struts2/trunk/plugins/portlet/src/test/java/org/apache/struts2/components/PortletUrlRendererTest.java

Modified: struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/components/PortletUrlRenderer.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/components/PortletUrlRenderer.java?rev=724182&r1=724181&r2=724182&view=diff
==============================================================================
--- struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/components/PortletUrlRenderer.java
(original)
+++ struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/components/PortletUrlRenderer.java
Sun Dec  7 11:32:45 2008
@@ -25,6 +25,7 @@
 import java.io.Writer;
 
 import org.apache.struts2.StrutsException;
+import org.apache.struts2.portlet.context.PortletActionContext;
 import org.apache.struts2.portlet.util.PortletUrlHelper;
 
 import com.opensymphony.xwork2.ActionContext;
@@ -40,47 +41,61 @@
 public class PortletUrlRenderer implements UrlRenderer {
 	
 	/**
+	 * The servlet renderer used when not executing in a portlet context.
+	 */
+	private UrlRenderer servletRenderer = null;
+	
+	public PortletUrlRenderer() {
+		this.servletRenderer = new ServletUrlRenderer();
+	}
+	
+	/**
 	 * {@inheritDoc}
 	 */
 	public void renderUrl(Writer writer, URL urlComponent) {
-		String action = null;
-		if(urlComponent.action != null) {
-			action = urlComponent.findString(urlComponent.action);
+		if(PortletActionContext.getPortletContext() == null || "none".equalsIgnoreCase(urlComponent.portletUrlType))
{
+			servletRenderer.renderUrl(writer, urlComponent);
 		}
-		String scheme = urlComponent.req.getScheme();
-
-		if (urlComponent.scheme != null) {
-			scheme = urlComponent.scheme;
+		else {
+			String action = null;
+			if(urlComponent.action != null) {
+				action = urlComponent.findString(urlComponent.action);
+			}
+			String scheme = urlComponent.req.getScheme();
+
+			if (urlComponent.scheme != null) {
+				scheme = urlComponent.scheme;
+			}
+
+			String result;
+			urlComponent.namespace = urlComponent.determineNamespace(urlComponent.namespace, urlComponent.stack,
urlComponent.req);
+			if (onlyActionSpecified(urlComponent)) {
+				result = PortletUrlHelper.buildUrl(action, urlComponent.namespace, urlComponent.method,
urlComponent.parameters, urlComponent.portletUrlType, urlComponent.portletMode, urlComponent.windowState);
+			} else if(onlyValueSpecified(urlComponent)){
+				result = PortletUrlHelper.buildResourceUrl(urlComponent.value, urlComponent.parameters);
+			}
+			else {
+				result = createDefaultUrl(urlComponent);
+			}
+			if ( urlComponent.anchor != null && urlComponent.anchor.length() > 0 ) {
+				result += '#' + urlComponent.findString(urlComponent.anchor);
+			}
+
+			String var = urlComponent.getVar();
+
+			if (var != null) {
+				urlComponent.putInContext(result);
+
+				// add to the request and page scopes as well
+				urlComponent.req.setAttribute(var, result);
+			} else {
+				try {
+					writer.write(result);
+				} catch (IOException e) {
+					throw new StrutsException("IOError: " + e.getMessage(), e);
+				}
+			}
 		}
-
-        String result;
-        urlComponent.namespace = urlComponent.determineNamespace(urlComponent.namespace,
urlComponent.stack, urlComponent.req);
-        if (onlyActionSpecified(urlComponent)) {
-                result = PortletUrlHelper.buildUrl(action, urlComponent.namespace, urlComponent.method,
urlComponent.parameters, urlComponent.portletUrlType, urlComponent.portletMode, urlComponent.windowState);
-        } else if(onlyValueSpecified(urlComponent)){
-                result = PortletUrlHelper.buildResourceUrl(urlComponent.value, urlComponent.parameters);
-        }
-        else {
-        	result = createDefaultUrl(urlComponent);
-        }
-        if ( urlComponent.anchor != null && urlComponent.anchor.length() > 0 )
{
-        	result += '#' + urlComponent.findString(urlComponent.anchor);
-        }
-
-        String var = urlComponent.getVar();
-
-        if (var != null) {
-            urlComponent.putInContext(result);
-
-            // add to the request and page scopes as well
-            urlComponent.req.setAttribute(var, result);
-        } else {
-            try {
-                writer.write(result);
-            } catch (IOException e) {
-                throw new StrutsException("IOError: " + e.getMessage(), e);
-            }
-        }
 	}
 
 	private String createDefaultUrl(URL urlComponent) {
@@ -104,47 +119,59 @@
 	 * {@inheritDoc}
 	 */
 	public void renderFormUrl(Form formComponent) {
-		String namespace = formComponent.determineNamespace(formComponent.namespace, formComponent.getStack(),
-				formComponent.request);
-		String action = null;
-        if (formComponent.action != null) {
-            action = formComponent.findString(formComponent.action);
-        }
-        else {
-        	ActionInvocation ai = (ActionInvocation) formComponent.getStack().getContext().get(ActionContext.ACTION_INVOCATION);
-        	action = ai.getProxy().getActionName();
-        }
-        String type = "action";
-        if (TextUtils.stringSet(formComponent.method)) {
-            if ("GET".equalsIgnoreCase(formComponent.method.trim())) {
-                type = "render";
-            }
-        }
-        if (action != null) {
-            String result = PortletUrlHelper.buildUrl(action, namespace, null,
-                    formComponent.getParameters(), type, formComponent.portletMode, formComponent.windowState);
-            formComponent.addParameter("action", result);
-
-
-            // name/id: cut out anything between / and . should be the id and
-            // name
-            String id = formComponent.getId();
-            if (id == null) {
-                int slash = action.lastIndexOf('/');
-                int dot = action.indexOf('.', slash);
-                if (dot != -1) {
-                    id = action.substring(slash + 1, dot);
-                } else {
-                    id = action.substring(slash + 1);
-                }
-                formComponent.addParameter("id", formComponent.escape(id));
-            }
-        }
-
+		if(PortletActionContext.getPortletContext() == null) {
+			servletRenderer.renderFormUrl(formComponent);
+		}
+		else {
+			String namespace = formComponent.determineNamespace(formComponent.namespace, formComponent.getStack(),
+					formComponent.request);
+			String action = null;
+			if (formComponent.action != null) {
+				action = formComponent.findString(formComponent.action);
+			}
+			else {
+				ActionInvocation ai = (ActionInvocation) formComponent.getStack().getContext().get(ActionContext.ACTION_INVOCATION);
+				action = ai.getProxy().getActionName();
+			}
+			String type = "action";
+			if (TextUtils.stringSet(formComponent.method)) {
+				if ("GET".equalsIgnoreCase(formComponent.method.trim())) {
+					type = "render";
+				}
+			}
+			if (action != null) {
+				String result = PortletUrlHelper.buildUrl(action, namespace, null,
+						formComponent.getParameters(), type, formComponent.portletMode, formComponent.windowState);
+				formComponent.addParameter("action", result);
+
+
+				// name/id: cut out anything between / and . should be the id and
+				// name
+				String id = formComponent.getId();
+				if (id == null) {
+					int slash = action.lastIndexOf('/');
+					int dot = action.indexOf('.', slash);
+					if (dot != -1) {
+						id = action.substring(slash + 1, dot);
+					} else {
+						id = action.substring(slash + 1);
+					}
+					formComponent.addParameter("id", formComponent.escape(id));
+				}
+			}
+		}
 		
 	}
 
 	public void beforeRenderUrl(URL urlComponent) {
+		if(PortletActionContext.getPortletContext() == null) {
+			servletRenderer.beforeRenderUrl(urlComponent);
+		}
+	}
+
+	public void setServletRenderer(UrlRenderer nonPortletRenderer) {
+		this.servletRenderer = nonPortletRenderer;
+		
 	}
 
 }

Modified: struts/struts2/trunk/plugins/portlet/src/test/java/org/apache/struts2/components/PortletUrlRendererTest.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/portlet/src/test/java/org/apache/struts2/components/PortletUrlRendererTest.java?rev=724182&r1=724181&r2=724182&view=diff
==============================================================================
--- struts/struts2/trunk/plugins/portlet/src/test/java/org/apache/struts2/components/PortletUrlRendererTest.java
(original)
+++ struts/struts2/trunk/plugins/portlet/src/test/java/org/apache/struts2/components/PortletUrlRendererTest.java
Sun Dec  7 11:32:45 2008
@@ -21,18 +21,24 @@
 
 package org.apache.struts2.components;
 
+import java.io.IOException;
 import java.io.StringWriter;
+import java.io.Writer;
 import java.util.HashMap;
 import java.util.Map;
 
 import javax.portlet.PortletMode;
 import javax.portlet.PortletURL;
 
+import org.apache.struts2.StrutsStatics;
 import org.apache.struts2.StrutsTestCase;
 import org.apache.struts2.portlet.PortletActionConstants;
+import org.apache.struts2.portlet.context.PortletActionContext;
 import org.apache.struts2.portlet.servlet.PortletServletRequest;
 import org.apache.struts2.portlet.servlet.PortletServletResponse;
+import org.easymock.EasyMock;
 import org.springframework.mock.web.portlet.MockPortalContext;
+import org.springframework.mock.web.portlet.MockPortletContext;
 import org.springframework.mock.web.portlet.MockPortletURL;
 import org.springframework.mock.web.portlet.MockRenderRequest;
 import org.springframework.mock.web.portlet.MockRenderResponse;
@@ -49,12 +55,14 @@
 	MockPortletURL actionUrl;
 	MockRenderRequest request;
 	MockRenderResponse response;
+	MockPortletContext context;
 	ActionContext ctx;
 	ValueStack stack;
 
 	public void setUp() throws Exception {
 		super.setUp();
 		renderer = new PortletUrlRenderer();
+		context = new MockPortletContext();
 		renderUrl = new MockPortletURL(
 				new MockPortalContext(), "render");
 		actionUrl = new MockPortletURL(
@@ -76,6 +84,7 @@
 				PortletActionConstants.RENDER_PHASE);
 		ctx.put(PortletActionConstants.REQUEST, request);
 		ctx.put(PortletActionConstants.RESPONSE, response);
+		ctx.put(StrutsStatics.STRUTS_PORTLET_CONTEXT, context);
 
 		Map<PortletMode, String> modeMap = new HashMap<PortletMode, String>();
 		modeMap.put(PortletMode.VIEW, "/view");
@@ -185,6 +194,41 @@
 		
 	}
 	
+	public void testShouldPassThroughRenderUrlToServletUrlRendererIfNotPortletRequest() throws
Exception {
+		UrlRenderer mockRenderer = EasyMock.createMock(UrlRenderer.class);
+		ActionContext.getContext().put(StrutsStatics.STRUTS_PORTLET_CONTEXT, null);
+		renderer.setServletRenderer(mockRenderer);
+		URL url = new URL(stack, new PortletServletRequest(request, null), new PortletServletResponse(response));
+		StringWriter renderOutput = new StringWriter();
+		mockRenderer.renderUrl(renderOutput, url);
+		EasyMock.replay(mockRenderer);
+		renderer.renderUrl(renderOutput, url);
+		EasyMock.verify(mockRenderer);
+	}
+	
+	public void testShouldPassThroughRenderFormUrlToServletUrlRendererIfNotPortletRequest()
throws Exception {
+		UrlRenderer mockRenderer = EasyMock.createMock(UrlRenderer.class);
+		ActionContext.getContext().put(StrutsStatics.STRUTS_PORTLET_CONTEXT, null);
+		renderer.setServletRenderer(mockRenderer);
+		Form form = new Form(stack, new PortletServletRequest(request, null), new PortletServletResponse(response));
+		mockRenderer.renderFormUrl(form);
+		EasyMock.replay(mockRenderer);
+		renderer.renderFormUrl(form);
+		EasyMock.verify(mockRenderer);
+	}
+	
+	public void testShouldPassThroughRenderUrlToServletUrlRendererWhenPortletUrlTypeIsNone()
throws Exception {
+		UrlRenderer mockRenderer = EasyMock.createMock(UrlRenderer.class);
+		renderer.setServletRenderer(mockRenderer);
+		URL url = new URL(stack, new PortletServletRequest(request, null), new PortletServletResponse(response));
+		url.setPortletUrlType("none");
+		StringWriter renderOutput = new StringWriter();
+		mockRenderer.renderUrl(renderOutput, url);
+		EasyMock.replay(mockRenderer);
+		renderer.renderUrl(renderOutput, url);
+		EasyMock.verify(mockRenderer);
+	}
+	
 	private final static class TestObject {
 		public String someProperty;
 	}



Mime
View raw message