001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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.ParamUtil;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.util.PortalUtil;
026    
027    import java.util.HashMap;
028    import java.util.Map;
029    
030    import javax.portlet.PortletRequest;
031    
032    import javax.servlet.http.HttpServletRequest;
033    
034    /**
035     * @author Brian Wing Shun Chan
036     * @author Connor McKay
037     */
038    public class AlloyFriendlyURLMapper extends DefaultFriendlyURLMapper {
039    
040            @Override
041            public String buildPath(LiferayPortletURL liferayPortletURL) {
042                    Map<String, String> routeParameters = new HashMap<String, String>();
043    
044                    buildRouteParameters(liferayPortletURL, routeParameters);
045    
046                    // Populate method parameter based on the portlet lifecycle
047    
048                    String lifecycle = liferayPortletURL.getLifecycle();
049    
050                    if (lifecycle.equals(PortletRequest.ACTION_PHASE)) {
051                            routeParameters.put("method", HttpMethods.POST);
052                    }
053                    else {
054                            routeParameters.put("method", HttpMethods.GET);
055                    }
056    
057                    // Map URL with router
058    
059                    String friendlyURLPath = router.parametersToUrl(routeParameters);
060    
061                    if (friendlyURLPath == null) {
062                            return null;
063                    }
064    
065                    // Remove mapped parameters from URL
066    
067                    addParametersIncludedInPath(liferayPortletURL, routeParameters);
068    
069                    // Remove method
070    
071                    int pos = friendlyURLPath.indexOf(CharPool.SLASH);
072    
073                    if (pos != -1) {
074                            friendlyURLPath = friendlyURLPath.substring(pos);
075                    }
076                    else {
077                            friendlyURLPath = StringPool.BLANK;
078                    }
079    
080                    // Add mapping
081    
082                    friendlyURLPath = StringPool.SLASH.concat(getMapping()).concat(
083                            friendlyURLPath);
084    
085                    return friendlyURLPath;
086            }
087    
088            @Override
089            public void populateParams(
090                    String friendlyURLPath, Map<String, String[]> parameterMap,
091                    Map<String, Object> requestContext) {
092    
093                    // Determine lifecycle from request method
094    
095                    HttpServletRequest request = (HttpServletRequest)requestContext.get(
096                            "request");
097    
098                    friendlyURLPath =
099                            request.getMethod() +
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(request));
128    
129                    populateParams(parameterMap, namespace, routeParameters);
130            }
131    
132            protected String getLifecycle(HttpServletRequest request) {
133                    String method = request.getMethod();
134    
135                    if (method.equalsIgnoreCase(HttpMethods.POST)) {
136                            return "1";
137                    }
138    
139                    return ParamUtil.getString(request, "p_p_lifecycle", "0");
140            }
141    
142            private static Log _log = LogFactoryUtil.getLog(
143                    AlloyFriendlyURLMapper.class);
144    
145    }