1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.portlet;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.PortalClassInvoker;
28  
29  import java.util.Map;
30  
31  /**
32   * <a href="BaseFriendlyURLMapper.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Jorge Ferrer
35   * @author Brian Wing Shun Chan
36   *
37   */
38  public abstract class BaseFriendlyURLMapper implements FriendlyURLMapper {
39  
40      public abstract String getPortletId();
41  
42      public boolean isCheckMappingWithPrefix() {
43          return _CHECK_MAPPING_WITH_PREFIX;
44      }
45  
46      protected void addParam(
47          Map<String, String[]> params, String name, boolean value) {
48  
49          addParam(params, name, String.valueOf(value));
50      }
51  
52      protected void addParam(
53          Map<String, String[]> params, String name, double value) {
54  
55          addParam(params, name, String.valueOf(value));
56      }
57  
58      protected void addParam(
59          Map<String, String[]> params, String name, int value) {
60  
61          addParam(params, name, String.valueOf(value));
62      }
63  
64      protected void addParam(
65          Map<String, String[]> params, String name, long value) {
66  
67          addParam(params, name, String.valueOf(value));
68      }
69  
70      protected void addParam(
71          Map<String, String[]> params, String name, short value) {
72  
73          addParam(params, name, String.valueOf(value));
74      }
75  
76      protected void addParam(
77          Map<String, String[]> params, String name, Object value) {
78  
79          addParam(params, name, String.valueOf(value));
80      }
81  
82      protected void addParam(
83          Map<String, String[]> params, String name, String value) {
84  
85          try {
86              if (!_isReservedParameter(name)) {
87                  Map<String, String> prpIdentifers =
88                      FriendlyURLMapperThreadLocal.getPRPIdentifiers();
89  
90                  if (prpIdentifers.containsKey(name)) {
91                      name = prpIdentifers.get(name);
92                  }
93                  else {
94                      name = getNamespace() + name;
95                  }
96              }
97  
98              params.put(name, new String[] {value});
99          }
100         catch (Exception e) {
101             _log.error(e, e);
102         }
103     }
104 
105     protected String getNamespace() {
106         try {
107             return _getPortletNamespace(getPortletId());
108         }
109         catch (Exception e) {
110             _log.error(e, e);
111 
112             return getPortletId();
113         }
114     }
115 
116     private String _getPortletNamespace(String portletId)
117         throws Exception {
118 
119         Object returnObj = PortalClassInvoker.invoke(
120             _CLASS, _METHOD_GETPORTLETNAMESPACE, portletId, false);
121 
122         if (returnObj != null) {
123             return (String)returnObj;
124         }
125         else {
126             return null;
127         }
128     }
129 
130     private boolean _isReservedParameter(String name) throws Exception {
131         Object returnObj = PortalClassInvoker.invoke(
132             _CLASS, _METHOD_ISRESERVEDPARAMETER, name, false);
133 
134         if (returnObj != null) {
135             return (Boolean)returnObj;
136         }
137         else {
138             return false;
139         }
140     }
141 
142     private static final boolean _CHECK_MAPPING_WITH_PREFIX = true;
143 
144     private static final String _CLASS = "com.liferay.portal.util.PortalUtil";
145 
146     private static final String _METHOD_GETPORTLETNAMESPACE =
147         "getPortletNamespace";
148 
149     private static final String _METHOD_ISRESERVEDPARAMETER =
150         "isReservedParameter";
151 
152     private static Log _log =
153         LogFactoryUtil.getLog(BaseFriendlyURLMapper.class);
154 
155 }