001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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 use {@link #addParameter(Map, String, Object)} instead
077             */
078            protected void addParam(
079                    Map<String, String[]> parameterMap, String name, Object value) {
080    
081                    addParameter(parameterMap, name, value);
082            }
083    
084            /**
085             * @deprecated use {@link #addParameter(String, Map, String, String)}
086             *                         instead
087             */
088            protected void addParam(
089                    Map<String, String[]> parameterMap, String name, String value) {
090    
091                    addParameter(parameterMap, name, value);
092            }
093    
094            /**
095             * Adds a default namespaced parameter of any type to the parameter map.
096             *
097             * <p>
098             * <b>Do not use this method with an instanceable portlet, it will not
099             * properly namespace parameter names.</b>
100             * </p>
101             *
102             * @param parameterMap the parameter map to populate
103             * @param name the name of the parameter
104             * @param value the value of the parameter
105             * @see   #addParameter(Map, String, String)
106             */
107            protected void addParameter(
108                    Map<String, String[]> parameterMap, String name, Object value) {
109    
110                    addParameter(getNamespace(), parameterMap, name, String.valueOf(value));
111            }
112    
113            /**
114             * Adds a default namespaced string parameter to the parameter map.
115             *
116             * <p>
117             * <b>Do not use this method with an instanceable portlet, it will not
118             * properly namespace parameter names.</b>
119             * </p>
120             *
121             * @param parameterMap the parameter map to populate
122             * @param name the name of the parameter
123             * @param value the value of the parameter
124             * @see   #getNamespace()
125             */
126            protected void addParameter(
127                    Map<String, String[]> parameterMap, String name, String value) {
128    
129                    addParameter(getNamespace(), parameterMap, name, value);
130            }
131    
132            /**
133             * Adds a namespaced parameter of any type to the parameter map.
134             *
135             * @param namespace the namespace for portlet parameters. For instanceable
136             *                portlets this must include the instance id.
137             * @param parameterMap the parameter map to populate
138             * @param name space the namespace for portlet parameters. For instanceable
139             *                portlets this must include the instance id.
140             * @param value the value of the parameter
141             * @see   #addParameter(String, Map, String, String)
142             */
143            protected void addParameter(
144                    String namespace, Map<String, String[]> parameterMap, String name,
145                    Object value) {
146    
147                    addParameter(namespace, parameterMap, name, String.valueOf(value));
148            }
149    
150            /**
151             * Adds a namespaced string parameter to the parameter map.
152             *
153             * @param namespace the namespace for portlet parameters. For instanceable
154             *                portlets this must include the instance id.
155             * @param parameterMap the parameter map to populate
156             * @param name space the namespace for portlet parameters. For instanceable
157             *                portlets this must include the instance id.
158             * @param value the value of the parameter
159             * @see   PortalUtil#getPortletNamespace(String)
160             * @see   DefaultFriendlyURLMapper#getPortletId(Map)
161             */
162            protected void addParameter(
163                    String namespace, Map<String, String[]> parameterMap, String name,
164                    String value) {
165    
166                    try {
167                            if (!PortalUtil.isReservedParameter(name)) {
168                                    Map<String, String> prpIdentifers =
169                                            FriendlyURLMapperThreadLocal.getPRPIdentifiers();
170    
171                                    if (prpIdentifers.containsKey(name)) {
172                                            name = prpIdentifers.get(name);
173                                    }
174                                    else {
175                                            name = namespace + name;
176                                    }
177                            }
178    
179                            parameterMap.put(name, new String[] {value});
180                    }
181                    catch (Exception e) {
182                            _log.error(e, e);
183                    }
184            }
185    
186            /**
187             * Gets the default namespace.
188             *
189             * <p>
190             * <b>Do not use this method with an instanceable portlet, it will not
191             * include the instance id.</b>
192             * </p>
193             *
194             * @return the default namespace, not including the instance id
195             * @see    PortalUtil#getPortletNamespace(String)
196             */
197            protected String getNamespace() {
198                    return PortalUtil.getPortletNamespace(getPortletId());
199            }
200    
201            protected Router router;
202    
203            private static final boolean _CHECK_MAPPING_WITH_PREFIX = true;
204    
205            private static Log _log = LogFactoryUtil.getLog(
206                    BaseFriendlyURLMapper.class);
207    
208            private String _mapping;
209            private String _portletId;
210            private boolean _portletInstanceable;
211    
212    }