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      protected String getNamespace() {
43          try {
44              return _getPortletNamespace(getPortletId());
45          }
46          catch (Exception e) {
47              _log.error(e, e);
48  
49              return getPortletId();
50          }
51      }
52  
53      protected void addParam(
54          Map<String, String[]> params, String name, boolean value) {
55  
56          addParam(params, name, String.valueOf(value));
57      }
58  
59      protected void addParam(
60          Map<String, String[]> params, String name, double value) {
61  
62          addParam(params, name, String.valueOf(value));
63      }
64  
65      protected void addParam(
66          Map<String, String[]> params, String name, int value) {
67  
68          addParam(params, name, String.valueOf(value));
69      }
70  
71      protected void addParam(
72          Map<String, String[]> params, String name, long value) {
73  
74          addParam(params, name, String.valueOf(value));
75      }
76  
77      protected void addParam(
78          Map<String, String[]> params, String name, short value) {
79  
80          addParam(params, name, String.valueOf(value));
81      }
82  
83      protected void addParam(
84          Map<String, String[]> params, String name, Object value) {
85  
86          addParam(params, name, String.valueOf(value));
87      }
88  
89      protected void addParam(
90          Map<String, String[]> params, String name, String value) {
91  
92          try {
93              if (!_isReservedParameter(name)) {
94                  name = getNamespace() + name;
95              }
96  
97              params.put(name, new String[] {value});
98          }
99          catch (Exception e) {
100             _log.error(e, e);
101         }
102     }
103 
104     private String _getPortletNamespace(String portletId)
105         throws Exception {
106 
107         Object returnObj = PortalClassInvoker.invoke(
108             _CLASS, _METHOD_GETPORTLETNAMESPACE, portletId, false);
109 
110         if (returnObj != null) {
111             return (String)returnObj;
112         }
113         else {
114             return null;
115         }
116     }
117 
118     private boolean _isReservedParameter(String name) throws Exception {
119         Object returnObj = PortalClassInvoker.invoke(
120             _CLASS, _METHOD_ISRESERVEDPARAMETER, name, false);
121 
122         if (returnObj != null) {
123             return (Boolean)returnObj;
124         }
125         else {
126             return false;
127         }
128     }
129 
130     private static final String _CLASS = "com.liferay.portal.util.PortalUtil";
131 
132     private static final String _METHOD_GETPORTLETNAMESPACE =
133         "getPortletNamespace";
134 
135     private static final String _METHOD_ISRESERVEDPARAMETER =
136         "isReservedParameter";
137 
138     private static Log _log =
139         LogFactoryUtil.getLog(BaseFriendlyURLMapper.class);
140 
141 }