openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1766411 - in /openwebbeans/trunk: webbeans-impl/src/main/java/org/apache/webbeans/component/ webbeans-web/src/main/java/org/apache/webbeans/web/lifecycle/ webbeans-web/src/main/java/org/apache/webbeans/web/lifecycle/test/ webbeans-web/src/...
Date Mon, 24 Oct 2016 14:26:15 GMT
Author: rmannibucau
Date: Mon Oct 24 14:26:15 2016
New Revision: 1766411

URL: http://svn.apache.org/viewvc?rev=1766411&view=rev
Log:
OWB-1159 exposing http servlet request bean

Added:
    openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/lifecycle/ServletRequestBean.java
      - copied, changed from r1766409, openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/lifecycle/ServletContextBean.java
    openwebbeans/trunk/webbeans-web/src/test/java/org/apache/webbeans/web/tests/WebBeansTest.java
Modified:
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/WebBeansType.java
    openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/lifecycle/WebContainerLifecycle.java
    openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/lifecycle/test/MockServletContext.java

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/WebBeansType.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/WebBeansType.java?rev=1766411&r1=1766410&r2=1766411&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/WebBeansType.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/WebBeansType.java
Mon Oct 24 14:26:15 2016
@@ -71,5 +71,6 @@ public enum WebBeansType
     VALIDATIONFACT,
     VALIDATION,
     METADATA,
-    SERVLET_CONTEXT
+    SERVLET_CONTEXT,
+    SERVLET_REQUEST
 }
\ No newline at end of file

Copied: openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/lifecycle/ServletRequestBean.java
(from r1766409, openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/lifecycle/ServletContextBean.java)
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/lifecycle/ServletRequestBean.java?p2=openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/lifecycle/ServletRequestBean.java&p1=openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/lifecycle/ServletContextBean.java&r1=1766409&r2=1766411&rev=1766411&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/lifecycle/ServletContextBean.java
(original)
+++ openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/lifecycle/ServletRequestBean.java
Mon Oct 24 14:26:15 2016
@@ -23,21 +23,24 @@ import org.apache.webbeans.component.Sim
 import org.apache.webbeans.component.WebBeansType;
 import org.apache.webbeans.config.WebBeansContext;
 import org.apache.webbeans.portable.ProviderBasedProducer;
+import org.apache.webbeans.web.context.ServletRequestContext;
+import org.apache.webbeans.web.context.WebContextsService;
 
 import javax.inject.Provider;
-import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
 
