001/*
002 * Copyright 2002-2016 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.web.context.request.async;
018
019import org.springframework.web.context.request.NativeWebRequest;
020
021/**
022 * Sends a 503 (SERVICE_UNAVAILABLE) in case of a timeout if the response is not
023 * already committed. As of 4.2.8 this is done indirectly by returning
024 * {@link AsyncRequestTimeoutException} as the result of processing which is
025 * then handled by Spring MVC's default exception handling as a 503 error.
026 *
027 * <p>Registered at the end, after all other interceptors and
028 * therefore invoked only if no other interceptor handles the timeout.
029 *
030 * <p>Note that according to RFC 7231, a 503 without a 'Retry-After' header is
031 * interpreted as a 500 error and the client should not retry. Applications
032 * can install their own interceptor to handle a timeout and add a 'Retry-After'
033 * header if necessary.
034 *
035 * @author Rossen Stoyanchev
036 * @since 3.2
037 */
038public class TimeoutDeferredResultProcessingInterceptor extends DeferredResultProcessingInterceptorAdapter {
039
040        @Override
041        public <T> boolean handleTimeout(NativeWebRequest request, DeferredResult<T> result) throws Exception {
042                result.setErrorResult(new AsyncRequestTimeoutException());
043                return false;
044        }
045
046}