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