1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.kernel.portlet;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.PortalClassInvoker;
20  
21  import java.util.Map;
22  
23  /**
24   * <a href="BaseFriendlyURLMapper.java.html"><b><i>View Source</i></b></a>
25   *
26   * @author Jorge Ferrer
27   * @author Brian Wing Shun Chan
28   */
29  public abstract class BaseFriendlyURLMapper implements FriendlyURLMapper {
30  
31      public abstract String getPortletId();
32  
33      public boolean isCheckMappingWithPrefix() {
34          return _CHECK_MAPPING_WITH_PREFIX;
35      }
36  
37      protected void addParam(
38          Map<String, String[]> params, String name, boolean value) {
39  
40          addParam(params, name, String.valueOf(value));
41      }
42  
43      protected void addParam(
44          Map<String, String[]> params, String name, double value) {
45  
46          addParam(params, name, String.valueOf(value));
47      }
48  
49      protected void addParam(
50          Map<String, String[]> params, String name, int value) {
51  
52          addParam(params, name, String.valueOf(value));
53      }
54  
55      protected void addParam(
56          Map<String, String[]> params, String name, long value) {
57  
58          addParam(params, name, String.valueOf(value));
59      }
60  
61      protected void addParam(
62          Map<String, String[]> params, String name, short value) {
63  
64          addParam(params, name, String.valueOf(value));
65      }
66  
67      protected void addParam(
68          Map<String, String[]> params, String name, Object value) {
69  
70          addParam(params, name, String.valueOf(value));
71      }
72  
73      protected void addParam(
74          Map<String, String[]> params, String name, String value) {
75  
76          try {
77              if (!_isReservedParameter(name)) {
78                  Map<String, String> prpIdentifers =
79                      FriendlyURLMapperThreadLocal.getPRPIdentifiers();
80  
81                  if (prpIdentifers.containsKey(name)) {
82                      name = prpIdentifers.get(name);
83                  }
84                  else {
85                      name = getNamespace() + name;
86                  }
87              }
88  
89              params.put(name, new String[] {value});
90          }
91          catch (Exception e) {
92              _log.error(e, e);
93          }
94      }
95  
96      protected String getNamespace() {
97          try {
98              return _getPortletNamespace(getPortletId());
99          }
100         catch (Exception e) {
101             _log.error(e, e);
102 
103             return getPortletId();
104         }
105     }
106 
107     private String _getPortletNamespace(String portletId)
108         throws Exception {
109 
110         Object returnObj = PortalClassInvoker.invoke(
111             _CLASS, _METHOD_GETPORTLETNAMESPACE, portletId, false);
112 
113         if (returnObj != null) {
114             return (String)returnObj;
115         }
116         else {
117             return null;
118         }
119     }
120 
121     private boolean _isReservedParameter(String name) throws Exception {
122         Object returnObj = PortalClassInvoker.invoke(
123             _CLASS, _METHOD_ISRESERVEDPARAMETER, name, false);
124 
125         if (returnObj != null) {
126             return (Boolean)returnObj;
127         }
128         else {
129             return false;
130         }
131     }
132 
133     private static final boolean _CHECK_MAPPING_WITH_PREFIX = true;
134 
135     private static final String _CLASS = "com.liferay.portal.util.PortalUtil";
136 
137     private static final String _METHOD_GETPORTLETNAMESPACE =
138         "getPortletNamespace";
139 
140     private static final String _METHOD_ISRESERVEDPARAMETER =
141         "isReservedParameter";
142 
143     private static Log _log = LogFactoryUtil.getLog(
144         BaseFriendlyURLMapper.class);
145 
146 }