001    /**
002     * Copyright (c) 2000-2011 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            @Override
040            public String buildPath(LiferayPortletURL liferayPortletURL) {
041                    Map<String, String> routeParameters = new HashMap<String, String>();
042    
043                    buildRouteParameters(liferayPortletURL, routeParameters);
044    
045                    // Populate method parameter based on the portlet lifecycle
046    
047                    String lifecycle = liferayPortletURL.getLifecycle();
048    
049                    if (lifecycle.equals(PortletRequest.ACTION_PHASE)) {
050                            routeParameters.put("method", HttpMethods.POST);
051                    }
052                    else {
053                            routeParameters.put("method", HttpMethods.GET);
054                    }
055    
056                    // Map URL with router
057    
058                    String friendlyURLPath = router.parametersToUrl(routeParameters);
059    
060                    if (friendlyURLPath == null) {
061                            return null;
062                    }
063    
064                    // Remove mapped parameters from URL
065    
066                    addParametersIncludedInPath(liferayPortletURL, routeParameters);
067    
068                    // Remove method
069    
070                    int pos = friendlyURLPath.indexOf(CharPool.SLASH);
071    
072                    if (pos != -1) {
073                            friendlyURLPath = friendlyURLPath.substring(pos);
074                    }
075                    else {
076                            friendlyURLPath = StringPool.BLANK;
077                    }
078    
079                    // Add mapping
080    
081                    friendlyURLPath = StringPool.SLASH.concat(getMapping()).concat(
082                            friendlyURLPath);
083    
084                    return friendlyURLPath;
085            }
086    
087            @Override
088            public void populateParams(
089                    String friendlyURLPath, Map<String, String[]> parameterMap,
090                    Map<String, Object> requestContext) {
091    
092                    // Determine lifecycle from request method
093    
094                    HttpServletRequest request = (HttpServletRequest)requestContext.get(
095                            "request");
096    
097                    String method = request.getMethod();
098    
099                    friendlyURLPath = method +
100                            friendlyURLPath.substring(getMapping().length() + 1);
101    
102                    if (friendlyURLPath.endsWith(StringPool.SLASH))    {
103                            friendlyURLPath = friendlyURLPath.substring(
104                                    0, friendlyURLPath.length() - 1);
105                    }
106    
107                    Map<String, String> routeParameters = new HashMap<String, String>();
108    
109                    if (!router.urlToParameters(friendlyURLPath, routeParameters)) {
110                            if (_log.isWarnEnabled()) {
111                                    _log.warn(
112                                            "No route could be found to match URL " + friendlyURLPath);
113                            }
114    
115                            return;
116                    }
117    
118                    String portletId = getPortletId(routeParameters);
119    
120                    if (portletId == null) {
121                            return;
122                    }
123    
124                    String namespace = PortalUtil.getPortletNamespace(portletId);
125    
126                    addParameter(namespace, parameterMap, "p_p_id", portletId);
127                    addParameter(parameterMap, "p_p_lifecycle", getLifecycle(method));
128    
129                    populateParams(parameterMap, namespace, routeParameters);
130            }
131    
132            protected String getLifecycle(String method) {
133                    if (method.equalsIgnoreCase(HttpMethods.POST)) {
134                            return "1";
135                    }
136                    else {
137                            return "0";
138                    }
139            }
140    
141            private static Log _log = LogFactoryUtil.getLog(
142                    AlloyFriendlyURLMapper.class);
143    
144    }