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