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