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