View Javadoc
1   /*
2    * Copyright 2002-2014 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.web.context.request.async;
17  
18  import java.util.List;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  
23  import org.springframework.web.context.request.NativeWebRequest;
24  
25  /**
26   * Assists with the invocation of {@link DeferredResultProcessingInterceptor}'s.
27   *
28   * @author Rossen Stoyanchev
29   * @since 3.2
30   */
31  class DeferredResultInterceptorChain {
32  
33  	private static final Log logger = LogFactory.getLog(DeferredResultInterceptorChain.class);
34  
35  	private final List<DeferredResultProcessingInterceptor> interceptors;
36  
37  	private int preProcessingIndex = -1;
38  
39  
40  	public DeferredResultInterceptorChain(List<DeferredResultProcessingInterceptor> interceptors) {
41  		this.interceptors = interceptors;
42  	}
43  
44  	public void applyBeforeConcurrentHandling(NativeWebRequest request, DeferredResult<?> deferredResult) throws Exception {
45  		for (DeferredResultProcessingInterceptor interceptor : this.interceptors) {
46  			interceptor.beforeConcurrentHandling(request, deferredResult);
47  		}
48  	}
49  
50  	public void applyPreProcess(NativeWebRequest request, DeferredResult<?> deferredResult) throws Exception {
51  		for (DeferredResultProcessingInterceptor interceptor : this.interceptors) {
52  			interceptor.preProcess(request, deferredResult);
53  			this.preProcessingIndex++;
54  		}
55  	}
56  
57  	public Object applyPostProcess(NativeWebRequest request,  DeferredResult<?> deferredResult, Object concurrentResult) {
58  		try {
59  			for (int i = this.preProcessingIndex; i >= 0; i--) {
60  				this.interceptors.get(i).postProcess(request, deferredResult, concurrentResult);
61  			}
62  		}
63  		catch (Throwable t) {
64  			return t;
65  		}
66  		return concurrentResult;
67  	}
68  
69  	public void triggerAfterTimeout(NativeWebRequest request, DeferredResult<?> deferredResult) throws Exception {
70  		for (DeferredResultProcessingInterceptor interceptor : this.interceptors) {
71  			if (deferredResult.isSetOrExpired()) {
72  				return;
73  			}
74  			if (!interceptor.handleTimeout(request, deferredResult)){
75  				break;
76  			}
77  		}
78  	}
79  
80  	public void triggerAfterCompletion(NativeWebRequest request, DeferredResult<?> deferredResult) {
81  		for (int i = this.preProcessingIndex; i >= 0; i--) {
82  			try {
83  				this.interceptors.get(i).afterCompletion(request, deferredResult);
84  			}
85  			catch (Throwable t) {
86  				logger.error("afterCompletion error", t);
87  			}
88  		}
89  	}
90  
91  }