001/*
002 * Copyright 2023 Web-Legacy
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.apache.tiles.request.jakarta.servlet;
017
018import java.io.IOException;
019
020import org.apache.tiles.request.ApplicationAccess;
021import org.apache.tiles.request.ApplicationContext;
022import org.apache.tiles.request.Request;
023import org.apache.tiles.request.RequestWrapper;
024
025import jakarta.servlet.ServletContext;
026import jakarta.servlet.ServletException;
027
028/**
029 * Utilities for Tiles request servlet support.
030 *
031 * <p>Copied from Apache tiles-request-servlet 1.0.7 and adapted for
032 * Jakarta EE 9.</p>
033 */
034public final class ServletUtil {
035
036    /**
037     * Constructor.
038     */
039    private ServletUtil() {
040    }
041
042    /**
043     * Wraps a ServletException to create an IOException with the root cause if
044     * present.
045     *
046     * @param ex      The exception to wrap.
047     * @param message The message of the exception.
048     *
049     * @return The wrapped exception.
050     */
051    public static IOException wrapServletException(ServletException ex,
052            String message) {
053        IOException retValue;
054        Throwable rootCause = ex.getRootCause();
055        if (rootCause != null) {
056            // Replace the ServletException with an IOException, with the root
057            // cause of the first as the cause of the latter.
058            retValue = new IOException(message, rootCause);
059        } else {
060            retValue = new IOException(message, ex);
061        }
062
063        return retValue;
064    }
065
066    /**
067     * Returns the application context getting it from the servlet context. It
068     * must be first saved creating a {@link ServletApplicationContext} and
069     * using {@link ApplicationAccess#register(ApplicationContext)}.
070     *
071     * @param servletContext The servlet context.
072     *
073     * @return The application context, if found, {@code null} otherwise.
074     */
075    public static ApplicationContext getApplicationContext(
076            ServletContext servletContext) {
077        return (ApplicationContext) servletContext
078                .getAttribute(ApplicationAccess.APPLICATION_CONTEXT_ATTRIBUTE);
079    }
080
081    /**
082     * Opens a TilesRequestContext until it finds a ServletTilesRequestContext.
083     *
084     * @param request The request to open.
085     *
086     * @return The servlet-based request context.
087     *
088     * @throws NotAServletEnvironmentException If a servlet-based request
089     *                                         context could not be found.
090     */
091    public static ServletRequest getServletRequest(Request request) {
092        Request currentRequest = request;
093        while (true) {
094            if (currentRequest == null) {
095                throw new NotAServletEnvironmentException(
096                        "Last Tiles request context is null");
097            }
098
099            if (currentRequest instanceof ServletRequest) {
100                return (ServletRequest) currentRequest;
101            }
102            if (!(currentRequest instanceof RequestWrapper)) {
103                throw new NotAServletEnvironmentException(
104                        "Not a Servlet environment, not supported");
105            }
106            currentRequest = ((RequestWrapper)
107                    currentRequest).getWrappedRequest();
108        }
109    }
110
111    /**
112     * Gets a servlet context from a TilesApplicationContext.
113     *
114     * @param applicationContext The application context to analyze.
115     *
116     * @return The servlet context.
117     *
118     * @throws NotAServletEnvironmentException If the application context is not
119     *                                         servlet-based.
120     */
121    public static ServletContext getServletContext(
122            ApplicationContext applicationContext) {
123        if (applicationContext instanceof ServletApplicationContext) {
124            return (ServletContext) ((ServletApplicationContext)
125                    applicationContext).getContext();
126        }
127
128        throw new NotAServletEnvironmentException(
129                "Not a Servlet-based environment");
130    }
131}