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