001    /**
002     * Copyright (c) 2000-2012 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            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            /**
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            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            /**
173             * Adds the parameters included in the path to the portlet URL.
174             *
175             * <p>
176             * Portlet URLs track which parameters are included in the friendly URL
177             * path. This method hides all the default ignored parameters, the
178             * parameters included in the path by the router, and the reserved
179             * parameters set to their defaults.
180             * </p>
181             *
182             * @param liferayPortletURL the portlet URL to which to add the parameters
183             *        included in the path
184             * @param routeParameters the parameter map populated by the router
185             * @see   com.liferay.portlet.PortletURLImpl#addParameterIncludedInPath(
186             *        String)
187             */
188            protected void addParametersIncludedInPath(
189                    LiferayPortletURL liferayPortletURL,
190                    Map<String, String> routeParameters) {
191    
192                    // Hide default ignored parameters
193    
194                    for (String name : defaultIgnoredParameters) {
195                            liferayPortletURL.addParameterIncludedInPath(name);
196                    }
197    
198                    // Hide application parameters removed by the router
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                    // Hide reserved parameters removed by the router or set to the defaults
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            /**
227             * Builds the parameter map to be used by the router by copying parameters
228             * from the portlet URL.
229             *
230             * <p>
231             * This method also populates the special virtual parameters
232             * <code>p_p_id</code> and <code>instanceId</code> for instanceable
233             * portlets.
234             * </p>
235             *
236             * @param liferayPortletURL the portlet URL to copy parameters from
237             * @param routeParameters the parameter map to populate for use by the
238             *        router
239             */
240            protected void buildRouteParameters(
241                    LiferayPortletURL liferayPortletURL,
242                    Map<String, String> routeParameters) {
243    
244                    // Copy application parameters
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                    // Populate virtual parameters for instanceable portlets
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                    // Copy reserved parameters
290    
291                    routeParameters.putAll(liferayPortletURL.getReservedParameterMap());
292            }
293    
294            /**
295             * Returns the portlet ID, including the instance ID if applicable, from the
296             * parameter map.
297             *
298             * @param  routeParameters the parameter map. For an instanceable portlet,
299             *         this must contain either <code>p_p_id</code> or
300             *         <code>instanceId</code>.
301             * @return the portlet ID, including the instance ID if applicable, or
302             *         <code>null</code> if it cannot be determined
303             */
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            /**
333             * Populates the parameter map using the parameters from the router and the
334             * default reserved parameters.
335             *
336             * @param parameterMap the parameter map to populate. This should be the map
337             *        passed to {@link #populateParams(String, Map, Map)} by {@link
338             *        com.liferay.portal.util.PortalImpl}.
339             * @param namespace the namespace to use for parameters added to
340             *        <code>parameterMap</code>
341             * @param routeParameters the parameter map populated by the router
342             */
343            protected void populateParams(
344                    Map<String, String[]> parameterMap, String namespace,
345                    Map<String, String> routeParameters) {
346    
347                    // Copy route parameters
348    
349                    for (Map.Entry<String, String> entry : routeParameters.entrySet()) {
350                            addParameter(
351                                    namespace, parameterMap, entry.getKey(), entry.getValue());
352                    }
353    
354                    // Copy default reserved parameters if they are not already set
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    }