001    /**
002     * Copyright (c) 2000-2013 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.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    /**
034     * The default friendly URL mapper to use with friendly URL routes.
035     *
036     * <p>
037     * In most cases, to add friendly URL mapping to a portlet, simply set this
038     * class as the friendly URL mapper in <code>liferay-portlet.xml</code>, and
039     * write a <code>friendly-url-routes.xml</code> file.
040     * </p>
041     *
042     * <p>
043     * If you do need to extend this class, use {@link
044     * com.liferay.util.bridges.alloy.AlloyFriendlyURLMapper} as a guide. The key
045     * methods to override are {@link #buildPath(LiferayPortletURL)} and {@link
046     * #populateParams(String, Map, Map)}.
047     * </p>
048     *
049     * @author Connor McKay
050     * @see    Router
051     */
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            /**
071             * Adds a default ignored parameter.
072             *
073             * <p>
074             * A default ignored parameter will always be hidden in friendly URLs.
075             * </p>
076             *
077             * @param name the name of the parameter
078             */
079            public void addDefaultIgnoredParameter(String name) {
080                    defaultIgnoredParameters.add(name);
081            }
082    
083            /**
084             * Adds a default reserved parameter.
085             *
086             * <p>
087             * A default reserved parameter will be hidden in friendly URLs when it is
088             * set to its default value.
089             * </p>
090             *
091             * @param name the name of the parameter
092             * @param value the default value of the parameter
093             */
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            /**
119             * Returns the default ignored parameters.
120             *
121             * @return the ignored parameter names
122             * @see    #addDefaultIgnoredParameter(String)
123             */
124            public Set<String> getDefaultIgnoredParameters() {
125                    return defaultIgnoredParameters;
126            }
127    
128            /**
129             * Returns the default reserved parameters.
130             *
131             * @return the default reserved parameter names and values
132             * @see    #addDefaultReservedParameter(String, String)
133             */
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            /**
175             * Adds the parameters included in the path to the portlet URL.
176             *
177             * <p>
178             * Portlet URLs track which parameters are included in the friendly URL
179             * path. This method hides all the default ignored parameters, the
180             * parameters included in the path by the router, and the reserved
181             * parameters set to their defaults.
182             * </p>
183             *
184             * @param liferayPortletURL the portlet URL to which to add the parameters
185             *        included in the path
186             * @param routeParameters the parameter map populated by the router
187             * @see   com.liferay.portlet.PortletURLImpl#addParameterIncludedInPath(
188             *        String)
189             */
190            protected void addParametersIncludedInPath(
191                    LiferayPortletURL liferayPortletURL,
192                    Map<String, String> routeParameters) {
193    
194                    // Hide default ignored parameters
195    
196                    for (String name : defaultIgnoredParameters) {
197                            liferayPortletURL.addParameterIncludedInPath(name);
198                    }
199    
200                    // Hide application parameters removed by the router
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                    // Hide reserved parameters removed by the router or set to the defaults
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            /**
229             * Builds the parameter map to be used by the router by copying parameters
230             * from the portlet URL.
231             *
232             * <p>
233             * This method also populates the special virtual parameters
234             * <code>p_p_id</code> and <code>instanceId</code> for instanceable
235             * portlets.
236             * </p>
237             *
238             * @param liferayPortletURL the portlet URL to copy parameters from
239             * @param routeParameters the parameter map to populate for use by the
240             *        router
241             */
242            protected void buildRouteParameters(
243                    LiferayPortletURL liferayPortletURL,
244                    Map<String, String> routeParameters) {
245    
246                    // Copy application parameters
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                    // Populate virtual parameters for instanceable portlets
262    
263                    if (isPortletInstanceable()) {
264                            String portletId = liferayPortletURL.getPortletId();
265    
266                            routeParameters.put("p_p_id", portletId);
267    
268                            if (Validator.isNotNull(portletId) &&
269                                    PortletConstants.hasInstanceId(portletId)) {
270    
271                                    routeParameters.put(
272                                            "instanceId", PortletConstants.getInstanceId(portletId));
273                            }
274                    }
275    
276                    // Copy reserved parameters
277    
278                    routeParameters.putAll(liferayPortletURL.getReservedParameterMap());
279            }
280    
281            /**
282             * Returns the portlet ID, including the instance ID if applicable, from the
283             * parameter map.
284             *
285             * @param  routeParameters the parameter map. For an instanceable portlet,
286             *         this must contain either <code>p_p_id</code> or
287             *         <code>instanceId</code>.
288             * @return the portlet ID, including the instance ID if applicable, or
289             *         <code>null</code> if it cannot be determined
290             */
291            protected String getPortletId(Map<String, String> routeParameters) {
292                    if (!isPortletInstanceable()) {
293                            return getPortletId();
294                    }
295    
296                    String portletId = routeParameters.remove("p_p_id");
297    
298                    if (Validator.isNotNull(portletId)) {
299                            return portletId;
300                    }
301    
302                    String instanceId = routeParameters.remove("instanceId");
303    
304                    if (Validator.isNull(instanceId)) {
305                            if (_log.isErrorEnabled()) {
306                                    _log.error(
307                                            "Either p_p_id or instanceId must be provided for an " +
308                                                    "instanceable portlet");
309                            }
310    
311                            return null;
312                    }
313    
314                    return PortletConstants.assemblePortletId(getPortletId(), instanceId);
315            }
316    
317            /**
318             * Populates the parameter map using the parameters from the router and the
319             * default reserved parameters.
320             *
321             * @param parameterMap the parameter map to populate. This should be the map
322             *        passed to {@link #populateParams(String, Map, Map)} by {@link
323             *        com.liferay.portal.util.PortalImpl}.
324             * @param namespace the namespace to use for parameters added to
325             *        <code>parameterMap</code>
326             * @param routeParameters the parameter map populated by the router
327             */
328            protected void populateParams(
329                    Map<String, String[]> parameterMap, String namespace,
330                    Map<String, String> routeParameters) {
331    
332                    // Copy route parameters
333    
334                    for (Map.Entry<String, String> entry : routeParameters.entrySet()) {
335                            addParameter(
336                                    namespace, parameterMap, entry.getKey(), entry.getValue());
337                    }
338    
339                    // Copy default reserved parameters if they are not already set
340    
341                    for (Map.Entry<String, String> entry :
342                                    defaultReservedParameters.entrySet()) {
343    
344                            String key = entry.getKey();
345    
346                            if (!parameterMap.containsKey(key)) {
347                                    addParameter(namespace, parameterMap, key, entry.getValue());
348                            }
349                    }
350            }
351    
352            protected Set<String> defaultIgnoredParameters;
353            protected Map<String, String> defaultReservedParameters;
354    
355            private static Log _log = LogFactoryUtil.getLog(
356                    DefaultFriendlyURLMapper.class);
357    
358    }