001/* 002 * Copyright 2002-2017 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 javax.servlet.ServletRequest; 020import javax.servlet.http.HttpServletRequest; 021import javax.servlet.http.HttpServletResponse; 022 023import org.springframework.util.ClassUtils; 024import org.springframework.web.context.request.RequestAttributes; 025import org.springframework.web.context.request.WebRequest; 026 027/** 028 * Utility methods related to processing asynchronous web requests. 029 * 030 * @author Rossen Stoyanchev 031 * @author Juergen Hoeller 032 * @since 3.2 033 */ 034public abstract class WebAsyncUtils { 035 036 public static final String WEB_ASYNC_MANAGER_ATTRIBUTE = WebAsyncManager.class.getName() + ".WEB_ASYNC_MANAGER"; 037 038 // Determine whether Servlet 3.0's ServletRequest.startAsync method is available 039 private static final boolean startAsyncAvailable = ClassUtils.hasMethod(ServletRequest.class, "startAsync"); 040 041 042 /** 043 * Obtain the {@link WebAsyncManager} for the current request, or if not 044 * found, create and associate it with the request. 045 */ 046 public static WebAsyncManager getAsyncManager(ServletRequest servletRequest) { 047 WebAsyncManager asyncManager = null; 048 Object asyncManagerAttr = servletRequest.getAttribute(WEB_ASYNC_MANAGER_ATTRIBUTE); 049 if (asyncManagerAttr instanceof WebAsyncManager) { 050 asyncManager = (WebAsyncManager) asyncManagerAttr; 051 } 052 if (asyncManager == null) { 053 asyncManager = new WebAsyncManager(); 054 servletRequest.setAttribute(WEB_ASYNC_MANAGER_ATTRIBUTE, asyncManager); 055 } 056 return asyncManager; 057 } 058 059 /** 060 * Obtain the {@link WebAsyncManager} for the current request, or if not 061 * found, create and associate it with the request. 062 */ 063 public static WebAsyncManager getAsyncManager(WebRequest webRequest) { 064 int scope = RequestAttributes.SCOPE_REQUEST; 065 WebAsyncManager asyncManager = null; 066 Object asyncManagerAttr = webRequest.getAttribute(WEB_ASYNC_MANAGER_ATTRIBUTE, scope); 067 if (asyncManagerAttr instanceof WebAsyncManager) { 068 asyncManager = (WebAsyncManager) asyncManagerAttr; 069 } 070 if (asyncManager == null) { 071 asyncManager = new WebAsyncManager(); 072 webRequest.setAttribute(WEB_ASYNC_MANAGER_ATTRIBUTE, asyncManager, scope); 073 } 074 return asyncManager; 075 } 076 077 /** 078 * Create an AsyncWebRequest instance. By default, an instance of 079 * {@link StandardServletAsyncWebRequest} gets created when running in 080 * Servlet 3.0 (or higher) environment - as a fallback, an instance 081 * of {@link NoSupportAsyncWebRequest} will be returned. 082 * @param request the current request 083 * @param response the current response 084 * @return an AsyncWebRequest instance (never {@code null}) 085 */ 086 public static AsyncWebRequest createAsyncWebRequest(HttpServletRequest request, HttpServletResponse response) { 087 return (startAsyncAvailable ? AsyncWebRequestFactory.createStandardAsyncWebRequest(request, response) : 088 new NoSupportAsyncWebRequest(request, response)); 089 } 090 091 092 /** 093 * Inner class to avoid a hard dependency on the Servlet 3.0 API. 094 */ 095 private static class AsyncWebRequestFactory { 096 097 public static AsyncWebRequest createStandardAsyncWebRequest(HttpServletRequest request, HttpServletResponse response) { 098 return new StandardServletAsyncWebRequest(request, response); 099 } 100 } 101 102}