001/* 002 * Copyright 2002-2012 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.mock.web.portlet; 018 019import java.io.IOException; 020import java.io.UnsupportedEncodingException; 021import java.io.Writer; 022import java.net.URLEncoder; 023import java.util.Collections; 024import java.util.LinkedHashMap; 025import java.util.Map; 026import java.util.Set; 027import javax.portlet.BaseURL; 028import javax.portlet.PortletSecurityException; 029 030import org.springframework.util.Assert; 031import org.springframework.util.StringUtils; 032 033/** 034 * Mock implementation of the {@link javax.portlet.BaseURL} interface. 035 * 036 * @author Juergen Hoeller 037 * @since 3.0 038 */ 039public abstract class MockBaseURL implements BaseURL { 040 041 public static final String URL_TYPE_RENDER = "render"; 042 043 public static final String URL_TYPE_ACTION = "action"; 044 045 private static final String ENCODING = "UTF-8"; 046 047 048 protected final Map<String, String[]> parameters = new LinkedHashMap<String, String[]>(); 049 050 private boolean secure = false; 051 052 private final Map<String, String[]> properties = new LinkedHashMap<String, String[]>(); 053 054 055 //--------------------------------------------------------------------- 056 // BaseURL methods 057 //--------------------------------------------------------------------- 058 059 @Override 060 public void setParameter(String key, String value) { 061 Assert.notNull(key, "Parameter key must be null"); 062 Assert.notNull(value, "Parameter value must not be null"); 063 this.parameters.put(key, new String[] {value}); 064 } 065 066 @Override 067 public void setParameter(String key, String[] values) { 068 Assert.notNull(key, "Parameter key must be null"); 069 Assert.notNull(values, "Parameter values must not be null"); 070 this.parameters.put(key, values); 071 } 072 073 @Override 074 public void setParameters(Map<String, String[]> parameters) { 075 Assert.notNull(parameters, "Parameters Map must not be null"); 076 this.parameters.clear(); 077 this.parameters.putAll(parameters); 078 } 079 080 public Set<String> getParameterNames() { 081 return this.parameters.keySet(); 082 } 083 084 public String getParameter(String name) { 085 String[] arr = this.parameters.get(name); 086 return (arr != null && arr.length > 0 ? arr[0] : null); 087 } 088 089 public String[] getParameterValues(String name) { 090 return this.parameters.get(name); 091 } 092 093 @Override 094 public Map<String, String[]> getParameterMap() { 095 return Collections.unmodifiableMap(this.parameters); 096 } 097 098 @Override 099 public void setSecure(boolean secure) throws PortletSecurityException { 100 this.secure = secure; 101 } 102 103 public boolean isSecure() { 104 return this.secure; 105 } 106 107 @Override 108 public void write(Writer out) throws IOException { 109 out.write(toString()); 110 } 111 112 @Override 113 public void write(Writer out, boolean escapeXML) throws IOException { 114 out.write(toString()); 115 } 116 117 @Override 118 public void addProperty(String key, String value) { 119 String[] values = this.properties.get(key); 120 if (values != null) { 121 this.properties.put(key, StringUtils.addStringToArray(values, value)); 122 } 123 else { 124 this.properties.put(key, new String[] {value}); 125 } 126 } 127 128 @Override 129 public void setProperty(String key, String value) { 130 this.properties.put(key, new String[] {value}); 131 } 132 133 public Map<String, String[]> getProperties() { 134 return Collections.unmodifiableMap(this.properties); 135 } 136 137 138 protected String encodeParameter(String name, String value) { 139 try { 140 return URLEncoder.encode(name, ENCODING) + "=" + URLEncoder.encode(value, ENCODING); 141 } 142 catch (UnsupportedEncodingException ex) { 143 return null; 144 } 145 } 146 147 protected String encodeParameter(String name, String[] values) { 148 try { 149 StringBuilder sb = new StringBuilder(); 150 for (int i = 0, n = values.length; i < n; i++) { 151 sb.append(i > 0 ? ";" : "").append(URLEncoder.encode(name, ENCODING)).append("=") 152 .append(URLEncoder.encode(values[i], ENCODING)); 153 } 154 return sb.toString(); 155 } 156 catch (UnsupportedEncodingException ex) { 157 return null; 158 } 159 } 160 161}