All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.atlassian.util.adapter.jakarta.servlet.JakartaServletContextAdapter Maven / Gradle / Ivy

Go to download

A collection of classes for adapting Servlet API 4.0 objects to Servlet API 5.0 and vice versa.

There is a newer version: 0.1.0
Show newest version
package io.atlassian.util.adapter.jakarta.servlet;

import io.atlassian.util.adapter.jakarta.servlet.descriptor.JakartaJspConfigDescriptorAdapter;
import io.atlassian.util.adapter.java.util.EnumerationAdapter;
import io.atlassian.util.adapter.javax.servlet.JavaXFilterAdapter;
import io.atlassian.util.adapter.javax.servlet.JavaXServletAdapter;
import io.atlassian.util.adapter.javax.servlet.JavaXServletContextAdapter;
import jakarta.servlet.Filter;
import jakarta.servlet.FilterRegistration;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.Servlet;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRegistration;
import jakarta.servlet.SessionCookieConfig;
import jakarta.servlet.SessionTrackingMode;
import jakarta.servlet.descriptor.JspConfigDescriptor;

import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collection;
import java.util.EnumSet;
import java.util.Enumeration;
import java.util.EventListener;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import static io.atlassian.util.adapter.jakarta.JakartaAdapters.asJakartaIfJavaX;
import static io.atlassian.util.adapter.javax.JavaXAdapters.asJavaXIfJakarta;
import static io.atlassian.util.adapter.util.WrapperUtil.applyIfNonNull;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toMap;

public class JakartaServletContextAdapter implements ServletContext {

    private final javax.servlet.ServletContext delegate;

    public static ServletContext from(javax.servlet.ServletContext delegate) {
        if (delegate instanceof JavaXServletContextAdapter castDelegate) {
            return castDelegate.getDelegate();
        }
        return applyIfNonNull(delegate, JakartaServletContextAdapter::new);
    }

    JakartaServletContextAdapter(javax.servlet.ServletContext delegate) {
        this.delegate = requireNonNull(delegate);
    }

    public javax.servlet.ServletContext getDelegate() {
        return delegate;
    }

    @Override
    public String getContextPath() {
        return delegate.getContextPath();
    }

    @Override
    public ServletContext getContext(String uripath) {
        return JakartaServletContextAdapter.from(delegate.getContext(uripath));
    }

    @Override
    public int getMajorVersion() {
        return delegate.getMajorVersion();
    }

    @Override
    public int getMinorVersion() {
        return delegate.getMinorVersion();
    }

    @Override
    public int getEffectiveMajorVersion() {
        return delegate.getEffectiveMajorVersion();
    }

    @Override
    public int getEffectiveMinorVersion() {
        return delegate.getEffectiveMinorVersion();
    }

    @Override
    public String getMimeType(String file) {
        return delegate.getMimeType(file);
    }

    @Override
    public Set getResourcePaths(String path) {
        return delegate.getResourcePaths(path);
    }

    @Override
    public URL getResource(String path) throws MalformedURLException {
        return delegate.getResource(path);
    }

    @Override
    public InputStream getResourceAsStream(String path) {
        return delegate.getResourceAsStream(path);
    }

    @Override
    public RequestDispatcher getRequestDispatcher(String path) {
        return JakartaRequestDispatcherAdapter.from(delegate.getRequestDispatcher(path));
    }

    @Override
    public RequestDispatcher getNamedDispatcher(String name) {
        return JakartaRequestDispatcherAdapter.from(delegate.getNamedDispatcher(name));
    }

    @Override
    public Servlet getServlet(String name) throws ServletException {
        try {
            return JakartaServletAdapter.from(delegate.getServlet(name));
        } catch (javax.servlet.ServletException e) {
            throw new ServletException(e);
        }
    }

