001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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.model.PortletConstants;
020    import com.liferay.portal.kernel.model.PortletInstance;
021    import com.liferay.portal.kernel.util.PortalUtil;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.Validator;
024    
025    import java.util.HashMap;
026    import java.util.LinkedHashMap;
027    import java.util.LinkedHashSet;
028    import java.util.Map;
029    import java.util.Set;
030    
031    import javax.portlet.PortletMode;
032    import javax.portlet.WindowState;
033    
034    /**
035     * The default friendly URL mapper to use with friendly URL routes.
036     *
037     * <p>
038     * In most cases, to add friendly URL mapping to a portlet, simply set this
039     * class as the friendly URL mapper in <code>liferay-portlet.xml</code>, and
040     * write a <code>friendly-url-routes.xml</code> file.
041     * </p>
042     *
043     * <p>
044     * If you do need to extend this class, the key methods to override are {@link
045     * #buildPath(LiferayPortletURL)} and {@link #populateParams(String, Map, Map)}.
046     * </p>
047     *
048     * @author Connor McKay
049     * @see    Router
050     */
051    public class DefaultFriendlyURLMapper extends BaseFriendlyURLMapper {
052    
053            public DefaultFriendlyURLMapper() {
054                    defaultIgnoredParameters = new LinkedHashSet<>();
055    
056                    defaultIgnoredParameters.add("p_p_id");
057                    defaultIgnoredParameters.add("p_p_col_id");
058                    defaultIgnoredParameters.add("p_p_col_pos");
059                    defaultIgnoredParameters.add("p_p_col_count");
060    
061                    defaultReservedParameters = new LinkedHashMap<>();
062    
063                    defaultReservedParameters.put("p_p_lifecycle", "0");
064                    defaultReservedParameters.put(
065                            "p_p_state", WindowState.NORMAL.toString());
066                    defaultReservedParameters.put("p_p_mode", PortletMode.VIEW.toString());
067            }
068    
069            /**
070             * Adds a default ignored parameter.
071             *
072             * <p>
073             * A default ignored parameter will always be hidden in friendly URLs.
074             * </p>
075             *
076             * @param name the name of the parameter
077             */
078            public void addDefaultIgnoredParameter(String name) {
079                    defaultIgnoredParameters.add(name);
080            }
081    
082            /**
083             * Adds a default reserved parameter.
084             *
085             * <p>
086             * A default reserved parameter will be hidden in friendly URLs when it is
087             * set to its default value.
088             * </p>
089             *
090             * @param name the name of the parameter
091             * @param value the default value of the parameter
092             */
093            public void addDefaultReservedParameter(String name, String value) {
094                    defaultReservedParameters.put(name, value);
095            }
096    
097            @Override
098            public String buildPath(LiferayPortletURL liferayPortletURL) {
099                    Map<String, String> routeParameters = new HashMap<>();
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            /**
118             * Returns the default ignored parameters.
119             *
120             * @return the ignored parameter names
121             * @see    #addDefaultIgnoredParameter(String)
122             */
123            public Set<String> getDefaultIgnoredParameters() {
124                    return defaultIgnoredParameters;
125            }
126    
127            /**
128             * Returns the default reserved parameters.
129             *
130             * @return the default reserved parameter names and values
131             * @see    #addDefaultReservedParameter(String, String)
132             */
133            public Map<String, String> getDefaultReservedParameters() {
134                    return defaultReservedParameters;
135            }
136    
137            @Override
138            public void populateParams(
139                    String friendlyURLPath, Map<String, String[]> parameterMap,
140                    Map<String, Object> requestContext) {
141    
142                    friendlyURLPath = friendlyURLPath.substring(getMapping().length() + 1);
143    
144                    if (friendlyURLPath.endsWith(StringPool.SLASH)) {
145                            friendlyURLPath = friendlyURLPath.substring(
146                                    0, friendlyURLPath.length() - 1);
147                    }
148    
149                    Map<String, String> routeParameters = new HashMap<>();
150    
151                    if (!router.urlToParameters(friendlyURLPath, routeParameters)) {
152                            if (_log.isWarnEnabled()) {
153                                    _log.warn(
154                                            "No route could be found to match URL " + friendlyURLPath);
155                            }
156    
157                            return;
158                    }
159    
160                    String namespace = null;
161    
162                    String portletInstanceKey = getPortletId(routeParameters);
163    
164                    if (Validator.isNotNull(portletInstanceKey)) {
165                            namespace = PortalUtil.getPortletNamespace(portletInstanceKey);
166    
167                            addParameter(namespace, parameterMap, "p_p_id", portletInstanceKey);
168                    }
169                    else if (isAllPublicRenderParameters(routeParameters)) {
170    
171                            // Portlet namespace is not needed if all the parameters are public
172                            // render parameters
173    
174                            addParameter(null, parameterMap, "p_p_id", getPortletId());
175                    }
176                    else {
177                            return;
178                    }
179    
180                    populateParams(parameterMap, namespace, routeParameters);
181            }
182    
183            /**
184             * Adds the parameters included in the path to the portlet URL.
185             *
186             * <p>
187             * Portlet URLs track which parameters are included in the friendly URL
188             * path. This method hides all the default ignored parameters, the
189             * parameters included in the path by the router, and the reserved
190             * parameters set to their defaults.
191             * </p>
192             *
193             * @param liferayPortletURL the portlet URL to which to add the parameters
194             *        included in the path
195             * @param routeParameters the parameter map populated by the router
196             * @see   com.liferay.portlet.PortletURLImpl#addParameterIncludedInPath(
197             *        String)
198             */
199            protected void addParametersIncludedInPath(
200                    LiferayPortletURL liferayPortletURL,
201                    Map<String, String> routeParameters) {
202    
203                    // Hide default ignored parameters
204    
205                    for (String name : defaultIgnoredParameters) {
206                            liferayPortletURL.addParameterIncludedInPath(name);
207                    }
208    
209                    // Hide application parameters removed by the router
210    
211                    Map<String, String[]> portletURLParameters =
212                            liferayPortletURL.getParameterMap();
213    
214                    for (String name : portletURLParameters.keySet()) {
215                            if (!routeParameters.containsKey(name)) {
216                                    liferayPortletURL.addParameterIncludedInPath(name);
217                            }
218                    }
219    
220                    // Hide reserved parameters removed by the router or set to the defaults
221    
222                    Map<String, String> reservedParameters =
223                            liferayPortletURL.getReservedParameterMap();
224    
225                    for (Map.Entry<String, String> entry : reservedParameters.entrySet()) {
226                            String key = entry.getKey();
227                            String value = entry.getValue();
228    
229                            if (!routeParameters.containsKey(key) ||
230                                    value.equals(defaultReservedParameters.get(key))) {
231    
232                                    liferayPortletURL.addParameterIncludedInPath(key);
233                            }
234                    }
235            }
236    
237            /**
238             * Builds the parameter map to be used by the router by copying parameters
239             * from the portlet URL.
240             *
241             * <p>
242             * This method also populates the special virtual parameters
243             * <code>p_p_id</code> and <code>instanceId</code> for instanceable
244             * portlets.
245             * </p>
246             *
247             * @param liferayPortletURL the portlet URL to copy parameters from
248             * @param routeParameters the parameter map to populate for use by the
249             *        router
250             */
251            protected void buildRouteParameters(
252                    LiferayPortletURL liferayPortletURL,
253                    Map<String, String> routeParameters) {
254    
255                    // Copy application parameters
256    
257                    Map<String, String[]> portletURLParameters =
258                            liferayPortletURL.getParameterMap();
259    
260                    for (Map.Entry<String, String[]> entry :
261                                    portletURLParameters.entrySet()) {
262    
263                            String[] values = entry.getValue();
264    
265                            if (values.length > 0) {
266                                    routeParameters.put(entry.getKey(), values[0]);
267                            }
268                    }
269    
270                    // Populate virtual parameters for instanceable portlets
271    
272                    String portletInstanceKey = liferayPortletURL.getPortletId();
273    
274                    if (Validator.isNotNull(portletInstanceKey)) {
275                            routeParameters.put("p_p_id", portletInstanceKey);
276    
277                            PortletInstance portletInstance =
278                                    PortletInstance.fromPortletInstanceKey(portletInstanceKey);
279    
280                            routeParameters.put(
281                                    "userIdAndInstanceId",
282                                    portletInstance.getUserIdAndInstanceId());
283    
284                            if (portletInstance.hasInstanceId()) {
285                                    routeParameters.put(
286                                            "instanceId", portletInstance.getInstanceId());
287                            }
288                    }
289    
290                    // Copy reserved parameters
291    
292                    routeParameters.putAll(liferayPortletURL.getReservedParameterMap());
293            }
294    
295            /**
296             * Returns the portlet ID, including the instance ID if applicable, from the
297             * parameter map.
298             *
299             * @deprecated As of 7.0.0, replaced by {@link #getPortletInstanceKey(Map)}
300             * @param  routeParameters the parameter map. For an instanceable portlet,
301             *         this must contain either <code>p_p_id</code> or
302             *         <code>instanceId</code>.
303             * @return the portlet ID, including the instance ID if applicable, or
304             *         <code>null</code> if it cannot be determined
305             */
306            @Deprecated
307            protected String getPortletId(Map<String, String> routeParameters) {
308                    return getPortletInstanceKey(routeParameters);
309            }
310    
311            /**
312             * Returns the portlet instance key, including the instance ID if
313             * applicable, from the parameter map.
314             *
315             * @param  routeParameters the parameter map. For an instanceable portlet,
316             *         this must contain either <code>p_p_id</code> or
317             *         <code>instanceId</code>.
318             * @return the portlet instance key, including the instance ID if
319             *         applicable, or <code>null</code> if it cannot be determined
320             */
321            protected String getPortletInstanceKey(
322                    Map<String, String> routeParameters) {
323    
324                    String userIdAndInstanceId = routeParameters.remove(
325                            "userIdAndInstanceId");
326    
327                    if (!isPortletInstanceable() && Validator.isNull(userIdAndInstanceId)) {
328                            return getPortletId();
329                    }
330    
331                    String portletInstanceKey = routeParameters.remove("p_p_id");
332    
333                    if (Validator.isNotNull(portletInstanceKey)) {
334                            return portletInstanceKey;
335                    }
336    
337                    if (Validator.isNotNull(userIdAndInstanceId)) {
338                            PortletInstance portletInstance =
339                                    PortletInstance.fromPortletNameAndUserIdAndInstanceId(
340                                            getPortletId(), userIdAndInstanceId);
341    
342                            return portletInstance.getPortletInstanceKey();
343                    }
344    
345                    String instanceId = routeParameters.remove("instanceId");
346    
347                    if (Validator.isNotNull(instanceId)) {
348                            return PortletConstants.assemblePortletId(
349                                    getPortletId(), instanceId);
350                    }
351    
352                    if (!isAllPublicRenderParameters(routeParameters)) {
353                            _log.error(
354                                    "Either p_p_id or instanceId must be provided for an " +
355                                            "instanceable portlet");
356                    }
357    
358                    return null;
359            }
360    
361            /**
362             * Returns <code>true</code> if all the route parameters are public render
363             * parameters.
364             *
365             * @param  routeParameters the parameter map
366             * @return <code>true</code> if all the route parameters are public render
367             *         parameters; <code>false</code> otherwise
368             */
369            protected boolean isAllPublicRenderParameters(
370                    Map<String, String> routeParameters) {
371    
372                    Set<String> routeParameterKeys = routeParameters.keySet();
373    
374                    Map<String, String> publicRenderParameters =
375                            FriendlyURLMapperThreadLocal.getPRPIdentifiers();
376    
377                    return routeParameterKeys.containsAll(publicRenderParameters.keySet());
378            }
379    
380            /**
381             * Populates the parameter map using the parameters from the router and the
382             * default reserved parameters.
383             *
384             * @param parameterMap the parameter map to populate. This should be the map
385             *        passed to {@link #populateParams(String, Map, Map)} by {@link
386             *        com.liferay.portal.util.PortalImpl}.
387             * @param namespace the namespace to use for parameters added to
388             *        <code>parameterMap</code>
389             * @param routeParameters the parameter map populated by the router
390             */
391            protected void populateParams(
392                    Map<String, String[]> parameterMap, String namespace,
393                    Map<String, String> routeParameters) {
394    
395                    // Copy route parameters
396    
397                    for (Map.Entry<String, String> entry : routeParameters.entrySet()) {
398                            addParameter(
399                                    namespace, parameterMap, entry.getKey(), entry.getValue());
400                    }
401    
402                    // Copy default reserved parameters if they are not already set
403    
404                    for (Map.Entry<String, String> entry :
405                                    defaultReservedParameters.entrySet()) {
406    
407                            String key = entry.getKey();
408    
409                            if (!parameterMap.containsKey(key)) {
410                                    addParameter(namespace, parameterMap, key, entry.getValue());
411                            }
412                    }
413            }
414    
415            protected Set<String> defaultIgnoredParameters;
416            protected Map<String, String> defaultReservedParameters;
417    
418            private static final Log _log = LogFactoryUtil.getLog(
419                    DefaultFriendlyURLMapper.class);
420    
421    }