deltaspike-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ch...@apache.org
Subject [06/10] git commit: Added support for injection of standard servlet objects
Date Thu, 13 Jun 2013 05:02:55 GMT
Added support for injection of standard servlet objects


Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/cab0ba0e
Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/cab0ba0e
Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/cab0ba0e

Branch: refs/heads/master
Commit: cab0ba0e3b8f5df46cd035e92a7d232edfc5625e
Parents: 28e5c71
Author: Christian Kaltepoth <christian@kaltepoth.de>
Authored: Fri May 24 07:22:52 2013 +0200
Committer: Christian Kaltepoth <christian@kaltepoth.de>
Committed: Thu Jun 13 06:53:29 2013 +0200

----------------------------------------------------------------------
 deltaspike/modules/servlet/impl/pom.xml         |   7 ++
 .../servlet/impl/RequestResponse.java           |  65 ++++++++++
 .../servlet/impl/RequestResponseHolder.java     |  92 ++++++++++++++
 .../servlet/impl/ServletEventBridgeFilter.java  |  27 +++-
 .../servlet/impl/ServletObjectProducer.java     | 119 ++++++++++++++++++
 .../producer/ServletObjectInjectionBean.java    | 124 +++++++++++++++++++
 .../producer/ServletObjectInjectionServlet.java |  75 +++++++++++
 .../producer/ServletObjectInjectionTest.java    | 112 +++++++++++++++++
 8 files changed, 615 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/cab0ba0e/deltaspike/modules/servlet/impl/pom.xml
----------------------------------------------------------------------
diff --git a/deltaspike/modules/servlet/impl/pom.xml b/deltaspike/modules/servlet/impl/pom.xml
index ee2ae40..01c9616 100644
--- a/deltaspike/modules/servlet/impl/pom.xml
+++ b/deltaspike/modules/servlet/impl/pom.xml
@@ -55,6 +55,13 @@
         </dependency>
 
         <dependency>
