Estou tentando fazer com que o Filter do VRaptor, faça requisições assíncronas. Implementando dessa maneira, o request é realmente assíncrono?
@WebFilter(filterName = "vraptor", urlPatterns = "/*", dispatcherTypes =
{
DispatcherType.FORWARD, DispatcherType.REQUEST
}, asyncSupported = true)
public class VRaptor implements Filter
{
public static final String VERSION = "4.2.0-RC3-SNAPSHOT";
private final Logger logger = getLogger(VRaptor.class);
private ServletContext servletContext;
@Inject
private StaticContentHandler staticHandler;
@Inject
private EncodingHandler encodingHandler;
@Inject
private Event<VRaptorInitialized> initializedEvent;
@Inject
private Event<RequestStarted> requestStartedEvent;
@Inject
private RequestStartedFactory requestStartedFactory;
@Inject
private CDIRequestFactories cdiRequestFactories;
@Override
public void init(FilterConfig cfg) throws ServletException
{
servletContext = cfg.getServletContext();
validateJavaEE7Environment();
validateIfCdiIsFound();
warnIfBeansXmlIsNotFound();
initializedEvent.fire(new VRaptorInitialized(servletContext));
logger.info("VRaptor {} successfuly initialized", VERSION);
}
@Override
public void doFilter(final ServletRequest req, final ServletResponse res, final FilterChain chain) throws IOException,
ServletException
{
validateServletEnvironment(req, res);
final HttpServletRequest baseRequest = (HttpServletRequest) req;
final HttpServletResponse baseResponse = (HttpServletResponse) res;
if (isWebsocketRequest(baseRequest))
{
chain.doFilter(req, res);
return;
}
else if (staticHandler.requestingStaticFile(baseRequest))
{
staticHandler.deferProcessingToContainer(chain, baseRequest, baseResponse);
return;
}
logger.trace("VRaptor received a new request {}", req);
if (!baseRequest.isAsyncSupported())
{
fireEvents(baseRequest, baseResponse, chain);
return;
}
final AsyncContext async = !baseRequest.isAsyncStarted()
? baseRequest.startAsync(baseRequest, baseResponse)
: baseRequest.getAsyncContext();
async.start(new Runnable()
{
@Override
public void run()
{
try
{
fireEvents(baseRequest, baseResponse, chain);
async.complete();
}
catch (ApplicationLogicException e)
{
// it is a business logic exception, we dont need to show
// all interceptors stack trace
async.dispatch(servletContext, servletContext.getContextPath());
throw new RuntimeException(e.getMessage(), e.getCause());
}
}
});
logger.debug("VRaptor ended the request");
}
private void fireEvents(final HttpServletRequest baseRequest, final HttpServletResponse baseResponse, final FilterChain chain)
{
encodingHandler.setEncoding(baseRequest, baseResponse);
RequestStarted requestStarted = requestStartedFactory.createEvent(baseRequest, baseResponse, chain);
cdiRequestFactories.setRequest(requestStarted);
requestStartedEvent.fire(requestStarted);
}
@Override
public void destroy()
{
servletContext = null;
}
private void validateServletEnvironment(ServletRequest req, ServletResponse res) throws ServletException
{
if (!(req instanceof HttpServletRequest) || !(res instanceof HttpServletResponse))
{
throw new ServletException("VRaptor must be run inside a Servlet environment. Portlets and others aren't supported.");
}
}
private void warnIfBeansXmlIsNotFound() throws ServletException
{
URL webInfFile = getResource("/WEB-INF/beans.xml");
URL metaInfFile = getResource("/WEB-INF/classes/META-INF/beans.xml");
if (webInfFile == null && metaInfFile == null)
{
logger.warn("A beans.xml isn't found. Check if is properly located at "
+ "/WEB-INF/beans.xml or /WEB-INF/classes/META-INF/beans.xml");
}
}
private URL getResource(String path) throws ServletException
{
try
{
return servletContext.getResource(path);
}
catch (MalformedURLException e)
{
logger.error("Something went wrong when trying to locate a beans.xml file", e);
return null;
}
}
private void validateJavaEE7Environment() throws ServletException
{
try
{
servletContext.getJspConfigDescriptor(); // check servlet 3
Priority.class.toString(); // check CDI 1.1
}
catch (NoClassDefFoundError | java.lang.NoSuchMethodError e)
{
throw new ServletException("VRaptor only runs under Java EE 7 environment or Servlet Containers that "
+ "supports Servlets 3 with CDI 1.1 jars.");
}
}
private void validateIfCdiIsFound() throws ServletException
{
if (staticHandler == null)
{
throw new ServletException("Dependencies were not set. Do you have a Weld/CDI listener setup in your web.xml?");
}
}
/**
* According to the Websocket spec (
https://tools.ietf.org/html/rfc6455):
* The WebSocket Protocol 5. The request MUST contain an |Upgrade| header
* field whose value MUST include the "websocket" keyword.
*/
private boolean isWebsocketRequest(HttpServletRequest request)
{
String upgradeHeader = request.getHeader("Upgrade");
return upgradeHeader != null && upgradeHeader.toLowerCase().contains("websocket");
}
}