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.PortalUtil; 020 import com.liferay.portal.kernel.util.Validator; 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 * Adds a default namespaced parameter of any type to the parameter map. 087 * 088 * <p> 089 * <b>Do not use this method with an instanceable portlet, it will not 090 * properly namespace parameter names.</b> 091 * </p> 092 * 093 * @param parameterMap the parameter map 094 * @param name the name of the parameter 095 * @param value the value of the parameter 096 * @see #addParameter(Map, String, String) 097 */ 098 protected void addParameter( 099 Map<String, String[]> parameterMap, String name, Object value) { 100 101 addParameter(getNamespace(), parameterMap, name, String.valueOf(value)); 102 } 103 104 /** 105 * Adds a default namespaced string parameter to the parameter map. 106 * 107 * <p> 108 * <b>Do not use this method with an instanceable portlet, it will not 109 * properly namespace parameter names.</b> 110 * </p> 111 * 112 * @param parameterMap the parameter map 113 * @param name the name of the parameter 114 * @param value the value of the parameter 115 * @see #getNamespace() 116 */ 117 protected void addParameter( 118 Map<String, String[]> parameterMap, String name, String value) { 119 120 addParameter(getNamespace(), parameterMap, name, new String[] {value}); 121 } 122 123 /** 124 * Adds a default namespaced string parameter to the parameter map. 125 * 126 * <p> 127 * <b>Do not use this method with an instanceable portlet, it will not 128 * properly namespace parameter names.</b> 129 * </p> 130 * 131 * @param parameterMap the parameter map 132 * @param name the name of the parameter 133 * @param values the values of the parameter 134 * @see #getNamespace() 135 */ 136 protected void addParameter( 137 Map<String, String[]> parameterMap, String name, String[] values) { 138 139 addParameter(getNamespace(), parameterMap, name, values); 140 } 141 142 /** 143 * Adds a namespaced parameter of any type to the parameter map. 144 * 145 * @param namespace the namespace for portlet parameters. For instanceable 146 * portlets this must include the instance ID. 147 * @param parameterMap the parameter map 148 * @param name space the namespace for portlet parameters. For instanceable 149 * portlets this must include the instance ID. 150 * @param value the value of the parameter 151 * @see #addParameter(String, Map, String, String) 152 */ 153 protected void addParameter( 154 String namespace, Map<String, String[]> parameterMap, String name, 155 Object value) { 156 157 addParameter( 158 namespace, parameterMap, name, 159 new String[] {String.valueOf(value)}); 160 } 161 162 /** 163 * Adds a namespaced string parameter to the parameter map. 164 * 165 * @param namespace the namespace for portlet parameters. For instanceable 166 * portlets this must include the instance ID. 167 * @param parameterMap the parameter map 168 * @param name space the namespace for portlet parameters. For instanceable 169 * portlets this must include the instance ID. 170 * @param value the value of the parameter 171 * @see PortalUtil#getPortletNamespace(String) 172 * @see DefaultFriendlyURLMapper#getPortletId(Map) 173 */ 174 protected void addParameter( 175 String namespace, Map<String, String[]> parameterMap, String name, 176 String value) { 177 178 addParameter(namespace, parameterMap, name, new String[] {value}); 179 } 180 181 /** 182 * Adds a namespaced string parameter to the parameter map. 183 * 184 * @param namespace the namespace for portlet parameters. For instanceable 185 * portlets this must include the instance ID. 186 * @param parameterMap the parameter map 187 * @param name space the namespace for portlet parameters. For instanceable 188 * portlets this must include the instance ID. 189 * @param values the values of the parameter 190 * @see PortalUtil#getPortletNamespace(String) 191 * @see DefaultFriendlyURLMapper#getPortletId(Map) 192 */ 193 protected void addParameter( 194 String namespace, Map<String, String[]> parameterMap, String name, 195 String[] values) { 196 197 try { 198 if (!PortalUtil.isReservedParameter(name)) { 199 Map<String, String> prpIdentifers = 200 FriendlyURLMapperThreadLocal.getPRPIdentifiers(); 201 202 String identiferValue = prpIdentifers.get(name); 203 204 if (identiferValue != null) { 205 name = identiferValue; 206 } 207 else if (Validator.isNotNull(namespace)) { 208 name = namespace.concat(name); 209 } 210 } 211 212 parameterMap.put(name, values); 213 } 214 catch (Exception e) { 215 _log.error(e, e); 216 } 217 } 218 219 /** 220 * Returns the default namespace. 221 * 222 * <p> 223 * <b>Do not use this method with an instanceable portlet, it will not 224 * include the instance ID.</b> 225 * </p> 226 * 227 * @return the default namespace, not including the instance ID 228 * @see PortalUtil#getPortletNamespace(String) 229 */ 230 protected String getNamespace() { 231 return PortalUtil.getPortletNamespace(getPortletId()); 232 } 233 234 protected Router router; 235 236 private static final boolean _CHECK_MAPPING_WITH_PREFIX = true; 237 238 private static final Log _log = LogFactoryUtil.getLog( 239 BaseFriendlyURLMapper.class); 240 241 private String _mapping; 242 private String _portletId; 243 private boolean _portletInstanceable; 244 245 }