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.util.PortalUtil;
020    
021    import java.util.Map;
022    
023    /**
024     * The base implementation of {@link FriendlyURLMapper}.
025     *
026     * <p>
027     * Typically not subclassed directly. {@link DefaultFriendlyURLMapper} and a
028     * <code>friendly-url-routes.xml</code> file will handle the needs of most
029     * portlets.
030     * </p>
031     *
032     * @author Jorge Ferrer
033     * @author Brian Wing Shun Chan
034     * @author Connor McKay
035     * @see    DefaultFriendlyURLMapper
036     */
037    public abstract class BaseFriendlyURLMapper implements FriendlyURLMapper {
038    
039            @Override
040            public String getMapping() {
041                    return _mapping;
042            }
043    
044            @Override
045            public String getPortletId() {
046                    return _portletId;
047            }
048    
049            @Override
050            public Router getRouter() {
051                    return router;
052            }
053    
054            @Override
055            public boolean isCheckMappingWithPrefix() {
056                    return _CHECK_MAPPING_WITH_PREFIX;
057            }
058    
059            @Override
060            public boolean isPortletInstanceable() {
061                    return _portletInstanceable;
062            }
063    
064            @Override
065            public void setMapping(String mapping) {
066                    _mapping = mapping;
067            }
068    
069            @Override
070            public void setPortletId(String portletId) {
071                    _portletId = portletId;
072            }
073    
074            @Override
075            public void setPortletInstanceable(boolean portletInstanceable) {
076                    _portletInstanceable = portletInstanceable;
077            }
078    
079            @Override
080            public void setRouter(Router router) {
081                    this.router = router;
082            }
083    
084            /**
085             * @deprecated As of 6.2.0, replaced by {@link #addParameter(Map, String,
086             *             Object)}
087             */
088            protected void addParam(
089                    Map<String, String[]> parameterMap, String name, Object value) {
090    
091                    addParameter(parameterMap, name, value);
092            }
093    
094            /**
095             * @deprecated As of 6.2.0, replaced by {@link #addParameter(String, Map,
096             *             String, String)}
097             */
098            protected void addParam(
099                    Map<String, String[]> parameterMap, String name, String value) {
100    
101                    addParameter(parameterMap, name, value);
102            }
103    
104            /**
105             * Adds a default namespaced parameter of any type to the parameter map.
106             *
107             * <p>
108             * <b>Do not use this method with an instanceable portlet, it will not
109             * properly namespace parameter names.</b>
110             * </p>
111             *
112             * @param parameterMap the parameter map
113             * @param name the name of the parameter
114             * @param value the value of the parameter
115             * @see   #addParameter(Map, String, String)
116             */
117            protected void addParameter(
118                    Map<String, String[]> parameterMap, String name, Object value) {
119    
120                    addParameter(getNamespace(), parameterMap, name, String.valueOf(value));
121            }
122    
123            /**
124             * Adds a default namespaced string parameter to the parameter map.
125             *
126             * <p>
127             * <b>Do not use this method with an instanceable portlet, it will not
128             * properly namespace parameter names.</b>
129             * </p>
130             *
131             * @param parameterMap the parameter map
132             * @param name the name of the parameter
133             * @param value the value of the parameter
134             * @see   #getNamespace()
135             */
136            protected void addParameter(
137                    Map<String, String[]> parameterMap, String name, String value) {
138    
139                    addParameter(getNamespace(), parameterMap, name, new String[] {value});
140            }
141    
142            /**
143             * Adds a default namespaced string parameter to the parameter map.
144             *
145             * <p>
146             * <b>Do not use this method with an instanceable portlet, it will not
147             * properly namespace parameter names.</b>
148             * </p>
149             *
150             * @param parameterMap the parameter map
151             * @param name the name of the parameter
152             * @param values the values of the parameter
153             * @see   #getNamespace()
154             */
155            protected void addParameter(
156                    Map<String, String[]> parameterMap, String name, String[] values) {
157    
158                    addParameter(getNamespace(), parameterMap, name, values);
159            }
160    
161            /**
162             * Adds a namespaced parameter of any type to the parameter map.
163             *
164             * @param namespace the namespace for portlet parameters. For instanceable
165             *        portlets this must include the instance ID.
166             * @param parameterMap the parameter map
167             * @param name space the namespace for portlet parameters. For instanceable
168             *        portlets this must include the instance ID.
169             * @param value the value of the parameter
170             * @see   #addParameter(String, Map, String, String)
171             */
172            protected void addParameter(
173                    String namespace, Map<String, String[]> parameterMap, String name,
174                    Object value) {
175    
176                    addParameter(
177                            namespace, parameterMap, name,
178                            new String[] {String.valueOf(value)});
179            }
180    
181            /**
182             * Adds a namespaced string parameter to the parameter map.
183             *
184             * @param namespace the namespace for portlet parameters. For instanceable
185             *        portlets this must include the instance ID.
186             * @param parameterMap the parameter map
187             * @param name space the namespace for portlet parameters. For instanceable
188             *        portlets this must include the instance ID.
189             * @param value the value of the parameter
190             * @see   PortalUtil#getPortletNamespace(String)
191             * @see   DefaultFriendlyURLMapper#getPortletId(Map)
192             */
193            protected void addParameter(
194                    String namespace, Map<String, String[]> parameterMap, String name,
195                    String value) {
196    
197                    addParameter(namespace, parameterMap, name, new String[] {value});
198            }
199    
200            /**
201             * Adds a namespaced string parameter to the parameter map.
202             *
203             * @param namespace the namespace for portlet parameters. For instanceable
204             *        portlets this must include the instance ID.
205             * @param parameterMap the parameter map
206             * @param name space the namespace for portlet parameters. For instanceable
207             *        portlets this must include the instance ID.
208             * @param values the values of the parameter
209             * @see   PortalUtil#getPortletNamespace(String)
210             * @see   DefaultFriendlyURLMapper#getPortletId(Map)
211             */
212            protected void addParameter(
213                    String namespace, Map<String, String[]> parameterMap, String name,
214                    String[] values) {
215    
216                    try {
217                            if (!PortalUtil.isReservedParameter(name)) {
218                                    Map<String, String> prpIdentifers =
219                                            FriendlyURLMapperThreadLocal.getPRPIdentifiers();
220    
221                                    String identiferValue = prpIdentifers.get(name);
222    
223                                    if (identiferValue != null) {
224                                            name = identiferValue;
225                                    }
226                                    else {
227                                            name = namespace.concat(name);
228                                    }
229                            }
230    
231                            parameterMap.put(name, values);
232                    }
233                    catch (Exception e) {
234                            _log.error(e, e);
235                    }
236            }
237    
238            /**
239             * Returns the default namespace.
240             *
241             * <p>
242             * <b>Do not use this method with an instanceable portlet, it will not
243             * include the instance ID.</b>
244             * </p>
245             *
246             * @return the default namespace, not including the instance ID
247             * @see    PortalUtil#getPortletNamespace(String)
248             */
249            protected String getNamespace() {
250                    return PortalUtil.getPortletNamespace(getPortletId());
251            }
252    
253            protected Router router;
254    
255            private static final boolean _CHECK_MAPPING_WITH_PREFIX = true;
256    
257            private static Log _log = LogFactoryUtil.getLog(
258                    BaseFriendlyURLMapper.class);
259    
260            private String _mapping;
261            private String _portletId;
262            private boolean _portletInstanceable;
263    
264    }