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