1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public abstract class BaseFriendlyURLMapper implements FriendlyURLMapper {
38  
39      public abstract String getPortletId();
40  
41      public boolean isCheckMappingWithPrefix() {
42          return _CHECK_MAPPING_WITH_PREFIX;
43      }
44  
45      protected void addParam(
46          Map<String, String[]> params, String name, boolean value) {
47  
48          addParam(params, name, String.valueOf(value));
49      }
50  
51      protected void addParam(
52          Map<String, String[]> params, String name, double value) {
53  
54          addParam(params, name, String.valueOf(value));
55      }
56  
57      protected void addParam(
58          Map<String, String[]> params, String name, int value) {
59  
60          addParam(params, name, String.valueOf(value));
61      }
62  
63      protected void addParam(
64          Map<String, String[]> params, String name, long value) {
65  
66          addParam(params, name, String.valueOf(value));
67      }
68  
69      protected void addParam(
70          Map<String, String[]> params, String name, short value) {
71  
72          addParam(params, name, String.valueOf(value));
73      }
74  
75      protected void addParam(
76          Map<String, String[]> params, String name, Object value) {
77  
78          addParam(params, name, String.valueOf(value));
79      }
80  
81      protected void addParam(
82          Map<String, String[]> params, String name, String value) {
83  
84          try {
85              if (!_isReservedParameter(name)) {
86                  Map<String, String> prpIdentifers =
87                      FriendlyURLMapperThreadLocal.getPRPIdentifiers();
88  
89                  if (prpIdentifers.containsKey(name)) {
90                      name = prpIdentifers.get(name);
91                  }
92                  else {
93                      name = getNamespace() + name;
94                  }
95              }
96  
97              params.put(name, new String[] {value});
98          }
99          catch (Exception e) {
100             _log.error(e, e);
101         }
102     }
103 
104     protected String getNamespace() {
105         try {
106             return _getPortletNamespace(getPortletId());
107         }
108         catch (Exception e) {
109             _log.error(e, e);
110 
111             return getPortletId();
112         }
113     }
114 
115     private String _getPortletNamespace(String portletId)
116         throws Exception {
117 
118         Object returnObj = PortalClassInvoker.invoke(
119             _CLASS, _METHOD_GETPORTLETNAMESPACE, portletId, false);
120 
121         if (returnObj != null) {
122             return (String)returnObj;
123         }
124         else {
125             return null;
126         }
127     }
128 
129     private boolean _isReservedParameter(String name) throws Exception {
130         Object returnObj = PortalClassInvoker.invoke(
131             _CLASS, _METHOD_ISRESERVEDPARAMETER, name, false);
132 
133         if (returnObj != null) {
134             return (Boolean)returnObj;
135         }
136         else {
137             return false;
138         }
139     }
140 
141     private static final boolean _CHECK_MAPPING_WITH_PREFIX = true;
142 
143     private static final String _CLASS = "com.liferay.portal.util.PortalUtil";
144 
145     private static final String _METHOD_GETPORTLETNAMESPACE =
146         "getPortletNamespace";
147 
148     private static final String _METHOD_ISRESERVEDPARAMETER =
149         "isReservedParameter";
150 
151     private static Log _log =
152         LogFactoryUtil.getLog(BaseFriendlyURLMapper.class);
153 
154 }