This is an automated email from the ASF dual-hosted git repository. cziegeler pushed a commit to branch http/jakarta in repository https://gitbox.apache.org/repos/asf/felix-dev.git
commit 18fbba744a9a3c253f9bb0e0a0ce4aa740b471e7 Author: Carsten Ziegeler <czieg...@adobe.com> AuthorDate: Tue Jan 4 16:42:21 2022 +0100 Fix exception handling and default servlet context --- .../http/base/internal/dispatch/Dispatcher.java | 6 ++- .../jakartawrappers/ServletContextWrapper.java | 26 ++++++------ .../jakartawrappers/ServletExceptionUtil.java | 16 ++------ ...ptionUtil.java => ServletExceptionWrapper.java} | 37 ++++++++--------- .../javaxwrappers/ServletContextWrapper.java | 26 ++++++------ .../javaxwrappers/ServletExceptionUtil.java | 16 ++------ .../javaxwrappers/ServletExceptionWrapper.java | 47 ++++++++++++++++++++++ .../base/internal/service/ServletContextImpl.java | 6 +-- .../internal/whiteboard/WhiteboardManager.java | 36 ++++++++++++++++- .../tracker/JavaxServletContextHelperTracker.java | 14 +++++-- 10 files changed, 148 insertions(+), 82 deletions(-) diff --git a/http/base/src/main/java/org/apache/felix/http/base/internal/dispatch/Dispatcher.java b/http/base/src/main/java/org/apache/felix/http/base/internal/dispatch/Dispatcher.java index 95b0233..71e4bd3 100644 --- a/http/base/src/main/java/org/apache/felix/http/base/internal/dispatch/Dispatcher.java +++ b/http/base/src/main/java/org/apache/felix/http/base/internal/dispatch/Dispatcher.java @@ -22,6 +22,7 @@ import java.util.Set; import org.apache.felix.http.base.internal.context.ExtServletContext; import org.apache.felix.http.base.internal.handler.FilterHandler; import org.apache.felix.http.base.internal.handler.HttpSessionWrapper; +import org.apache.felix.http.base.internal.jakartawrappers.ServletExceptionWrapper; import org.apache.felix.http.base.internal.logger.SystemLogger; import org.apache.felix.http.base.internal.registry.HandlerRegistry; import org.apache.felix.http.base.internal.registry.PathResolution; @@ -152,8 +153,11 @@ public final class Dispatcher filterChain.doFilter(wrappedRequest, wrappedResponse); } - catch ( final Exception e) + catch ( Exception e) { + if ( e instanceof ServletExceptionWrapper ) { + e = ((ServletExceptionWrapper)e).getException(); + } SystemLogger.error("Exception while processing request to " + requestURI, e); req.setAttribute(RequestDispatcher.ERROR_EXCEPTION, e); req.setAttribute(RequestDispatcher.ERROR_EXCEPTION_TYPE, e.getClass().getName()); diff --git a/http/base/src/main/java/org/apache/felix/http/base/internal/jakartawrappers/ServletContextWrapper.java b/http/base/src/main/java/org/apache/felix/http/base/internal/jakartawrappers/ServletContextWrapper.java index 62a03ad..e936fcc 100644 --- a/http/base/src/main/java/org/apache/felix/http/base/internal/jakartawrappers/ServletContextWrapper.java +++ b/http/base/src/main/java/org/apache/felix/http/base/internal/jakartawrappers/ServletContextWrapper.java @@ -212,22 +212,22 @@ public class ServletContextWrapper implements ServletContext { @Override public ServletRegistration.Dynamic addServlet(final String servletName, final String className) { - throw new IllegalStateException(); + throw new UnsupportedOperationException(); } @Override public ServletRegistration.Dynamic addServlet(final String servletName, final Servlet servlet) { - throw new IllegalStateException(); + throw new UnsupportedOperationException(); } @Override public ServletRegistration.Dynamic addServlet(final String servletName, final Class<? extends Servlet> servletClass) { - throw new IllegalStateException(); + throw new UnsupportedOperationException(); } @Override public <T extends Servlet> T createServlet(final Class<T> clazz) throws ServletException { - throw new IllegalStateException(); + throw new UnsupportedOperationException(); } @Override @@ -251,22 +251,22 @@ public class ServletContextWrapper implements ServletContext { @Override public FilterRegistration.Dynamic addFilter(final String filterName, final String className) { - throw new IllegalStateException(); + throw new UnsupportedOperationException(); } @Override public FilterRegistration.Dynamic addFilter(final String filterName, final Filter filter) { - throw new IllegalStateException(); + throw new UnsupportedOperationException(); } @Override public FilterRegistration.Dynamic addFilter(final String filterName, final Class<? extends Filter> filterClass) { - throw new IllegalStateException(); + throw new UnsupportedOperationException(); } @Override public <T extends Filter> T createFilter(final Class<T> clazz) throws ServletException { - throw new IllegalStateException(); + throw new UnsupportedOperationException(); } @Override @@ -342,22 +342,22 @@ public class ServletContextWrapper implements ServletContext { @Override public void addListener(String className) { - throw new IllegalStateException(); + throw new UnsupportedOperationException(); } @Override public <T extends EventListener> void addListener(final T t) { - throw new IllegalStateException(); + throw new UnsupportedOperationException(); } @Override public void addListener(final Class<? extends EventListener> listenerClass) { - throw new IllegalStateException(); + throw new UnsupportedOperationException(); } @Override public <T extends EventListener> T createListener(final Class<T> clazz) throws ServletException { - throw new IllegalStateException(); + throw new UnsupportedOperationException(); } @Override @@ -382,7 +382,7 @@ public class ServletContextWrapper implements ServletContext { @Override public Dynamic addJspFile(final String servletName, final String jspFile) { - throw new IllegalStateException(); + throw new UnsupportedOperationException(); } @Override diff --git a/http/base/src/main/java/org/apache/felix/http/base/internal/jakartawrappers/ServletExceptionUtil.java b/http/base/src/main/java/org/apache/felix/http/base/internal/jakartawrappers/ServletExceptionUtil.java index ee029fd..b55a604 100644 --- a/http/base/src/main/java/org/apache/felix/http/base/internal/jakartawrappers/ServletExceptionUtil.java +++ b/http/base/src/main/java/org/apache/felix/http/base/internal/jakartawrappers/ServletExceptionUtil.java @@ -19,7 +19,6 @@ package org.apache.felix.http.base.internal.jakartawrappers; import org.jetbrains.annotations.NotNull; import jakarta.servlet.ServletException; -import jakarta.servlet.UnavailableException; /** * Helper class to wrap servlet exceptions @@ -32,19 +31,10 @@ public class ServletExceptionUtil { * @return The exception */ public static ServletException getServletException(@NotNull final javax.servlet.ServletException e) { - if ( e instanceof javax.servlet.UnavailableException ) { - final UnavailableException t = new UnavailableException(e.getMessage(), ((javax.servlet.UnavailableException)e).getUnavailableSeconds()); - if ( e.getCause() != null ) { - t.initCause(e.getCause()); - } - return t; + if ( e instanceof org.apache.felix.http.base.internal.javaxwrappers.ServletExceptionWrapper) { + return ((org.apache.felix.http.base.internal.javaxwrappers.ServletExceptionWrapper)e).getException(); } - if ( e.getCause() instanceof ServletException ) { - return (ServletException)e.getCause(); - } - final ServletException ne = new ServletException(e.getMessage(), e.getCause()); - ne.setStackTrace(e.getStackTrace()); - return ne; + return new ServletExceptionWrapper(e); } } diff --git a/http/base/src/main/java/org/apache/felix/http/base/internal/jakartawrappers/ServletExceptionUtil.java b/http/base/src/main/java/org/apache/felix/http/base/internal/jakartawrappers/ServletExceptionWrapper.java similarity index 51% copy from http/base/src/main/java/org/apache/felix/http/base/internal/jakartawrappers/ServletExceptionUtil.java copy to http/base/src/main/java/org/apache/felix/http/base/internal/jakartawrappers/ServletExceptionWrapper.java index ee029fd..cc835a2 100644 --- a/http/base/src/main/java/org/apache/felix/http/base/internal/jakartawrappers/ServletExceptionUtil.java +++ b/http/base/src/main/java/org/apache/felix/http/base/internal/jakartawrappers/ServletExceptionWrapper.java @@ -19,32 +19,29 @@ package org.apache.felix.http.base.internal.jakartawrappers; import org.jetbrains.annotations.NotNull; import jakarta.servlet.ServletException; -import jakarta.servlet.UnavailableException; /** - * Helper class to wrap servlet exceptions + * Wrapper for servlet exception */ -public class ServletExceptionUtil { +public class ServletExceptionWrapper extends ServletException { + + private static final long serialVersionUID = 1L; + + private final javax.servlet.ServletException exception; /** - * Throw jakarta servlet exception - * @param e javax exception - * @return The exception + * Create new wrapepr exception + * @param e Original exception */ - public static ServletException getServletException(@NotNull final javax.servlet.ServletException e) { - if ( e instanceof javax.servlet.UnavailableException ) { - final UnavailableException t = new UnavailableException(e.getMessage(), ((javax.servlet.UnavailableException)e).getUnavailableSeconds()); - if ( e.getCause() != null ) { - t.initCause(e.getCause()); - } - return t; - } - if ( e.getCause() instanceof ServletException ) { - return (ServletException)e.getCause(); - } - final ServletException ne = new ServletException(e.getMessage(), e.getCause()); - ne.setStackTrace(e.getStackTrace()); - return ne; + public ServletExceptionWrapper(@NotNull final javax.servlet.ServletException e) { + this.exception = e; } + /** + * Get the original exception + * @return The original exception + */ + @NotNull public javax.servlet.ServletException getException() { + return this.exception; + } } diff --git a/http/base/src/main/java/org/apache/felix/http/base/internal/javaxwrappers/ServletContextWrapper.java b/http/base/src/main/java/org/apache/felix/http/base/internal/javaxwrappers/ServletContextWrapper.java index 21a9585..199c9f6 100644 --- a/http/base/src/main/java/org/apache/felix/http/base/internal/javaxwrappers/ServletContextWrapper.java +++ b/http/base/src/main/java/org/apache/felix/http/base/internal/javaxwrappers/ServletContextWrapper.java @@ -207,22 +207,22 @@ public class ServletContextWrapper implements javax.servlet.ServletContext { @Override public javax.servlet.ServletRegistration.Dynamic addServlet(final String servletName, final String className) { - throw new IllegalStateException(); + throw new UnsupportedOperationException(); } @Override public javax.servlet.ServletRegistration.Dynamic addServlet(final String servletName, final javax.servlet.Servlet servlet) { - throw new IllegalStateException(); + throw new UnsupportedOperationException(); } @Override public javax.servlet.ServletRegistration.Dynamic addServlet(final String servletName, final Class<? extends javax.servlet.Servlet> servletClass) { - throw new IllegalStateException(); + throw new UnsupportedOperationException(); } @Override public <T extends javax.servlet.Servlet> T createServlet(final Class<T> clazz) throws javax.servlet.ServletException { - throw new IllegalStateException(); + throw new UnsupportedOperationException(); } @Override @@ -246,22 +246,22 @@ public class ServletContextWrapper implements javax.servlet.ServletContext { @Override public javax.servlet.FilterRegistration.Dynamic addFilter(final String filterName, final String className) { - throw new IllegalStateException(); + throw new UnsupportedOperationException(); } @Override public javax.servlet.FilterRegistration.Dynamic addFilter(final String filterName, final javax.servlet.Filter filter) { - throw new IllegalStateException(); + throw new UnsupportedOperationException(); } @Override public javax.servlet.FilterRegistration.Dynamic addFilter(final String filterName, final Class<? extends javax.servlet.Filter> filterClass) { - throw new IllegalStateException(); + throw new UnsupportedOperationException(); } @Override public <T extends javax.servlet.Filter> T createFilter(final Class<T> clazz) throws javax.servlet.ServletException { - throw new IllegalStateException(); + throw new UnsupportedOperationException(); } @Override @@ -341,22 +341,22 @@ public class ServletContextWrapper implements javax.servlet.ServletContext { @Override public void addListener(String className) { - throw new IllegalStateException(); + throw new UnsupportedOperationException(); } @Override public <T extends EventListener> void addListener(final T t) { - throw new IllegalStateException(); + throw new UnsupportedOperationException(); } @Override public void addListener(final Class<? extends EventListener> listenerClass) { - throw new IllegalStateException(); + throw new UnsupportedOperationException(); } @Override public <T extends EventListener> T createListener(final Class<T> clazz) throws javax.servlet.ServletException { - throw new IllegalStateException(); + throw new UnsupportedOperationException(); } @Override @@ -381,7 +381,7 @@ public class ServletContextWrapper implements javax.servlet.ServletContext { @Override public javax.servlet.ServletRegistration.Dynamic addJspFile(final String servletName, final String jspFile) { - throw new IllegalStateException(); + throw new UnsupportedOperationException(); } @Override diff --git a/http/base/src/main/java/org/apache/felix/http/base/internal/javaxwrappers/ServletExceptionUtil.java b/http/base/src/main/java/org/apache/felix/http/base/internal/javaxwrappers/ServletExceptionUtil.java index b7ac31d..ad7ecb7 100644 --- a/http/base/src/main/java/org/apache/felix/http/base/internal/javaxwrappers/ServletExceptionUtil.java +++ b/http/base/src/main/java/org/apache/felix/http/base/internal/javaxwrappers/ServletExceptionUtil.java @@ -19,7 +19,6 @@ package org.apache.felix.http.base.internal.javaxwrappers; import org.jetbrains.annotations.NotNull; import jakarta.servlet.ServletException; -import jakarta.servlet.UnavailableException; /** * Helper class to wrap servlet exceptions @@ -32,19 +31,10 @@ public class ServletExceptionUtil { * @return The exception */ public static javax.servlet.ServletException getServletException(@NotNull final ServletException e) { - if ( e instanceof UnavailableException ) { - final javax.servlet.UnavailableException t = new javax.servlet.UnavailableException(e.getMessage(), ((UnavailableException)e).getUnavailableSeconds()); - if ( e.getCause() != null ) { - t.initCause(e.getCause()); - } - return t; + if ( e instanceof org.apache.felix.http.base.internal.jakartawrappers.ServletExceptionWrapper) { + return ((org.apache.felix.http.base.internal.jakartawrappers.ServletExceptionWrapper)e).getException(); } - if ( e.getCause() instanceof javax.servlet.ServletException ) { - return (javax.servlet.ServletException)e.getCause(); - } - final javax.servlet.ServletException ne = new javax.servlet.ServletException(e.getMessage(), e.getCause()); - ne.setStackTrace(e.getStackTrace()); - return ne; + return new ServletExceptionWrapper(e); } } diff --git a/http/base/src/main/java/org/apache/felix/http/base/internal/javaxwrappers/ServletExceptionWrapper.java b/http/base/src/main/java/org/apache/felix/http/base/internal/javaxwrappers/ServletExceptionWrapper.java new file mode 100644 index 0000000..5ec1a31 --- /dev/null +++ b/http/base/src/main/java/org/apache/felix/http/base/internal/javaxwrappers/ServletExceptionWrapper.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.felix.http.base.internal.javaxwrappers; + +import org.jetbrains.annotations.NotNull; + +import jakarta.servlet.ServletException; + +/** + * Wrapper for servlet exception + */ +public class ServletExceptionWrapper extends javax.servlet.ServletException { + + private static final long serialVersionUID = 1L; + + private final ServletException exception; + + /** + * Create new wrapepr exception + * @param e Original exception + */ + public ServletExceptionWrapper(@NotNull final ServletException e) { + this.exception = e; + } + + /** + * Get the original exception + * @return The original exception + */ + @NotNull public ServletException getException() { + return this.exception; + } +} diff --git a/http/base/src/main/java/org/apache/felix/http/base/internal/service/ServletContextImpl.java b/http/base/src/main/java/org/apache/felix/http/base/internal/service/ServletContextImpl.java index 73f7f64..c472feb 100644 --- a/http/base/src/main/java/org/apache/felix/http/base/internal/service/ServletContextImpl.java +++ b/http/base/src/main/java/org/apache/felix/http/base/internal/service/ServletContextImpl.java @@ -586,7 +586,7 @@ public class ServletContextImpl implements ExtServletContext @Override public void setSessionTimeout(final int sessionTimeout) { - throw new IllegalStateException(); + throw new UnsupportedOperationException(); } @Override @@ -596,7 +596,7 @@ public class ServletContextImpl implements ExtServletContext @Override public void setRequestCharacterEncoding(final String encoding) { - throw new IllegalStateException(); + throw new UnsupportedOperationException(); } @Override @@ -606,7 +606,7 @@ public class ServletContextImpl implements ExtServletContext @Override public void setResponseCharacterEncoding(final String encoding) { - throw new IllegalStateException(); + throw new UnsupportedOperationException(); } @Override diff --git a/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/WhiteboardManager.java b/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/WhiteboardManager.java index 3e06ee3..75d6771 100644 --- a/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/WhiteboardManager.java +++ b/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/WhiteboardManager.java @@ -126,6 +126,8 @@ public final class WhiteboardManager private volatile ServiceRegistration<ServletContextHelper> defaultContextRegistration; + private volatile ServiceRegistration<org.osgi.service.http.context.ServletContextHelper> defaultJavaxContextRegistration; + /** * Create a new whiteboard http manager * @@ -169,7 +171,7 @@ public final class WhiteboardManager httpServiceFactory, webContext, this.httpBundleContext.getBundle())); this.contextMap.put(HttpServiceFactory.HTTP_SERVICE_CONTEXT_NAME, list); - // add default context + // register default context final Dictionary<String, Object> props = new Hashtable<>(); props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME, HttpWhiteboardConstants.HTTP_WHITEBOARD_DEFAULT_CONTEXT_NAME); props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH, "/"); @@ -199,12 +201,38 @@ public final class WhiteboardManager // nothing to do } }, props); + // register default context for javax whiteboard + this.defaultJavaxContextRegistration = httpBundleContext.registerService( + org.osgi.service.http.context.ServletContextHelper.class, + new ServiceFactory<org.osgi.service.http.context.ServletContextHelper>() + { + + @Override + public org.osgi.service.http.context.ServletContextHelper getService( + final Bundle bundle, + final ServiceRegistration<org.osgi.service.http.context.ServletContextHelper> registration) + { + return new org.osgi.service.http.context.ServletContextHelper(bundle) + { + // nothing to override + }; + } + + @Override + public void ungetService( + final Bundle bundle, + final ServiceRegistration<org.osgi.service.http.context.ServletContextHelper> registration, + final org.osgi.service.http.context.ServletContextHelper service) + { + // nothing to do + } + }, props); // addTracker(new FilterTracker(this.httpBundleContext, this)); // addTracker(new ListenersTracker(this.httpBundleContext, this)); // addTracker(new PreprocessorTracker(this.httpBundleContext, this)); +// addTracker(new ServletTracker(this.httpBundleContext, this)); addTracker(new ResourceTracker(this.httpBundleContext, this)); addTracker(new ServletContextHelperTracker(this.httpBundleContext, this)); -// addTracker(new ServletTracker(this.httpBundleContext, this)); addTracker(new JavaxServletContextHelperTracker(httpBundleContext, this)); addTracker(new JavaxFilterTracker(httpBundleContext, this)); addTracker(new JavaxServletTracker(httpBundleContext, this)); @@ -244,6 +272,10 @@ public final class WhiteboardManager this.failureStateHandler.clear(); this.registry.reset(); + if ( this.defaultJavaxContextRegistration != null ) { + this.defaultJavaxContextRegistration.unregister(); + this.defaultJavaxContextRegistration = null; + } if (this.defaultContextRegistration != null) { this.defaultContextRegistration.unregister(); diff --git a/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/tracker/JavaxServletContextHelperTracker.java b/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/tracker/JavaxServletContextHelperTracker.java index 316e994..ffd5da3 100644 --- a/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/tracker/JavaxServletContextHelperTracker.java +++ b/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/tracker/JavaxServletContextHelperTracker.java @@ -39,6 +39,8 @@ public final class JavaxServletContextHelperTracker extends ServiceTracker<Servl { private final WhiteboardManager contextManager; + private final long selfBundleId; + /** Map containing all info objects reported from the trackers. */ private final Map<Long, ServletContextHelperInfo> allInfos = new ConcurrentHashMap<Long, ServletContextHelperInfo>(); @@ -65,6 +67,7 @@ public final class JavaxServletContextHelperTracker extends ServiceTracker<Servl { super(context, createFilter(context), null); this.contextManager = manager; + this.selfBundleId = context.getBundle().getBundleId(); } @Override @@ -95,10 +98,13 @@ public final class JavaxServletContextHelperTracker extends ServiceTracker<Servl private void added(@NotNull final ServiceReference<ServletContextHelper> ref) { - final ServletContextHelperInfo info = new JavaxServletContextHelperInfo(ref); - if ( this.contextManager.addContextHelper(info) ) - { - this.allInfos.put((Long)ref.getProperty(Constants.SERVICE_ID), info); + // ignore all contexts registered by this bundle + if ( ref.getBundle().getBundleId() != this.selfBundleId ) { + final ServletContextHelperInfo info = new JavaxServletContextHelperInfo(ref); + if ( this.contextManager.addContextHelper(info) ) + { + this.allInfos.put((Long)ref.getProperty(Constants.SERVICE_ID), info); + } } }