001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.chain.web.portlet; 018 019import java.security.Principal; 020import java.util.Enumeration; 021import java.util.HashMap; 022import java.util.Locale; 023import java.util.Map; 024 025import javax.portlet.PortalContext; 026import javax.portlet.PortletContext; 027import javax.portlet.PortletMode; 028import javax.portlet.PortletPreferences; 029import javax.portlet.PortletRequest; 030import javax.portlet.PortletSession; 031import javax.portlet.RenderParameters; 032import javax.portlet.WindowState; 033import javax.servlet.http.Cookie; 034 035import org.apache.commons.chain.web.MockEnumeration; 036import org.apache.commons.chain.web.MockPrincipal; 037 038/** 039 * Mock Object for {@code PortletRequest} 040 */ 041public class MockPortletRequest implements PortletRequest { 042 private String contextPath; 043 private String authType; 044 private Locale locale; 045 private String scheme = "http"; 046 private String serverName = "localhost"; 047 private int serverPort = 8080; 048 private PortalContext portalContext; 049 private PortletContext context; 050 private PortletSession session; 051 private PortletMode portletMode; 052 private PortletPreferences portletPreferences; 053 private WindowState windowState; 054 private Principal principal; 055 private Map<String, String[]> parameters = new HashMap<>(); 056 private Map<String, Object> attributes = new HashMap<>(); 057 private Map<String, String[]> properties = new HashMap<>(); 058 private Cookie[] cookies = new Cookie[0]; 059 060 public MockPortletRequest() { 061 this(null, null, null); 062 } 063 064 public MockPortletRequest(String contextPath, PortletContext context, PortletSession session) { 065 this.contextPath = contextPath; 066 this.context = context == null ? new MockPortletContext() : context; 067 this.session = session; 068 } 069 070 // --------------------------------------------------------- Public Methods 071 072 public void addParameter(String name, String value) { 073 String values[] = parameters.get(name); 074 if (values == null) { 075 String results[] = new String[] { value }; 076 parameters.put(name, results); 077 return; 078 } 079 String results[] = new String[values.length + 1]; 080 System.arraycopy(values, 0, results, 0, values.length); 081 results[values.length] = value; 082 parameters.put(name, results); 083 } 084 085 public void addProperty(String name, String value) { 086 String values[] = properties.get(name); 087 if (values == null) { 088 String results[] = new String[] { value }; 089 properties.put(name, results); 090 return; 091 } 092 String results[] = new String[values.length + 1]; 093 System.arraycopy(values, 0, results, 0, values.length); 094 results[values.length] = value; 095 properties.put(name, results); 096 } 097 098 public void addCookie(String name, String value) { 099 addCookie(new Cookie(name, value)); 100 } 101 102 public void addCookie(Cookie cookie) { 103 Cookie[] newValues = new Cookie[cookies.length + 1]; 104 System.arraycopy(cookies, 0, newValues, 0, cookies.length); 105 cookies = newValues; 106 cookies[cookies.length - 1] = cookie; 107 } 108 109 public void setAuthType(String authType) { 110 this.authType = authType; 111 } 112 113 public void setContextPath(String contextPath) { 114 this.contextPath = contextPath; 115 } 116 117 public void setLocale(Locale locale) { 118 this.locale = locale; 119 } 120 121 public void setPortalContext(PortalContext portalContext) { 122 this.portalContext = portalContext; 123 } 124 125 public void setPortletContext(PortletContext context) { 126 this.context = context; 127 } 128 129 public void setPortletMode(PortletMode portletMode) { 130 this.portletMode = portletMode; 131 } 132 133 public void setPortletPreferences(PortletPreferences portletPreferences) { 134 this.portletPreferences = portletPreferences; 135 } 136 137 public void setPortletSession(PortletSession session) { 138 this.session = session; 139 } 140 141 public void setScheme(String scheme) { 142 this.scheme = scheme; 143 } 144 145 public void setServerName(String serverName) { 146 this.serverName = serverName; 147 } 148 149 public void setServerPort(int serverPort) { 150 this.serverPort = serverPort; 151 } 152 153 public void setUserPrincipal(Principal principal) { 154 this.principal = principal; 155 } 156 157 public void setUserPrincipal(WindowState windowState) { 158 this.windowState = windowState; 159 } 160 161 // --------------------------------------------- PortletRequest Methods 162 163 @Override 164 public Object getAttribute(String name) { 165 return attributes.get(name); 166 } 167 168 @Override 169 public Enumeration<String> getAttributeNames() { 170 return new MockEnumeration<>(attributes.keySet().iterator()); 171 } 172 173 @Override 174 public String getAuthType() { 175 return authType; 176 } 177 178 @Override 179 public String getContextPath() { 180 return contextPath; 181 } 182 183 @Override 184 public Locale getLocale() { 185 return locale; 186 } 187 188 @Override 189 public Enumeration<Locale> getLocales() { 190 throw new UnsupportedOperationException(); 191 } 192 193 @Override 194 @Deprecated 195 public String getParameter(String name) { 196 String values[] = parameters.get(name); 197 if (values != null) { 198 return values[0]; 199 } else { 200 return null; 201 } 202 } 203 204 @Override 205 @Deprecated 206 public Map<String, String[]> getParameterMap() { 207 return parameters; 208 } 209 210 @Override 211 @Deprecated 212 public Enumeration<String> getParameterNames() { 213 return new MockEnumeration<>(parameters.keySet().iterator()); 214 } 215 216 @Override 217 @Deprecated 218 public String[] getParameterValues(String name) { 219 return parameters.get(name); 220 } 221 222 @Override 223 public PortalContext getPortalContext() { 224 return portalContext; 225 } 226 227 @Override 228 public PortletMode getPortletMode() { 229 return portletMode; 230 } 231 232 @Override 233 public PortletSession getPortletSession() { 234 return getPortletSession(true); 235 } 236 237 @Override 238 public PortletSession getPortletSession(boolean create) { 239 if (create && session == null) { 240 session = new MockPortletSession(context); 241 } 242 return session; 243 } 244 245 @Override 246 public PortletPreferences getPreferences() { 247 return portletPreferences; 248 } 249 250 @Override 251 public Enumeration<String> getProperties(String name) { 252 throw new UnsupportedOperationException(); 253 } 254 255 @Override 256 public String getProperty(String name) { 257 String values[] = properties.get(name); 258 if (values != null) { 259 return values[0]; 260 } else { 261 return null; 262 } 263 } 264 265 @Override 266 public Enumeration<String> getPropertyNames() { 267 return new MockEnumeration<>(properties.keySet().iterator()); 268 } 269 270 @Override 271 public String getRemoteUser() { 272 if (principal != null) { 273 return principal.getName(); 274 } else { 275 return null; 276 } 277 } 278 279 @Override 280 public String getRequestedSessionId() { 281 throw new UnsupportedOperationException(); 282 } 283 284 @Override 285 public String getResponseContentType() { 286 throw new UnsupportedOperationException(); 287 } 288 289 @Override 290 public Enumeration<String> getResponseContentTypes() { 291 throw new UnsupportedOperationException(); 292 } 293 294 @Override 295 public String getScheme() { 296 return scheme; 297 } 298 299 @Override 300 public String getServerName() { 301 return serverName; 302 } 303 304 @Override 305 public int getServerPort() { 306 return serverPort; 307 } 308 309 @Override 310 public Principal getUserPrincipal() { 311 return principal; 312 } 313 314 @Override 315 public WindowState getWindowState() { 316 return windowState; 317 } 318 319 @Override 320 public boolean isPortletModeAllowed(PortletMode mode) { 321 throw new UnsupportedOperationException(); 322 } 323 324 @Override 325 public boolean isRequestedSessionIdValid() { 326 throw new UnsupportedOperationException(); 327 } 328 329 @Override 330 public boolean isSecure() { 331 return false; 332 } 333 334 @Override 335 public boolean isUserInRole(String role) { 336 if (principal instanceof MockPrincipal) { 337 return ((MockPrincipal)principal).isUserInRole(role); 338 } else { 339 return false; 340 } 341 } 342 343 @Override 344 public boolean isWindowStateAllowed(WindowState state) { 345 throw new UnsupportedOperationException(); 346 } 347 348 @Override 349 public void removeAttribute(String name) { 350 attributes.remove(name); 351 } 352 353 354 @Override 355 public void setAttribute(String name, Object value) { 356 if (value == null) { 357 attributes.remove(name); 358 } else { 359 attributes.put(name, value); 360 } 361 } 362 363 @Override 364 public RenderParameters getRenderParameters() { 365 throw new UnsupportedOperationException(); 366 } 367 368 @Override 369 public PortletContext getPortletContext() { 370 return context; 371 } 372 373 @Override 374 public String getWindowID() { 375 throw new UnsupportedOperationException(); 376 } 377 378 @Override 379 public Cookie[] getCookies() { 380 return cookies; 381 } 382 383 @Override 384 @Deprecated 385 public Map<String, String[]> getPrivateParameterMap() { 386 throw new UnsupportedOperationException(); 387 } 388 389 @Override 390 @Deprecated 391 public Map<String, String[]> getPublicParameterMap() { 392 throw new UnsupportedOperationException(); 393 } 394 395 @Override 396 public String getUserAgent() { 397 throw new UnsupportedOperationException(); 398 } 399}