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 (Validator.isNull(friendlyURLPath)) {
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 (Validator.isNull(portletId)) {
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
188 protected void addParametersIncludedInPath(
189 LiferayPortletURL liferayPortletURL,
190 Map<String, String> routeParameters) {
191
192
193
194 for (String name : defaultIgnoredParameters) {
195 liferayPortletURL.addParameterIncludedInPath(name);
196 }
197
198
199
200 Map<String, String[]> portletURLParameters =
201 liferayPortletURL.getParameterMap();
202
203 for (String name : portletURLParameters.keySet()) {
204 if (!routeParameters.containsKey(name)) {
205 liferayPortletURL.addParameterIncludedInPath(name);
206 }
207 }
208
209
210
211 Map<String, String> reservedParameters =
212 liferayPortletURL.getReservedParameterMap();
213
214 for (Map.Entry<String, String> entry : reservedParameters.entrySet()) {
215 String key = entry.getKey();
216 String value = entry.getValue();
217
218 if (!routeParameters.containsKey(key) ||
219 value.equals(defaultReservedParameters.get(key))) {
220
221 liferayPortletURL.addParameterIncludedInPath(key);
222 }
223 }
224 }
225
226
240 protected void buildRouteParameters(
241 LiferayPortletURL liferayPortletURL,
242 Map<String, String> routeParameters) {
243
244
245
246 Map<String, String[]> portletURLParameters =
247 liferayPortletURL.getParameterMap();
248
249 for (Map.Entry<String, String[]> entry :
250 portletURLParameters.entrySet()) {
251
252 String[] values = entry.getValue();
253
254 if (values.length > 0) {
255 routeParameters.put(entry.getKey(), values[0]);
256 }
257 }
258
259
260
261 if (isPortletInstanceable()) {
262 String portletId = liferayPortletURL.getPortletId();
263
264 routeParameters.put("p_p_id", portletId);
265
266 if (Validator.isNotNull(portletId)) {
267 int x = portletId.indexOf(PortletConstants.INSTANCE_SEPARATOR);
268
269 if (x != -1) {
270 x += PortletConstants.INSTANCE_SEPARATOR.length();
271
272 String instanceId = null;
273
274 int y = portletId.indexOf(portletId, x);
275
276 if (y != -1) {
277 instanceId = portletId.substring(x, y);
278 }
279 else {
280 instanceId = portletId.substring(x);
281 }
282
283 routeParameters.put("instanceId", instanceId);
284 }
285
286 }
287 }
288
289
290
291 routeParameters.putAll(liferayPortletURL.getReservedParameterMap());
292 }
293
294
304 protected String getPortletId(Map<String, String> routeParameters) {
305 if (!isPortletInstanceable()) {
306 return getPortletId();
307 }
308
309 String portletId = routeParameters.remove("p_p_id");
310
311 if (Validator.isNotNull(portletId)) {
312 return portletId;
313 }
314
315 String instanceId = routeParameters.remove("instanceId");
316
317 if (Validator.isNull(instanceId)) {
318 if (_log.isErrorEnabled()) {
319 _log.error(
320 "Either p_p_id or instanceId must be provided for an " +
321 "instanceable portlet");
322 }
323
324 return null;
325 }
326 else {
327 return getPortletId().concat(
328 PortletConstants.INSTANCE_SEPARATOR).concat(instanceId);
329 }
330 }
331
332
343 protected void populateParams(
344 Map<String, String[]> parameterMap, String namespace,
345 Map<String, String> routeParameters) {
346
347
348
349 for (Map.Entry<String, String> entry : routeParameters.entrySet()) {
350 addParameter(
351 namespace, parameterMap, entry.getKey(), entry.getValue());
352 }
353
354
355
356 for (Map.Entry<String, String> entry :
357 defaultReservedParameters.entrySet()) {
358
359 String key = entry.getKey();
360
361 if (!parameterMap.containsKey(key)) {
362 addParameter(namespace, parameterMap, key, entry.getValue());
363 }
364 }
365 }
366
367 protected Set<String> defaultIgnoredParameters;
368 protected Map<String, String> defaultReservedParameters;
369
370 private static Log _log = LogFactoryUtil.getLog(
371 DefaultFriendlyURLMapper.class);
372
373 }