001 /** 002 * Copyright (c) 2000-present 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 @Override 040 public String getMapping() { 041 return _mapping; 042 } 043 044 @Override 045 public String getPortletId() { 046 return _portletId; 047 } 048 049 @Override 050 public Router getRouter() { 051 return router; 052 } 053 054 @Override 055 public boolean isCheckMappingWithPrefix() { 056 return _CHECK_MAPPING_WITH_PREFIX; 057 } 058 059 @Override 060 public boolean isPortletInstanceable() { 061 return _portletInstanceable; 062 } 063 064 @Override 065 public void setMapping(String mapping) { 066 _mapping = mapping; 067 } 068 069 @Override 070 public void setPortletId(String portletId) { 071 _portletId = portletId; 072 } 073 074 @Override 075 public void setPortletInstanceable(boolean portletInstanceable) { 076 _portletInstanceable = portletInstanceable; 077 } 078 079 @Override 080 public void setRouter(Router router) { 081 this.router = router; 082 } 083 084 /** 085 * @deprecated As of 6.2.0, replaced by {@link #addParameter(Map, String, 086 * Object)} 087 */ 088 @Deprecated 089 protected void addParam( 090 Map<String, String[]> parameterMap, String name, Object value) { 091 092 addParameter(parameterMap, name, value); 093 } 094 095 /** 096 * @deprecated As of 6.2.0, replaced by {@link #addParameter(String, Map, 097 * String, String)} 098 */ 099 @Deprecated 100 protected void addParam( 101 Map<String, String[]> parameterMap, String name, String value) { 102 103 addParameter(parameterMap, name, value); 104 } 105 106 /** 107 * Adds a default namespaced parameter of any type to the parameter map. 108 * 109 * <p> 110 * <b>Do not use this method with an instanceable portlet, it will not 111 * properly namespace parameter names.</b> 112 * </p> 113 * 114 * @param parameterMap the parameter map 115 * @param name the name of the parameter 116 * @param value the value of the parameter 117 * @see #addParameter(Map, String, String) 118 */ 119 protected void addParameter( 120 Map<String, String[]> parameterMap, String name, Object value) { 121 122 addParameter(getNamespace(), parameterMap, name, String.valueOf(value)); 123 } 124 125 /** 126 * Adds a default namespaced string parameter to the parameter map. 127 * 128 * <p> 129 * <b>Do not use this method with an instanceable portlet, it will not 130 * properly namespace parameter names.</b> 131 * </p> 132 * 133 * @param parameterMap the parameter map 134 * @param name the name of the parameter 135 * @param value the value of the parameter 136 * @see #getNamespace() 137 */ 138 protected void addParameter( 139 Map<String, String[]> parameterMap, String name, String value) { 140 141 addParameter(getNamespace(), parameterMap, name, new String[] {value}); 142 } 143 144 /** 145 * Adds a default namespaced string parameter to the parameter map. 146 * 147 * <p> 148 * <b>Do not use this method with an instanceable portlet, it will not 149 * properly namespace parameter names.</b> 150 * </p> 151 * 152 * @param parameterMap the parameter map 153 * @param name the name of the parameter 154 * @param values the values of the parameter 155 * @see #getNamespace() 156 */ 157 protected void addParameter( 158 Map<String, String[]> parameterMap, String name, String[] values) { 159 160 addParameter(getNamespace(), parameterMap, name, values); 161 } 162 163 /** 164 * Adds a namespaced parameter of any type to the parameter map. 165 * 166 * @param namespace the namespace for portlet parameters. For instanceable 167 * portlets this must include the instance ID. 168 * @param parameterMap the parameter map 169 * @param name space the namespace for portlet parameters. For instanceable 170 * portlets this must include the instance ID. 171 * @param value the value of the parameter 172 * @see #addParameter(String, Map, String, String) 173 */ 174 protected void addParameter( 175 String namespace, Map<String, String[]> parameterMap, String name, 176 Object value) { 177 178 addParameter( 179 namespace, parameterMap, name, 180 new String[] {String.valueOf(value)}); 181 } 182 183 /** 184 * Adds a namespaced string parameter to the parameter map. 185 * 186 * @param namespace the namespace for portlet parameters. For instanceable 187 * portlets this must include the instance ID. 188 * @param parameterMap the parameter map 189 * @param name space the namespace for portlet parameters. For instanceable 190 * portlets this must include the instance ID. 191 * @param value the value of the parameter 192 * @see PortalUtil#getPortletNamespace(String) 193 * @see DefaultFriendlyURLMapper#getPortletId(Map) 194 */ 195 protected void addParameter( 196 String namespace, Map<String, String[]> parameterMap, String name, 197 String value) { 198 199 addParameter(namespace, parameterMap, name, new String[] {value}); 200 } 201 202 /** 203 * Adds a namespaced string parameter to the parameter map. 204 * 205 * @param namespace the namespace for portlet parameters. For instanceable 206 * portlets this must include the instance ID. 207 * @param parameterMap the parameter map 208 * @param name space the namespace for portlet parameters. For instanceable 209 * portlets this must include the instance ID. 210 * @param values the values of the parameter 211 * @see PortalUtil#getPortletNamespace(String) 212 * @see DefaultFriendlyURLMapper#getPortletId(Map) 213 */ 214 protected void addParameter( 215 String namespace, Map<String, String[]> parameterMap, String name, 216 String[] values) { 217 218 try { 219 if (!PortalUtil.isReservedParameter(name)) { 220 Map<String, String> prpIdentifers = 221 FriendlyURLMapperThreadLocal.getPRPIdentifiers(); 222 223 String identiferValue = prpIdentifers.get(name); 224 225 if (identiferValue != null) { 226 name = identiferValue; 227 } 228 else { 229 name = namespace.concat(name); 230 } 231 } 232 233 parameterMap.put(name, values); 234 } 235 catch (Exception e) { 236 _log.error(e, e); 237 } 238 } 239 240 /** 241 * Returns the default namespace. 242 * 243 * <p> 244 * <b>Do not use this method with an instanceable portlet, it will not 245 * include the instance ID.</b> 246 * </p> 247 * 248 * @return the default namespace, not including the instance ID 249 * @see PortalUtil#getPortletNamespace(String) 250 */ 251 protected String getNamespace() { 252 return PortalUtil.getPortletNamespace(getPortletId()); 253 } 254 255 protected Router router; 256 257 private static final boolean _CHECK_MAPPING_WITH_PREFIX = true; 258 259 private static final Log _log = LogFactoryUtil.getLog( 260 BaseFriendlyURLMapper.class); 261 262 private String _mapping; 263 private String _portletId; 264 private boolean _portletInstanceable; 265 266 }