1   /*
2    * Copyright 2002-2004 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.huihoo.jfox.soaf.util.filter;
18  
19  import javax.servlet.RequestDispatcher;
20  import javax.servlet.ServletRequest;
21  import javax.servlet.ServletResponse;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  
26  /***
27   * Mock implementation of the RequestDispatcher interface.
28   *
29   * <p>Used for testing the web framework; typically not
30   * necessary for testing application controllers.
31   *
32   * @author Rod Johnson
33   * @author Juergen Hoeller
34   * @since 1.0.2
35   */
36  public class MockRequestDispatcher implements RequestDispatcher {
37  
38  	private final Log logger = LogFactory.getLog(getClass());
39  
40  	private final String url;
41  
42  	public MockRequestDispatcher(String url) {
43  		this.url = url;
44  	}
45  
46  	public void forward(ServletRequest servletRequest, ServletResponse servletResponse) {
47  		if (servletResponse.isCommitted()) {
48  			throw new IllegalStateException("Cannot perform forward - response is already committed");
49  		}
50  		if (!(servletResponse instanceof MockHttpServletResponse)) {
51  			throw new IllegalArgumentException("MockRequestDispatcher requires MockHttpServletResponse");
52  		}
53  		((MockHttpServletResponse) servletResponse).setForwardedUrl(this.url);
54  		if (logger.isDebugEnabled()) {
55  			logger.debug("MockRequestDispatcher: forwarding to URL [" + this.url + "]");
56  		}
57  	}
58  
59  	public void include(ServletRequest servletRequest, ServletResponse servletResponse) {
60  		if (!(servletResponse instanceof MockHttpServletResponse)) {
61  			throw new IllegalArgumentException("MockRequestDispatcher requires MockHttpServletResponse");
62  		}
63  		((MockHttpServletResponse) servletResponse).setIncludedUrl(this.url);
64  		if (logger.isDebugEnabled()) {
65  			logger.debug("MockRequestDispatcher: including URL [" + this.url + "]");
66  		}
67  	}
68  
69  }