View Javadoc
1   /*
2    * Copyright 2002-2013 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  package org.springframework.mock.web.test;
17  
18  import java.io.IOException;
19  import java.util.ArrayList;
20  import java.util.List;
21  import javax.servlet.AsyncContext;
22  import javax.servlet.AsyncEvent;
23  import javax.servlet.AsyncListener;
24  import javax.servlet.ServletContext;
25  import javax.servlet.ServletException;
26  import javax.servlet.ServletRequest;
27  import javax.servlet.ServletResponse;
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  
31  import org.springframework.beans.BeanUtils;
32  import org.springframework.web.util.WebUtils;
33  
34  /**
35   * Mock implementation of the {@link AsyncContext} interface.
36   *
37   * @author Rossen Stoyanchev
38   * @since 3.2
39   */
40  public class MockAsyncContext implements AsyncContext {
41  
42  	private final HttpServletRequest request;
43  
44  	private final HttpServletResponse response;
45  
46  	private final List<AsyncListener> listeners = new ArrayList<AsyncListener>();
47  
48  	private String dispatchedPath;
49  
50  	private long timeout = 10 * 1000L;	// 10 seconds is Tomcat's default
51  
52  
53  	public MockAsyncContext(ServletRequest request, ServletResponse response) {
54  		this.request = (HttpServletRequest) request;
55  		this.response = (HttpServletResponse) response;
56  	}
57  
58  
59  	@Override
60  	public ServletRequest getRequest() {
61  		return this.request;
62  	}
63  
64  	@Override
65  	public ServletResponse getResponse() {
66  		return this.response;
67  	}
68  
69  	@Override
70  	public boolean hasOriginalRequestAndResponse() {
71  		return (this.request instanceof MockHttpServletRequest) && (this.response instanceof MockHttpServletResponse);
72  	}
73  
74  	@Override
75  	public void dispatch() {
76  		dispatch(this.request.getRequestURI());
77   	}
78  
79  	@Override
80  	public void dispatch(String path) {
81  		dispatch(null, path);
82  	}
83  
84  	@Override
85  	public void dispatch(ServletContext context, String path) {
86  		this.dispatchedPath = path;
87  	}
88  
89  	public String getDispatchedPath() {
90  		return this.dispatchedPath;
91  	}
92  
93  	@Override
94  	public void complete() {
95  		MockHttpServletRequest mockRequest = WebUtils.getNativeRequest(request, MockHttpServletRequest.class);
96  		if (mockRequest != null) {
97  			mockRequest.setAsyncStarted(false);
98  		}
99  		for (AsyncListener listener : this.listeners) {
100 			try {
101 				listener.onComplete(new AsyncEvent(this, this.request, this.response));
102 			}
103 			catch (IOException e) {
104 				throw new IllegalStateException("AsyncListener failure", e);
105 			}
106 		}
107 	}
108 
109 	@Override
110 	public void start(Runnable runnable) {
111 		runnable.run();
112 	}
113 
114 	@Override
115 	public void addListener(AsyncListener listener) {
116 		this.listeners.add(listener);
117 	}
118 
119 	@Override
120 	public void addListener(AsyncListener listener, ServletRequest request, ServletResponse response) {
121 		this.listeners.add(listener);
122 	}
123 
124 	public List<AsyncListener> getListeners() {
125 		return this.listeners;
126 	}
127 
128 	@Override
129 	public <T extends AsyncListener> T createListener(Class<T> clazz) throws ServletException {
130 		return BeanUtils.instantiateClass(clazz);
131 	}
132 
133 	@Override
134 	public void setTimeout(long timeout) {
135 		this.timeout = timeout;
136 	}
137 
138 	@Override
139 	public long getTimeout() {
140 		return this.timeout;
141 	}
142 
143 }