+            <groupId>org.hamcrest</groupId>
+            <artifactId>hamcrest-library</artifactId>
+            <version>1.3</version>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
             <groupId>org.apache.httpcomponents</groupId>
             <artifactId>httpclient</artifactId>
             <version>4.2.5</version>

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/cab0ba0e/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/RequestResponse.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/RequestResponse.java
b/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/RequestResponse.java
new file mode 100644
index 0000000..16b3d28
--- /dev/null
+++ b/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/RequestResponse.java
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.deltaspike.servlet.impl;
+
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+
+/**
+ * Simple value objects that holds a request/response pair.
+ * 
+ * @author Christian Kaltepoth
+ */
+class RequestResponse
+{
+
+    private final ServletRequest request;
+    private final ServletResponse response;
+
+    /**
+     * Creates a new instance of this class.
+     * 
+     * @param request
+     *            the request
+     * @param response
+     *            the response
+     */
+    RequestResponse(ServletRequest request, ServletResponse response)
+    {
+        this.request = request;
+        this.response = response;
+    }
+
+    /**
+     * Returns the request stored in this object.
+     */
+    ServletRequest getRequest()
+    {
+        return request;
+    }
+
+    /**
+     * Returns the response stored in this object.
+     */
+    ServletResponse getResponse()
+    {
+        return response;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/cab0ba0e/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/RequestResponseHolder.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/RequestResponseHolder.java
b/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/RequestResponseHolder.java
new file mode 100644
index 0000000..add047c
--- /dev/null
+++ b/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/RequestResponseHolder.java
@@ -0,0 +1,92 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.deltaspike.servlet.impl;
+
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+
+/**
+ * The {@link RequestResponseHolder} holds {@link RequestResponse} objects in a {@link ThreadLocal}.
+ * 
+ * @author Christian Kaltepoth
+ */
+class RequestResponseHolder
+{
+
+    private static final ThreadLocal<RequestResponse> requestResponseThreadLocal =
new ThreadLocal<RequestResponse>();
+
+    private RequestResponseHolder()
+    {
+        // no instance creation allowed
+    }
+
+    /**
+     * Binds the a request/response pair to the current thread.
+     * 
+     * @param request
+     *            The request, must not be <code>null</code>
+     * @param response
+     *            The response, must not be <code>null</code>
+     */
+    static void bind(ServletRequest request, ServletResponse response)
+    {
+        bind(new RequestResponse(request, response));
+    }
+
+    /**
+     * Binds the a request/response pair to the current thread.
+     * 
+     * @param pair
+     *            The request/response pair, must not be <code>null</code>
+     */
+    static void bind(RequestResponse pair)
+    {
+        if (requestResponseThreadLocal.get() != null)
+        {
+            throw new IllegalStateException("There is already an instance stored for this
thread.");
+        }
+        requestResponseThreadLocal.set(pair);
+    }
+
+    /**
+     * Releases the stored request/response pair for the current thread.
+     */
+    static void release()
+    {
+        requestResponseThreadLocal.remove();
+    }
+
+    /**
+     * Retrieves the request/response pair associated with the current thread.
+     * 
+     * @return The request/response pair, never <code>null</code>
+     * @throws IllegalStateException
+     *             if no pair is associated with the thread
+     */
+    static RequestResponse get()
+    {
+        RequestResponse requestResponse = requestResponseThreadLocal.get();
+        if (requestResponse == null)
+        {
+            throw new IllegalStateException("Attempt to access the request/response without
an active HTTP request");
+        }
+        return requestResponse;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/cab0ba0e/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/ServletEventBridgeFilter.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/ServletEventBridgeFilter.java
b/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/ServletEventBridgeFilter.java
index 90cadd3..182f185 100644
--- a/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/ServletEventBridgeFilter.java
+++ b/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/ServletEventBridgeFilter.java
@@ -48,17 +48,32 @@ public class ServletEventBridgeFilter extends EventEmitter implements
Filter
             ServletException
     {
 
-        fireEvent(request, WebLiteral.INSTANCE, InitializedLiteral.INSTANCE);
-        fireEvent(response, WebLiteral.INSTANCE, InitializedLiteral.INSTANCE);
-
+        // store the request/response in a holder to access it later
+        RequestResponseHolder.bind(request, response);
         try
         {
-            chain.doFilter(request, response);
+
+            // fire @Initialized events
+            fireEvent(request, WebLiteral.INSTANCE, InitializedLiteral.INSTANCE);
+            fireEvent(response, WebLiteral.INSTANCE, InitializedLiteral.INSTANCE);
+
+            try
+            {
+                chain.doFilter(request, response);
+            }
+            finally
+            {
+                // fire @Destroyed events
+                fireEvent(request, WebLiteral.INSTANCE, DestroyedLiteral.INSTANCE);
+                fireEvent(response, WebLiteral.INSTANCE, DestroyedLiteral.INSTANCE);
+            }
+
         }
+
+        // release in an outer finally block to ensure the ThreadLocal is cleaned correctly
         finally
         {
-            fireEvent(request, WebLiteral.INSTANCE, DestroyedLiteral.INSTANCE);
-            fireEvent(response, WebLiteral.INSTANCE, DestroyedLiteral.INSTANCE);
+            RequestResponseHolder.release();
         }
 
     }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/cab0ba0e/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/ServletObjectProducer.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/ServletObjectProducer.java
b/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/ServletObjectProducer.java
new file mode 100644
index 0000000..7421c49
--- /dev/null
+++ b/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/ServletObjectProducer.java
@@ -0,0 +1,119 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.deltaspike.servlet.impl;
+
+import java.security.Principal;
+
+import javax.enterprise.context.RequestScoped;
+import javax.enterprise.context.SessionScoped;
+import javax.enterprise.inject.Produces;
+import javax.enterprise.inject.Typed;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
+
+import org.apache.deltaspike.servlet.api.Web;
+
+/**
+ * Producer for standard servlet objects. All objects are produced with the {@link Web} qualifier.
Currently the
+ * following objects are supported:
+ * 
+ * <ul>
+ * <li>{@link ServletRequest}</li>
+ * <li>{@link HttpServletRequest}</li>
+ * <li>{@link ServletResponse}</li>
+ * <li>{@link HttpServletResponse}</li>
+ * <li>{@link HttpSession}</li>
+ * <li>{@link Principal}</li>
+ * </ul>
+ * 
+ * @author Christian Kaltepoth
+ */
+public class ServletObjectProducer
+{
+
+    @Produces
+    @Web
+    @RequestScoped
+    public ServletRequest getServletRequest()
+    {
+        return RequestResponseHolder.get().getRequest();
+    }
+
+    @Produces
+    @Typed(HttpServletRequest.class)
+    @Web
+    @RequestScoped
+    public HttpServletRequest getHttpServletRequest()
+    {
+        ServletRequest request = RequestResponseHolder.get().getRequest();
+        if (request instanceof HttpServletRequest)
+        {
+            return (HttpServletRequest) request;
+        }
+        throw new IllegalStateException("The current request is not a HttpServletRequest");
+    }
+
+    @Produces
+    @Web
+    @RequestScoped
+    public ServletResponse getServletResponse()
+    {
+        return RequestResponseHolder.get().getResponse();
+    }
+
+    @Produces
+    @Typed(HttpServletResponse.class)
+    @Web
+    @RequestScoped
+    public HttpServletResponse getHttpServletResponse()
+    {
+        ServletResponse response = RequestResponseHolder.get().getResponse();
+        if (response instanceof HttpServletResponse)
+        {
+            return (HttpServletResponse) response;
+        }
+        throw new IllegalStateException("The current response is not a HttpServletResponse");
+    }
+
+    @Produces
+    @Web
+    @SessionScoped
+    public HttpSession getHttpSession()
+    {
+        ServletRequest request = RequestResponseHolder.get().getRequest();
+        if (request instanceof HttpServletRequest)
+        {
+            return ((HttpServletRequest) request).getSession(true);
+        }
+        throw new IllegalStateException(
+                "Cannot produce HttpSession because the current request is not a HttpServletRequest");
+    }
+
+    @Produces
+    @Web
+    @RequestScoped
+    public Principal getPrincipal()
+    {
+        return getHttpServletRequest().getUserPrincipal();
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/cab0ba0e/deltaspike/modules/servlet/impl/src/test/java/org/apache/deltaspike/test/servlet/impl/producer/ServletObjectInjectionBean.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/servlet/impl/src/test/java/org/apache/deltaspike/test/servlet/impl/producer/ServletObjectInjectionBean.java
b/deltaspike/modules/servlet/impl/src/test/java/org/apache/deltaspike/test/servlet/impl/producer/ServletObjectInjectionBean.java
new file mode 100644
index 0000000..9922176
--- /dev/null
+++ b/deltaspike/modules/servlet/impl/src/test/java/org/apache/deltaspike/test/servlet/impl/producer/ServletObjectInjectionBean.java
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.deltaspike.test.servlet.impl.producer;
+
+import java.security.Principal;
+
+import javax.inject.Inject;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
+
+import org.apache.deltaspike.servlet.api.Web;
+
+/**
+ * Simple CDI beans with various injection points for servlet ojbects.
+ * 
+ * @author Christian Kaltepoth
+ */
+public class ServletObjectInjectionBean
+{
+
+    @Web
+    @Inject
+    private ServletRequest servletRequest;
+
+    @Web
+    @Inject
+    private HttpServletRequest httpServletRequest;
+
+    @Web
+    @Inject
+    private ServletResponse servletResponse;
+
+    @Web
+    @Inject
+    private HttpServletResponse httpServletResponse;
+
+    @Web
+    @Inject
+    private HttpSession httpSession;
+
+    @Web
+    @Inject
+    private Principal principal;
+
+    public ServletRequest getServletRequest()
+    {
+        return servletRequest;
+    }
+
+    public void setServletRequest(ServletRequest servletRequest)
+    {
+        this.servletRequest = servletRequest;
+    }
+
+    public HttpServletRequest getHttpServletRequest()
+    {
+        return httpServletRequest;
+    }
+
+    public void setHttpServletRequest(HttpServletRequest httpServletRequest)
+    {
+        this.httpServletRequest = httpServletRequest;
+    }
+
+    public ServletResponse getServletResponse()
+    {
+        return servletResponse;
+    }
+
+    public void setServletResponse(ServletResponse servletResponse)
+    {
+        this.servletResponse = servletResponse;
+    }
+
+    public HttpServletResponse getHttpServletResponse()
+    {
+        return httpServletResponse;
+    }
+
+    public void setHttpServletResponse(HttpServletResponse httpServletResponse)
+    {
+        this.httpServletResponse = httpServletResponse;
+    }
+
+    public HttpSession getHttpSession()
+    {
+        return httpSession;
+    }
+
+    public void setHttpSession(HttpSession httpSession)
+    {
+        this.httpSession = httpSession;
+    }
+
+    public Principal getPrincipal()
+    {
+        return principal;
+    }
+
+    public void setPrincipal(Principal principal)
+    {
+        this.principal = principal;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/cab0ba0e/deltaspike/modules/servlet/impl/src/test/java/org/apache/deltaspike/test/servlet/impl/producer/ServletObjectInjectionServlet.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/servlet/impl/src/test/java/org/apache/deltaspike/test/servlet/impl/producer/ServletObjectInjectionServlet.java
b/deltaspike/modules/servlet/impl/src/test/java/org/apache/deltaspike/test/servlet/impl/producer/ServletObjectInjectionServlet.java
new file mode 100644
index 0000000..5b0af1e
--- /dev/null
+++ b/deltaspike/modules/servlet/impl/src/test/java/org/apache/deltaspike/test/servlet/impl/producer/ServletObjectInjectionServlet.java
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.deltaspike.test.servlet.impl.producer;
+
+import java.io.IOException;
+
+import javax.servlet.ServletException;
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.deltaspike.core.api.provider.BeanProvider;
+
+/**
+ * Simple servlet that logs details about the servlet objects injected into {@link ServletObjectInjectionBean}.
+ * 
+ * @author Christian Kaltepoth
+ */
+public class ServletObjectInjectionServlet extends HttpServlet
+{
+
+    private static final long serialVersionUID = 1L;
+
+    @Override
+    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
IOException
+    {
+
+        /*
+         * The ServletObjectInjectionBean is manually looked up using BeanProvider because
not all containers may
+         * support injection into servlets.
+         */
+        ServletObjectInjectionBean bean =
+                BeanProvider.getContextualReference(ServletObjectInjectionBean.class);
+
+        ServletOutputStream stream = resp.getOutputStream();
+        logDetails(stream, "ServletRequest", bean.getServletRequest());
+        logDetails(stream, "HttpServletRequest", bean.getHttpServletRequest());
+        logDetails(stream, "ServletResponse", bean.getServletResponse());
+        logDetails(stream, "HttpServletResponse", bean.getHttpServletResponse());
+        logDetails(stream, "HttpSession", bean.getHttpSession());
+        logDetails(stream, "Principal", bean.getPrincipal());
+
+    }
+
+    /**
+     * Writes debug information to the supplied {@link ServletOutputStream}.
+     */
+    private void logDetails(ServletOutputStream stream, String name, Object obj)
+            throws IOException
+    {
+        stream.print("[");
+        stream.print(name);
+        stream.print("=");
+        stream.print(obj != null ? "OK" : "null");
+        stream.print("]\n");
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/cab0ba0e/deltaspike/modules/servlet/impl/src/test/java/org/apache/deltaspike/test/servlet/impl/producer/ServletObjectInjectionTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/servlet/impl/src/test/java/org/apache/deltaspike/test/servlet/impl/producer/ServletObjectInjectionTest.java
b/deltaspike/modules/servlet/impl/src/test/java/org/apache/deltaspike/test/servlet/impl/producer/ServletObjectInjectionTest.java
new file mode 100644
index 0000000..34f8fa9
--- /dev/null
+++ b/deltaspike/modules/servlet/impl/src/test/java/org/apache/deltaspike/test/servlet/impl/producer/ServletObjectInjectionTest.java
@@ -0,0 +1,112 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.deltaspike.test.servlet.impl.producer;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThat;
+
+import java.net.URL;
+
+import org.apache.deltaspike.test.category.WebProfileCategory;
+import org.apache.deltaspike.test.servlet.impl.Deployments;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.util.EntityUtils;
+import org.hamcrest.Matchers;
+import org.jboss.arquillian.container.test.api.Deployment;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.arquillian.test.api.ArquillianResource;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.asset.StringAsset;
+import org.jboss.shrinkwrap.api.spec.WebArchive;
+import org.jboss.shrinkwrap.descriptor.api.Descriptors;
+import org.jboss.shrinkwrap.descriptor.api.spec.servlet.web.WebAppDescriptor;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.junit.runner.RunWith;
+
+/**
+ * Test which validates that servlet objects are correctly injected.
+ * 
+ * @author Christian Kaltepoth
+ */
+@RunWith(Arquillian.class)
+@Category(WebProfileCategory.class)
+public class ServletObjectInjectionTest
+{
+
+    @Deployment(testable = false)
+    public static WebArchive getDeployment()
+    {
+        return ShrinkWrap.create(WebArchive.class, "test.war")
+                .addAsLibraries(Deployments.getDeltaSpikeCoreArchives())
+                .addAsLibraries(Deployments.getDeltaSpikeServletArchives())
+                .addClass(ServletObjectInjectionBean.class)
+                .addClass(ServletObjectInjectionServlet.class)
+                .addAsWebInfResource(new StringAsset("<beans/>"), "beans.xml")
+                .setWebXML(new StringAsset(
+                        Descriptors.create(WebAppDescriptor.class)
+                                .servlet(ServletObjectInjectionServlet.class, "/servlet-object-injecetion")
+                                .exportAsString()));
+
+    }
+
+    @ArquillianResource
+    private URL contextPath;
+
+    @Test
+    public void shouldInjectServletRequest() throws Exception
+    {
+        assertThat(responseFromServlet(), Matchers.containsString("[ServletRequest=OK]"));
+    }
+
+    @Test
+    public void shouldInjectHttpServletRequest() throws Exception
+    {
+        assertThat(responseFromServlet(), Matchers.containsString("[HttpServletRequest=OK]"));
+    }
+
+    @Test
+    public void shouldInjectServletResponse() throws Exception
+    {
+        assertThat(responseFromServlet(), Matchers.containsString("[ServletResponse=OK]"));
+    }
+
+    @Test
+    public void shouldInjectHttpServletResponse() throws Exception
+    {
+        assertThat(responseFromServlet(), Matchers.containsString("[HttpServletResponse=OK]"));
+    }
+
+    @Test
+    public void shouldInjectHttpSession() throws Exception
+    {
+        assertThat(responseFromServlet(), Matchers.containsString("[HttpSession=OK]"));
+    }
+
+    private String responseFromServlet() throws Exception
+    {
+        String url = new URL(contextPath, "servlet-object-injecetion").toString();
+        HttpResponse response = new DefaultHttpClient().execute(new HttpGet(url));
+        assertEquals(200, response.getStatusLine().getStatusCode());
+        return EntityUtils.toString(response.getEntity());
+    }
+
+}


Mime
View raw message