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.util.Map; 021import javax.portlet.ActionResponse; 022import javax.portlet.PortalContext; 023import javax.portlet.PortletMode; 024import javax.portlet.PortletModeException; 025import javax.portlet.WindowState; 026import javax.portlet.WindowStateException; 027 028import org.springframework.util.Assert; 029 030/** 031 * Mock implementation of the {@link javax.portlet.ActionResponse} interface. 032 * 033 * @author John A. Lewis 034 * @author Juergen Hoeller 035 * @since 2.0 036 */ 037public class MockActionResponse extends MockStateAwareResponse implements ActionResponse { 038 039 private boolean redirectAllowed = true; 040 041 private String redirectedUrl; 042 043 044 /** 045 * Create a new MockActionResponse with a default {@link MockPortalContext}. 046 * @see MockPortalContext 047 */ 048 public MockActionResponse() { 049 super(); 050 } 051 052 /** 053 * Create a new MockActionResponse. 054 * @param portalContext the PortalContext defining the supported 055 * PortletModes and WindowStates 056 */ 057 public MockActionResponse(PortalContext portalContext) { 058 super(portalContext); 059 } 060 061 062 @Override 063 public void setWindowState(WindowState windowState) throws WindowStateException { 064 if (this.redirectedUrl != null) { 065 throw new IllegalStateException("Cannot set WindowState after sendRedirect has been called"); 066 } 067 super.setWindowState(windowState); 068 this.redirectAllowed = false; 069 } 070 071 @Override 072 public void setPortletMode(PortletMode portletMode) throws PortletModeException { 073 if (this.redirectedUrl != null) { 074 throw new IllegalStateException("Cannot set PortletMode after sendRedirect has been called"); 075 } 076 super.setPortletMode(portletMode); 077 this.redirectAllowed = false; 078 } 079 080 @Override 081 public void setRenderParameters(Map<String, String[]> parameters) { 082 if (this.redirectedUrl != null) { 083 throw new IllegalStateException("Cannot set render parameters after sendRedirect has been called"); 084 } 085 super.setRenderParameters(parameters); 086 this.redirectAllowed = false; 087 } 088 089 @Override 090 public void setRenderParameter(String key, String value) { 091 if (this.redirectedUrl != null) { 092 throw new IllegalStateException("Cannot set render parameters after sendRedirect has been called"); 093 } 094 super.setRenderParameter(key, value); 095 this.redirectAllowed = false; 096 } 097 098 @Override 099 public void setRenderParameter(String key, String[] values) { 100 if (this.redirectedUrl != null) { 101 throw new IllegalStateException("Cannot set render parameters after sendRedirect has been called"); 102 } 103 super.setRenderParameter(key, values); 104 this.redirectAllowed = false; 105 } 106 107 @Override 108 public void sendRedirect(String location) throws IOException { 109 if (!this.redirectAllowed) { 110 throw new IllegalStateException( 111 "Cannot call sendRedirect after windowState, portletMode, or renderParameters have been set"); 112 } 113 Assert.notNull(location, "Redirect URL must not be null"); 114 this.redirectedUrl = location; 115 } 116 117 @Override 118 public void sendRedirect(String location, String renderUrlParamName) throws IOException { 119 sendRedirect(location); 120 if (renderUrlParamName != null) { 121 setRenderParameter(renderUrlParamName, location); 122 } 123 } 124 125 public String getRedirectedUrl() { 126 return this.redirectedUrl; 127 } 128 129}