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 @Override
099 public String buildPath(LiferayPortletURL liferayPortletURL) {
100 Map<String, String> routeParameters = new HashMap<String, String>();
101
102 buildRouteParameters(liferayPortletURL, routeParameters);
103
104 String friendlyURLPath = router.parametersToUrl(routeParameters);
105
106 if (Validator.isNull(friendlyURLPath)) {
107 return null;
108 }
109
110 addParametersIncludedInPath(liferayPortletURL, routeParameters);
111
112 friendlyURLPath = StringPool.SLASH.concat(getMapping()).concat(
113 friendlyURLPath);
114
115 return friendlyURLPath;
116 }
117
118
124 public Set<String> getDefaultIgnoredParameters() {
125 return defaultIgnoredParameters;
126 }
127
128
134 public Map<String, String> getDefaultReservedParameters() {
135 return defaultReservedParameters;
136 }
137
138 @Override
139 public void populateParams(
140 String friendlyURLPath, Map<String, String[]> parameterMap,
141 Map<String, Object> requestContext) {
142
143 friendlyURLPath = friendlyURLPath.substring(getMapping().length() + 1);
144
145 if (friendlyURLPath.endsWith(StringPool.SLASH)) {
146 friendlyURLPath = friendlyURLPath.substring(
147 0, friendlyURLPath.length() - 1);
148 }
149
150 Map<String, String> routeParameters = new HashMap<String, String>();
151
152 if (!router.urlToParameters(friendlyURLPath, routeParameters)) {
153 if (_log.isWarnEnabled()) {
154 _log.warn(
155 "No route could be found to match URL " + friendlyURLPath);
156 }
157
158 return;
159 }
160
161 String portletId = getPortletId(routeParameters);
162
163 if (Validator.isNull(portletId)) {
164 return;
165 }
166
167 String namespace = PortalUtil.getPortletNamespace(portletId);
168
169 addParameter(namespace, parameterMap, "p_p_id", portletId);
170
171 populateParams(parameterMap, namespace, routeParameters);
172 }
173
174
190 protected void addParametersIncludedInPath(
191 LiferayPortletURL liferayPortletURL,
192 Map<String, String> routeParameters) {
193
194
195
196 for (String name : defaultIgnoredParameters) {
197 liferayPortletURL.addParameterIncludedInPath(name);
198 }
199
200
201
202 Map<String, String[]> portletURLParameters =
203 liferayPortletURL.getParameterMap();
204
205 for (String name : portletURLParameters.keySet()) {
206 if (!routeParameters.containsKey(name)) {
207 liferayPortletURL.addParameterIncludedInPath(name);
208 }
209 }
210
211
212
213 Map<String, String> reservedParameters =
214 liferayPortletURL.getReservedParameterMap();
215
216 for (Map.Entry<String, String> entry : reservedParameters.entrySet()) {
217 String key = entry.getKey();
218 String value = entry.getValue();
219
220 if (!routeParameters.containsKey(key) ||
221 value.equals(defaultReservedParameters.get(key))) {
222
223 liferayPortletURL.addParameterIncludedInPath(key);
224 }
225 }
226 }
227
228
242 protected void buildRouteParameters(
243 LiferayPortletURL liferayPortletURL,
244 Map<String, String> routeParameters) {
245
246
247
248 Map<String, String[]> portletURLParameters =
249 liferayPortletURL.getParameterMap();
250
251 for (Map.Entry<String, String[]> entry :
252 portletURLParameters.entrySet()) {
253
254 String[] values = entry.getValue();
255
256 if (values.length > 0) {
257 routeParameters.put(entry.getKey(), values[0]);
258 }
259 }
260
261
262
263 if (isPortletInstanceable()) {
264 String portletId = liferayPortletURL.getPortletId();
265
266 routeParameters.put("p_p_id", portletId);
267
268 if (Validator.isNotNull(portletId)) {
269 int x = portletId.indexOf(PortletConstants.INSTANCE_SEPARATOR);
270
271 if (x != -1) {
272 x += PortletConstants.INSTANCE_SEPARATOR.length();
273
274 String instanceId = null;
275
276 int y = portletId.indexOf(portletId, x);
277
278 if (y != -1) {
279 instanceId = portletId.substring(x, y);
280 }
281 else {
282 instanceId = portletId.substring(x);
283 }
284
285 routeParameters.put("instanceId", instanceId);
286 }
287
288 }
289 }
290
291
292
293 routeParameters.putAll(liferayPortletURL.getReservedParameterMap());
294 }
295
296
306 protected String getPortletId(Map<String, String> routeParameters) {
307 if (!isPortletInstanceable()) {
308 return getPortletId();
309 }
310
311 String portletId = routeParameters.remove("p_p_id");
312
313 if (Validator.isNotNull(portletId)) {
314 return portletId;
315 }
316
317 String instanceId = routeParameters.remove("instanceId");
318
319 if (Validator.isNull(instanceId)) {
320 if (_log.isErrorEnabled()) {
321 _log.error(
322 "Either p_p_id or instanceId must be provided for an " +
323 "instanceable portlet");
324 }
325
326 return null;
327 }
328 else {
329 return getPortletId().concat(
330 PortletConstants.INSTANCE_SEPARATOR).concat(instanceId);
331 }
332 }
333
334
345 protected void populateParams(
346 Map<String, String[]> parameterMap, String namespace,
347 Map<String, String> routeParameters) {
348
349
350
351 for (Map.Entry<String, String> entry : routeParameters.entrySet()) {
352 addParameter(
353 namespace, parameterMap, entry.getKey(), entry.getValue());
354 }
355
356
357
358 for (Map.Entry<String, String> entry :
359 defaultReservedParameters.entrySet()) {
360
361 String key = entry.getKey();
362
363 if (!parameterMap.containsKey(key)) {
364 addParameter(namespace, parameterMap, key, entry.getValue());
365 }
366 }
367 }
368
369 protected Set<String> defaultIgnoredParameters;
370 protected Map<String, String> defaultReservedParameters;
371
372 private static Log _log = LogFactoryUtil.getLog(
373 DefaultFriendlyURLMapper.class);
374
375 }