com.nec.harvest.servlet.interceptor.async.TimeoutCallableProcessingInterceptor.java Source code

Java tutorial

Introduction

Here is the source code for com.nec.harvest.servlet.interceptor.async.TimeoutCallableProcessingInterceptor.java

Source

/*
 * Copyright(C) 2014
 * NEC Corporation All rights reserved.
 * 
 * No permission to use, copy, modify and distribute this software
 * and its documentation for any purpose is granted.
 * This software is provided under applicable license agreement only.
 */
package com.nec.harvest.servlet.interceptor.async;

import java.util.concurrent.Callable;

import javax.servlet.http.HttpServletResponse;

import org.springframework.http.HttpStatus;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.async.CallableProcessingInterceptor;
import org.springframework.web.context.request.async.CallableProcessingInterceptorAdapter;

/**
 * Sends a 503 (SERVICE_UNAVAILABLE) in case of a timeout if the response is not
 * already committed. Registered at the end, after all other interceptors and
 * therefore invoked only if no other interceptor handles the timeout.
 *
 * <p>
 * Note that according to RFC 2616, a 503 without a 'Retry-After' header is
 * interpreted as a 500 error and the client should not retry. Applications can
 * install their own interceptor to handle a timeout and add a 'Retry-After'
 * header if necessary.
 *
 * @since 3.2
 */
public class TimeoutCallableProcessingInterceptor extends CallableProcessingInterceptorAdapter {

    @Override
    public <T> Object handleTimeout(NativeWebRequest request, Callable<T> task) throws Exception {
        final HttpServletResponse servletResponse = request.getNativeResponse(HttpServletResponse.class);
        if (!servletResponse.isCommitted()) {
            servletResponse.sendError(HttpStatus.SERVICE_UNAVAILABLE.value());
        }

        return CallableProcessingInterceptor.RESPONSE_HANDLED;
    }

}