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 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 }