FileDocCategorySizeDatePackage
PortletUrlHelper.javaAPI DocExample12039Mon Jul 23 13:26:38 BST 2007org.apache.struts2.portlet.util

PortletUrlHelper

public class PortletUrlHelper extends Object
Helper class for creating Portlet URLs. Portlet URLs are fundamentally different from regular servlet URLs since they never target the application itself; all requests go through the portlet container and must therefore be programatically constructed using the {@link javax.portlet.RenderResponse#createActionURL()} and {@link javax.portlet.RenderResponse#createRenderURL()} APIs.

Fields Summary
public static final String
ENCODING
private static final Log
LOG
Constructors Summary
Methods Summary
public static java.lang.StringbuildResourceUrl(java.lang.String value, java.util.Map params)
Encode an url to a non Struts action resource, like stylesheet, image or servlet.

param
value
return
encoded url to non Struts action resources.

        StringBuffer sb = new StringBuffer();
        // Relative URLs are not allowed in a portlet
        if (!value.startsWith("/")) {
            sb.append("/");
        }
        sb.append(value);
        if(params != null && params.size() > 0) {
            sb.append("?");
            Iterator it = params.keySet().iterator();
            try {
            while(it.hasNext()) {
                String key = (String)it.next();
                String val = (String)params.get(key);

                sb.append(URLEncoder.encode(key, ENCODING)).append("=");
                sb.append(URLEncoder.encode(val, ENCODING));
                if(it.hasNext()) {
                    sb.append("&");
                }
            }
            } catch (UnsupportedEncodingException e) {
                throw new StrutsException("Encoding "+ENCODING+" not found");
            }
        }
        RenderResponse resp = PortletActionContext.getRenderResponse();
        RenderRequest req = PortletActionContext.getRenderRequest();
        return resp.encodeURL(req.getContextPath() + sb.toString());
    
public static java.lang.StringbuildUrl(java.lang.String action, java.lang.String namespace, java.util.Map params, java.lang.String type, java.lang.String mode, java.lang.String state)
Create a portlet URL with for the specified action and namespace.

param
action The action the URL should invoke.
param
namespace The namespace of the action to invoke.
param
params The parameters of the URL.
param
type The type of the url, either action or render
param
mode The PortletMode of the URL.
param
state The WindowState of the URL.
return
The URL String.


                                                                         
            
                  
        return buildUrl(action, namespace, params, null, type, mode, state,
                true, true);
    
public static java.lang.StringbuildUrl(java.lang.String action, java.lang.String namespace, java.util.Map params, java.lang.String scheme, java.lang.String type, java.lang.String portletMode, java.lang.String windowState, boolean includeContext, boolean encodeResult)
Create a portlet URL with for the specified action and namespace.

see
#buildUrl(String, String, Map, String, String, String)

        RenderRequest request = PortletActionContext.getRenderRequest();
        RenderResponse response = PortletActionContext.getRenderResponse();
        LOG.debug("Creating url. Action = " + action + ", Namespace = "
                + namespace + ", Type = " + type);
        namespace = prependNamespace(namespace, portletMode);
        if (!TextUtils.stringSet(portletMode)) {
            portletMode = PortletActionContext.getRenderRequest().getPortletMode().toString();
        }
        String result = null;
        int paramStartIndex = action.indexOf('?");
        if (paramStartIndex > 0) {
            String value = action;
            action = value.substring(0, value.indexOf('?"));
            String queryStr = value.substring(paramStartIndex + 1);
            StringTokenizer tok = new StringTokenizer(queryStr, "&");
            while (tok.hasMoreTokens()) {
                String paramVal = tok.nextToken();
                String key = paramVal.substring(0, paramVal.indexOf('="));
                String val = paramVal.substring(paramVal.indexOf('=") + 1);
                params.put(key, new String[] { val });
            }
        }
        if (TextUtils.stringSet(namespace)) {
            StringBuffer sb = new StringBuffer();
            sb.append(namespace);
            if(!action.startsWith("/") && !namespace.endsWith("/")) {
                sb.append("/");
            }
            action = sb.append(action).toString();
            LOG.debug("Resulting actionPath: " + action);
        }
        params.put(PortletActionConstants.ACTION_PARAM, new String[] { action });

        PortletURL url = null;
        if ("action".equalsIgnoreCase(type)) {
            LOG.debug("Creating action url");
            url = response.createActionURL();
        } else {
            LOG.debug("Creating render url");
            url = response.createRenderURL();
        }

        params.put(PortletActionConstants.MODE_PARAM, portletMode);
        url.setParameters(ensureParamsAreStringArrays(params));

        if ("HTTPS".equalsIgnoreCase(scheme)) {
            try {
                url.setSecure(true);
            } catch (PortletSecurityException e) {
                LOG.error("Cannot set scheme to https", e);
            }
        }
        try {
            url.setPortletMode(getPortletMode(request, portletMode));
            url.setWindowState(getWindowState(request, windowState));
        } catch (Exception e) {
            LOG.error("Unable to set mode or state:" + e.getMessage(), e);
        }
        result = url.toString();
        // TEMP BUG-WORKAROUND FOR DOUBLE ESCAPING OF AMPERSAND
        if(result.indexOf("&") >= 0) {
            result = result.replace("&", "&");
        }
        return result;

    
public static java.util.MapensureParamsAreStringArrays(java.util.Map params)
Will ensure that all entries in params are String arrays, as requried by the setParameters on the PortletURL.

param
params The parameters to the URL.
return
A Map with all parameters as String arrays.

        Map result = null;
        if (params != null) {
            result = new HashMap(params.size());
            Iterator it = params.keySet().iterator();
            while (it.hasNext()) {
                Object key = it.next();
                Object val = params.get(key);
                if (val instanceof String[]) {
                    result.put(key, val);
                } else {
                    result.put(key, new String[] { val.toString() });
                }
            }
        }
        return result;
    
private static javax.portlet.PortletModegetPortletMode(javax.portlet.RenderRequest portletReq, java.lang.String portletMode)
Convert the given String to a PortletMode object.

param
portletReq The RenderRequest.
param
portletMode The PortletMode as a String.
return
The PortletMode that mathces the portletMode String, or if the Sring is blank, the current PortletMode.

        PortletMode mode = portletReq.getPortletMode();

        if (TextUtils.stringSet(portletMode)) {
            mode = portletReq.getPortletMode();
            if ("edit".equalsIgnoreCase(portletMode)) {
                mode = PortletMode.EDIT;
            } else if ("view".equalsIgnoreCase(portletMode)) {
                mode = PortletMode.VIEW;
            } else if ("help".equalsIgnoreCase(portletMode)) {
                mode = PortletMode.HELP;
            }
        }
        if(mode == null) {
            mode = PortletMode.VIEW;
        }
        return mode;
    
private static javax.portlet.WindowStategetWindowState(javax.portlet.RenderRequest portletReq, java.lang.String windowState)
Convert the given String to a WindowState object.

param
portletReq The RenderRequest.
param
windowState The WindowState as a String.
return
The WindowState that mathces the windowState String, or if the Sring is blank, the current WindowState.

        WindowState state = portletReq.getWindowState();
        if (TextUtils.stringSet(windowState)) {
            state = portletReq.getWindowState();
            if ("maximized".equalsIgnoreCase(windowState)) {
                state = WindowState.MAXIMIZED;
            } else if ("normal".equalsIgnoreCase(windowState)) {
                state = WindowState.NORMAL;
            } else if ("minimized".equalsIgnoreCase(windowState)) {
                state = WindowState.MINIMIZED;
            }
        }
        if(state == null) {
            state = WindowState.NORMAL;
        }
        return state;
    
private static java.lang.StringprependNamespace(java.lang.String namespace, java.lang.String portletMode)
Prepend the namespace configuration for the specified namespace and PortletMode.

param
namespace The base namespace.
param
portletMode The PortletMode.
return
prepended namespace.

        StringBuffer sb = new StringBuffer();
        PortletMode mode = PortletActionContext.getRenderRequest().getPortletMode();
        if(TextUtils.stringSet(portletMode)) {
            mode = new PortletMode(portletMode);
        }
        String portletNamespace = PortletActionContext.getPortletNamespace();
        String modeNamespace = (String)PortletActionContext.getModeNamespaceMap().get(mode);
        LOG.debug("PortletNamespace: " + portletNamespace + ", modeNamespace: " + modeNamespace);
        if(TextUtils.stringSet(portletNamespace)) {
            sb.append(portletNamespace);
        }
        if(TextUtils.stringSet(modeNamespace)) {
            if(!modeNamespace.startsWith("/")) {
                sb.append("/");
            }
            sb.append(modeNamespace);
        }
        if(TextUtils.stringSet(namespace)) {
            if(!namespace.startsWith("/")) {
                sb.append("/");
            }
            sb.append(namespace);
        }
        LOG.debug("Resulting namespace: " + sb);
        return sb.toString();