001
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
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
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
055
056 String friendlyURLPath = router.parametersToUrl(routeParameters);
057
058 if (friendlyURLPath == null) {
059 return null;
060 }
061
062
063
064 addParametersIncludedInPath(liferayPortletURL, routeParameters);
065
066
067
068 int pos = friendlyURLPath.indexOf(StringPool.SLASH);
069
070 friendlyURLPath = friendlyURLPath.substring(pos);
071
072
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
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 Map<String, String> routeParameters = new HashMap<String, String>();
095
096 if (!router.urlToParameters(friendlyURLPath, routeParameters)) {
097 if (_log.isWarnEnabled()) {
098 _log.warn(
099 "No route could be found to match URL " + friendlyURLPath);
100 }
101
102 return;
103 }
104
105 String portletId = getPortletId(routeParameters);
106
107 if (portletId == null) {
108 return;
109 }
110
111 String namespace = PortalUtil.getPortletNamespace(portletId);
112
113 addParameter(namespace, parameterMap, "p_p_id", portletId);
114 addParameter(parameterMap, "p_p_lifecycle", getLifecycle(method));
115
116 populateParams(parameterMap, namespace, routeParameters);
117 }
118
119 protected String getLifecycle(String method) {
120 if (method.equalsIgnoreCase(HttpMethods.POST)) {
121 return "1";
122 }
123 else {
124 return "0";
125 }
126 }
127
128 private static Log _log = LogFactoryUtil.getLog(
129 AlloyFriendlyURLMapper.class);
130
131 }