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