Refactored servlet module code into multiple filters and listeners
Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/7df63a86 Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/7df63a86 Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/7df63a86 Branch: refs/heads/master Commit: 7df63a86762f9f0688a1d9699ccaae5612bfd11a Parents: 9fc325a Author: Christian Kaltepoth <[email protected]> Authored: Tue Jun 11 07:07:19 2013 +0200 Committer: Christian Kaltepoth <[email protected]> Committed: Thu Jun 13 06:53:29 2013 +0200 ---------------------------------------------------------------------- .../deltaspike/servlet/impl/EventEmitter.java | 43 ------- .../servlet/impl/RequestResponse.java | 65 ---------- .../servlet/impl/RequestResponseHolder.java | 92 ------------- .../servlet/impl/ServletContextHolder.java | 105 --------------- .../servlet/impl/ServletEventBridgeFilter.java | 87 ------------- .../impl/ServletEventBridgeListener.java | 69 ---------- .../servlet/impl/ServletObjectProducer.java | 128 ------------------- .../impl/event/EventBridgeContextListener.java | 48 +++++++ .../servlet/impl/event/EventBridgeFilter.java | 76 +++++++++++ .../impl/event/EventBridgeSessionListener.java | 47 +++++++ .../servlet/impl/event/EventEmitter.java | 43 +++++++ .../servlet/impl/produce/RequestResponse.java | 65 ++++++++++ .../impl/produce/RequestResponseHolder.java | 92 +++++++++++++ .../produce/RequestResponseHolderFilter.java | 67 ++++++++++ .../impl/produce/ServletContextHolder.java | 105 +++++++++++++++ .../produce/ServletContextHolderListener.java | 44 +++++++ .../impl/produce/ServletObjectProducer.java | 128 +++++++++++++++++++ .../main/resources/META-INF/web-fragment.xml | 33 ++++- 18 files changed, 742 insertions(+), 595 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/deltaspike/blob/7df63a86/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/EventEmitter.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/EventEmitter.java b/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/EventEmitter.java deleted file mode 100644 index a9af77e..0000000 --- a/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/EventEmitter.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * 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.lang.annotation.Annotation; - -import org.apache.deltaspike.core.api.provider.BeanManagerProvider; - -/** - * Base class for classes which send servlet events to the CDI event bus. This class uses {@link BeanManagerProvider} to - * obtain the BeanManager. - * - * @author Christian Kaltepoth - */ -abstract class EventEmitter -{ - - protected void fireEvent(Object event, Annotation... qualifier) - { - /* - * No need to cache the BeanManager reference because the providers already does this on a context class loader - * level. - */ - BeanManagerProvider.getInstance().getBeanManager().fireEvent(event, qualifier); - } - -} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/7df63a86/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 deleted file mode 100644 index 16b3d28..0000000 --- a/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/RequestResponse.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * 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/7df63a86/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 deleted file mode 100644 index add047c..0000000 --- a/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/RequestResponseHolder.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * 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/7df63a86/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/ServletContextHolder.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/ServletContextHolder.java b/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/ServletContextHolder.java deleted file mode 100644 index e590cc7..0000000 --- a/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/ServletContextHolder.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * 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.util.Collections; -import java.util.Map; -import java.util.WeakHashMap; -import java.util.logging.Logger; - -import javax.servlet.ServletContext; - -/** - * This class holds the {@link ServletContext} for each context class loader. - * - * @author Christian Kaltepoth - */ -class ServletContextHolder -{ - - private static final Logger log = Logger.getLogger(ServletContextHolder.class.getName()); - - private static final Map<ClassLoader, ServletContext> CONTEXT_BY_CLASSLOADER = Collections.synchronizedMap( - new WeakHashMap<ClassLoader, ServletContext>()); - - private ServletContextHolder() - { - // hide constructor - } - - /** - * Bind the supplied {@link ServletContext} to the current context class loader. Subsequent calls to {@link #get()} - * with the same context class loader will always return this context. - * - * @param servletContext - * The context to bind to the context class loader - */ - static void bind(ServletContext servletContext) - { - ClassLoader classLoader = getContextClassLoader(); - ServletContext existingContext = CONTEXT_BY_CLASSLOADER.put(classLoader, servletContext); - if (existingContext != null) - { - throw new IllegalArgumentException("There is already a ServletContext associated with class loader: " - + classLoader); - } - } - - /** - * Returns the {@link ServletContext} associated with the current context class loader. - * - * @throws IllegalStateException - * if there is no {@link ServletContext} stored for the current context class loader - */ - static ServletContext get() - { - ClassLoader classLoader = getContextClassLoader(); - ServletContext servletContext = CONTEXT_BY_CLASSLOADER.get(classLoader); - if (servletContext == null) - { - throw new IllegalStateException("There is no ServletContext stored for class loader: " + classLoader); - } - return servletContext; - } - - /** - * Releases the {@link ServletContext} from the current context class loader. Subsequent calls to {@link #get()} - * with the same context class loader will return <code>null</code>. - */ - static void release() - { - ClassLoader classLoader = getContextClassLoader(); - ServletContext removedContext = CONTEXT_BY_CLASSLOADER.remove(classLoader); - if (removedContext == null) - { - log.warning("Cannot find a ServletContext to release for class loader: " + classLoader); - } - } - - private static ClassLoader getContextClassLoader() - { - ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); - if (classLoader == null) - { - throw new IllegalStateException("Unable to obtain the context class loader for the current thread"); - } - return classLoader; - } - -} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/7df63a86/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 deleted file mode 100644 index 182f185..0000000 --- a/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/ServletEventBridgeFilter.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * 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.io.IOException; - -import javax.servlet.Filter; -import javax.servlet.FilterChain; -import javax.servlet.FilterConfig; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; - -import org.apache.deltaspike.servlet.api.literal.DestroyedLiteral; -import org.apache.deltaspike.servlet.api.literal.InitializedLiteral; -import org.apache.deltaspike.servlet.api.literal.WebLiteral; - -/** - * @author Christian Kaltepoth - */ -public class ServletEventBridgeFilter extends EventEmitter implements Filter -{ - - @Override - public void init(FilterConfig config) throws ServletException - { - // nothing yet - } - - @Override - public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, - ServletException - { - - // store the request/response in a holder to access it later - RequestResponseHolder.bind(request, response); - try - { - - // 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 - { - RequestResponseHolder.release(); - } - - } - - @Override - public void destroy() - { - // nothing yet - } - -} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/7df63a86/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/ServletEventBridgeListener.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/ServletEventBridgeListener.java b/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/ServletEventBridgeListener.java deleted file mode 100644 index 21f7450..0000000 --- a/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/ServletEventBridgeListener.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * 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.ServletContextEvent; -import javax.servlet.ServletContextListener; -import javax.servlet.http.HttpSessionEvent; -import javax.servlet.http.HttpSessionListener; - -import org.apache.deltaspike.servlet.api.literal.DestroyedLiteral; -import org.apache.deltaspike.servlet.api.literal.InitializedLiteral; -import org.apache.deltaspike.servlet.api.literal.WebLiteral; - -/** - * This class listens for various servlet events and forwards them to the CDI event bus. - * - * @author Christian Kaltepoth - */ -public class ServletEventBridgeListener extends EventEmitter implements ServletContextListener, HttpSessionListener -{ - - @Override - public void contextInitialized(ServletContextEvent sce) - { - ServletContextHolder.bind(sce.getServletContext()); - fireEvent(sce.getServletContext(), WebLiteral.INSTANCE, InitializedLiteral.INSTANCE); - } - - @Override - public void contextDestroyed(ServletContextEvent sce) - { - try - { - fireEvent(sce.getServletContext(), WebLiteral.INSTANCE, DestroyedLiteral.INSTANCE); - } - finally - { - ServletContextHolder.release(); - } - } - - @Override - public void sessionCreated(HttpSessionEvent se) - { - fireEvent(se.getSession(), WebLiteral.INSTANCE, InitializedLiteral.INSTANCE); - } - - @Override - public void sessionDestroyed(HttpSessionEvent se) - { - fireEvent(se.getSession(), WebLiteral.INSTANCE, DestroyedLiteral.INSTANCE); - } -} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/7df63a86/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 deleted file mode 100644 index 07d1798..0000000 --- a/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/ServletObjectProducer.java +++ /dev/null @@ -1,128 +0,0 @@ -/* - * 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.ServletContext; -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 ServletContext}</li> - * <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 - public ServletContext getServletContext() - { - return ServletContextHolder.get(); - } - - @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/7df63a86/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/event/EventBridgeContextListener.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/event/EventBridgeContextListener.java b/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/event/EventBridgeContextListener.java new file mode 100644 index 0000000..a993bce --- /dev/null +++ b/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/event/EventBridgeContextListener.java @@ -0,0 +1,48 @@ +/* + * 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.event; + +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; + +import org.apache.deltaspike.servlet.api.literal.DestroyedLiteral; +import org.apache.deltaspike.servlet.api.literal.InitializedLiteral; +import org.apache.deltaspike.servlet.api.literal.WebLiteral; + +/** + * This class listens for servlet context events and forwards them to the CDI event bus. + * + * @author Christian Kaltepoth + */ +public class EventBridgeContextListener extends EventEmitter implements ServletContextListener +{ + + @Override + public void contextInitialized(ServletContextEvent sce) + { + fireEvent(sce.getServletContext(), WebLiteral.INSTANCE, InitializedLiteral.INSTANCE); + } + + @Override + public void contextDestroyed(ServletContextEvent sce) + { + fireEvent(sce.getServletContext(), WebLiteral.INSTANCE, DestroyedLiteral.INSTANCE); + } + +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/7df63a86/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/event/EventBridgeFilter.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/event/EventBridgeFilter.java b/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/event/EventBridgeFilter.java new file mode 100644 index 0000000..79db1a7 --- /dev/null +++ b/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/event/EventBridgeFilter.java @@ -0,0 +1,76 @@ +/* + * 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.event; + +import java.io.IOException; + +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; + +import org.apache.deltaspike.servlet.api.literal.DestroyedLiteral; +import org.apache.deltaspike.servlet.api.literal.InitializedLiteral; +import org.apache.deltaspike.servlet.api.literal.WebLiteral; + +/** + * This filter sends events to the CDI event bus when requests and responses get created and destroyed. + * + * @author Christian Kaltepoth + */ +public class EventBridgeFilter extends EventEmitter implements Filter +{ + + @Override + public void init(FilterConfig config) throws ServletException + { + // nothing yet + } + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, + ServletException + { + + // 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); + } + + } + + @Override + public void destroy() + { + // nothing yet + } + +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/7df63a86/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/event/EventBridgeSessionListener.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/event/EventBridgeSessionListener.java b/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/event/EventBridgeSessionListener.java new file mode 100644 index 0000000..691f3b9 --- /dev/null +++ b/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/event/EventBridgeSessionListener.java @@ -0,0 +1,47 @@ +/* + * 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.event; + +import javax.servlet.http.HttpSessionEvent; +import javax.servlet.http.HttpSessionListener; + +import org.apache.deltaspike.servlet.api.literal.DestroyedLiteral; +import org.apache.deltaspike.servlet.api.literal.InitializedLiteral; +import org.apache.deltaspike.servlet.api.literal.WebLiteral; + +/** + * This class listens for HTTP session events and forwards them to the CDI event bus. + * + * @author Christian Kaltepoth + */ +public class EventBridgeSessionListener extends EventEmitter implements HttpSessionListener +{ + + @Override + public void sessionCreated(HttpSessionEvent se) + { + fireEvent(se.getSession(), WebLiteral.INSTANCE, InitializedLiteral.INSTANCE); + } + + @Override + public void sessionDestroyed(HttpSessionEvent se) + { + fireEvent(se.getSession(), WebLiteral.INSTANCE, DestroyedLiteral.INSTANCE); + } +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/7df63a86/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/event/EventEmitter.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/event/EventEmitter.java b/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/event/EventEmitter.java new file mode 100644 index 0000000..899e3f7 --- /dev/null +++ b/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/event/EventEmitter.java @@ -0,0 +1,43 @@ +/* + * 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.event; + +import java.lang.annotation.Annotation; + +import org.apache.deltaspike.core.api.provider.BeanManagerProvider; + +/** + * Base class for classes which send servlet events to the CDI event bus. This class uses {@link BeanManagerProvider} to + * obtain the BeanManager. + * + * @author Christian Kaltepoth + */ +abstract class EventEmitter +{ + + protected void fireEvent(Object event, Annotation... qualifier) + { + /* + * No need to cache the BeanManager reference because the providers already does this on a context class loader + * level. + */ + BeanManagerProvider.getInstance().getBeanManager().fireEvent(event, qualifier); + } + +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/7df63a86/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/produce/RequestResponse.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/produce/RequestResponse.java b/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/produce/RequestResponse.java new file mode 100644 index 0000000..fa257cb --- /dev/null +++ b/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/produce/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.produce; + +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/7df63a86/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/produce/RequestResponseHolder.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/produce/RequestResponseHolder.java b/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/produce/RequestResponseHolder.java new file mode 100644 index 0000000..3996dda --- /dev/null +++ b/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/produce/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.produce; + +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/7df63a86/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/produce/RequestResponseHolderFilter.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/produce/RequestResponseHolderFilter.java b/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/produce/RequestResponseHolderFilter.java new file mode 100644 index 0000000..932875c --- /dev/null +++ b/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/produce/RequestResponseHolderFilter.java @@ -0,0 +1,67 @@ +/* + * 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.produce; + +import java.io.IOException; + +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; + +/** + * This filter stores the current request and response in the {@link RequestResponseHolder}. + * + * @author Christian Kaltepoth + */ +public class RequestResponseHolderFilter implements Filter +{ + + @Override + public void init(FilterConfig config) throws ServletException + { + // nothing yet + } + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, + ServletException + { + + RequestResponseHolder.bind(request, response); + try + { + chain.doFilter(request, response); + } + finally + { + RequestResponseHolder.release(); + } + + } + + @Override + public void destroy() + { + // nothing yet + } + +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/7df63a86/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/produce/ServletContextHolder.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/produce/ServletContextHolder.java b/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/produce/ServletContextHolder.java new file mode 100644 index 0000000..14044d8 --- /dev/null +++ b/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/produce/ServletContextHolder.java @@ -0,0 +1,105 @@ +/* + * 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.produce; + +import java.util.Collections; +import java.util.Map; +import java.util.WeakHashMap; +import java.util.logging.Logger; + +import javax.servlet.ServletContext; + +/** + * This class holds the {@link ServletContext} for each context class loader. + * + * @author Christian Kaltepoth + */ +class ServletContextHolder +{ + + private static final Logger log = Logger.getLogger(ServletContextHolder.class.getName()); + + private static final Map<ClassLoader, ServletContext> CONTEXT_BY_CLASSLOADER = Collections.synchronizedMap( + new WeakHashMap<ClassLoader, ServletContext>()); + + private ServletContextHolder() + { + // hide constructor + } + + /** + * Bind the supplied {@link ServletContext} to the current context class loader. Subsequent calls to {@link #get()} + * with the same context class loader will always return this context. + * + * @param servletContext + * The context to bind to the context class loader + */ + static void bind(ServletContext servletContext) + { + ClassLoader classLoader = getContextClassLoader(); + ServletContext existingContext = CONTEXT_BY_CLASSLOADER.put(classLoader, servletContext); + if (existingContext != null) + { + throw new IllegalArgumentException("There is already a ServletContext associated with class loader: " + + classLoader); + } + } + + /** + * Returns the {@link ServletContext} associated with the current context class loader. + * + * @throws IllegalStateException + * if there is no {@link ServletContext} stored for the current context class loader + */ + static ServletContext get() + { + ClassLoader classLoader = getContextClassLoader(); + ServletContext servletContext = CONTEXT_BY_CLASSLOADER.get(classLoader); + if (servletContext == null) + { + throw new IllegalStateException("There is no ServletContext stored for class loader: " + classLoader); + } + return servletContext; + } + + /** + * Releases the {@link ServletContext} from the current context class loader. Subsequent calls to {@link #get()} + * with the same context class loader will return <code>null</code>. + */ + static void release() + { + ClassLoader classLoader = getContextClassLoader(); + ServletContext removedContext = CONTEXT_BY_CLASSLOADER.remove(classLoader); + if (removedContext == null) + { + log.warning("Cannot find a ServletContext to release for class loader: " + classLoader); + } + } + + private static ClassLoader getContextClassLoader() + { + ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); + if (classLoader == null) + { + throw new IllegalStateException("Unable to obtain the context class loader for the current thread"); + } + return classLoader; + } + +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/7df63a86/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/produce/ServletContextHolderListener.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/produce/ServletContextHolderListener.java b/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/produce/ServletContextHolderListener.java new file mode 100644 index 0000000..88e1246 --- /dev/null +++ b/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/produce/ServletContextHolderListener.java @@ -0,0 +1,44 @@ +/* + * 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.produce; + +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; + +/** + * This class stores the ServletContext in the {@link ServletContextHolder}. + * + * @author Christian Kaltepoth + */ +public class ServletContextHolderListener implements ServletContextListener +{ + + @Override + public void contextInitialized(ServletContextEvent sce) + { + ServletContextHolder.bind(sce.getServletContext()); + } + + @Override + public void contextDestroyed(ServletContextEvent sce) + { + ServletContextHolder.release(); + } + +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/7df63a86/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/produce/ServletObjectProducer.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/produce/ServletObjectProducer.java b/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/produce/ServletObjectProducer.java new file mode 100644 index 0000000..68534f9 --- /dev/null +++ b/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/produce/ServletObjectProducer.java @@ -0,0 +1,128 @@ +/* + * 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.produce; + +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.ServletContext; +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 ServletContext}</li> + * <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 + public ServletContext getServletContext() + { + return ServletContextHolder.get(); + } + + @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/7df63a86/deltaspike/modules/servlet/impl/src/main/resources/META-INF/web-fragment.xml ---------------------------------------------------------------------- diff --git a/deltaspike/modules/servlet/impl/src/main/resources/META-INF/web-fragment.xml b/deltaspike/modules/servlet/impl/src/main/resources/META-INF/web-fragment.xml index af56b7d..d8ec9f6 100644 --- a/deltaspike/modules/servlet/impl/src/main/resources/META-INF/web-fragment.xml +++ b/deltaspike/modules/servlet/impl/src/main/resources/META-INF/web-fragment.xml @@ -2,6 +2,8 @@ <web-fragment version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-fragment_3_0.xsd"> + <name>deltaspike_servlet_module</name> + <distributable /> <ordering> @@ -11,18 +13,37 @@ </ordering> <listener> - <display-name>ServletEventBridgeListener</display-name> - <listener-class>org.apache.deltaspike.servlet.impl.ServletEventBridgeListener</listener-class> + <display-name>EventBridgeContextListener</display-name> + <listener-class>org.apache.deltaspike.servlet.impl.event.EventBridgeContextListener</listener-class> + </listener> + + <listener> + <display-name>EventBridgeSessionListener</display-name> + <listener-class>org.apache.deltaspike.servlet.impl.event.EventBridgeSessionListener</listener-class> + </listener> + + <listener> + <display-name>ServletContextHolderListener</display-name> + <listener-class>org.apache.deltaspike.servlet.impl.produce.ServletContextHolderListener</listener-class> </listener> <filter> - <display-name>ServletEventBridgeFilter</display-name> - <filter-name>ServletEventBridgeFilter</filter-name> - <filter-class>org.apache.deltaspike.servlet.impl.ServletEventBridgeFilter</filter-class> + <display-name>RequestResponseHolderFilter</display-name> + <filter-name>RequestResponseHolderFilter</filter-name> + <filter-class>org.apache.deltaspike.servlet.impl.produce.RequestResponseHolderFilter</filter-class> </filter> + <filter-mapping> + <filter-name>RequestResponseHolderFilter</filter-name> + <url-pattern>/*</url-pattern> + </filter-mapping> + <filter> + <display-name>EventBridgeFilter</display-name> + <filter-name>EventBridgeFilter</filter-name> + <filter-class>org.apache.deltaspike.servlet.impl.event.EventBridgeFilter</filter-class> + </filter> <filter-mapping> - <filter-name>ServletEventBridgeFilter</filter-name> + <filter-name>EventBridgeFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