    @Override
    public Enumeration getServlets() {
        var servlets = delegate.getServlets();
        if (servlets == null) {
            return null;
        }
        return new EnumerationAdapter<>(delegate.getServlets(), JakartaServletAdapter::from);
    }

    @Override
    public Enumeration getServletNames() {
        return delegate.getServletNames();
    }

    @Override
    public void log(String msg) {
        delegate.log(msg);
    }

    @Override
    public void log(Exception exception, String msg) {
        delegate.log(exception, msg);
    }

    @Override
    public void log(String message, Throwable throwable) {
        delegate.log(message, throwable);
    }

    @Override
    public String getRealPath(String path) {
        return delegate.getRealPath(path);
    }

    @Override
    public String getServerInfo() {
        return delegate.getServerInfo();
    }

    @Override
    public String getInitParameter(String name) {
        return delegate.getInitParameter(name);
    }

    @Override
    public Enumeration getInitParameterNames() {
        return delegate.getInitParameterNames();
    }

    @Override
    public boolean setInitParameter(String name, String value) {
        return delegate.setInitParameter(name, value);
    }

    @Override
    public Object getAttribute(String name) {
        return asJakartaIfJavaX(delegate.getAttribute(name));
    }

    @Override
    public Enumeration getAttributeNames() {
        return delegate.getAttributeNames();
    }

    @Override
    public void setAttribute(String name, Object object) {
        delegate.setAttribute(name, asJavaXIfJakarta(object));
    }

    @Override
    public void removeAttribute(String name) {
        delegate.removeAttribute(name);
    }

    @Override
    public String getServletContextName() {
        return delegate.getServletContextName();
    }

    @Override
    public ServletRegistration.Dynamic addServlet(String servletName, String className) {
        return JakartaDynamicServletRegistrationAdapter.from(delegate.addServlet(servletName, className));
    }

    @Override
    public ServletRegistration.Dynamic addServlet(String servletName, Servlet servlet) {
        return JakartaDynamicServletRegistrationAdapter.from(delegate.addServlet(servletName, JavaXServletAdapter.from(servlet)));
    }

    @Override
    public ServletRegistration.Dynamic addServlet(String servletName, Class servletClass) {
        // ClassCastException likely
        return JakartaDynamicServletRegistrationAdapter.from(delegate.addServlet(servletName, (Class) servletClass));
    }

    @Override
    public ServletRegistration.Dynamic addJspFile(String servletName, String jspFile) {
        return JakartaDynamicServletRegistrationAdapter.from(delegate.addJspFile(servletName, jspFile));
    }

    @Override
    public  T createServlet(Class clazz) throws ServletException {
        try {
            // ClassCastException likely
            return (T) JakartaServletAdapter.from(delegate.createServlet((Class) clazz));
        } catch (javax.servlet.ServletException e) {
            throw new ServletException(e);
        }
    }

    @Override
    public ServletRegistration getServletRegistration(String servletName) {
        return JakartaServletRegistrationAdapter.from(delegate.getServletRegistration(servletName));
    }

    @Override
    public Map getServletRegistrations() {
        var servletRegistrations = delegate.getServletRegistrations();
        if (servletRegistrations == null) {
            return null;
        }
        return servletRegistrations.entrySet().stream()
                .collect(toMap(Entry::getKey, e -> JakartaServletRegistrationAdapter.from(e.getValue())));
    }

    @Override
    public FilterRegistration.Dynamic addFilter(String filterName, String className) {
        return JakartaDynamicFilterRegistrationAdapter.from(delegate.addFilter(filterName, className));
    }

    @Override
    public FilterRegistration.Dynamic addFilter(String filterName, Filter filter) {
        return JakartaDynamicFilterRegistrationAdapter.from(delegate.addFilter(filterName, JavaXFilterAdapter.from(filter)));
    }

    @Override
    public FilterRegistration.Dynamic addFilter(String filterName, Class filterClass) {
        // ClassCastException likely
        return JakartaDynamicFilterRegistrationAdapter.from(delegate.addFilter(filterName, (Class) filterClass));
    }

