001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.util.bridges.alloy;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.portlet.DefaultFriendlyURLMapper;
020    import com.liferay.portal.kernel.portlet.LiferayPortletURL;
021    import com.liferay.portal.kernel.servlet.HttpMethods;
022    import com.liferay.portal.kernel.util.CharPool;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.util.PortalUtil;
025    
026    import java.util.HashMap;
027    import java.util.Map;
028    
029    import javax.portlet.PortletRequest;
030    
031    import javax.servlet.http.HttpServletRequest;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     * @author Connor McKay
036     */
037    public class AlloyFriendlyURLMapper extends DefaultFriendlyURLMapper {
038    
039            public String buildPath(LiferayPortletURL liferayPortletURL) {
040                    Map<String, String> routeParameters = new HashMap<String, String>();
041    
042                    buildRouteParameters(liferayPortletURL, routeParameters);
043    
044                    // Populate method parameter based on the portlet lifecycle
045    
046                    String lifecycle = liferayPortletURL.getLifecycle();
047    
048                    if (lifecycle.equals(PortletRequest.ACTION_PHASE)) {
049                            routeParameters.put("method", HttpMethods.POST);
050                    }
051                    else {
052                            routeParameters.put("method", HttpMethods.GET);
053                    }
054    
055                    // Map URL with router
056    
057                    String friendlyURLPath = router.parametersToUrl(routeParameters);
058    
059                    if (friendlyURLPath == null) {
060                            return null;
061                    }
062    
063                    // Remove mapped parameters from URL
064    
065                    addParametersIncludedInPath(liferayPortletURL, routeParameters);
066    
067                    // Remove method
068    
069                    int pos = friendlyURLPath.indexOf(CharPool.SLASH);
070    
071                    friendlyURLPath = friendlyURLPath.substring(pos);
072    
073                    // Add mapping
074    
075                    friendlyURLPath = StringPool.SLASH.concat(getMapping()).concat(
076                            friendlyURLPath);
077    
078                    return friendlyURLPath;
079            }
080    
081            public void populateParams(
082                    String friendlyURLPath, Map<String, String[]> parameterMap,
083                    Map<String, Object> requestContext) {
084    
085                    // Determine lifecycle from request method
086    
087                    HttpServletRequest request = (HttpServletRequest)requestContext.get(
088                            "request");
089    
090                    String method = request.getMethod();
091    
092                    friendlyURLPath = method +
093                            friendlyURLPath.substring(getMapping().length() + 1);
094    
095                    if (friendlyURLPath.endsWith(StringPool.SLASH))    {
096                            friendlyURLPath = friendlyURLPath.substring(
097                                    0, friendlyURLPath.length() - 1);
098                    }
099    
100                    Map<String, String> routeParameters = new HashMap<String, String>();
101    
102                    if (!router.urlToParameters(friendlyURLPath, routeParameters)) {
103                            if (_log.isWarnEnabled()) {
104                                    _log.warn(
105                                            "No route could be found to match URL " + friendlyURLPath);
106                            }
107    
108                            return;
109                    }
110    
111                    String portletId = getPortletId(routeParameters);
112    
113                    if (portletId == null) {
114                            return;
115                    }
116    
117                    String namespace = PortalUtil.getPortletNamespace(portletId);
118    
119                    addParameter(namespace, parameterMap, "p_p_id", portletId);
120                    addParameter(parameterMap, "p_p_lifecycle", getLifecycle(method));
121    
122                    populateParams(parameterMap, namespace, routeParameters);
123            }
124    
125            protected String getLifecycle(String method) {
126                    if (method.equalsIgnoreCase(HttpMethods.POST)) {
127                            return "1";
128                    }
129                    else {
130                            return "0";
131                    }
132            }
133    
134            private static Log _log = LogFactoryUtil.getLog(
135                    AlloyFriendlyURLMapper.class);
136    
137    }