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, value); 130 } 131 132 /** 133 * Adds a namespaced parameter of any type to the parameter map. 134 * 135 * @param namespace the namespace for portlet parameters. For instanceable 136 * portlets this must include the instance ID. 137 * @param parameterMap the parameter map 138 * @param name space the namespace for portlet parameters. For instanceable 139 * portlets this must include the instance ID. 140 * @param value the value of the parameter 141 * @see #addParameter(String, Map, String, String) 142 */ 143 protected void addParameter( 144 String namespace, Map<String, String[]> parameterMap, String name, 145 Object value) { 146 147 addParameter(namespace, parameterMap, name, String.valueOf(value)); 148 } 149 150 /** 151 * Adds a namespaced string parameter to the parameter map. 152 * 153 * @param namespace the namespace for portlet parameters. For instanceable 154 * portlets this must include the instance ID. 155 * @param parameterMap the parameter map 156 * @param name space the namespace for portlet parameters. For instanceable 157 * portlets this must include the instance ID. 158 * @param value the value of the parameter 159 * @see PortalUtil#getPortletNamespace(String) 160 * @see DefaultFriendlyURLMapper#getPortletId(Map) 161 */ 162 protected void addParameter( 163 String namespace, Map<String, String[]> parameterMap, String name, 164 String value) { 165 166 try { 167 if (!PortalUtil.isReservedParameter(name)) { 168 Map<String, String> prpIdentifers = 169 FriendlyURLMapperThreadLocal.getPRPIdentifiers(); 170 171 String identiferValue = prpIdentifers.get(name); 172 173 if (identiferValue != null) { 174 name = identiferValue; 175 } 176 else { 177 name = namespace.concat(name); 178 } 179 } 180 181 parameterMap.put(name, new String[] {value}); 182 } 183 catch (Exception e) { 184 _log.error(e, e); 185 } 186 } 187 188 /** 189 * Returns the default namespace. 190 * 191 * <p> 192 * <b>Do not use this method with an instanceable portlet, it will not 193 * include the instance ID.</b> 194 * </p> 195 * 196 * @return the default namespace, not including the instance ID 197 * @see PortalUtil#getPortletNamespace(String) 198 */ 199 protected String getNamespace() { 200 return PortalUtil.getPortletNamespace(getPortletId()); 201 } 202 203 protected Router router; 204 205 private static final boolean _CHECK_MAPPING_WITH_PREFIX = true; 206 207 private static Log _log = LogFactoryUtil.getLog( 208 BaseFriendlyURLMapper.class); 209 210 private String _mapping; 211 private String _portletId; 212 private boolean _portletInstanceable; 213 214 }