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