001/* 002 * Copyright 2002-2018 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.web.context.request.RequestAttributes; 024import org.springframework.web.context.request.WebRequest; 025 026/** 027 * Utility methods related to processing asynchronous web requests. 028 * 029 * @author Rossen Stoyanchev 030 * @author Juergen Hoeller 031 * @since 3.2 032 */ 033public abstract class WebAsyncUtils { 034 035 /** 036 * The name attribute containing the {@link WebAsyncManager}. 037 */ 038 public static final String WEB_ASYNC_MANAGER_ATTRIBUTE = 039 WebAsyncManager.class.getName() + ".WEB_ASYNC_MANAGER"; 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. 080 * @param request the current request 081 * @param response the current response 082 * @return an AsyncWebRequest instance (never {@code null}) 083 */ 084 public static AsyncWebRequest createAsyncWebRequest(HttpServletRequest request, HttpServletResponse response) { 085 return new StandardServletAsyncWebRequest(request, response); 086 } 087 088}