WW-3282 Changes logic to add namespace only once when building urls and cleans up code

git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@1327601 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Lukasz Lenart
2012-04-18 18:13:57 +00:00
parent 73e763faf8
commit 5a9048065f
26 changed files with 709 additions and 760 deletions
@@ -23,8 +23,6 @@ package org.apache.struts2.components;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.StrutsException;
import org.apache.struts2.dispatcher.mapper.ActionMapper;
@@ -32,6 +30,7 @@ import org.apache.struts2.portlet.context.PortletActionContext;
import org.apache.struts2.portlet.util.PortletUrlHelper;
import org.apache.struts2.portlet.util.PortletUrlHelperJSR286;
import javax.portlet.PortletMode;
import java.io.IOException;
import java.io.Writer;
@@ -42,8 +41,6 @@ import java.io.Writer;
*/
public class PortletUrlRenderer implements UrlRenderer {
private static final Logger LOG = LoggerFactory.getLogger(PortletUrlRenderer.class);
/**
* The servlet renderer used when not executing in a portlet context.
*/
@@ -73,16 +70,31 @@ public class PortletUrlRenderer implements UrlRenderer {
servletRenderer.renderUrl(writer, urlComponent);
return;
}
String result;
urlComponent.setNamespace(urlComponent.determineNamespace(urlComponent.getNamespace(), urlComponent.getStack(), urlComponent.getHttpServletRequest()));
if (onlyActionSpecified(urlComponent)) {
result = portletUrlHelper.buildUrl(urlComponent.getAction(), urlComponent.getNamespace(), urlComponent.getMethod(),
urlComponent.getParameters(), urlComponent.getPortletUrlType(), urlComponent.getPortletMode(), urlComponent.getWindowState());
} else if (onlyValueSpecified(urlComponent)) {
result = portletUrlHelper.buildResourceUrl(urlComponent.getValue(), urlComponent.getParameters());
if (isPortletModeChange(urlComponent,PortletActionContext.getRequest().getPortletMode())
&& StringUtils.isEmpty(urlComponent.getNamespace())
) {
String mode = urlComponent.getPortletMode();
PortletMode portletMode = new PortletMode(mode);
String action = urlComponent.getAction();
if (StringUtils.isEmpty(action)) {
action = PortletActionContext.getModeActionMap().get(portletMode).getName();
}
String modeNamespace = PortletActionContext.getModeNamespaceMap().get(portletMode);
result = portletUrlHelper.buildUrl(action, modeNamespace, urlComponent.getMethod(),
urlComponent.getParameters(), urlComponent.getPortletUrlType(), mode, urlComponent.getWindowState());
} else {
result = createDefaultUrl(urlComponent);
String namespace = urlComponent.determineNamespace(urlComponent.getNamespace(), urlComponent.getStack(), urlComponent.getHttpServletRequest());
urlComponent.setNamespace(namespace);
if (onlyActionSpecified(urlComponent)) {
result = portletUrlHelper.buildUrl(urlComponent.getAction(), urlComponent.getNamespace(), urlComponent.getMethod(),
urlComponent.getParameters(), urlComponent.getPortletUrlType(), urlComponent.getPortletMode(), urlComponent.getWindowState());
} else if (onlyValueSpecified(urlComponent)) {
result = portletUrlHelper.buildResourceUrl(urlComponent.getValue(), urlComponent.getParameters());
} else {
result = createDefaultUrl(urlComponent);
}
}
final String anchor = urlComponent.getAnchor();
if (anchor != null && anchor.length() > 0) {
@@ -105,6 +117,14 @@ public class PortletUrlRenderer implements UrlRenderer {
}
}
boolean isPortletModeChange(UrlProvider urlComponent,PortletMode currentMode) {
if (StringUtils.isNotEmpty(urlComponent.getPortletMode())) {
PortletMode newPortletMode = new PortletMode(urlComponent.getPortletMode());
return !(newPortletMode.equals(currentMode));
}
return false;
}
private String createDefaultUrl(UrlProvider urlComponent) {
String result;
ActionInvocation ai = (ActionInvocation) urlComponent.getStack().getContext().get(
@@ -1,140 +0,0 @@
/*
* $Id: PortletActionConstants.java 671156 2008-06-24 12:33:59Z nilsga $
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.struts2.portlet;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.portlet.dispatcher.DispatcherServlet;
/**
* Interface defining some constants used in the Struts portlet implementation
*
*/
public interface PortletActionConstants {
/**
* Default action name to use when no default action has been configured in the portlet
* init parameters.
*/
String DEFAULT_ACTION_NAME = "default";
/**
* Action name parameter name
*/
String ACTION_PARAM = "struts.portlet.action";
/**
* Key for parameter holding the last executed portlet mode.
*/
String MODE_PARAM = "struts.portlet.mode";
/**
* Key used for looking up and storing the portlet phase
*/
String PHASE = "struts.portlet.phase";
/**
* Constant used for the render phase (
* {@link javax.portlet.Portlet#render(javax.portlet.RenderRequest, javax.portlet.RenderResponse)})
*/
Integer RENDER_PHASE = new Integer(1);
/**
* Constant used for the action phase (
* {@link javax.portlet.Portlet#processAction(javax.portlet.ActionRequest, javax.portlet.ActionResponse)})
*/
Integer ACTION_PHASE = new Integer(2);
/**
* Key used for looking up and storing the
* {@link javax.portlet.PortletRequest}
*/
String REQUEST = "struts.portlet.request";
/**
* Key used for looking up and storing the
* {@link javax.portlet.PortletResponse}
*/
String RESPONSE = "struts.portlet.response";
/**
* Key used for looking up and storing the action that was invoked in the event phase.
*/
String EVENT_ACTION = "struts.portlet.eventAction";
/**
* Key used for looking up and storing the
* {@link javax.portlet.PortletConfig}
*/
String PORTLET_CONFIG = "struts.portlet.config";
/**
* Name of the action used as error handler
*/
String ERROR_ACTION = "errorHandler";
/**
* Key for the portlet namespace stored in the
* {@link org.apache.struts2.portlet.context.PortletActionContext}.
*/
String PORTLET_NAMESPACE = "struts.portlet.portletNamespace";
/**
* Key for the mode-to-namespace map stored in the
* {@link org.apache.struts2.portlet.context.PortletActionContext}.
*/
String MODE_NAMESPACE_MAP = "struts.portlet.modeNamespaceMap";
/**
* Key for the default action name for the portlet, stored in the
* {@link org.apache.struts2.portlet.context.PortletActionContext}.
*/
String DEFAULT_ACTION_FOR_MODE = "struts.portlet.defaultActionForMode";
/**
* Key for request attribute indicating if the action has been reset.
*/
String ACTION_RESET = "struts.portlet.actionReset";
/**
* Key for session attribute indicating the location of the render direct action.
*/
String RENDER_DIRECT_LOCATION = "struts.portlet.renderDirectLocation";
/**
* Key for the dispatch instruction for the {@link DispatcherServlet}
*/
String DISPATCH_TO = "struts.portlet.dispatchTo";
/**
* Session key where the value stack from the event phase is stored.
*/
String STACK_FROM_EVENT_PHASE = "struts.portlet.valueStackFromEventPhase";
/**
* Default name of dispatcher servlet in web.xml
*/
String DEFAULT_DISPATCHER_SERVLET_NAME = "Struts2PortletDispatcherServlet";
/**
* Key for the action mapping in the context
*/
String ACTION_MAPPING = ServletActionContext.ACTION_MAPPING;
}
@@ -20,6 +20,7 @@
*/
package org.apache.struts2.portlet;
import javax.portlet.PortletContext;
import java.io.Serializable;
import java.util.AbstractMap;
import java.util.Enumeration;
@@ -27,20 +28,18 @@ import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.portlet.PortletContext;
/**
* Portlet specific {@link java.util.Map} implementation representing the
* {@link javax.portlet.PortletContext} of a Portlet.
*
*/
public class PortletApplicationMap extends AbstractMap implements Serializable {
public class PortletApplicationMap extends AbstractMap<String, Object> implements Serializable {
private static final long serialVersionUID = 2296107511063504414L;
private PortletContext context;
private Set<Object> entries;
private Set<Entry<String, Object>> entries;
/**
* Creates a new map object given the {@link PortletContext}.
@@ -72,9 +71,9 @@ public class PortletApplicationMap extends AbstractMap implements Serializable {
* @return a Set of all portlet context attributes as well as context init
* parameters.
*/
public Set entrySet() {
public Set<Entry<String, Object>> entrySet() {
if (entries == null) {
entries = new HashSet<Object>();
entries = new HashSet<Entry<String, Object>>();
// Add portlet context attributes
Enumeration enumeration = context.getAttributeNames();
@@ -82,7 +81,7 @@ public class PortletApplicationMap extends AbstractMap implements Serializable {
while (enumeration.hasMoreElements()) {
final String key = enumeration.nextElement().toString();
final Object value = context.getAttribute(key);
entries.add(new Map.Entry() {
entries.add(new Entry<String, Object>() {
public boolean equals(Object obj) {
Map.Entry entry = (Map.Entry) obj;
@@ -97,7 +96,7 @@ public class PortletApplicationMap extends AbstractMap implements Serializable {
^ ((value == null) ? 0 : value.hashCode());
}
public Object getKey() {
public String getKey() {
return key;
}
@@ -106,7 +105,7 @@ public class PortletApplicationMap extends AbstractMap implements Serializable {
}
public Object setValue(Object obj) {
context.setAttribute(key.toString(), obj);
context.setAttribute(key, obj);
return value;
}
@@ -119,7 +118,7 @@ public class PortletApplicationMap extends AbstractMap implements Serializable {
while (enumeration.hasMoreElements()) {
final String key = enumeration.nextElement().toString();
final Object value = context.getInitParameter(key);
entries.add(new Map.Entry() {
entries.add(new Entry<String, Object>() {
public boolean equals(Object obj) {
Map.Entry entry = (Map.Entry) obj;
@@ -134,7 +133,7 @@ public class PortletApplicationMap extends AbstractMap implements Serializable {
^ ((value == null) ? 0 : value.hashCode());
}
public Object getKey() {
public String getKey() {
return key;
}
@@ -143,7 +142,7 @@ public class PortletApplicationMap extends AbstractMap implements Serializable {
}
public Object setValue(Object obj) {
context.setAttribute(key.toString(), obj);
context.setAttribute(key, obj);
return value;
}
@@ -163,13 +162,12 @@ public class PortletApplicationMap extends AbstractMap implements Serializable {
* @return the portlet context attribute or init parameter or <tt>null</tt>
* if the entry is not found.
*/
public Object get(Object key) {
public Object get(String key) {
// Try context attributes first, then init params
// This gives the proper shadowing effects
String keyString = key.toString();
Object value = context.getAttribute(keyString);
Object value = context.getAttribute(key);
return (value == null) ? context.getInitParameter(keyString) : value;
return (value == null) ? context.getInitParameter(key) : value;
}
/**
@@ -181,9 +179,9 @@ public class PortletApplicationMap extends AbstractMap implements Serializable {
* the value to set.
* @return the attribute that was just set.
*/
public Object put(Object key, Object value) {
public Object put(String key, Object value) {
entries = null;
context.setAttribute(key.toString(), value);
context.setAttribute(key, value);
return get(key);
}
@@ -195,11 +193,11 @@ public class PortletApplicationMap extends AbstractMap implements Serializable {
* the attribute to remove.
* @return the entry that was just removed.
*/
public Object remove(Object key) {
public Object remove(String key) {
entries = null;
Object value = get(key);
context.removeAttribute(key.toString());
context.removeAttribute(key);
return value;
}
@@ -1,5 +1,6 @@
package org.apache.struts2.portlet;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.portlet.dispatcher.DispatcherServlet;
public class PortletConstants {
@@ -24,28 +25,6 @@ public class PortletConstants {
*/
public static String PHASE = "struts.portlet.phase";
/**
* Constant used for the render phase (
* {@link javax.portlet.Portlet#render(javax.portlet.RenderRequest, javax.portlet.RenderResponse)})
*/
public static Integer RENDER_PHASE = new Integer(1);
/**
* Constant used for the action phase (
* {@link javax.portlet.Portlet#processAction(javax.portlet.ActionRequest, javax.portlet.ActionResponse)})
*/
public static Integer ACTION_PHASE = new Integer(2);
/**
* Constant used for the event phase
*/
public static Integer EVENT_PHASE = new Integer(3);
/**
*
*/
public static Integer SERVE_RESOURCE_PHASE = new Integer(4);
/**
* Key used for looking up and storing the
* {@link javax.portlet.PortletRequest}
@@ -86,6 +65,12 @@ public class PortletConstants {
*/
public static String MODE_NAMESPACE_MAP = "struts.portlet.modeNamespaceMap";
/**
* Key for the mode-to-default-action map stored in the
* {@link org.apache.struts2.portlet.context.PortletActionContext}.
*/
public static String DEFAULT_ACTION_MAP = "struts.portlet.modeActionMap";
/**
* Key for the default action name for the portlet, stored in the
* {@link org.apache.struts2.portlet.context.PortletActionContext}.
@@ -101,7 +86,12 @@ public class PortletConstants {
* Key for session attribute indicating the location of the render direct action.
*/
public static String RENDER_DIRECT_LOCATION = "struts.portlet.renderDirectLocation";
/**
* Namespace use for executing the renderDirect action. Necessary to get the namespaces on s:urls correct.
*/
public static String RENDER_DIRECT_NAMESPACE = "struts.portlet.renderDirectNamespace";
/**
* Key for the dispatch instruction for the {@link DispatcherServlet}
*/
@@ -116,4 +106,10 @@ public class PortletConstants {
* Default name of dispatcher servlet in web.xml
*/
public static String DEFAULT_DISPATCHER_SERVLET_NAME = "Struts2PortletDispatcherServlet";
/**
* Key for the action mapping in the context
*/
public static String ACTION_MAPPING = ServletActionContext.ACTION_MAPPING;
}
@@ -0,0 +1,46 @@
package org.apache.struts2.portlet;
/**
* TODO lukaszlenart: add a comment
*/
public enum PortletPhase {
/**
* Constant used for the render phase (
* {@link javax.portlet.Portlet#render(javax.portlet.RenderRequest, javax.portlet.RenderResponse)})
*/
RENDER_PHASE,
/**
* Constant used for the action phase (
* {@link javax.portlet.Portlet#processAction(javax.portlet.ActionRequest, javax.portlet.ActionResponse)})
*/
ACTION_PHASE,
/**
* Constant used for the event phase
*/
EVENT_PHASE,
/**
*
*/
SERVE_RESOURCE_PHASE;
public boolean isRender() {
return this.equals(RENDER_PHASE);
}
public boolean isAction() {
return this.equals(ACTION_PHASE);
}
public boolean isEvent() {
return this.equals(EVENT_PHASE);
}
public boolean isResource() {
return this.equals(SERVE_RESOURCE_PHASE);
}
}
@@ -20,25 +20,19 @@
*/
package org.apache.struts2.portlet;
import javax.portlet.PortletRequest;
import java.util.AbstractMap;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;
import javax.portlet.PortletRequest;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;
/**
* A simple implementation of the {@link java.util.Map} interface to handle a collection of request attributes.
*
*/
public class PortletRequestMap extends AbstractMap {
public class PortletRequestMap extends AbstractMap<String, Object> {
private static final Logger LOG = LoggerFactory.getLogger(PortletRequestMap.class);
private Set<Object> entries = null;
private Set<Entry<String, Object>> entries = null;
private PortletRequest request = null;
/**
@@ -69,16 +63,16 @@ public class PortletRequestMap extends AbstractMap {
*
* @return a Set of attributes from the portlet request.
*/
public Set entrySet() {
public Set<Entry<String, Object>> entrySet() {
if (entries == null) {
entries = new HashSet<Object>();
entries = new HashSet<Entry<String, Object>>();
Enumeration enumeration = request.getAttributeNames();
Enumeration<String> enumeration = request.getAttributeNames();
while (enumeration.hasMoreElements()) {
final String key = enumeration.nextElement().toString();
final String key = enumeration.nextElement();
final Object value = request.getAttribute(key);
entries.add(new Entry() {
entries.add(new Entry<String, Object>() {
public boolean equals(Object obj) {
Entry entry = (Entry) obj;
@@ -93,7 +87,7 @@ public class PortletRequestMap extends AbstractMap {
^ ((value == null) ? 0 : value.hashCode());
}
public Object getKey() {
public String getKey() {
return key;
}
@@ -131,9 +125,9 @@ public class PortletRequestMap extends AbstractMap {
* @param value the value to set.
* @return the object that was just set.
*/
public Object put(Object key, Object value) {
public Object put(String key, Object value) {
entries = null;
request.setAttribute(key.toString(), value);
request.setAttribute(key, value);
return get(key);
}
@@ -20,30 +20,24 @@
*/
package org.apache.struts2.portlet;
import javax.portlet.PortletRequest;
import javax.portlet.PortletSession;
import java.util.AbstractMap;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.portlet.PortletRequest;
import javax.portlet.PortletSession;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;
/**
* A simple implementation of the {@link java.util.Map} interface to handle a collection of portlet session
* attributes. The {@link #entrySet()} method enumerates over all session attributes and creates a Set of entries.
* Note, this will occur lazily - only when the entry set is asked for.
*
*/
public class PortletSessionMap extends AbstractMap {
public class PortletSessionMap extends AbstractMap<String, Object> {
private static final Logger LOG = LoggerFactory.getLogger(PortletSessionMap.class);
private PortletSession session = null;
private Set<Object> entries = null;
private final PortletSession session;
private Set<Entry<String, Object>> entries = null;
/**
* Creates a new session map given a portlet request.
@@ -57,17 +51,17 @@ public class PortletSessionMap extends AbstractMap {
/**
* @see java.util.Map#entrySet()
*/
public Set entrySet() {
public Set<Entry<String, Object>> entrySet() {
synchronized (session) {
if (entries == null) {
entries = new HashSet<Object>();
entries = new HashSet<Entry<String, Object>>();
Enumeration enumeration = session.getAttributeNames();
while (enumeration.hasMoreElements()) {
final String key = enumeration.nextElement().toString();
final Object value = session.getAttribute(key);
entries.add(new Map.Entry() {
entries.add(new Entry<String, Object>() {
public boolean equals(Object obj) {
Map.Entry entry = (Map.Entry) obj;
@@ -82,7 +76,7 @@ public class PortletSessionMap extends AbstractMap {
^ ((value == null) ? 0 : value.hashCode());
}
public Object getKey() {
public String getKey() {
return key;
}
@@ -123,10 +117,10 @@ public class PortletSessionMap extends AbstractMap {
* @param value the value to set.
* @return the object that was just set.
*/
public Object put(Object key, Object value) {
public Object put(String key, Object value) {
synchronized (session) {
entries = null;
session.setAttribute(key.toString(), value);
session.setAttribute(key, value);
return get(key);
}
@@ -23,28 +23,27 @@ package org.apache.struts2.portlet.context;
import com.opensymphony.xwork2.ActionContext;
import org.apache.struts2.StrutsStatics;
import org.apache.struts2.dispatcher.mapper.ActionMapping;
import org.apache.struts2.portlet.PortletConstants;
import org.apache.struts2.portlet.PortletPhase;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.PortletContext;
import javax.portlet.PortletMode;
import javax.portlet.PortletRequest;
import javax.portlet.PortletResponse;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import java.util.Map;
import static org.apache.struts2.portlet.PortletConstants.ACTION_PHASE;
import static org.apache.struts2.portlet.PortletConstants.DEFAULT_ACTION_FOR_MODE;
import static org.apache.struts2.portlet.PortletConstants.EVENT_PHASE;
import static org.apache.struts2.portlet.PortletConstants.MODE_NAMESPACE_MAP;
import static org.apache.struts2.portlet.PortletConstants.PHASE;
import static org.apache.struts2.portlet.PortletConstants.PORTLET_CONFIG;
import static org.apache.struts2.portlet.PortletConstants.PORTLET_NAMESPACE;
import static org.apache.struts2.portlet.PortletConstants.RENDER_PHASE;
import static org.apache.struts2.portlet.PortletConstants.REQUEST;
import static org.apache.struts2.portlet.PortletConstants.RESPONSE;
import static org.apache.struts2.portlet.PortletConstants.SERVE_RESOURCE_PHASE;
/**
@@ -70,9 +69,8 @@ public class PortletActionContext {
* @throws IllegalStateException If the method is invoked in the wrong phase.
*/
public static RenderRequest getRenderRequest() {
if (!isRender()) {
throw new IllegalStateException(
"RenderRequest cannot be obtained in event phase");
if (!getPhase().isRender()) {
throw new IllegalStateException("RenderRequest cannot be obtained in event phase");
}
return (RenderRequest) getContext().get(REQUEST);
}
@@ -84,9 +82,8 @@ public class PortletActionContext {
* @throws IllegalStateException If the method is invoked in the wrong phase.
*/
public static RenderResponse getRenderResponse() {
if (!isRender()) {
throw new IllegalStateException(
"RenderResponse cannot be obtained in event phase");
if (!getPhase().isRender()) {
throw new IllegalStateException("RenderResponse cannot be obtained in event phase");
}
return (RenderResponse) getContext().get(RESPONSE);
}
@@ -98,9 +95,8 @@ public class PortletActionContext {
* @throws IllegalStateException If the method is invoked in the wrong phase.
*/
public static ActionRequest getActionRequest() {
if (!isAction()) {
throw new IllegalStateException(
"ActionRequest cannot be obtained in render phase");
if (!getPhase().isAction()) {
throw new IllegalStateException("ActionRequest cannot be obtained in render phase");
}
return (ActionRequest) getContext().get(REQUEST);
}
@@ -112,9 +108,8 @@ public class PortletActionContext {
* @throws IllegalStateException If the method is invoked in the wrong phase.
*/
public static ActionResponse getActionResponse() {
if (!isAction()) {
throw new IllegalStateException(
"ActionResponse cannot be obtained in render phase");
if (!getPhase().isAction()) {
throw new IllegalStateException("ActionResponse cannot be obtained in render phase");
}
return (ActionResponse) getContext().get(RESPONSE);
}
@@ -150,32 +145,11 @@ public class PortletActionContext {
/**
* Get the phase that the portlet is executing in.
*
* @return {@link PortletActionConstants#RENDER_PHASE} in render phase, and
* {@link PortletActionConstants#ACTION_PHASE} in the event phase.
* @return {@link PortletPhase#RENDER_PHASE} in render phase, and
* {@link PortletPhase#ACTION_PHASE} in the event phase.
*/
public static Integer getPhase() {
return (Integer) getContext().get(PHASE);
}
/**
* @return <code>true</code> if the Portlet is executing in render phase.
*/
public static boolean isRender() {
return RENDER_PHASE.equals(getPhase());
}
/**
* @return <code>true</code> if the Portlet is executing in the event phase.
*/
public static boolean isAction() {
return ACTION_PHASE.equals(getPhase());
}
/**
* @return <code>true</code> if the Portlet is executing in the resource phase.
*/
public static boolean isResource() {
return SERVE_RESOURCE_PHASE.equals(getPhase());
public static PortletPhase getPhase() {
return (PortletPhase) getContext().get(PHASE);
}
/**
@@ -208,8 +182,19 @@ public class PortletActionContext {
*
* @return The map of the namespaces for each mode.
*/
public static Map getModeNamespaceMap() {
return (Map) getContext().get(MODE_NAMESPACE_MAP);
@SuppressWarnings("unchecked")
public static Map<PortletMode,String> getModeNamespaceMap() {
return (Map<PortletMode,String>) getContext().get(MODE_NAMESPACE_MAP);
}
/**
* Get the mode to default action mappings.
*
* @return The map of default action mapping for each mode
*/
@SuppressWarnings("unchecked")
public static Map<PortletMode,ActionMapping> getModeActionMap() {
return (Map<PortletMode,ActionMapping>) getContext().get(PortletConstants.DEFAULT_ACTION_MAP);
}
/**
@@ -221,10 +206,6 @@ public class PortletActionContext {
return (PortletContext) getContext().get(StrutsStatics.STRUTS_PORTLET_CONTEXT);
}
public static boolean isEvent() {
return EVENT_PHASE.equals(getPhase());
}
/**
* Whether JSR286 features are supported.
*
@@ -21,13 +21,12 @@
package org.apache.struts2.portlet.dispatcher;
import com.opensymphony.xwork2.Action;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.portlet.PortletConstants;
import java.io.Serializable;
import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.portlet.PortletActionConstants;
/**
* When a portlet is targetted for an <code>event</code>, the portlet will receive two
* portlet requests, one for the <code>event</code> phase, and then followed by a <code>render</code>
@@ -42,7 +41,7 @@ import org.apache.struts2.portlet.PortletActionConstants;
* specifying this action and the location of the view, which then will be executed in the
* following render request.
*/
public class DirectRenderFromEventAction implements SessionAware, PortletActionConstants, Action, Serializable {
public class DirectRenderFromEventAction implements SessionAware, Action, Serializable {
private static final long serialVersionUID = -1814807772308405785L;
@@ -67,6 +66,6 @@ public class DirectRenderFromEventAction implements SessionAware, PortletActionC
}
public void setSession(Map session) {
location = (String)session.get(RENDER_DIRECT_LOCATION);
location = (String)session.get(PortletConstants.RENDER_DIRECT_LOCATION);
}
}
@@ -20,24 +20,23 @@
*/
package org.apache.struts2.portlet.dispatcher;
import java.io.IOException;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.dispatcher.StrutsRequestWrapper;
import org.apache.struts2.portlet.PortletConstants;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.dispatcher.StrutsRequestWrapper;
import org.apache.struts2.portlet.PortletActionConstants;
public class DispatcherServlet extends HttpServlet implements PortletActionConstants {
public class DispatcherServlet extends HttpServlet {
private static final long serialVersionUID = -266147033645951967L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String dispatchTo = (String) request.getAttribute(DISPATCH_TO);
String dispatchTo = (String) request.getAttribute(PortletConstants.DISPATCH_TO);
HttpServletRequest wrapper = wrapRequestIfNecessary(request);
if(StringUtils.isNotEmpty(dispatchTo)) {
request.getRequestDispatcher(dispatchTo).include(wrapper, response);
@@ -41,8 +41,9 @@ import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.dispatcher.mapper.ActionMapper;
import org.apache.struts2.dispatcher.mapper.ActionMapping;
import org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper;
import org.apache.struts2.portlet.PortletActionConstants;
import org.apache.struts2.portlet.PortletApplicationMap;
import org.apache.struts2.portlet.PortletConstants;
import org.apache.struts2.portlet.PortletPhase;
import org.apache.struts2.portlet.PortletRequestMap;
import org.apache.struts2.portlet.PortletSessionMap;
import org.apache.struts2.portlet.context.PortletActionContext;
@@ -72,16 +73,13 @@ import java.util.Locale;
import java.util.Map;
import static org.apache.struts2.portlet.PortletConstants.ACTION_PARAM;
import static org.apache.struts2.portlet.PortletConstants.ACTION_PHASE;
import static org.apache.struts2.portlet.PortletConstants.ACTION_RESET;
import static org.apache.struts2.portlet.PortletConstants.DEFAULT_ACTION_FOR_MODE;
import static org.apache.struts2.portlet.PortletConstants.DEFAULT_ACTION_NAME;
import static org.apache.struts2.portlet.PortletConstants.MODE_NAMESPACE_MAP;
import static org.apache.struts2.portlet.PortletConstants.MODE_PARAM;
import static org.apache.struts2.portlet.PortletConstants.PHASE;
import static org.apache.struts2.portlet.PortletConstants.PORTLET_CONFIG;
import static org.apache.struts2.portlet.PortletConstants.PORTLET_NAMESPACE;
import static org.apache.struts2.portlet.PortletConstants.RENDER_PHASE;
import static org.apache.struts2.portlet.PortletConstants.REQUEST;
import static org.apache.struts2.portlet.PortletConstants.RESPONSE;
@@ -179,18 +177,13 @@ public class Jsr168Dispatcher extends GenericPortlet implements StrutsStatics {
private static final Logger LOG = LoggerFactory.getLogger(Jsr168Dispatcher.class);
protected String portletNamespace = null;
private ActionProxyFactory factory = null;
private Map<PortletMode,String> modeMap = new HashMap<PortletMode,String>(3);
private Map<PortletMode,ActionMapping> actionMap = new HashMap<PortletMode,ActionMapping>(3);
String portletNamespace = null;
private Map<PortletMode, String> modeMap = new HashMap<PortletMode, String>(3);
private Map<PortletMode, ActionMapping> actionMap = new HashMap<PortletMode, ActionMapping>(3);
private Dispatcher dispatcherUtils;
private ActionMapper actionMapper;
private Container container;
/**
@@ -198,9 +191,11 @@ public class Jsr168Dispatcher extends GenericPortlet implements StrutsStatics {
*/
public void init(PortletConfig cfg) throws PortletException {
super.init(cfg);
if (LOG.isDebugEnabled()) LOG.debug("Initializing portlet " + getPortletName());
if (LOG.isDebugEnabled()) {
LOG.debug("Initializing portlet " + getPortletName());
}
Map<String,String> params = new HashMap<String,String>();
Map<String, String> params = new HashMap<String, String>();
for (Enumeration e = cfg.getInitParameterNames(); e.hasMoreElements(); ) {
String name = (String) e.nextElement();
String value = cfg.getInitParameter(name);
@@ -215,7 +210,9 @@ public class Jsr168Dispatcher extends GenericPortlet implements StrutsStatics {
factory = dispatcherUtils.getConfigurationManager().getConfiguration().getContainer().getInstance(ActionProxyFactory.class);
}
portletNamespace = cfg.getInitParameter("portletNamespace");
if (LOG.isDebugEnabled()) LOG.debug("PortletNamespace: " + portletNamespace);
if (LOG.isDebugEnabled()) {
LOG.debug("PortletNamespace: " + portletNamespace);
}
parseModeConfig(actionMap, cfg, PortletMode.VIEW, "viewNamespace",
"defaultViewAction");
parseModeConfig(actionMap, cfg, PortletMode.EDIT, "editNamespace",
@@ -235,8 +232,7 @@ public class Jsr168Dispatcher extends GenericPortlet implements StrutsStatics {
if (StringUtils.isEmpty(portletNamespace)) {
portletNamespace = "";
}
LocalizedTextUtil
.addDefaultResourceBundle("org/apache/struts2/struts-messages");
LocalizedTextUtil.addDefaultResourceBundle("org/apache/struts2/struts-messages");
container = dispatcherUtils.getContainer();
//check for configuration reloading
@@ -259,24 +255,23 @@ public class Jsr168Dispatcher extends GenericPortlet implements StrutsStatics {
* execute for the mode is configured.
*/
void parseModeConfig(Map<PortletMode, ActionMapping> actionMap, PortletConfig portletConfig,
PortletMode portletMode, String nameSpaceParam,
String defaultActionParam) {
PortletMode portletMode, String nameSpaceParam,
String defaultActionParam) {
String namespace = portletConfig.getInitParameter(nameSpaceParam);
if (StringUtils.isEmpty(namespace)) {
namespace = "";
}
modeMap.put(portletMode, namespace);
String defaultAction = portletConfig
.getInitParameter(defaultActionParam);
String defaultAction = portletConfig.getInitParameter(defaultActionParam);
String method = null;
if (StringUtils.isEmpty(defaultAction)) {
defaultAction = DEFAULT_ACTION_NAME;
}
if(defaultAction.indexOf('!') >= 0) {
method = defaultAction.substring(defaultAction.indexOf('!') + 1);
defaultAction = defaultAction.substring(0, defaultAction.indexOf('!'));
if (defaultAction.indexOf('!') >= 0) {
method = defaultAction.substring(defaultAction.indexOf('!') + 1);
defaultAction = defaultAction.substring(0, defaultAction.indexOf('!'));
}
StringBuffer fullPath = new StringBuffer();
StringBuilder fullPath = new StringBuilder();
if (StringUtils.isNotEmpty(portletNamespace)) {
fullPath.append(portletNamespace);
}
@@ -289,8 +284,8 @@ public class Jsr168Dispatcher extends GenericPortlet implements StrutsStatics {
ActionMapping mapping = new ActionMapping();
mapping.setName(getActionName(fullPath.toString()));
mapping.setNamespace(getNamespace(fullPath.toString()));
if(method != null) {
mapping.setMethod(method);
if (method != null) {
mapping.setMethod(method);
}
actionMap.put(portletMode, mapping);
}
@@ -303,12 +298,14 @@ public class Jsr168Dispatcher extends GenericPortlet implements StrutsStatics {
*/
public void processAction(ActionRequest request, ActionResponse response)
throws PortletException, IOException {
if (LOG.isDebugEnabled()) LOG.debug("Entering processAction");
if (LOG.isDebugEnabled()) {
LOG.debug("Entering processAction in mode ", request.getPortletMode().toString());
}
resetActionContext();
try {
serviceAction(request, response, getRequestMap(request), getParameterMap(request),
getSessionMap(request), getApplicationMap(),
portletNamespace, ACTION_PHASE);
portletNamespace, PortletPhase.ACTION_PHASE);
if (LOG.isDebugEnabled()) LOG.debug("Leaving processAction");
} finally {
ActionContext.setContext(null);
@@ -324,19 +321,21 @@ public class Jsr168Dispatcher extends GenericPortlet implements StrutsStatics {
public void render(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
if (LOG.isDebugEnabled()) LOG.debug("Entering render");
if (LOG.isDebugEnabled()) {
LOG.debug("Entering render in mode ", request.getPortletMode().toString());
}
resetActionContext();
response.setTitle(getTitle(request));
if(!request.getWindowState().equals(WindowState.MINIMIZED)) {
try {
// Check to see if an event set the render to be included directly
serviceAction(request, response, getRequestMap(request), getParameterMap(request),
getSessionMap(request), getApplicationMap(),
portletNamespace, RENDER_PHASE);
if (LOG.isDebugEnabled()) LOG.debug("Leaving render");
} finally {
resetActionContext();
}
if (!request.getWindowState().equals(WindowState.MINIMIZED)) {
try {
// Check to see if an event set the render to be included directly
serviceAction(request, response, getRequestMap(request), getParameterMap(request),
getSessionMap(request), getApplicationMap(),
portletNamespace, PortletPhase.RENDER_PHASE);
if (LOG.isDebugEnabled()) LOG.debug("Leaving render");
} finally {
resetActionContext();
}
}
}
@@ -359,15 +358,17 @@ public class Jsr168Dispatcher extends GenericPortlet implements StrutsStatics {
* @param response the PortletResponse object.
* @param portletConfig the PortletConfig object.
* @param phase The portlet phase (render or action, see
* {@link PortletActionConstants})
* {@link PortletConstants})
* @return a HashMap representing the <tt>Action</tt> context.
*/
public HashMap<String, Object> createContextMap(Map<String, Object> requestMap, Map<String, String[]> parameterMap,
Map<String, Object> sessionMap, Map<String, Object> applicationMap, PortletRequest request,
PortletResponse response, HttpServletRequest servletRequest, HttpServletResponse servletResponse, ServletContext servletContext, PortletConfig portletConfig, Integer phase) throws IOException {
Map<String, Object> sessionMap, Map<String, Object> applicationMap,
PortletRequest request, PortletResponse response, HttpServletRequest servletRequest,
HttpServletResponse servletResponse, ServletContext servletContext,
PortletConfig portletConfig, PortletPhase phase) throws IOException {
// TODO Must put http request/response objects into map for use with
container.inject(servletRequest);
container.inject(servletRequest);
// ServletActionContext
HashMap<String, Object> extraContext = new HashMap<String, Object>();
@@ -381,7 +382,7 @@ public class Jsr168Dispatcher extends GenericPortlet implements StrutsStatics {
extraContext.put(ActionContext.APPLICATION, applicationMap);
String defaultLocale = dispatcherUtils.getContainer().getInstance(String.class, StrutsConstants.STRUTS_LOCALE);
Locale locale = null;
Locale locale;
if (defaultLocale != null) {
locale = LocalizedTextUtil.localeFromString(defaultLocale, request.getLocale());
} else {
@@ -401,8 +402,9 @@ public class Jsr168Dispatcher extends GenericPortlet implements StrutsStatics {
extraContext.put("application", applicationMap);
extraContext.put("parameters", parameterMap);
extraContext.put(MODE_NAMESPACE_MAP, modeMap);
extraContext.put(PortletConstants.DEFAULT_ACTION_MAP, actionMap);
extraContext.put(PHASE, phase);
extraContext.put(PortletConstants.PHASE, phase);
AttributeMap attrMap = new AttributeMap(extraContext);
extraContext.put("attr", attrMap);
@@ -423,50 +425,55 @@ public class Jsr168Dispatcher extends GenericPortlet implements StrutsStatics {
* @param sessionMap a Map of all session attributes.
* @param applicationMap a Map of all application attributes.
* @param portletNamespace the namespace or context of the action.
* @param phase The portlet phase (render or action, see
* {@link PortletActionConstants})
* @param phase The portlet phase (render or action, see {@link PortletConstants})
*/
public void serviceAction(PortletRequest request, PortletResponse response, Map<String, Object> requestMap, Map<String, String[]> parameterMap,
Map<String, Object> sessionMap, Map<String, Object> applicationMap, String portletNamespace,
Integer phase) throws PortletException {
Map<String, Object> sessionMap, Map<String, Object> applicationMap, String portletNamespace,
PortletPhase phase) throws PortletException {
if (LOG.isDebugEnabled()) LOG.debug("serviceAction");
Dispatcher.setInstance(dispatcherUtils);
String actionName = null;
String namespace = null;
String namespace;
try {
ServletContext servletContext = new PortletServletContext(getPortletContext());
HttpServletRequest servletRequest = new PortletServletRequest(request, getPortletContext());
HttpServletResponse servletResponse = createPortletServletResponse(response);
if(ACTION_PHASE.equals(phase)) {
servletRequest = dispatcherUtils.wrapRequest(servletRequest, servletContext);
if(servletRequest instanceof MultiPartRequestWrapper) {
// Multipart request. Request parameters are encoded in the multipart data,
// so we need to manually add them to the parameter map.
parameterMap.putAll(servletRequest.getParameterMap());
}
}
if (phase.isAction()) {
servletRequest = dispatcherUtils.wrapRequest(servletRequest, servletContext);
if (servletRequest instanceof MultiPartRequestWrapper) {
// Multipart request. Request parameters are encoded in the multipart data,
// so we need to manually add them to the parameter map.
parameterMap.putAll(servletRequest.getParameterMap());
}
}
container.inject(servletRequest);
ActionMapping mapping = getActionMapping(request, servletRequest);
actionName = mapping.getName();
namespace = mapping.getNamespace();
if ("renderDirect".equals(actionName)) {
namespace = request.getParameter(PortletConstants.RENDER_DIRECT_NAMESPACE);
} else {
namespace = mapping.getNamespace();
}
HashMap<String, Object> extraContext = createContextMap(requestMap, parameterMap,
sessionMap, applicationMap, request, response, servletRequest, servletResponse,
servletContext, getPortletConfig(), phase);
extraContext.put(PortletActionConstants.ACTION_MAPPING, mapping);
LOG.debug("Creating action proxy for name = " + actionName
+ ", namespace = " + namespace);
ActionProxy proxy = factory.createActionProxy(namespace,
actionName, mapping.getMethod(), extraContext);
request.setAttribute("struts.valueStack", proxy.getInvocation()
.getStack());
extraContext.put(PortletConstants.ACTION_MAPPING, mapping);
if (LOG.isDebugEnabled()) {
LOG.debug("Creating action proxy for name = " + actionName + ", namespace = " + namespace);
}
ActionProxy proxy = factory.createActionProxy(namespace, actionName, mapping.getMethod(), extraContext);
request.setAttribute("struts.valueStack", proxy.getInvocation().getStack());
proxy.execute();
} catch (ConfigurationException e) {
LOG.error("Could not find action", e);
if (LOG.isErrorEnabled()) {
LOG.error("Could not find action", e);
}
throw new PortletException("Could not find action " + actionName, e);
} catch (Exception e) {
LOG.error("Could not execute action", e);
throw new PortletException("Error executing action " + actionName,
e);
if (LOG.isErrorEnabled()) {
LOG.error("Could not execute action", e);
}
throw new PortletException("Error executing action " + actionName, e);
} finally {
Dispatcher.setInstance(null);
}
@@ -478,7 +485,7 @@ public class Jsr168Dispatcher extends GenericPortlet implements StrutsStatics {
*
* @return a Map of all application attributes.
*/
protected Map getApplicationMap() {
protected Map<String, Object> getApplicationMap() {
return new PortletApplicationMap(getPortletContext());
}
@@ -494,15 +501,14 @@ public class Jsr168Dispatcher extends GenericPortlet implements StrutsStatics {
* @return the namespace of the action.
*/
protected ActionMapping getActionMapping(final PortletRequest portletRequest, final HttpServletRequest servletRequest) {
ActionMapping mapping = null;
ActionMapping mapping;
String actionPath = getDefaultActionPath(portletRequest);
if (resetAction(portletRequest)) {
mapping = (ActionMapping) actionMap.get(portletRequest.getPortletMode());
mapping = actionMap.get(portletRequest.getPortletMode());
} else {
actionPath = servletRequest.getParameter(ACTION_PARAM);
if (StringUtils.isEmpty(actionPath)) {
mapping = (ActionMapping) actionMap.get(portletRequest
.getPortletMode());
mapping = actionMap.get(portletRequest.getPortletMode());
} else {
// Use the usual action mapper, but it is expecting an action extension
@@ -513,13 +519,12 @@ public class Jsr168Dispatcher extends GenericPortlet implements StrutsStatics {
}
if (mapping == null) {
throw new StrutsException("Unable to locate action mapping for request, probably due to " +
"an invalid action path: "+actionPath);
throw new StrutsException("Unable to locate action mapping for request, probably due to an invalid action path: " + actionPath);
}
return mapping;
}
protected String getDefaultActionPath( PortletRequest portletRequest ) {
protected String getDefaultActionPath(PortletRequest portletRequest) {
return null;
}
@@ -572,7 +577,7 @@ public class Jsr168Dispatcher extends GenericPortlet implements StrutsStatics {
* @param request the PortletRequest object.
* @return a Map of all request attributes.
*/
protected Map getRequestMap(PortletRequest request) {
protected Map<String, Object> getRequestMap(PortletRequest request) {
return new PortletRequestMap(request);
}
@@ -584,7 +589,7 @@ public class Jsr168Dispatcher extends GenericPortlet implements StrutsStatics {
* @param request the PortletRequest object.
* @return a Map of all session attributes.
*/
protected Map getSessionMap(PortletRequest request) {
protected Map<String, Object> getSessionMap(PortletRequest request) {
return new PortletSessionMap(request);
}
@@ -615,20 +620,21 @@ public class Jsr168Dispatcher extends GenericPortlet implements StrutsStatics {
reset = true;
}
}
if(reset) {
request.setAttribute(ACTION_RESET, Boolean.TRUE);
}
else {
request.setAttribute(ACTION_RESET, Boolean.FALSE);
if (reset) {
request.setAttribute(ACTION_RESET, Boolean.TRUE);
} else {
request.setAttribute(ACTION_RESET, Boolean.FALSE);
}
return reset;
}
public void destroy() {
if (dispatcherUtils == null) {
LOG.warn("something is seriously wrong, DispatcherUtil is not initialized (null) ");
} else {
if (dispatcherUtils != null) {
dispatcherUtils.cleanup();
} else {
if (LOG.isWarnEnabled()) {
LOG.warn("Something is seriously wrong, DispatcherUtil is not initialized (null) ");
}
}
}
@@ -3,55 +3,61 @@ package org.apache.struts2.portlet.dispatcher;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;
import org.apache.struts2.portlet.PortletPhase;
import org.apache.struts2.portlet.servlet.PortletServletResponse;
import org.apache.struts2.portlet.servlet.PortletServletResponseJSR286;
import javax.portlet.*;
import javax.portlet.EventRequest;
import javax.portlet.EventResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletRequest;
import javax.portlet.PortletResponse;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;
import java.io.IOException;
import static org.apache.struts2.portlet.PortletConstants.EVENT_PHASE;
import static org.apache.struts2.portlet.PortletConstants.SERVE_RESOURCE_PHASE;
public class Jsr286Dispatcher extends Jsr168Dispatcher {
private final static Logger LOG = LoggerFactory.getLogger(Jsr286Dispatcher.class);
private final static Logger LOG = LoggerFactory.getLogger(Jsr286Dispatcher.class);
@Override
public void processEvent( EventRequest request, EventResponse response)
throws PortletException, IOException {
if (LOG.isDebugEnabled()) LOG.debug("Entering processEvent");
resetActionContext();
try {
// We'll use the event name as the "action"
serviceAction(request, response,
getRequestMap(request), getParameterMap(request),
getSessionMap(request), getApplicationMap(),
portletNamespace, EVENT_PHASE);
if (LOG.isDebugEnabled()) LOG.debug("Leaving processEvent");
} finally {
ActionContext.setContext(null);
}
}
@Override
public void serveResource( ResourceRequest request, ResourceResponse response)
throws PortletException, IOException {
if (LOG.isDebugEnabled()) LOG.debug("Entering serveResource");
resetActionContext();
try {
serviceAction(request, response,
getRequestMap(request), getParameterMap(request),
getSessionMap(request), getApplicationMap(),
portletNamespace, SERVE_RESOURCE_PHASE);
}
finally {
ActionContext.setContext(null);
}
}
@Override
protected String getDefaultActionPath( PortletRequest portletRequest ) {
public void processEvent(EventRequest request, EventResponse response) throws PortletException, IOException {
if (LOG.isDebugEnabled()) {
LOG.debug("Entering processEvent");
}
resetActionContext();
try {
// We'll use the event name as the "action"
serviceAction(request, response,
getRequestMap(request), getParameterMap(request),
getSessionMap(request), getApplicationMap(),
portletNamespace, PortletPhase.EVENT_PHASE);
if (LOG.isDebugEnabled()) LOG.debug("Leaving processEvent");
} finally {
ActionContext.setContext(null);
}
}
@Override
public void serveResource(ResourceRequest request, ResourceResponse response)
throws PortletException, IOException {
if (LOG.isDebugEnabled()) {
LOG.debug("Entering serveResource");
}
resetActionContext();
try {
serviceAction(request, response,
getRequestMap(request), getParameterMap(request),
getSessionMap(request), getApplicationMap(),
portletNamespace, PortletPhase.SERVE_RESOURCE_PHASE);
} finally {
ActionContext.setContext(null);
}
}
@Override
protected String getDefaultActionPath(PortletRequest portletRequest) {
if (portletRequest instanceof EventRequest) {
return ((EventRequest) portletRequest).getEvent().getName();
}
@@ -59,7 +65,8 @@ public class Jsr286Dispatcher extends Jsr168Dispatcher {
}
@Override
protected PortletServletResponse createPortletServletResponse( PortletResponse response ) {
protected PortletServletResponse createPortletServletResponse(PortletResponse response) {
return new PortletServletResponseJSR286(response);
}
}
@@ -20,21 +20,20 @@
*/
package org.apache.struts2.portlet.interceptor;
import javax.portlet.PortletContext;
import javax.portlet.PortletRequest;
import javax.portlet.PortletResponse;
import org.apache.struts2.StrutsStatics;
import org.apache.struts2.interceptor.PrincipalAware;
import org.apache.struts2.portlet.PortletActionConstants;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;
import org.apache.struts2.StrutsStatics;
import org.apache.struts2.interceptor.PrincipalAware;
import org.apache.struts2.portlet.PortletConstants;
public class PortletAwareInterceptor extends AbstractInterceptor implements PortletActionConstants, StrutsStatics {
import javax.portlet.PortletContext;
import javax.portlet.PortletRequest;
import javax.portlet.PortletResponse;
public class PortletAwareInterceptor extends AbstractInterceptor implements StrutsStatics {
private static final long serialVersionUID = 2476509721059587700L;
@@ -52,24 +51,24 @@ public class PortletAwareInterceptor extends AbstractInterceptor implements Port
final ActionContext context = invocation.getInvocationContext();
if (action instanceof PortletRequestAware) {
PortletRequest request = (PortletRequest) context.get(REQUEST);
PortletRequest request = (PortletRequest) context.get(PortletConstants.REQUEST);
((PortletRequestAware) action).setPortletRequest(request);
}
if (action instanceof PortletResponseAware) {
PortletResponse response = (PortletResponse) context.get(RESPONSE);
PortletResponse response = (PortletResponse) context.get(PortletConstants.RESPONSE);
((PortletResponseAware) action).setPortletResponse(response);
}
if (action instanceof PrincipalAware) {
PortletRequest request = (PortletRequest) context.get(REQUEST);
PortletRequest request = (PortletRequest) context.get(PortletConstants.REQUEST);
((PrincipalAware) action).setPrincipalProxy(new PortletPrincipalProxy(request));
}
if (action instanceof PortletContextAware) {
PortletContext portletContext = (PortletContext) context.get(STRUTS_PORTLET_CONTEXT);
PortletContext portletContext = (PortletContext) context.get(StrutsStatics.STRUTS_PORTLET_CONTEXT);
((PortletContextAware) action).setPortletContext(portletContext);
}
if (action instanceof PortletPreferencesAware) {
PortletRequest request = (PortletRequest) context.get(REQUEST);
PortletRequest request = (PortletRequest) context.get(PortletConstants.REQUEST);
// Check if running in a servlet environment
if (request == null) {
@@ -20,28 +20,25 @@
*/
package org.apache.struts2.portlet.interceptor;
import static org.apache.struts2.portlet.PortletConstants.ACTION_PHASE;
import static org.apache.struts2.portlet.PortletConstants.EVENT_ACTION;
import static org.apache.struts2.portlet.PortletConstants.PHASE;
import static org.apache.struts2.portlet.PortletConstants.RENDER_PHASE;
import static org.apache.struts2.portlet.PortletConstants.REQUEST;
import static org.apache.struts2.portlet.PortletConstants.RESPONSE;
import static org.apache.struts2.portlet.PortletConstants.STACK_FROM_EVENT_PHASE;
import java.util.Map;
import javax.portlet.ActionResponse;
import javax.portlet.RenderRequest;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.portlet.dispatcher.DirectRenderFromEventAction;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.util.CompoundRoot;
import com.opensymphony.xwork2.util.ValueStack;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.portlet.PortletConstants;
import org.apache.struts2.portlet.PortletPhase;
import org.apache.struts2.portlet.dispatcher.DirectRenderFromEventAction;
import javax.portlet.ActionResponse;
import javax.portlet.RenderRequest;
import java.util.Map;
import static org.apache.struts2.portlet.PortletConstants.EVENT_ACTION;
import static org.apache.struts2.portlet.PortletConstants.REQUEST;
import static org.apache.struts2.portlet.PortletConstants.RESPONSE;
import static org.apache.struts2.portlet.PortletConstants.STACK_FROM_EVENT_PHASE;
public class PortletStateInterceptor extends AbstractInterceptor {
@@ -51,11 +48,11 @@ public class PortletStateInterceptor extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation invocation) throws Exception {
Integer phase = (Integer) invocation.getInvocationContext().get(PHASE);
if (RENDER_PHASE.equals(phase)) {
PortletPhase phase = (PortletPhase) invocation.getInvocationContext().get(PortletConstants.PHASE);
if (phase.isRender()) {
restoreStack(invocation);
return invocation.invoke();
} else if (ACTION_PHASE.equals(phase)) {
} else if (phase.isAction()) {
try {
return invocation.invoke();
} finally {
@@ -20,25 +20,21 @@
*/
package org.apache.struts2.portlet.result;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.portlet.PortletMode;
import org.apache.struts2.StrutsConstants;
import org.apache.struts2.dispatcher.ServletActionRedirectResult;
import org.apache.struts2.dispatcher.mapper.ActionMapper;
import org.apache.struts2.dispatcher.mapper.ActionMapping;
import org.apache.struts2.portlet.PortletActionConstants;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.config.entities.ResultConfig;
import com.opensymphony.xwork2.inject.Inject;
import org.apache.struts2.dispatcher.ServletActionRedirectResult;
import org.apache.struts2.dispatcher.mapper.ActionMapper;
import org.apache.struts2.dispatcher.mapper.ActionMapping;
import org.apache.struts2.portlet.PortletConstants;
import org.apache.struts2.views.util.UrlHelper;
import javax.portlet.PortletMode;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
*
* Portlet modification of the {@link ServletActionRedirectResult}.
@@ -178,7 +174,7 @@ public class PortletActionRedirectResult extends PortletResult {
*/
public void execute(ActionInvocation invocation) throws Exception {
actionName = conditionalParse(actionName, invocation);
String portletNamespace = (String)invocation.getInvocationContext().get(PortletActionConstants.PORTLET_NAMESPACE);
String portletNamespace = (String)invocation.getInvocationContext().get(PortletConstants.PORTLET_NAMESPACE);
if (portletMode != null) {
Map<PortletMode, String> namespaceMap = getNamespaceMap(invocation);
namespace = namespaceMap.get(portletMode);
@@ -216,7 +212,7 @@ public class PortletActionRedirectResult extends PortletResult {
@SuppressWarnings("unchecked")
private Map<PortletMode, String> getNamespaceMap(ActionInvocation invocation) {
return (Map<PortletMode, String>) invocation.getInvocationContext().get(PortletActionConstants.MODE_NAMESPACE_MAP);
return (Map<PortletMode, String>) invocation.getInvocationContext().get(PortletConstants.MODE_NAMESPACE_MAP);
}
/**
@@ -20,9 +20,16 @@
*/
package org.apache.struts2.portlet.result;
import java.io.IOException;
import java.util.Map;
import java.util.StringTokenizer;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.StrutsResultSupport;
import org.apache.struts2.portlet.PortletConstants;
import org.apache.struts2.portlet.PortletPhase;
import org.apache.struts2.portlet.context.PortletActionContext;
import javax.portlet.PortletContext;
import javax.portlet.PortletException;
@@ -35,29 +42,21 @@ import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.StrutsResultSupport;
import org.apache.struts2.portlet.PortletActionConstants;
import org.apache.struts2.portlet.context.PortletActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;
import java.io.IOException;
import java.util.Map;
import java.util.StringTokenizer;
/**
* Result type that includes a JSP to render.
*
*/
public class PortletResult extends StrutsResultSupport implements PortletActionConstants {
public class PortletResult extends StrutsResultSupport {
private static final long serialVersionUID = 434251393926178567L;
private boolean useDispatcherServlet;
private String dispatcherServletName = DEFAULT_DISPATCHER_SERVLET_NAME;
private String dispatcherServletName = PortletConstants.DEFAULT_DISPATCHER_SERVLET_NAME;
/**
* Logger instance.
@@ -99,9 +98,10 @@ public class PortletResult extends StrutsResultSupport implements PortletActionC
*/
public void doExecute(String finalLocation, ActionInvocation actionInvocation) throws Exception {
if (PortletActionContext.isRender() || PortletActionContext.isResource()) {
PortletPhase phase = PortletActionContext.getPhase();
if (phase.isRender() || phase.isResource()) {
executeMimeResult(finalLocation);
} else if (PortletActionContext.isAction() || PortletActionContext.isEvent()) {
} else if (phase.isAction() || phase.isEvent()) {
executeActionResult(finalLocation, actionInvocation);
} else {
executeRegularServletResult(finalLocation, actionInvocation);
@@ -110,9 +110,6 @@ public class PortletResult extends StrutsResultSupport implements PortletActionC
/**
* Executes the regular servlet result.
*
* @param finalLocation
* @param actionInvocation
*/
private void executeRegularServletResult(String finalLocation, ActionInvocation actionInvocation)
throws ServletException, IOException {
@@ -137,30 +134,36 @@ public class PortletResult extends StrutsResultSupport implements PortletActionC
* @param invocation
*/
protected void executeActionResult(String finalLocation, ActionInvocation invocation) throws Exception {
String phase = (PortletActionContext.isEvent()) ? "Event" : "Action";
if (LOG.isDebugEnabled()) LOG.debug("Executing result in "+phase+" phase");
Map sessionMap = invocation.getInvocationContext().getSession();
if (LOG.isDebugEnabled()) LOG.debug("Setting event render parameter: " + finalLocation);
if (finalLocation.indexOf('?') != -1) {
convertQueryParamsToRenderParams(finalLocation.substring(finalLocation.indexOf('?') + 1));
finalLocation = finalLocation.substring(0, finalLocation.indexOf('?'));
String location = finalLocation;
String namespace = invocation.getProxy().getNamespace();
if (LOG.isDebugEnabled()) {
String phase = (PortletActionContext.getPhase().isEvent()) ? "Event" : "Action";
LOG.debug("Executing result in "+phase+" phase");
LOG.debug("Setting event render parameter location : " + location);
LOG.debug("Setting event render parameter namespace: " + namespace);
}
Map<String, Object> sessionMap = invocation.getInvocationContext().getSession();
if (location.indexOf('?') != -1) {
convertQueryParamsToRenderParams(location.substring(location.indexOf('?') + 1));
location = location.substring(0, location.indexOf('?'));
}
PortletResponse response = PortletActionContext.getResponse();
if (finalLocation.endsWith(".action")) {
if (location.endsWith(".action")) {
// View is rendered with a view action...luckily...
finalLocation = finalLocation.substring(0, finalLocation.lastIndexOf("."));
resultHelper.setRenderParameter(response, ACTION_PARAM, finalLocation);
location = location.substring(0, location.lastIndexOf("."));
resultHelper.setRenderParameter(response, PortletConstants.ACTION_PARAM, location);
} else {
// View is rendered outside an action...uh oh...
resultHelper.setRenderParameter(response, ACTION_PARAM, "renderDirect");
sessionMap.put(RENDER_DIRECT_LOCATION, finalLocation);
resultHelper.setRenderParameter(response, PortletConstants.ACTION_PARAM, "renderDirect");
sessionMap.put(PortletConstants.RENDER_DIRECT_LOCATION, location);
}
resultHelper.setRenderParameter(response, PortletConstants.RENDER_DIRECT_NAMESPACE, namespace);
if(portletMode != null) {
resultHelper.setPortletMode(response, portletMode);
resultHelper.setRenderParameter(response, PortletActionConstants.MODE_PARAM, portletMode.toString());
resultHelper.setRenderParameter(response, PortletConstants.MODE_PARAM, portletMode.toString());
}
else {
resultHelper.setRenderParameter(response, PortletActionConstants.MODE_PARAM, PortletActionContext.getRequest().getPortletMode()
resultHelper.setRenderParameter(response, PortletConstants.MODE_PARAM, PortletActionContext.getRequest().getPortletMode()
.toString());
}
}
@@ -168,7 +171,6 @@ public class PortletResult extends StrutsResultSupport implements PortletActionC
/**
* Converts the query params to render params.
*
* @param response
* @param queryParams
*/
protected void convertQueryParamsToRenderParams(String queryParams) {
@@ -200,7 +202,7 @@ public class PortletResult extends StrutsResultSupport implements PortletActionC
if (LOG.isDebugEnabled()) LOG.debug("Location: " + finalLocation);
PortletRequestDispatcher dispatcher;
if (useDispatcherServlet) {
req.setAttribute(DISPATCH_TO, finalLocation);
req.setAttribute(PortletConstants.DISPATCH_TO, finalLocation);
dispatcher = ctx.getNamedDispatcher(dispatcherServletName);
if(dispatcher == null) {
throw new PortletException("Could not locate dispatcher servlet \"" + dispatcherServletName + "\". Please configure it in your web.xml file");
@@ -20,8 +20,23 @@
*/
package org.apache.struts2.portlet.result;
import java.io.OutputStreamWriter;
import java.io.Writer;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.util.ValueStack;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.StrutsConstants;
import org.apache.struts2.dispatcher.StrutsResultSupport;
import org.apache.struts2.portlet.PortletConstants;
import org.apache.struts2.portlet.PortletPhase;
import org.apache.struts2.portlet.context.PortletActionContext;
import org.apache.struts2.views.JspSupportServlet;
import org.apache.struts2.views.velocity.VelocityManager;
import org.apache.velocity.Template;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.context.Context;
import javax.portlet.ActionResponse;
import javax.servlet.Servlet;
@@ -30,24 +45,8 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspFactory;
import javax.servlet.jsp.PageContext;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.StrutsConstants;
import org.apache.struts2.dispatcher.StrutsResultSupport;
import org.apache.struts2.portlet.PortletActionConstants;
import org.apache.struts2.portlet.context.PortletActionContext;
import org.apache.struts2.views.JspSupportServlet;
import org.apache.struts2.views.velocity.VelocityManager;
import org.apache.velocity.Template;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.context.Context;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.util.ValueStack;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;
import java.io.OutputStreamWriter;
import java.io.Writer;
/**
* <!-- START SNIPPET: description -->
@@ -118,11 +117,11 @@ public class PortletVelocityResult extends StrutsResultSupport {
/* (non-Javadoc)
* @see org.apache.struts2.dispatcher.StrutsResultSupport#doExecute(java.lang.String, com.opensymphony.xwork2.ActionInvocation)
*/
public void doExecute(String location, ActionInvocation invocation)
throws Exception {
if (PortletActionContext.isAction()) {
public void doExecute(String location, ActionInvocation invocation) throws Exception {
PortletPhase phase = PortletActionContext.getPhase();
if (phase.isAction()) {
executeActionResult(location, invocation);
} else if (PortletActionContext.isRender()) {
} else if (phase.isRender()) {
executeRenderResult(location, invocation);
}
}
@@ -133,16 +132,12 @@ public class PortletVelocityResult extends StrutsResultSupport {
* @param location The location string
* @param invocation The action invocation
*/
private void executeActionResult(String location,
ActionInvocation invocation) {
private void executeActionResult(String location, ActionInvocation invocation) {
ActionResponse res = PortletActionContext.getActionResponse();
// View is rendered outside an action...uh oh...
res.setRenderParameter(PortletActionConstants.ACTION_PARAM,
"freemarkerDirect");
res.setRenderParameter(PortletConstants.ACTION_PARAM, "freemarkerDirect");
res.setRenderParameter("location", location);
res.setRenderParameter(PortletActionConstants.MODE_PARAM, PortletActionContext
.getRequest().getPortletMode().toString());
res.setRenderParameter(PortletConstants.MODE_PARAM, PortletActionContext.getRequest().getPortletMode().toString());
}
/**
@@ -155,27 +150,22 @@ public class PortletVelocityResult extends StrutsResultSupport {
* loading or executing the template or writing output to the
* servlet response stream.
*/
public void executeRenderResult(String finalLocation,
ActionInvocation invocation) throws Exception {
public void executeRenderResult(String finalLocation, ActionInvocation invocation) throws Exception {
ValueStack stack = ActionContext.getContext().getValueStack();
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
ServletContext servletContext = ServletActionContext
.getServletContext();
ServletContext servletContext = ServletActionContext.getServletContext();
Servlet servlet = JspSupportServlet.jspSupportServlet;
velocityManager.init(servletContext);
boolean usedJspFactory = false;
PageContext pageContext = (PageContext) ActionContext.getContext().get(
ServletActionContext.PAGE_CONTEXT);
PageContext pageContext = (PageContext) ActionContext.getContext().get(ServletActionContext.PAGE_CONTEXT);
if (pageContext == null && servlet != null) {
pageContext = jspFactory.getPageContext(servlet, request, response,
null, true, 8192, true);
ActionContext.getContext().put(ServletActionContext.PAGE_CONTEXT,
pageContext);
pageContext = jspFactory.getPageContext(servlet, request, response, null, true, 8192, true);
ActionContext.getContext().put(ServletActionContext.PAGE_CONTEXT, pageContext);
usedJspFactory = true;
}
@@ -187,14 +177,10 @@ public class PortletVelocityResult extends StrutsResultSupport {
contentType = contentType + ";charset=" + encoding;
}
response.setContentType(contentType);
Template t = getTemplate(stack,
velocityManager.getVelocityEngine(), invocation,
finalLocation, encoding);
Template t = getTemplate(stack, velocityManager.getVelocityEngine(), invocation, finalLocation, encoding);
Context context = createContext(velocityManager, stack, request,
response, finalLocation);
Writer writer = new OutputStreamWriter(response.getOutputStream(),
encoding);
Context context = createContext(velocityManager, stack, request, response, finalLocation);
Writer writer = new OutputStreamWriter(response.getOutputStream(), encoding);
t.merge(context, writer);
@@ -204,16 +190,13 @@ public class PortletVelocityResult extends StrutsResultSupport {
// deprecated, we'll oblige :)
writer.flush();
} catch (Exception e) {
LOG.error("Unable to render Velocity Template, '" + finalLocation
+ "'", e);
LOG.error("Unable to render Velocity Template, '" + finalLocation + "'", e);
throw e;
} finally {
if (usedJspFactory) {
jspFactory.releasePageContext(pageContext);
}
}
return;
}
/**
@@ -259,16 +242,12 @@ public class PortletVelocityResult extends StrutsResultSupport {
* @return the template to render
* @throws Exception when the requested template could not be found
*/
protected Template getTemplate(ValueStack stack,
VelocityEngine velocity, ActionInvocation invocation,
String location, String encoding) throws Exception {
protected Template getTemplate(ValueStack stack, VelocityEngine velocity, ActionInvocation invocation,
String location, String encoding) throws Exception {
if (!location.startsWith("/")) {
location = invocation.getProxy().getNamespace() + "/" + location;
}
Template template = velocity.getTemplate(location, encoding);
return template;
return velocity.getTemplate(location, encoding);
}
/**
@@ -280,8 +259,7 @@ public class PortletVelocityResult extends StrutsResultSupport {
* @param location the name of the template that is being used
* @return the a minted Velocity context.
*/
protected Context createContext(VelocityManager velocityManager,
ValueStack stack, HttpServletRequest request,
protected Context createContext(VelocityManager velocityManager, ValueStack stack, HttpServletRequest request,
HttpServletResponse response, String location) {
return velocityManager.createContext(stack, request, response);
}
@@ -88,11 +88,11 @@ public class PortletUrlHelper {
public String buildUrl(String action, String namespace, String method, Map<String, Object> params,
String scheme, String type, String portletMode, String windowState,
boolean includeContext, boolean encodeResult) {
StringBuffer resultingAction = new StringBuffer();
StringBuilder resultingAction = new StringBuilder();
PortletRequest request = PortletActionContext.getRequest();
LOG.debug("Creating url. Action = " + action + ", Namespace = "
+ namespace + ", Type = " + type);
namespace = prependNamespace(namespace, portletMode, !URLTYPE_NAME_RESOURCE.equalsIgnoreCase(type));
namespace = prependNamespace(namespace, portletMode, false);
if (StringUtils.isEmpty(portletMode)) {
portletMode = PortletActionContext.getRequest().getPortletMode().toString();
}
@@ -137,7 +137,7 @@ public class PortletUrlHelper {
String result = url.toString();
// TEMP BUG-WORKAROUND FOR DOUBLE ESCAPING OF AMPERSAND
if(result.indexOf("&amp;") >= 0) {
if(result.contains("&amp;")) {
result = result.replace("&amp;", "&");
}
return result;
@@ -179,14 +179,14 @@ public class PortletUrlHelper {
* @return prepended namespace.
*/
private String prependNamespace(String namespace, String portletMode, boolean prependModeNamespace) {
StringBuffer sb = new StringBuffer();
StringBuilder sb = new StringBuilder();
String modeNamespace;
if (prependModeNamespace) {
PortletMode mode = PortletActionContext.getRequest().getPortletMode();
if(StringUtils.isNotEmpty(portletMode)) {
mode = new PortletMode(portletMode);
}
modeNamespace = (String)PortletActionContext.getModeNamespaceMap().get(mode);
modeNamespace = PortletActionContext.getModeNamespaceMap().get(mode);
} else {
modeNamespace = null;
}
@@ -20,37 +20,34 @@
*/
package org.apache.struts2.views.freemarker;
import java.io.IOException;
import java.io.Writer;
import java.util.Locale;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.StrutsResultSupport;
import org.apache.struts2.portlet.PortletActionConstants;
import org.apache.struts2.portlet.context.PortletActionContext;
import org.apache.struts2.views.util.ResourceUtil;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.util.ValueStack;
import freemarker.template.Configuration;
import freemarker.template.ObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.StrutsResultSupport;
import org.apache.struts2.portlet.PortletConstants;
import org.apache.struts2.portlet.PortletPhase;
import org.apache.struts2.portlet.context.PortletActionContext;
import org.apache.struts2.views.util.ResourceUtil;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;
import java.util.Locale;
/**
*/
public class PortletFreemarkerResult extends StrutsResultSupport implements PortletActionConstants {
public class PortletFreemarkerResult extends StrutsResultSupport {
private static final long serialVersionUID = -5570612389289887543L;
@@ -104,39 +101,26 @@ public class PortletFreemarkerResult extends StrutsResultSupport implements Port
*/
public void doExecute(String location, ActionInvocation invocation)
throws IOException, TemplateException, PortletException {
if (PortletActionContext.isAction()) {
PortletPhase phase = PortletActionContext.getPhase();
if (phase.isAction()) {
executeActionResult(location, invocation);
} else if (PortletActionContext.isRender()) {
} else if (phase.isRender()) {
executeRenderResult(location, invocation);
}
}
/**
* @param location
* @param invocation
*/
private void executeActionResult(String location,
ActionInvocation invocation) {
private void executeActionResult(String location, ActionInvocation invocation) {
ActionResponse res = PortletActionContext.getActionResponse();
// View is rendered outside an action...uh oh...
invocation.getInvocationContext().getSession().put(RENDER_DIRECT_LOCATION, location);
res.setRenderParameter(PortletActionConstants.ACTION_PARAM, "freemarkerDirect");
invocation.getInvocationContext().getSession().put(PortletConstants.RENDER_DIRECT_LOCATION, location);
res.setRenderParameter(PortletConstants.ACTION_PARAM, "freemarkerDirect");
res.setRenderParameter("location", location);
res.setRenderParameter(PortletActionConstants.MODE_PARAM, PortletActionContext
.getRequest().getPortletMode().toString());
res.setRenderParameter(PortletConstants.MODE_PARAM, PortletActionContext.getRequest().getPortletMode().toString());
}
/**
* @param location
* @param invocation
* @throws TemplateException
* @throws IOException
* @throws TemplateModelException
*/
private void executeRenderResult(String location,
ActionInvocation invocation) throws TemplateException, IOException,
TemplateModelException, PortletException {
private void executeRenderResult(String location, ActionInvocation invocation)
throws TemplateException, IOException, PortletException {
this.location = location;
this.invocation = invocation;
this.configuration = getConfiguration();
@@ -173,8 +157,7 @@ public class PortletFreemarkerResult extends StrutsResultSupport implements Port
* from the ConfigurationManager instance. </b>
*/
protected Configuration getConfiguration() throws TemplateException {
return freemarkerManager.getConfiguration(
ServletActionContext.getServletContext());
return freemarkerManager.getConfiguration(ServletActionContext.getServletContext());
}
/**
@@ -218,11 +201,8 @@ public class PortletFreemarkerResult extends StrutsResultSupport implements Port
.getServletContext();
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
ValueStack stack = ServletActionContext.getContext()
.getValueStack();
return freemarkerManager.buildTemplateModel(stack,
invocation.getAction(), servletContext, request, response,
wrapper);
ValueStack stack = ServletActionContext.getContext().getValueStack();
return freemarkerManager.buildTemplateModel(stack, invocation.getAction(), servletContext, request, response, wrapper);
}
/**
@@ -239,8 +219,7 @@ public class PortletFreemarkerResult extends StrutsResultSupport implements Port
* the default implementation of postTemplateProcess applies the contentType
* parameter
*/
protected void postTemplateProcess(Template template, TemplateModel data)
throws IOException {
protected void postTemplateProcess(Template template, TemplateModel data) throws IOException {
}
/**
@@ -253,8 +232,7 @@ public class PortletFreemarkerResult extends StrutsResultSupport implements Port
* @return true to process the template, false to suppress template
* processing.
*/
protected boolean preTemplateProcess(Template template, TemplateModel model)
throws IOException {
protected boolean preTemplateProcess(Template template, TemplateModel model) throws IOException {
Object attrContentType = template.getCustomAttribute("content_type");
if (attrContentType != null) {
@@ -11,6 +11,8 @@ import org.springframework.mock.web.portlet.MockPortletContext;
import org.springframework.mock.web.portlet.MockPortletRequest;
import javax.portlet.PortletContext;
import javax.portlet.PortletMode;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Collections;
@@ -46,5 +48,14 @@ public class PortletUrlRendererTest extends StrutsTestCase {
// then
assertTrue("/portlettest".equals(component.getNamespace()));
}
public void testIsPortelModeChanged() {
PortletUrlRenderer renderer = new PortletUrlRenderer();
PortletMode mode = new PortletMode("test");
UrlProvider provider = new ComponentUrlProvider(null, null);
provider.setPortletMode("test2");
assertTrue(renderer.isPortletModeChange(provider, mode));
}
}
@@ -20,23 +20,36 @@
*/
package org.apache.struts2.portlet.context;
import static org.apache.struts2.portlet.PortletConstants.*;
import static org.apache.struts2.portlet.context.PortletActionContext.*;
import java.util.HashMap;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import org.apache.struts2.dispatcher.mapper.ActionMapping;
import org.apache.struts2.portlet.PortletPhase;
import org.jmock.Mock;
import org.jmock.MockObjectTestCase;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import java.util.HashMap;
import java.util.Map;
import org.apache.struts2.dispatcher.mapper.ActionMapping;
import org.jmock.Mock;
import org.jmock.MockObjectTestCase;
import com.opensymphony.xwork2.ActionContext;
import static org.apache.struts2.portlet.PortletConstants.DEFAULT_ACTION_FOR_MODE;
import static org.apache.struts2.portlet.PortletConstants.PHASE;
import static org.apache.struts2.portlet.PortletConstants.PORTLET_CONFIG;
import static org.apache.struts2.portlet.PortletConstants.PORTLET_NAMESPACE;
import static org.apache.struts2.portlet.PortletConstants.REQUEST;
import static org.apache.struts2.portlet.PortletConstants.RESPONSE;
import static org.apache.struts2.portlet.context.PortletActionContext.getActionRequest;
import static org.apache.struts2.portlet.context.PortletActionContext.getActionResponse;
import static org.apache.struts2.portlet.context.PortletActionContext.getDefaultActionForMode;
import static org.apache.struts2.portlet.context.PortletActionContext.getPhase;
import static org.apache.struts2.portlet.context.PortletActionContext.getPortletConfig;
import static org.apache.struts2.portlet.context.PortletActionContext.getPortletNamespace;
import static org.apache.struts2.portlet.context.PortletActionContext.getRenderRequest;
import static org.apache.struts2.portlet.context.PortletActionContext.getRenderResponse;
import static org.apache.struts2.portlet.context.PortletActionContext.getRequest;
import static org.apache.struts2.portlet.context.PortletActionContext.getResponse;
/**
*/
@@ -77,33 +90,39 @@ public class PortletActionContextTest extends MockObjectTestCase {
}
public void testGetPhase() {
context.put(PHASE, RENDER_PHASE);
context.put(PHASE, PortletPhase.RENDER_PHASE);
assertEquals(RENDER_PHASE, getPhase());
assertEquals(PortletPhase.RENDER_PHASE, getPhase());
}
public void testIsRender() {
context.put(PHASE, RENDER_PHASE);
context.put(PHASE, PortletPhase.RENDER_PHASE);
assertTrue(isRender());
assertFalse(isAction());
assertFalse(isEvent());
PortletPhase phase = getPhase();
assertTrue(phase.isRender());
assertFalse(phase.isAction());
assertFalse(phase.isEvent());
}
public void testIsAction() {
context.put(PHASE, ACTION_PHASE);
context.put(PHASE, PortletPhase.ACTION_PHASE);
assertTrue(isAction());
assertFalse(isRender());
assertFalse(isEvent());
PortletPhase phase = getPhase();
assertTrue(phase.isAction());
assertFalse(phase.isRender());
assertFalse(phase.isEvent());
}
public void testIsEvent() {
context.put(PHASE, EVENT_PHASE);
assertTrue(isEvent());
assertFalse(isAction());
assertFalse(isRender());
context.put(PHASE, PortletPhase.EVENT_PHASE);
PortletPhase phase = getPhase();
assertTrue(phase.isEvent());
assertFalse(phase.isAction());
assertFalse(phase.isRender());
}
public void testGetPortletConfig() {
@@ -114,7 +133,7 @@ public class PortletActionContextTest extends MockObjectTestCase {
public void testGetRenderRequestAndResponse() {
context.put(REQUEST, renderRequest);
context.put(RESPONSE, renderResponse);
context.put(PHASE, RENDER_PHASE);
context.put(PHASE, PortletPhase.RENDER_PHASE);
assertSame(renderRequest, getRenderRequest());
assertSame(renderResponse, getRenderResponse());
assertSame(renderRequest, getRequest());
@@ -124,7 +143,7 @@ public class PortletActionContextTest extends MockObjectTestCase {
public void testGetRenderRequestAndResponseInEventPhase() {
context.put(REQUEST, renderRequest);
context.put(RESPONSE, renderResponse);
context.put(PHASE, ACTION_PHASE);
context.put(PHASE, PortletPhase.ACTION_PHASE);
try {
getRenderRequest();
fail("Should throw IllegalStateException!");
@@ -144,7 +163,7 @@ public class PortletActionContextTest extends MockObjectTestCase {
public void testGetActionRequestAndResponse() {
context.put(REQUEST, actionRequest);
context.put(RESPONSE, actionResponse);
context.put(PHASE, ACTION_PHASE);
context.put(PHASE, PortletPhase.ACTION_PHASE);
assertSame(actionRequest, getActionRequest());
assertSame(actionResponse, getActionResponse());
assertSame(actionRequest, getRequest());
@@ -154,7 +173,7 @@ public class PortletActionContextTest extends MockObjectTestCase {
public void testGetActionRequestAndResponseInRenderPhase() {
context.put(REQUEST, actionRequest);
context.put(RESPONSE, actionResponse);
context.put(PHASE, RENDER_PHASE);
context.put(PHASE, PortletPhase.RENDER_PHASE);
try {
getActionRequest();
fail("Should throw IllegalStateException!");
@@ -20,20 +20,17 @@
*/
package org.apache.struts2.portlet.interceptor;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import junit.framework.TestCase;
import org.apache.struts2.portlet.PortletConstants;
import org.easymock.EasyMock;
import javax.portlet.PortletRequest;
import java.util.HashMap;
import java.util.Map;
import javax.portlet.PortletRequest;
import junit.framework.TestCase;
import org.apache.struts2.portlet.PortletActionConstants;
import org.easymock.EasyMock;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
public class PortletAwareInterceptorTest extends TestCase implements PortletActionConstants {
public class PortletAwareInterceptorTest extends TestCase {
private PortletAwareInterceptor interceptor;
@@ -49,7 +46,7 @@ public class PortletAwareInterceptorTest extends TestCase implements PortletActi
public void testPortletRequestIsSet() throws Exception {
PortletRequest request = EasyMock.createMock(PortletRequest.class);
Map<String, Object> ctx = new HashMap<String, Object>();
ctx.put(REQUEST, request);
ctx.put(PortletConstants.REQUEST, request);
PortletRequestAware action = EasyMock.createMock(PortletRequestAware.class);
action.setPortletRequest(request);
@@ -20,23 +20,26 @@
*/
package org.apache.struts2.portlet.interceptor;
import java.util.HashMap;
import java.util.Map;
import javax.portlet.ActionResponse;
import javax.portlet.RenderRequest;
import org.apache.struts2.StrutsTestCase;
import org.apache.struts2.dispatcher.DefaultActionSupport;
import org.apache.struts2.portlet.dispatcher.DirectRenderFromEventAction;
import org.easymock.EasyMock;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.util.ValueStack;
import com.opensymphony.xwork2.util.ValueStackFactory;
import org.apache.struts2.StrutsTestCase;
import org.apache.struts2.dispatcher.DefaultActionSupport;
import org.apache.struts2.portlet.PortletPhase;
import org.apache.struts2.portlet.dispatcher.DirectRenderFromEventAction;
import org.easymock.EasyMock;
import static org.apache.struts2.portlet.PortletConstants.*;
import javax.portlet.ActionResponse;
import javax.portlet.RenderRequest;
import java.util.HashMap;
import java.util.Map;
import static org.apache.struts2.portlet.PortletConstants.EVENT_ACTION;
import static org.apache.struts2.portlet.PortletConstants.PHASE;
import static org.apache.struts2.portlet.PortletConstants.REQUEST;
import static org.apache.struts2.portlet.PortletConstants.RESPONSE;
import static org.apache.struts2.portlet.PortletConstants.STACK_FROM_EVENT_PHASE;
public class PortletStateInterceptorTest extends StrutsTestCase {
@@ -52,7 +55,7 @@ public class PortletStateInterceptorTest extends StrutsTestCase {
ActionInvocation invocation = EasyMock.createNiceMock(ActionInvocation.class);
Map<String, Object> ctxMap = new HashMap<String, Object>();
ctxMap.put(PHASE, ACTION_PHASE);
ctxMap.put(PHASE, PortletPhase.ACTION_PHASE);
ctxMap.put(RESPONSE, actionResponse);
Map<String, Object> session = new HashMap<String, Object>();
@@ -92,7 +95,7 @@ public class PortletStateInterceptorTest extends StrutsTestCase {
session.put(STACK_FROM_EVENT_PHASE, eventPhaseStack);
ctxMap.put(PHASE, RENDER_PHASE);
ctxMap.put(PHASE, PortletPhase.RENDER_PHASE);
ctxMap.put(REQUEST, renderRequest);
ActionContext ctx = new ActionContext(ctxMap);
@@ -133,7 +136,7 @@ public class PortletStateInterceptorTest extends StrutsTestCase {
session.put(STACK_FROM_EVENT_PHASE, eventPhaseStack);
ctxMap.put(PHASE, RENDER_PHASE);
ctxMap.put(PHASE, PortletPhase.RENDER_PHASE);
ctxMap.put(REQUEST, renderRequest);
ActionContext ctx = new ActionContext(ctxMap);
@@ -20,11 +20,16 @@
*/
package org.apache.struts2.portlet.result;
import static org.apache.struts2.portlet.PortletConstants.*;
import static com.opensymphony.xwork2.ActionContext.*;
import java.util.HashMap;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionProxy;
import junit.textui.TestRunner;
import org.apache.struts2.StrutsStatics;
import org.apache.struts2.portlet.PortletConstants;
import org.apache.struts2.portlet.PortletPhase;
import org.jmock.Mock;
import org.jmock.cglib.MockObjectTestCase;
import org.jmock.core.Constraint;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
@@ -33,16 +38,17 @@ import javax.portlet.PortletMode;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import java.util.HashMap;
import java.util.Map;
import junit.textui.TestRunner;
import org.apache.struts2.StrutsStatics;
import org.jmock.Mock;
import org.jmock.cglib.MockObjectTestCase;
import org.jmock.core.Constraint;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import static com.opensymphony.xwork2.ActionContext.PARAMETERS;
import static com.opensymphony.xwork2.ActionContext.SESSION;
import static org.apache.struts2.portlet.PortletConstants.ACTION_PARAM;
import static org.apache.struts2.portlet.PortletConstants.MODE_PARAM;
import static org.apache.struts2.portlet.PortletConstants.PHASE;
import static org.apache.struts2.portlet.PortletConstants.RENDER_DIRECT_LOCATION;
import static org.apache.struts2.portlet.PortletConstants.REQUEST;
import static org.apache.struts2.portlet.PortletConstants.RESPONSE;
/**
* PortletResultTest. Insert description.
@@ -51,13 +57,16 @@ import com.opensymphony.xwork2.ActionInvocation;
public class PortletResultTest extends MockObjectTestCase implements StrutsStatics {
Mock mockInvocation = null;
Mock mockConfig = null;
Mock mockCtx = null;
Mock mockProxy = null;
ActionProxy proxy = null;
ActionInvocation invocation = null;
public void setUp() throws Exception {
super.setUp();
mockInvocation = mock(ActionInvocation.class);
mockCtx = mock(PortletContext.class);
mockProxy = mock(ActionProxy.class);
Map<String, String[]> paramMap = new HashMap<String, String[]>();
Map<String, Object> sessionMap = new HashMap<String, Object>();
@@ -68,8 +77,11 @@ public class PortletResultTest extends MockObjectTestCase implements StrutsStati
context.put(STRUTS_PORTLET_CONTEXT, mockCtx.proxy());
ActionContext.setContext(new ActionContext(context));
mockProxy.stubs().method("getNamespace").will(returnValue("/test"));
proxy = (ActionProxy) mockProxy.proxy();
mockInvocation.stubs().method("getInvocationContext").will(returnValue(ActionContext.getContext()));
mockInvocation.stubs().method("getProxy").will(returnValue(proxy));
invocation = (ActionInvocation) mockInvocation.proxy();
}
@@ -96,7 +108,7 @@ public class PortletResultTest extends MockObjectTestCase implements StrutsStati
ctxMap.put(RESPONSE, res);
ctxMap.put(REQUEST, req);
ctxMap.put(SERVLET_CONTEXT, ctx);
ctxMap.put(PHASE, RENDER_PHASE);
ctxMap.put(PHASE, PortletPhase.RENDER_PHASE);
PortletResult result = new PortletResult();
try {
@@ -118,17 +130,20 @@ public class PortletResultTest extends MockObjectTestCase implements StrutsStati
mockResponse.expects(once()).method("setRenderParameter").with(params);
params = new Constraint[]{eq(MODE_PARAM), eq(PortletMode.VIEW.toString())};
mockResponse.expects(once()).method("setRenderParameter").with(params);
params = new Constraint[]{eq(PortletConstants.RENDER_DIRECT_NAMESPACE), eq("/test")};
mockResponse.expects(once()).method("setRenderParameter").with(params);
mockRequest.stubs().method("getPortletMode").will(returnValue(PortletMode.VIEW));
mockCtx.expects(atLeastOnce()).method("getMajorVersion").will(returnValue(1));
ActionContext ctx = ActionContext.getContext();
ctx.put(REQUEST, mockRequest.proxy());
ctx.put(RESPONSE, mockResponse.proxy());
ctx.put(PHASE, ACTION_PHASE);
ctx.put(PHASE, PortletPhase.ACTION_PHASE);
PortletResult result = new PortletResult();
try {
result.doExecute("testView.action", (ActionInvocation)mockInvocation.proxy());
result.doExecute("testView.action", invocation);
}
catch(Exception e) {
e.printStackTrace();
@@ -145,6 +160,9 @@ public class PortletResultTest extends MockObjectTestCase implements StrutsStati
mockResponse.expects(once()).method("setRenderParameter").with(params);
params = new Constraint[]{eq(MODE_PARAM), eq(PortletMode.VIEW.toString())};
mockResponse.expects(once()).method("setRenderParameter").with(params);
params = new Constraint[]{eq(PortletConstants.RENDER_DIRECT_NAMESPACE), eq("/test")};
mockResponse.expects(once()).method("setRenderParameter").with(params);
mockRequest.stubs().method("getPortletMode").will(returnValue(PortletMode.VIEW));
mockCtx.expects(atLeastOnce()).method("getMajorVersion").will(returnValue(1));
@@ -154,7 +172,7 @@ public class PortletResultTest extends MockObjectTestCase implements StrutsStati
ctx.put(REQUEST, mockRequest.proxy());
ctx.put(RESPONSE, mockResponse.proxy());
ctx.put(PHASE, ACTION_PHASE);
ctx.put(PHASE, PortletPhase.ACTION_PHASE);
ctx.put(ActionContext.SESSION, session);
PortletResult result = new PortletResult();
@@ -180,6 +198,9 @@ public class PortletResultTest extends MockObjectTestCase implements StrutsStati
mockResponse.expects(once()).method("setRenderParameter").with(params);
params = new Constraint[]{eq(MODE_PARAM), eq(PortletMode.VIEW.toString())};
mockResponse.expects(once()).method("setRenderParameter").with(params);
params = new Constraint[]{eq(PortletConstants.RENDER_DIRECT_NAMESPACE), eq("/test")};
mockResponse.expects(once()).method("setRenderParameter").with(params);
mockRequest.stubs().method("getPortletMode").will(returnValue(PortletMode.VIEW));
mockCtx.expects(atLeastOnce()).method("getMajorVersion").will(returnValue(1));
@@ -187,7 +208,7 @@ public class PortletResultTest extends MockObjectTestCase implements StrutsStati
ctx.put(REQUEST, mockRequest.proxy());
ctx.put(RESPONSE, mockResponse.proxy());
ctx.put(PHASE, ACTION_PHASE);
ctx.put(PHASE, PortletPhase.ACTION_PHASE);
PortletResult result = new PortletResult();
try {
@@ -220,7 +241,7 @@ public class PortletResultTest extends MockObjectTestCase implements StrutsStati
ctxMap.put(RESPONSE, res);
ctxMap.put(REQUEST, req);
ctxMap.put(SERVLET_CONTEXT, ctx);
ctxMap.put(PHASE, RENDER_PHASE);
ctxMap.put(PHASE, PortletPhase.RENDER_PHASE);
mockResponse.expects(atLeastOnce()).method("setTitle").with(eq("testTitle"));
mockResponse.expects(atLeastOnce()).method("setContentType").with(eq("testContentType"));
@@ -20,12 +20,10 @@
*/
package org.apache.struts2.portlet.util;
import static org.apache.struts2.portlet.PortletConstants.*;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import junit.framework.TestCase;
import org.apache.struts2.portlet.PortletPhase;
import org.easymock.EasyMock;
import javax.portlet.PortletMode;
import javax.portlet.PortletModeException;
@@ -35,12 +33,17 @@ import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.WindowState;
import javax.portlet.WindowStateException;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import junit.framework.TestCase;
import org.easymock.EasyMock;
import com.opensymphony.xwork2.ActionContext;
import static org.apache.struts2.portlet.PortletConstants.ACTION_PARAM;
import static org.apache.struts2.portlet.PortletConstants.MODE_NAMESPACE_MAP;
import static org.apache.struts2.portlet.PortletConstants.MODE_PARAM;
import static org.apache.struts2.portlet.PortletConstants.PHASE;
import static org.apache.struts2.portlet.PortletConstants.REQUEST;
import static org.apache.struts2.portlet.PortletConstants.RESPONSE;
/**
*/
@@ -70,7 +73,7 @@ public class PortletUrlHelperTest extends TestCase {
Map<String, Object> context = new HashMap<String, Object>();
context.put(REQUEST, renderRequest);
context.put(RESPONSE, renderResponse);
context.put(PHASE, RENDER_PHASE);
context.put(PHASE, PortletPhase.RENDER_PHASE);
context.put(MODE_NAMESPACE_MAP, modeNamespaceMap);
ActionContext.setContext(new ActionContext(context));
@@ -84,7 +87,7 @@ public class PortletUrlHelperTest extends TestCase {
EasyMock.replay(renderResponse);
(new PortletUrlHelper()).buildUrl("testAction", null, null,
new HashMap(), null, null, null);
new HashMap<String, Object>(), null, null, null);
assertEquals(PortletMode.VIEW, url.getPortletMode());
assertEquals(WindowState.NORMAL, url.getWindowState());
assertEquals("testAction", url.getParameterMap().get(ACTION_PARAM)[0]);
@@ -98,7 +101,7 @@ public class PortletUrlHelperTest extends TestCase {
EasyMock.replay(renderResponse);
(new PortletUrlHelper()).buildUrl("testAction", null, null,
new HashMap(), null, "edit", null);
new HashMap<String, Object>(), null, "edit", null);
assertEquals(PortletMode.EDIT, url.getPortletMode());
assertEquals(WindowState.NORMAL, url.getWindowState());
@@ -113,7 +116,7 @@ public class PortletUrlHelperTest extends TestCase {
EasyMock.replay(renderResponse);
(new PortletUrlHelper()).buildUrl("testAction", null, null,
new HashMap(), null, null, "maximized");
new HashMap<String, Object>(), null, null, "maximized");
assertEquals(PortletMode.VIEW, url.getPortletMode());
assertEquals(WindowState.MAXIMIZED, url.getWindowState());
@@ -128,7 +131,7 @@ public class PortletUrlHelperTest extends TestCase {
EasyMock.replay(renderRequest);
(new PortletUrlHelper()).buildUrl("testAction", null, null,
new HashMap(), "action", null, null);
new HashMap<String, Object>(), "action", null, null);
assertEquals(PortletMode.VIEW, url.getPortletMode());
assertEquals(WindowState.NORMAL, url.getWindowState());
@@ -28,7 +28,9 @@ import com.opensymphony.xwork2.util.ValueStack;
import com.opensymphony.xwork2.util.ValueStackFactory;
import junit.textui.TestRunner;
import org.apache.struts2.dispatcher.Dispatcher;
import org.apache.struts2.portlet.PortletActionConstants;
import org.apache.struts2.dispatcher.mapper.ActionMapping;
import org.apache.struts2.portlet.PortletConstants;
import org.apache.struts2.portlet.PortletPhase;
import org.apache.struts2.portlet.util.PortletUrlHelper;
import org.jmock.Mock;
import org.jmock.cglib.MockObjectTestCase;
@@ -36,8 +38,6 @@ import org.jmock.core.Constraint;
import javax.portlet.PortletContext;
import javax.portlet.PortletMode;
import javax.portlet.PortletRequest;
import javax.portlet.PortletResponse;
import javax.portlet.PortletURL;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
@@ -77,6 +77,10 @@ public class PortletUrlTagTest extends MockObjectTestCase {
Mock mockCtx = null;
ValueStack stack = null;
Mock mockActionProxy = null;
Mock mockActionInvocation = null;
public static void main(String[] args) {
TestRunner.run(PortletUrlTagTest.class);
@@ -92,10 +96,11 @@ public class PortletUrlTagTest extends MockObjectTestCase {
stack = du.getContainer().getInstance(ValueStackFactory.class).createValueStack();
stack.getContext().put(ActionContext.CONTAINER, du.getContainer());
ActionContext.setContext(new ActionContext(stack.getContext()));
ActionContext context = new ActionContext(stack.getContext());
ActionContext.setContext(context);
mockActionInvocation = mock(ActionInvocation.class);
mockActionProxy = mock(ActionProxy.class);
mockHttpReq = mock(HttpServletRequest.class);
mockHttpRes = mock(HttpServletResponse.class);
mockPortletReq = mock(RenderRequest.class);
@@ -104,11 +109,14 @@ public class PortletUrlTagTest extends MockObjectTestCase {
mockPortletUrl = mock(PortletURL.class);
mockJspWriter = new MockJspWriter();
mockCtx = mock(PortletContext.class);
mockActionProxy.stubs().method("getNamespace").will(returnValue("/view"));
mockActionInvocation.stubs().method("getProxy").will(returnValue(
mockActionProxy.proxy()));
mockPageCtx.stubs().method("getRequest").will(
returnValue((HttpServletRequest) mockHttpReq.proxy()));
returnValue(mockHttpReq.proxy()));
mockPageCtx.stubs().method("getResponse").will(
returnValue((HttpServletResponse) mockHttpRes.proxy()));
returnValue(mockHttpRes.proxy()));
mockPageCtx.stubs().method("getOut").will(returnValue(mockJspWriter));
mockHttpReq.stubs().method("getScheme").will(returnValue("http"));
@@ -116,10 +124,10 @@ public class PortletUrlTagTest extends MockObjectTestCase {
eq("struts.valueStack")).will(returnValue(stack));
mockHttpReq.stubs().method("getAttribute").with(
eq("javax.portlet.response")).will(
returnValue((PortletResponse) mockPortletRes.proxy()));
returnValue(mockPortletRes.proxy()));
mockHttpReq.stubs().method("getAttribute").with(
eq("javax.portlet.request")).will(
returnValue((PortletRequest) mockPortletReq.proxy()));
returnValue(mockPortletReq.proxy()));
mockHttpReq.stubs().method("getAttribute").with(
eq("javax.servlet.include.servlet_path")).will(
returnValue("/servletPath"));
@@ -136,17 +144,26 @@ public class PortletUrlTagTest extends MockObjectTestCase {
modeMap.put(PortletMode.VIEW, "/view");
modeMap.put(PortletMode.HELP, "/help");
modeMap.put(PortletMode.EDIT, "/edit");
Map<PortletMode,ActionMapping> actionMap = new HashMap<PortletMode,ActionMapping>();
actionMap.put(PortletMode.VIEW, new ActionMapping("defaultView", "/view", "execute", new HashMap<String,Object>()));
actionMap.put(PortletMode.HELP, new ActionMapping("defaultHelp", "/help", "execute", new HashMap<String,Object>()));
actionMap.put(PortletMode.EDIT, new ActionMapping("defaultEdit", "/edit", "execute", new HashMap<String,Object>()));
Map sessionMap = new HashMap();
Map contextMap = new HashMap();
contextMap.put(ActionContext.SESSION, sessionMap);
contextMap.put(PortletActionConstants.REQUEST, mockPortletReq.proxy());
contextMap.put(PortletActionConstants.RESPONSE, mockPortletRes.proxy());
contextMap.put(PortletActionConstants.PHASE, PortletActionConstants.RENDER_PHASE);
contextMap.put(PortletActionConstants.MODE_NAMESPACE_MAP, modeMap);
contextMap.put(PortletConstants.REQUEST, mockPortletReq.proxy());
contextMap.put(PortletConstants.RESPONSE, mockPortletRes.proxy());
contextMap.put(PortletConstants.PHASE, PortletPhase.RENDER_PHASE);
contextMap.put(PortletConstants.MODE_NAMESPACE_MAP, modeMap);
contextMap.put(PortletConstants.DEFAULT_ACTION_MAP, actionMap);
contextMap.put(STRUTS_PORTLET_CONTEXT, mockCtx.proxy());
ActionContext ctx = new ActionContext(contextMap);
ctx.setValueStack(stack);
ActionInvocation ai = (ActionInvocation)mockActionInvocation.proxy();
stack.getContext().put(ActionContext.ACTION_INVOCATION, ai);
ActionContext.setContext(ctx);
}
public void testEnsureParamsAreStringArrays() {
@@ -166,12 +183,12 @@ public class PortletUrlTagTest extends MockObjectTestCase {
mockHttpReq.stubs().method("getQueryString").will(returnValue(""));
mockPortletRes.expects(once()).method("createRenderURL").will(
returnValue((PortletURL) mockPortletUrl.proxy()));
returnValue(mockPortletUrl.proxy()));
mockCtx.expects(atLeastOnce()).method("getMajorVersion").will(returnValue(1));
Map paramMap = new HashMap();
paramMap.put(PortletActionConstants.ACTION_PARAM, new String[]{"/view/testAction"});
paramMap.put(PortletActionConstants.MODE_PARAM, new String[]{mode.toString()});
paramMap.put(PortletConstants.ACTION_PARAM, new String[]{"/view/testAction"});
paramMap.put(PortletConstants.MODE_PARAM, new String[]{mode.toString()});
mockPortletUrl.expects(once()).method("setParameters").with(new ParamMapConstraint(paramMap));
mockPortletUrl.expects(once()).method("setWindowState").with(eq(WindowState.MAXIMIZED));
@@ -191,22 +208,46 @@ public class PortletUrlTagTest extends MockObjectTestCase {
mockHttpReq.stubs().method("getQueryString").will(returnValue(""));
mockPortletRes.expects(once()).method("createRenderURL").will(
returnValue((PortletURL) mockPortletUrl.proxy()));
returnValue(mockPortletUrl.proxy()));
mockCtx.expects(atLeastOnce()).method("getMajorVersion").will(returnValue(1));
Map paramMap = new HashMap();
paramMap.put(PortletActionConstants.ACTION_PARAM, new String[]{"/help/testAction"});
paramMap.put(PortletActionConstants.MODE_PARAM, new String[]{mode.toString()});
paramMap.put(PortletConstants.ACTION_PARAM, new String[]{"/help/testAction"});
paramMap.put(PortletConstants.MODE_PARAM, new String[]{mode.toString()});
mockPortletUrl.expects(once()).method("setParameters").with(new ParamMapConstraint(paramMap));
mockPortletUrl.expects(once()).method("setPortletMode").with(eq(PortletMode.HELP));
mockPortletUrl.expects(once()).method("setWindowState").with(eq(WindowState.NORMAL));
tag.setNamespace("/help");
tag.setAction("testAction");
tag.setPortletMode("help");
tag.doStartTag();
tag.doEndTag();
}
public void testWhenPortletModeDiffersFromCurrentAndNoParametersAreSetRenderTheDefaults()
throws Exception {
PortletMode mode = PortletMode.HELP;
mockHttpReq.stubs().method("getQueryString").will(returnValue(""));
mockPortletRes.expects(once()).method("createRenderURL").will(
returnValue(mockPortletUrl.proxy()));
mockCtx.expects(atLeastOnce()).method("getMajorVersion").will(returnValue(1));
Map paramMap = new HashMap();
paramMap.put(PortletConstants.ACTION_PARAM, new String[]{"/help/defaultHelp"});
paramMap.put(PortletConstants.MODE_PARAM, new String[]{mode.toString()});
mockPortletUrl.expects(once()).method("setParameters").with(new ParamMapConstraint(paramMap));
mockPortletUrl.expects(once()).method("setPortletMode").with(eq(PortletMode.HELP));
mockPortletUrl.expects(once()).method("setWindowState").with(eq(WindowState.NORMAL));
tag.setPortletMode("help");
tag.doStartTag();
tag.doEndTag();
}
public void testUrlWithQueryParams() throws Exception {
@@ -215,13 +256,13 @@ public class PortletUrlTagTest extends MockObjectTestCase {
mockHttpReq.stubs().method("getQueryString").will(returnValue(""));
mockPortletRes.expects(once()).method("createRenderURL").will(
returnValue((PortletURL) mockPortletUrl.proxy()));
returnValue(mockPortletUrl.proxy()));
mockCtx.expects(atLeastOnce()).method("getMajorVersion").will(returnValue(1));
Map paramMap = new HashMap();
paramMap.put(PortletActionConstants.ACTION_PARAM, new String[]{"/view/testAction"});
paramMap.put(PortletConstants.ACTION_PARAM, new String[]{"/view/testAction"});
paramMap.put("testParam1", new String[]{"testValue1"});
paramMap.put(PortletActionConstants.MODE_PARAM, new String[]{mode.toString()});
paramMap.put(PortletConstants.MODE_PARAM, new String[]{mode.toString()});
mockPortletUrl.expects(once()).method("setParameters").with(new ParamMapConstraint(paramMap));
mockPortletUrl.expects(once()).method("setPortletMode").with(eq(PortletMode.VIEW));
@@ -239,17 +280,18 @@ public class PortletUrlTagTest extends MockObjectTestCase {
mockHttpReq.stubs().method("getQueryString").will(returnValue(""));
mockPortletRes.expects(once()).method("createActionURL").will(
returnValue((PortletURL) mockPortletUrl.proxy()));
returnValue(mockPortletUrl.proxy()));
mockCtx.expects(atLeastOnce()).method("getMajorVersion").will(returnValue(1));
Map paramMap = new HashMap();
paramMap.put(PortletActionConstants.ACTION_PARAM, new String[]{"/view/testAction"});
paramMap.put(PortletActionConstants.MODE_PARAM, new String[]{mode.toString()});
paramMap.put(PortletConstants.ACTION_PARAM, new String[]{"/view/testAction"});
paramMap.put(PortletConstants.MODE_PARAM, new String[]{mode.toString()});
mockPortletUrl.expects(once()).method("setParameters").with(new ParamMapConstraint(paramMap));
mockPortletUrl.expects(once()).method("setPortletMode").with(eq(PortletMode.VIEW));
mockPortletUrl.expects(once()).method("setWindowState").with(eq(WindowState.NORMAL));
tag.setNamespace("/view");
tag.setAction("testAction");
tag.setPortletUrlType("action");
tag.doStartTag();
@@ -316,12 +358,12 @@ public class PortletUrlTagTest extends MockObjectTestCase {
PortletMode mode = PortletMode.VIEW;
mockHttpReq.stubs().method("getQueryString").will(returnValue(""));
mockPortletRes.expects(once()).method("createRenderURL").will(
returnValue((PortletURL) mockPortletUrl.proxy()));
returnValue(mockPortletUrl.proxy()));
mockCtx.expects(atLeastOnce()).method("getMajorVersion").will(returnValue(1));
tag.setAction("testAction");
Map paramMap = new HashMap();
paramMap.put(PortletActionConstants.ACTION_PARAM, new String[]{"/view/testAction!input"});
paramMap.put(PortletActionConstants.MODE_PARAM, new String[]{mode.toString()});
paramMap.put(PortletConstants.ACTION_PARAM, new String[]{"/view/testAction!input"});
paramMap.put(PortletConstants.MODE_PARAM, new String[]{mode.toString()});
mockPortletUrl.expects(once()).method("setParameters").with(new ParamMapConstraint(paramMap));
mockPortletUrl.expects(once()).method("setPortletMode").with(eq(PortletMode.VIEW));
mockPortletUrl.expects(once()).method("setWindowState").with(eq(WindowState.NORMAL));
@@ -334,22 +376,16 @@ public class PortletUrlTagTest extends MockObjectTestCase {
PortletMode mode = PortletMode.VIEW;
mockHttpReq.stubs().method("getQueryString").will(returnValue(""));
mockPortletRes.expects(once()).method("createRenderURL").will(
returnValue((PortletURL) mockPortletUrl.proxy()));
returnValue(mockPortletUrl.proxy()));
mockCtx.expects(atLeastOnce()).method("getMajorVersion").will(returnValue(1));
Map paramMap = new HashMap();
Mock mockActionProxy = mock(ActionProxy.class);
mockActionProxy.stubs().method("getActionName").will(returnValue("currentExecutingAction"));
mockActionProxy.stubs().method("getNamespace").will(returnValue(""));
final ActionProxy proxy = (ActionProxy)mockActionProxy.proxy();
Mock mockActionInvocation = mock(ActionInvocation.class);
mockActionInvocation.stubs().method("getProxy").will(returnValue(proxy));
ActionInvocation ai = (ActionInvocation)mockActionInvocation.proxy();
stack.getContext().put(ActionContext.ACTION_INVOCATION, ai);
paramMap.put(PortletActionConstants.ACTION_PARAM, new String[]{"/view/currentExecutingAction"});
paramMap.put(PortletActionConstants.MODE_PARAM, new String[]{mode.toString()});
mockActionProxy.stubs().method("getActionName").will(returnValue("currentExecutingAction"));
paramMap.put(PortletConstants.ACTION_PARAM, new String[]{"/view/currentExecutingAction"});
paramMap.put(PortletConstants.MODE_PARAM, new String[]{mode.toString()});
mockPortletUrl.expects(once()).method("setParameters").with(new ParamMapConstraint(paramMap));
mockPortletUrl.expects(once()).method("setPortletMode").with(eq(PortletMode.VIEW));
mockPortletUrl.expects(once()).method("setWindowState").with(eq(WindowState.NORMAL));
@@ -404,11 +440,20 @@ public class PortletUrlTagTest extends MockObjectTestCase {
* @see org.jmock.core.SelfDescribing#describeTo(java.lang.StringBuffer)
*/
public StringBuffer describeTo(StringBuffer sb) {
Iterator<String> it = myExpectedMap.keySet().iterator();
sb.append("\n Expected: ");
describeTo(myExpectedMap, sb);
sb.append("\n Actual: ");
describeTo(myActualMap, sb);
return sb;
}
private StringBuffer describeTo(Map map,StringBuffer sb) {
Iterator<String> it = map.keySet().iterator();
while(it.hasNext()) {
String key = it.next();
sb.append(key).append("=");
String[] value = (String[])myExpectedMap.get(key);
String[] value = (String[])map.get(key);
sb.append(value[0]);
if(it.hasNext()) {
sb.append(", ");