-class ServletContextBean extends BuiltInOwbBean<ServletContext>
+class ServletRequestBean extends BuiltInOwbBean<HttpServletRequest>
 {
-    ServletContextBean(final WebBeansContext ctx, final ServletContext context)
+    ServletRequestBean(final WebBeansContext ctx, final WebContextsService contexts)
     {
-        super(ctx, WebBeansType.SERVLET_CONTEXT, ServletContext.class, new SimpleProducerFactory<ServletContext>(
-                new ProviderBasedProducer<>(ctx, ServletContext.class, new Provider<ServletContext>()
+        super(ctx, WebBeansType.SERVLET_REQUEST, HttpServletRequest.class, new SimpleProducerFactory<HttpServletRequest>(
+                new ProviderBasedProducer<>(ctx, HttpServletRequest.class, new Provider<HttpServletRequest>()
                 {
                     @Override
-                    public ServletContext get()
+                    public HttpServletRequest get()
                     {
-                        return context;
+                        final ServletRequestContext requestContext = contexts.getRequestContext(false);
+                        return requestContext == null ? null : requestContext.getServletRequest();
                     }
                 }, true)));
     }
@@ -45,6 +48,6 @@ class ServletContextBean extends BuiltIn
     @Override
     public Class<?> proxyableType()
     {
-        return ServletContext.class;
+        return HttpServletRequest.class;
     }
 }

Modified: openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/lifecycle/WebContainerLifecycle.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/lifecycle/WebContainerLifecycle.java?rev=1766411&r1=1766410&r2=1766411&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/lifecycle/WebContainerLifecycle.java
(original)
+++ openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/lifecycle/WebContainerLifecycle.java
Mon Oct 24 14:26:15 2016
@@ -25,8 +25,10 @@ import org.apache.webbeans.el.ELContextS
 import org.apache.webbeans.exception.WebBeansException;
 import org.apache.webbeans.lifecycle.AbstractLifeCycle;
 import org.apache.webbeans.logger.WebBeansLoggerFacade;
+import org.apache.webbeans.spi.ContextsService;
 import org.apache.webbeans.spi.ResourceInjectionService;
 import org.apache.webbeans.spi.adaptor.ELAdaptor;
+import org.apache.webbeans.web.context.WebContextsService;
 import org.apache.webbeans.web.util.ServletCompatibilityUtil;
 
 import javax.enterprise.context.ApplicationScoped;
@@ -86,6 +88,12 @@ public final class WebContainerLifecycle
         if ("true".equalsIgnoreCase(getWebBeansContext().getOpenWebBeansConfiguration().getProperty("org.apache.webbeans.web.add-beans",
"true")))
         {
             webBeansContext.getBeanManagerImpl().addInternalBean(new ServletContextBean(webBeansContext,
servletContext));
+
+            final ContextsService contextsService = webBeansContext.getContextsService();
+            if (WebContextsService.class.isInstance(contextsService))
+            {
+                webBeansContext.getBeanManagerImpl().addInternalBean(new ServletRequestBean(webBeansContext,
WebContextsService.class.cast(contextsService)));
+            }
         }
         super.startApplication(servletContext);
     }

Modified: openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/lifecycle/test/MockServletContext.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/lifecycle/test/MockServletContext.java?rev=1766411&r1=1766410&r2=1766411&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/lifecycle/test/MockServletContext.java
(original)
+++ openwebbeans/trunk/webbeans-web/src/main/java/org/apache/webbeans/web/lifecycle/test/MockServletContext.java
Mon Oct 24 14:26:15 2016
@@ -23,6 +23,7 @@ import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.Enumeration;
 import java.util.EventListener;
+import java.util.HashMap;
 import java.util.Map;
 import java.util.Set;
 
@@ -37,12 +38,16 @@ import javax.servlet.SessionCookieConfig
 import javax.servlet.SessionTrackingMode;
 import javax.servlet.descriptor.JspConfigDescriptor;
 
+import static java.util.Collections.enumeration;
+
 /**
  * Implement the ServletContext interface for testing.
  */
 public class MockServletContext implements ServletContext
 {
 
+    private Map<String, Object> attributes = new HashMap<>();
+
     @Override
     public String getContextPath()
     {
@@ -172,19 +177,19 @@ public class MockServletContext implemen
     @Override
     public Object getAttribute(String name)
     {
-        return null;
+        return attributes.get(name);
     }
 
     @Override
     public Enumeration<String> getAttributeNames()
     {
-        return null;
+        return enumeration(attributes.keySet());
     }
 
     @Override
     public void setAttribute(String name, Object object)
     {
-
+        attributes.put(name, object);
     }
 
     @Override

Added: openwebbeans/trunk/webbeans-web/src/test/java/org/apache/webbeans/web/tests/WebBeansTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-web/src/test/java/org/apache/webbeans/web/tests/WebBeansTest.java?rev=1766411&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-web/src/test/java/org/apache/webbeans/web/tests/WebBeansTest.java
(added)
+++ openwebbeans/trunk/webbeans-web/src/test/java/org/apache/webbeans/web/tests/WebBeansTest.java
Mon Oct 24 14:26:15 2016
@@ -0,0 +1,86 @@
+/*
+ * 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.webbeans.web.tests;
+
+import org.apache.webbeans.config.WebBeansContext;
+import org.apache.webbeans.corespi.scanner.xbean.CdiArchive;
+import org.apache.webbeans.corespi.scanner.xbean.OwbAnnotationFinder;
+import org.apache.webbeans.corespi.se.DefaultScannerService;
+import org.apache.webbeans.spi.ContainerLifecycle;
+import org.apache.webbeans.spi.ContextsService;
+import org.apache.webbeans.spi.ScannerService;
+import org.apache.webbeans.web.context.WebContextsService;
+import org.apache.webbeans.web.lifecycle.test.MockServletContext;
+import org.apache.webbeans.xml.DefaultBeanArchiveService;
+import org.apache.xbean.finder.archive.ClassesArchive;
+import org.junit.Test;
+
+import javax.enterprise.inject.spi.Bean;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletContextEvent;
+import javax.servlet.http.HttpServletRequest;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Properties;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+public class WebBeansTest {
+    @Test
+    public void implicitBeans()
+    {
+        final WebBeansContext webBeansContext = new WebBeansContext(new HashMap<Class<?>,
Object>(), new Properties()
+        {{
+            setProperty(ContextsService.class.getName(), WebContextsService.class.getName());
+            setProperty(ScannerService.class.getName(), NoScan.class.getName());
+        }});
+
+        final ServletContextEvent ctx = new ServletContextEvent(new MockServletContext()
+        {
+            {
+                setAttribute("test", "ok");
+            }
+        });
+        webBeansContext.getService(ContainerLifecycle.class).startApplication(ctx);
+        try
+        {
+            final BeanManager mgr = webBeansContext.getBeanManagerImpl();
+            final Bean<?> context = mgr.resolve(mgr.getBeans(ServletContext.class));
+            assertEquals("ok", ServletContext.class.cast(mgr.getReference(context, ServletContext.class,
null)).getAttribute("test"));
+            assertNotNull(context);
+            assertNotNull(mgr.resolve(mgr.getBeans(HttpServletRequest.class)));
+        }
+        finally
+        {
+            webBeansContext.getService(ContainerLifecycle.class).stopApplication(ctx);
+        }
+    }
+
+    public static class NoScan extends DefaultScannerService
+    {
+        @Override
+        public void scan()
+        {
+            archive = new CdiArchive(new DefaultBeanArchiveService(), Thread.currentThread().getContextClassLoader(),
new HashMap<String, URL>(), null);
+            finder = new OwbAnnotationFinder(new ClassesArchive());
+        }
+    }
+}



Mime
View raw message