1   /**
2    * Copyright (c) 2000-2008 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                  name = getNamespace() + name;
88              }
89  
90              params.put(name, new String[] {value});
91          }
92          catch (Exception e) {
93              _log.error(e, e);
94          }
95      }
96  
97      protected String getNamespace() {
98          try {
99              return _getPortletNamespace(getPortletId());
100         }
101         catch (Exception e) {
102             _log.error(e, e);
103 
104             return getPortletId();
105         }
106     }
107 
108     private String _getPortletNamespace(String portletId)
109         throws Exception {
110 
111         Object returnObj = PortalClassInvoker.invoke(
112             _CLASS, _METHOD_GETPORTLETNAMESPACE, portletId, false);
113 
114         if (returnObj != null) {
115             return (String)returnObj;
116         }
117         else {
118             return null;
119         }
120     }
121 
122     private boolean _isReservedParameter(String name) throws Exception {
123         Object returnObj = PortalClassInvoker.invoke(
124             _CLASS, _METHOD_ISRESERVEDPARAMETER, name, false);
125 
126         if (returnObj != null) {
127             return (Boolean)returnObj;
128         }
129         else {
130             return false;
131         }
132     }
133 
134     private static final boolean _CHECK_MAPPING_WITH_PREFIX = true;
135 
136     private static final String _CLASS = "com.liferay.portal.util.PortalUtil";
137 
138     private static final String _METHOD_GETPORTLETNAMESPACE =
139         "getPortletNamespace";
140 
141     private static final String _METHOD_ISRESERVEDPARAMETER =
142         "isReservedParameter";
143 
144     private static Log _log =
145         LogFactoryUtil.getLog(BaseFriendlyURLMapper.class);
146 
147 }