    @Override
    public  T createFilter(Class clazz) throws ServletException {
        try {
            // ClassCastException likely
            return (T) JakartaFilterAdapter.from(delegate.createFilter((Class) clazz));
        } catch (javax.servlet.ServletException e) {
            throw new ServletException(e);
        }
    }

    @Override
    public FilterRegistration getFilterRegistration(String filterName) {
        return JakartaFilterRegistrationAdapter.from(delegate.getFilterRegistration(filterName));
    }

    @Override
    public Map getFilterRegistrations() {
        var filterRegistrations = delegate.getFilterRegistrations();
        if (filterRegistrations == null) {
            return null;
        }
        return filterRegistrations.entrySet().stream()
                .collect(toMap(Entry::getKey, e -> JakartaFilterRegistrationAdapter.from(e.getValue())));
    }

    @Override
    public SessionCookieConfig getSessionCookieConfig() {
        return JakartaSessionCookieConfigAdapter.from(delegate.getSessionCookieConfig());
    }

    @Override
    public void setSessionTrackingModes(Set sessionTrackingModes) {
        delegate.setSessionTrackingModes(JavaXServletContextAdapter.toJavaXSessionTrackingModeSet(sessionTrackingModes));
    }

    @Override
    public Set getDefaultSessionTrackingModes() {
        return JakartaServletContextAdapter.toJakartaSessionTrackingModeSet(delegate.getDefaultSessionTrackingModes());
    }

    @Override
    public Set getEffectiveSessionTrackingModes() {
        return JakartaServletContextAdapter.toJakartaSessionTrackingModeSet(delegate.getEffectiveSessionTrackingModes());
    }

    @Override
    public void addListener(String className) {
        delegate.addListener(className);
    }

    @Override
    public  void addListener(T t) {
        delegate.addListener(t);
    }

    @Override
    public void addListener(Class listenerClass) {
        delegate.addListener(listenerClass);
    }

    @Override
    public  T createListener(Class clazz) throws ServletException {
        try {
            return delegate.createListener(clazz);
        } catch (javax.servlet.ServletException e) {
            throw new ServletException(e);
        }
    }

    @Override
    public JspConfigDescriptor getJspConfigDescriptor() {
        return JakartaJspConfigDescriptorAdapter.from(delegate.getJspConfigDescriptor());
    }

    @Override
    public ClassLoader getClassLoader() {
        return delegate.getClassLoader();
    }

    @Override
    public void declareRoles(String... roleNames) {
        delegate.declareRoles(roleNames);
    }

    @Override
    public String getVirtualServerName() {
        return delegate.getVirtualServerName();
    }

    @Override
    public int getSessionTimeout() {
        return delegate.getSessionTimeout();
    }

    @Override
    public void setSessionTimeout(int sessionTimeout) {
        delegate.setSessionTimeout(sessionTimeout);
    }

    @Override
    public String getRequestCharacterEncoding() {
        return delegate.getRequestCharacterEncoding();
    }

    @Override
    public void setRequestCharacterEncoding(String encoding) {
        delegate.setRequestCharacterEncoding(encoding);
    }

    @Override
    public String getResponseCharacterEncoding() {
        return delegate.getResponseCharacterEncoding();
    }

    @Override
    public void setResponseCharacterEncoding(String encoding) {
        delegate.setResponseCharacterEncoding(encoding);
    }

    public static Set toJakartaSessionTrackingModeSet(Collection sessionTrackingModes) {
        if (sessionTrackingModes == null) {
            return null;
        }
        var result = EnumSet.noneOf(SessionTrackingMode.class);
        for (javax.servlet.SessionTrackingMode sessionTrackingMode : sessionTrackingModes) {
            result.add(SessionTrackingMode.valueOf(sessionTrackingMode.name()));
        }
        return result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy