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