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