001 /** 002 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved. 003 * 004 * This library is free software; you can redistribute it and/or modify it under 005 * the terms of the GNU Lesser General Public License as published by the Free 006 * Software Foundation; either version 2.1 of the License, or (at your option) 007 * any later version. 008 * 009 * This library is distributed in the hope that it will be useful, but WITHOUT 010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 011 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 012 * details. 013 */ 014 015 package com.liferay.portal.kernel.portlet; 016 017 import com.liferay.portal.kernel.log.Log; 018 import com.liferay.portal.kernel.log.LogFactoryUtil; 019 import com.liferay.portal.util.PortalUtil; 020 021 import java.util.Map; 022 023 /** 024 * The base implementation of {@link FriendlyURLMapper}. 025 * 026 * <p> 027 * Typically not subclassed directly. {@link DefaultFriendlyURLMapper} and a 028 * <code>friendly-url-routes.xml</code> file will handle the needs of most 029 * portlets. 030 * </p> 031 * 032 * @author Jorge Ferrer 033 * @author Brian Wing Shun Chan 034 * @author Connor McKay 035 * @see DefaultFriendlyURLMapper 036 */ 037 public abstract class BaseFriendlyURLMapper implements FriendlyURLMapper { 038 039 public String getMapping() { 040 return _mapping; 041 } 042 043 public String getPortletId() { 044 return _portletId; 045 } 046 047 public Router getRouter() { 048 return router; 049 } 050 051 public boolean isCheckMappingWithPrefix() { 052 return _CHECK_MAPPING_WITH_PREFIX; 053 } 054 055 public boolean isPortletInstanceable() { 056 return _portletInstanceable; 057 } 058 059 public void setMapping(String mapping) { 060 _mapping = mapping; 061 } 062 063 public void setPortletId(String portletId) { 064 _portletId = portletId; 065 } 066 067 public void setPortletInstanceable(boolean portletInstanceable) { 068 _portletInstanceable = portletInstanceable; 069 } 070 071 public void setRouter(Router router) { 072 this.router = router; 073 } 074 075 /** 076 * @deprecated use {@link #addParameter(Map, String, Object)} instead 077 */ 078 protected void addParam( 079 Map<String, String[]> parameterMap, String name, Object value) { 080 081 addParameter(parameterMap, name, value); 082 } 083 084 /** 085 * @deprecated use {@link #addParameter(String, Map, String, String)} 086 * instead 087 */ 088 protected void addParam( 089 Map<String, String[]> parameterMap, String name, String value) { 090 091 addParameter(parameterMap, name, value); 092 } 093 094 /** 095 * Adds a default namespaced parameter of any type to the parameter map. 096 * 097 * <p> 098 * <b>Do not use this method with an instanceable portlet, it will not 099 * properly namespace parameter names.</b> 100 * </p> 101 * 102 * @param parameterMap the parameter map 103 * @param name the name of the parameter 104 * @param value the value of the parameter 105 * @see #addParameter(Map, String, String) 106 */ 107 protected void addParameter( 108 Map<String, String[]> parameterMap, String name, Object value) { 109 110 addParameter(getNamespace(), parameterMap, name, String.valueOf(value)); 111 } 112 113 /** 114 * Adds a default namespaced string parameter to the parameter map. 115 * 116 * <p> 117 * <b>Do not use this method with an instanceable portlet, it will not 118 * properly namespace parameter names.</b> 119 * </p> 120 * 121 * @param parameterMap the parameter map 122 * @param name the name of the parameter 123 * @param value the value of the parameter 124 * @see #getNamespace() 125 */ 126 protected void addParameter( 127 Map<String, String[]> parameterMap, String name, String value) { 128 129 addParameter(getNamespace(), parameterMap, name, new String[] {value}); 130 } 131 132 /** 133 * Adds a default namespaced string parameter to the parameter map. 134 * 135 * <p> 136 * <b>Do not use this method with an instanceable portlet, it will not 137 * properly namespace parameter names.</b> 138 * </p> 139 * 140 * @param parameterMap the parameter map 141 * @param name the name of the parameter 142 * @param values the values of the parameter 143 * @see #getNamespace() 144 */ 145 protected void addParameter( 146 Map<String, String[]> parameterMap, String name, String[] values) { 147 148 addParameter(getNamespace(), parameterMap, name, values); 149 } 150 151 /** 152 * Adds a namespaced parameter of any type to the parameter map. 153 * 154 * @param namespace the namespace for portlet parameters. For instanceable 155 * portlets this must include the instance ID. 156 * @param parameterMap the parameter map 157 * @param name space the namespace for portlet parameters. For instanceable 158 * portlets this must include the instance ID. 159 * @param value the value of the parameter 160 * @see #addParameter(String, Map, String, String) 161 */ 162 protected void addParameter( 163 String namespace, Map<String, String[]> parameterMap, String name, 164 Object value) { 165 166 addParameter( 167 namespace, parameterMap, name, 168 new String[] {String.valueOf(value)}); 169 } 170 171 /** 172 * Adds a namespaced string parameter to the parameter map. 173 * 174 * @param namespace the namespace for portlet parameters. For instanceable 175 * portlets this must include the instance ID. 176 * @param parameterMap the parameter map 177 * @param name space the namespace for portlet parameters. For instanceable 178 * portlets this must include the instance ID. 179 * @param value the value of the parameter 180 * @see PortalUtil#getPortletNamespace(String) 181 * @see DefaultFriendlyURLMapper#getPortletId(Map) 182 */ 183 protected void addParameter( 184 String namespace, Map<String, String[]> parameterMap, String name, 185 String value) { 186 187 addParameter(namespace, parameterMap, name, new String[] {value}); 188 } 189 190 /** 191 * Adds a namespaced string parameter to the parameter map. 192 * 193 * @param namespace the namespace for portlet parameters. For instanceable 194 * portlets this must include the instance ID. 195 * @param parameterMap the parameter map 196 * @param name space the namespace for portlet parameters. For instanceable 197 * portlets this must include the instance ID. 198 * @param values the values of the parameter 199 * @see PortalUtil#getPortletNamespace(String) 200 * @see DefaultFriendlyURLMapper#getPortletId(Map) 201 */ 202 protected void addParameter( 203 String namespace, Map<String, String[]> parameterMap, String name, 204 String[] values) { 205 206 try { 207 if (!PortalUtil.isReservedParameter(name)) { 208 Map<String, String> prpIdentifers = 209 FriendlyURLMapperThreadLocal.getPRPIdentifiers(); 210 211 String identiferValue = prpIdentifers.get(name); 212 213 if (identiferValue != null) { 214 name = identiferValue; 215 } 216 else { 217 name = namespace.concat(name); 218 } 219 } 220 221 parameterMap.put(name, values); 222 } 223 catch (Exception e) { 224 _log.error(e, e); 225 } 226 } 227 228 /** 229 * Returns the default namespace. 230 * 231 * <p> 232 * <b>Do not use this method with an instanceable portlet, it will not 233 * include the instance ID.</b> 234 * </p> 235 * 236 * @return the default namespace, not including the instance ID 237 * @see PortalUtil#getPortletNamespace(String) 238 */ 239 protected String getNamespace() { 240 return PortalUtil.getPortletNamespace(getPortletId()); 241 } 242 243 protected Router router; 244 245 private static final boolean _CHECK_MAPPING_WITH_PREFIX = true; 246 247 private static Log _log = LogFactoryUtil.getLog( 248 BaseFriendlyURLMapper.class); 249 250 private String _mapping; 251 private String _portletId; 252 private boolean _portletInstanceable; 253 254 }