View Javadoc
1   /*
2    * Copyright 2002-2012 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.mock.web.test;
18  
19  import javax.servlet.RequestDispatcher;
20  import javax.servlet.ServletRequest;
21  import javax.servlet.ServletResponse;
22  import javax.servlet.http.HttpServletResponseWrapper;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  
27  import org.springframework.util.Assert;
28  
29  /**
30   * Mock implementation of the {@link javax.servlet.RequestDispatcher} interface.
31   *
32   * <p>Used for testing the web framework; typically not necessary for
33   * testing application controllers.
34   *
35   * @author Rod Johnson
36   * @author Juergen Hoeller
37   * @author Sam Brannen
38   * @since 1.0.2
39   */
40  public class MockRequestDispatcher implements RequestDispatcher {
41  
42  	private final Log logger = LogFactory.getLog(getClass());
43  
44  	private final String resource;
45  
46  
47  	/**
48  	 * Create a new MockRequestDispatcher for the given resource.
49  	 * @param resource the server resource to dispatch to, located at a
50  	 * particular path or given by a particular name
51  	 */
52  	public MockRequestDispatcher(String resource) {
53  		Assert.notNull(resource, "resource must not be null");
54  		this.resource = resource;
55  	}
56  
57  
58  	@Override
59  	public void forward(ServletRequest request, ServletResponse response) {
60  		Assert.notNull(request, "Request must not be null");
61  		Assert.notNull(response, "Response must not be null");
62  		if (response.isCommitted()) {
63  			throw new IllegalStateException("Cannot perform forward - response is already committed");
64  		}
65  		getMockHttpServletResponse(response).setForwardedUrl(this.resource);
66  		if (logger.isDebugEnabled()) {
67  			logger.debug("MockRequestDispatcher: forwarding to [" + this.resource + "]");
68  		}
69  	}
70  
71  	@Override
72  	public void include(ServletRequest request, ServletResponse response) {
73  		Assert.notNull(request, "Request must not be null");
74  		Assert.notNull(response, "Response must not be null");
75  		getMockHttpServletResponse(response).addIncludedUrl(this.resource);
76  		if (logger.isDebugEnabled()) {
77  			logger.debug("MockRequestDispatcher: including [" + this.resource + "]");
78  		}
79  	}
80  
81  	/**
82  	 * Obtain the underlying {@link MockHttpServletResponse}, unwrapping
83  	 * {@link HttpServletResponseWrapper} decorators if necessary.
84  	 */
85  	protected MockHttpServletResponse getMockHttpServletResponse(ServletResponse response) {
86  		if (response instanceof MockHttpServletResponse) {
87  			return (MockHttpServletResponse) response;
88  		}
89  		if (response instanceof HttpServletResponseWrapper) {
90  			return getMockHttpServletResponse(((HttpServletResponseWrapper) response).getResponse());
91  		}
92  		throw new IllegalArgumentException("MockRequestDispatcher requires MockHttpServletResponse");
93  	}
94  
95  }