405944 Check annotation and resource injection is supported for AsyncListener

This commit is contained in:
Jan Bartel 2013-05-06 16:32:38 +10:00
parent 8d0cf5e949
commit b182f09b2c
5 changed files with 149 additions and 8 deletions

View File

@ -247,12 +247,17 @@
<configuration>
<artifactItems>
<artifactItem>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1-b08</version>
<!--
<groupId>org.eclipse.jetty.orbit</groupId>
<artifactId>javax.servlet</artifactId>
<version>${orbit-servlet-api-version}</version>
-->
<overWrite>true</overWrite>
<outputDirectory>${assembly-directory}/lib</outputDirectory>
<destFileName>servlet-api-3.0.jar</destFileName>
<destFileName>servlet-api-3.1.jar</destFileName>
</artifactItem>
</artifactItems>
</configuration>

View File

@ -28,6 +28,8 @@ import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.eclipse.jetty.server.handler.ContextHandler;
public class AsyncContextState implements AsyncContext
{
@ -92,17 +94,20 @@ public class AsyncContextState implements AsyncContext
@Override
public <T extends AsyncListener> T createListener(Class<T> clazz) throws ServletException
{
{
ContextHandler contextHandler = state().getContextHandler();
if (contextHandler != null)
return contextHandler.getServletContext().createListener(clazz);
try
{
return clazz.newInstance();
}
catch(Exception e)
catch (Exception e)
{
throw new ServletException(e);
}
}
@Override
public void dispatch()
{

View File

@ -92,7 +92,7 @@ $(jetty.home)/lib/jetty-io-$(version).jar ! available org.eclipse.jetty.io.Buf
$(jetty.home)/lib/jetty-xml-$(version).jar ! available org.eclipse.jetty.xml.XmlParser
[Server,All,server,default]
$(jetty.home)/lib/servlet-api-3.0.jar ! available javax.servlet.ServletContext
$(jetty.home)/lib/servlet-api-3.1.jar ! available javax.servlet.ServletContext
$(jetty.home)/lib/jetty-http-$(version).jar ! available org.eclipse.jetty.http.HttpParser
$(jetty.home)/lib/jetty-continuation-$(version).jar ! available org.eclipse.jetty.continuation.Continuation
$(jetty.home)/lib/jetty-server-$(version).jar ! available org.eclipse.jetty.server.Server

View File

@ -307,10 +307,15 @@ public class AnnotationTest extends HttpServlet
context = request.getContextPath();
if (!context.endsWith("/"))
context += "/";
context += "sec/foo";
out.println("<form action="+context+" method=\"post\"><button type=\"submit\">Test ServletSecurity Annotation</button></form>");
String path = context +"sec/foo";
out.println("<form action="+path+" method=\"post\"><button type=\"submit\">Test ServletSecurity Annotation</button></form>");
out.println("<h2>AsyncListener Resource Injection</h2>");
out.println("<p>Click the following link to test that javax.servlet.AsyncListeners are injectable</p>");
path = context+"asy/xx";
out.println("<form action="+path+" method=\"post\"><button type=\"submit\">Test AsyncListener</button></form>");
out.println("</body>");
out.println("</html>");
out.flush();

View File

@ -0,0 +1,126 @@
//
// ========================================================================
// Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package com.acme;
import java.io.IOException;
import java.io.PrintWriter;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.AsyncContext;
import javax.servlet.AsyncEvent;
import javax.servlet.AsyncListener;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns="/asy/*", asyncSupported=true)
public class AsyncListenerServlet extends HttpServlet
{
public static class MyAsyncListener implements AsyncListener
{
@Resource(mappedName="maxAmount")
private Double maxAmount;
boolean postConstructCalled = false;
boolean resourceInjected = false;
@PostConstruct
public void postConstruct()
{
postConstructCalled = true;
resourceInjected = (maxAmount != null);
}
public boolean isPostConstructCalled()
{
return postConstructCalled;
}
public boolean isResourceInjected()
{
return resourceInjected;
}
@Override
public void onComplete(AsyncEvent event) throws IOException
{
// TODO Auto-generated method stub
}
@Override
public void onTimeout(AsyncEvent event) throws IOException
{
// TODO Auto-generated method stub
}
@Override
public void onError(AsyncEvent event) throws IOException
{
// TODO Auto-generated method stub
}
@Override
public void onStartAsync(AsyncEvent event) throws IOException
{
// TODO Auto-generated method stub
}
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
AsyncContext asyncContext = req.startAsync();
MyAsyncListener listener = asyncContext.createListener(MyAsyncListener.class);
PrintWriter writer = resp.getWriter();
writer.println( "<html>");
writer.println( "<body>");
writer.println("<h1>AsyncListener</h2>");
writer.println("<pre>");
writer.println("<h2>@PostConstruct Callback</h2>");
writer.println("<pre>");
writer.println("@PostConstruct");
writer.println("private void postConstruct ()");
writer.println("{}");
writer.println("</pre>");
writer.println("<br/><b>Result: "+listener.isPostConstructCalled()+"</b>");
writer.println("<h2>@Resource Injection for env-entry </h2>");
writer.println("<pre>");
writer.println("@Resource(mappedName=\"maxAmount\")");
writer.println("private Double maxAmount;");
writer.println("</pre>");
writer.println("<br/><b>Result: "+(listener.isResourceInjected()?" PASS":" FAIL")+"</b>");
writer.println( "</body>");
writer.println( "</html>");
writer.flush();
writer.close();
asyncContext.complete();
}
}