001
014
015 package com.liferay.portal.kernel.portlet;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.portal.kernel.util.StringPool;
020 import com.liferay.portal.kernel.util.Validator;
021 import com.liferay.portal.model.PortletConstants;
022 import com.liferay.portal.util.PortalUtil;
023
024 import java.util.HashMap;
025 import java.util.LinkedHashMap;
026 import java.util.LinkedHashSet;
027 import java.util.Map;
028 import java.util.Set;
029
030 import javax.portlet.PortletMode;
031 import javax.portlet.WindowState;
032
033
052 public class DefaultFriendlyURLMapper extends BaseFriendlyURLMapper {
053
054 public DefaultFriendlyURLMapper() {
055 defaultIgnoredParameters = new LinkedHashSet<String>();
056
057 defaultIgnoredParameters.add("p_p_id");
058 defaultIgnoredParameters.add("p_p_col_id");
059 defaultIgnoredParameters.add("p_p_col_pos");
060 defaultIgnoredParameters.add("p_p_col_count");
061
062 defaultReservedParameters = new LinkedHashMap<String, String>();
063
064 defaultReservedParameters.put("p_p_lifecycle", "0");
065 defaultReservedParameters.put(
066 "p_p_state", WindowState.NORMAL.toString());
067 defaultReservedParameters.put("p_p_mode", PortletMode.VIEW.toString());
068 }
069
070
079 public void addDefaultIgnoredParameter(String name) {
080 defaultIgnoredParameters.add(name);
081 }
082
083
094 public void addDefaultReservedParameter(String name, String value) {
095 defaultReservedParameters.put(name, value);
096 }
097
098 public String buildPath(LiferayPortletURL liferayPortletURL) {
099 Map<String, String> routeParameters = new HashMap<String, String>();
100
101 buildRouteParameters(liferayPortletURL, routeParameters);
102
103 String friendlyURLPath = router.parametersToUrl(routeParameters);
104
105 if (friendlyURLPath == null) {
106 return null;
107 }
108
109 addParametersIncludedInPath(liferayPortletURL, routeParameters);
110
111 friendlyURLPath = StringPool.SLASH.concat(getMapping()).concat(
112 friendlyURLPath);
113
114 return friendlyURLPath;
115 }
116
117
123 public Set<String> getDefaultIgnoredParameters() {
124 return defaultIgnoredParameters;
125 }
126
127
133 public Map<String, String> getDefaultReservedParameters() {
134 return defaultReservedParameters;
135 }
136
137 public void populateParams(
138 String friendlyURLPath, Map<String, String[]> parameterMap,
139 Map<String, Object> requestContext) {
140
141 friendlyURLPath = friendlyURLPath.substring(getMapping().length() + 1);
142
143 if (friendlyURLPath.endsWith(StringPool.SLASH)) {
144 friendlyURLPath = friendlyURLPath.substring(
145 0, friendlyURLPath.length() - 1);
146 }
147
148 Map<String, String> routeParameters = new HashMap<String, String>();
149
150 if (!router.urlToParameters(friendlyURLPath, routeParameters)) {
151 if (_log.isWarnEnabled()) {
152 _log.warn(
153 "No route could be found to match URL " + friendlyURLPath);
154 }
155
156 return;
157 }
158
159 String portletId = getPortletId(routeParameters);
160
161 if (portletId == null) {
162 return;
163 }
164
165 String namespace = PortalUtil.getPortletNamespace(portletId);
166
167 addParameter(namespace, parameterMap, "p_p_id", portletId);
168
169 populateParams(parameterMap, namespace, routeParameters);
170 }
171
172
186 protected void buildRouteParameters(
187 LiferayPortletURL liferayPortletURL,
188 Map<String, String> routeParameters) {
189
190
191
192 Map<String, String[]> portletURLParameters =
193 liferayPortletURL.getParameterMap();
194
195 for (Map.Entry<String, String[]> entry :
196 portletURLParameters.entrySet()) {
197
198 String[] values = entry.getValue();
199
200 if (values.length > 0) {
201 routeParameters.put(entry.getKey(), values[0]);
202 }
203 }
204
205
206
207 if (isPortletInstanceable()) {
208 String portletId = liferayPortletURL.getPortletId();
209
210 routeParameters.put("p_p_id", portletId);
211
212 if (Validator.isNotNull(portletId)) {
213 String[] parts = portletId.split(
214 PortletConstants.INSTANCE_SEPARATOR);
215
216 if (parts.length > 1) {
217 routeParameters.put("instanceId", parts[1]);
218 }
219 }
220 }
221
222
223
224 routeParameters.putAll(liferayPortletURL.getReservedParameterMap());
225 }
226
227
237 protected String getPortletId(Map<String, String> routeParameters) {
238 if (!isPortletInstanceable()) {
239 return getPortletId();
240 }
241
242 String portletId = routeParameters.remove("p_p_id");
243
244 if (Validator.isNotNull(portletId)) {
245 return portletId;
246 }
247
248 String instanceId = routeParameters.remove("instanceId");
249
250 if (Validator.isNull(instanceId)) {
251 if (_log.isErrorEnabled()) {
252 _log.error(
253 "Either p_p_id or instanceId must be provided for an " +
254 "instanceable portlet");
255 }
256
257 return null;
258 }
259 else {
260 return getPortletId().concat(
261 PortletConstants.INSTANCE_SEPARATOR).concat(instanceId);
262 }
263 }
264
265
276 protected void populateParams(
277 Map<String, String[]> parameterMap, String namespace,
278 Map<String, String> routeParameters) {
279
280
281
282 for (Map.Entry<String, String> entry : routeParameters.entrySet()) {
283 addParameter(
284 namespace, parameterMap, entry.getKey(), entry.getValue());
285 }
286
287
288
289 for (Map.Entry<String, String> entry :
290 defaultReservedParameters.entrySet()) {
291
292 String key = entry.getKey();
293
294 if (!parameterMap.containsKey(key)) {
295 addParameter(namespace, parameterMap, key, entry.getValue());
296 }
297 }
298 }
299
300
316 protected void addParametersIncludedInPath(
317 LiferayPortletURL liferayPortletURL,
318 Map<String, String> routeParameters) {
319
320
321
322 for (String name : defaultIgnoredParameters) {
323 liferayPortletURL.addParameterIncludedInPath(name);
324 }
325
326
327
328 Map<String, String[]> portletURLParameters =
329 liferayPortletURL.getParameterMap();
330
331 for (String name : portletURLParameters.keySet()) {
332 if (!routeParameters.containsKey(name)) {
333 liferayPortletURL.addParameterIncludedInPath(name);
334 }
335 }
336
337
338
339 Map<String, String> reservedParameters =
340 liferayPortletURL.getReservedParameterMap();
341
342 for (Map.Entry<String, String> entry : reservedParameters.entrySet()) {
343 String key = entry.getKey();
344 String value = entry.getValue();
345
346 if (!routeParameters.containsKey(key) ||
347 value.equals(defaultReservedParameters.get(key))) {
348
349 liferayPortletURL.addParameterIncludedInPath(key);
350 }
351 }
352 }
353
354 protected Set<String> defaultIgnoredParameters;
355 protected Map<String, String> defaultReservedParameters;
356
357 private static Log _log = LogFactoryUtil.getLog(
358 DefaultFriendlyURLMapper.class);
359
360 }