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