001    /**
002     * Copyright (c) 2000-2010 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 to hide
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 to hide
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 (friendlyURLPath == null) {
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             * Gets 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             * Gets 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 (portletId == null) {
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             * Builds the parameter map to be used by the router by copying parameters
174             * from the portlet URL.
175             *
176             * <p>
177             * This method also populates the special virtual parameters
178             * <code>p_p_id</code> and <code>instanceId</code> for instanceable
179             * portlets.
180             * </p>
181             *
182             * @param liferayPortletURL the portlet URL to copy parameters from
183             * @param routeParameters the parameter map to populate for use by the
184             *                router
185             */
186            protected void buildRouteParameters(
187                    LiferayPortletURL liferayPortletURL,
188                    Map<String, String> routeParameters) {
189    
190                    // Copy application parameters
191    
192                    Map<String, String[]> portletURLParameters =
193                            liferayPortletURL.getParameterMap();
194    
195                    for (Map.Entry<String, String[]> entry :
196                                    portletURLParameters.entrySet()) {
197    
198                            String[] values = entry.getValue();
199    
200                            if (values.length > 0) {
201                                    routeParameters.put(entry.getKey(), values[0]);
202                            }
203                    }
204    
205                    // Populate virtual parameters for instanceable portlets
206    
207                    if (isPortletInstanceable()) {
208                            String portletId = liferayPortletURL.getPortletId();
209    
210                            routeParameters.put("p_p_id", portletId);
211    
212                            if (Validator.isNotNull(portletId)) {
213                                    String[] parts = portletId.split(
214                                            PortletConstants.INSTANCE_SEPARATOR);
215    
216                                    if (parts.length > 1) {
217                                            routeParameters.put("instanceId", parts[1]);
218                                    }
219                            }
220                    }
221    
222                    // Copy reserved parameters
223    
224                    routeParameters.putAll(liferayPortletURL.getReservedParameterMap());
225            }
226    
227            /**
228             * Gets the portlet id, including the instance id if applicable, from the
229             * parameter map.
230             *
231             * @param  routeParameters the parameter map to get the portlet id from. For
232             *                 an instanceable portlet, this must contain either
233             *                 <code>p_p_id</code> or <code>instanceId</code>.
234             * @return the portlet id, including the instance id if applicable, or
235             *                 <code>null</code> if it cannot be determined
236             */
237            protected String getPortletId(Map<String, String> routeParameters) {
238                    if (!isPortletInstanceable()) {
239                            return getPortletId();
240                    }
241    
242                    String portletId = routeParameters.remove("p_p_id");
243    
244                    if (Validator.isNotNull(portletId)) {
245                            return portletId;
246                    }
247    
248                    String instanceId = routeParameters.remove("instanceId");
249    
250                    if (Validator.isNull(instanceId)) {
251                            if (_log.isErrorEnabled()) {
252                                    _log.error(
253                                            "Either p_p_id or instanceId must be provided for an " +
254                                                    "instanceable portlet");
255                            }
256    
257                            return null;
258                    }
259                    else {
260                            return getPortletId().concat(
261                                    PortletConstants.INSTANCE_SEPARATOR).concat(instanceId);
262                    }
263            }
264    
265            /**
266             * Populates the parameter map using the parameters from the router and the
267             * default reserved parameters.
268             *
269             * @param parameterMap the parameter map to populate. This should be the map
270             *                passed to {@link #populateParams(String, Map, Map)} by {@link
271             *                com.liferay.portal.util.PortalImpl}.
272             * @param namespace the namespace to use for parameters added to
273             *                <code>parameterMap</code>
274             * @param routeParameters the parameter map populated by the router
275             */
276            protected void populateParams(
277                    Map<String, String[]> parameterMap, String namespace,
278                    Map<String, String> routeParameters) {
279    
280                    // Copy route parameters
281    
282                    for (Map.Entry<String, String> entry : routeParameters.entrySet()) {
283                            addParameter(
284                                    namespace, parameterMap, entry.getKey(), entry.getValue());
285                    }
286    
287                    // Copy default reserved parameters if they are not already set
288    
289                    for (Map.Entry<String, String> entry :
290                                    defaultReservedParameters.entrySet()) {
291    
292                            String key = entry.getKey();
293    
294                            if (!parameterMap.containsKey(key)) {
295                                    addParameter(namespace, parameterMap, key, entry.getValue());
296                            }
297                    }
298            }
299    
300            /**
301             * Adds the parameters included in the path to the portlet URL.
302             *
303             * <p>
304             * Portlet URLs track which parameters are included in the friendly URL
305             * path. This method hides all the default ignored parameters, the
306             * parameters included in the path by the router, and the reserved
307             * parameters set to their defaults.
308             * </p>
309             *
310             * @param liferayPortletURL the portlet URL to add the parameters included
311             *                in the path to
312             * @param routeParameters the parameter map populated by the router
313             * @see   com.liferay.portlet.PortletURLImpl#addParameterIncludedInPath(
314             *                String)
315             */
316            protected void addParametersIncludedInPath(
317                    LiferayPortletURL liferayPortletURL,
318                    Map<String, String> routeParameters) {
319    
320                    // Hide default ignored parameters
321    
322                    for (String name : defaultIgnoredParameters) {
323                            liferayPortletURL.addParameterIncludedInPath(name);
324                    }
325    
326                    // Hide application parameters removed by the router
327    
328                    Map<String, String[]> portletURLParameters =
329                            liferayPortletURL.getParameterMap();
330    
331                    for (String name : portletURLParameters.keySet()) {
332                            if (!routeParameters.containsKey(name)) {
333                                    liferayPortletURL.addParameterIncludedInPath(name);
334                            }
335                    }
336    
337                    // Hide reserved parameters removed by the router or set to the defaults
338    
339                    Map<String, String> reservedParameters =
340                            liferayPortletURL.getReservedParameterMap();
341    
342                    for (Map.Entry<String, String> entry : reservedParameters.entrySet()) {
343                            String key = entry.getKey();
344                            String value = entry.getValue();
345    
346                            if (!routeParameters.containsKey(key) ||
347                                    value.equals(defaultReservedParameters.get(key))) {
348    
349                                    liferayPortletURL.addParameterIncludedInPath(key);
350                            }
351                    }
352            }
353    
354            protected Set<String> defaultIgnoredParameters;
355            protected Map<String, String> defaultReservedParameters;
356    
357            private static Log _log = LogFactoryUtil.getLog(
358                    DefaultFriendlyURLMapper.class);
359    
360    }