001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nicolas Chapurlat <nchapurlat@nuxeo.com>
016 */
017
018package org.nuxeo.ecm.webengine.jaxrs.coreiodelegate;
019
020import java.util.Enumeration;
021import java.util.Map;
022
023import javax.servlet.ServletRequest;
024import javax.servlet.http.HttpServletRequest;
025
026import org.nuxeo.ecm.core.api.CoreSession;
027import org.nuxeo.ecm.core.io.registry.context.RenderingContext;
028import org.nuxeo.ecm.core.io.registry.context.RenderingContext.CtxBuilder;
029import org.nuxeo.ecm.core.io.registry.context.RenderingContextImpl.RenderingContextBuilder;
030import org.nuxeo.ecm.webengine.jaxrs.session.SessionFactory;
031
032/**
033 * Utility class that get or create a {@link RenderingContext} from the current {@link HttpServletRequest}.
034 *
035 * @since 7.2
036 */
037public final class RenderingContextWebUtils {
038
039    private static final String CTX_KEY = "_STORED_GENERATED_RENDERING_CONTEXT";
040
041    private static final String REQUEST_KEY = "_STORED_GENERATED_HTTP_SERVLET_REQUEST";
042
043    private RenderingContextWebUtils() {
044    }
045
046    /**
047     * Get the current context in the current {@link HttpServletRequest} or create it from the current
048     * {@link HttpServletRequest}.
049     *
050     * @since 7.2
051     */
052    public static RenderingContext getContext(ServletRequest request) {
053        // try to get an existing RenderingContext from the request
054        Object stored = request.getAttribute(CTX_KEY);
055        if (stored != null) {
056            return (RenderingContext) stored;
057        }
058        RenderingContextBuilder builder = CtxBuilder.builder();
059        fillContext(builder, request);
060        RenderingContext ctx = builder.get();
061        request.setAttribute(CTX_KEY, ctx);
062        return ctx;
063    }
064
065    /**
066     * Create an {@link RenderingContextBuilder}, fill it with the current {@link HttpServletRequest} and return it.
067     *
068     * @since 7.2
069     */
070    public static RenderingContextBuilder getBuilder(ServletRequest request) {
071        RenderingContextBuilder builder = CtxBuilder.builder();
072        fillContext(builder, request);
073        return builder;
074    }
075
076    /**
077     * Fill an existing with the current {@link HttpServletRequest}.
078     *
079     * @since 7.2
080     */
081    public static RenderingContextBuilder fillContext(RenderingContextBuilder builder, ServletRequest request) {
082        // create a context builder and put base url, session and request
083        builder.param(REQUEST_KEY, request);
084        // for web context, put the base url, the session and the headers
085        if (request instanceof HttpServletRequest) {
086            HttpServletRequest webRequest = (HttpServletRequest) request;
087            // base url
088            @SuppressWarnings("deprecation")
089            String baseURL = VirtualHostHelper.getBaseURL(webRequest);
090            // current session
091            CoreSession session = SessionFactory.getSession(webRequest);
092            builder.base(baseURL).session(session);
093            // headers
094            Enumeration<String> headerNames = webRequest.getHeaderNames();
095            while (headerNames.hasMoreElements()) {
096                String headerName = headerNames.nextElement();
097                Enumeration<String> headerValues = webRequest.getHeaders(headerName);
098                while (headerValues.hasMoreElements()) {
099                    builder.param(headerName, headerValues.nextElement());
100                }
101            }
102        }
103        // parameters
104        for (Map.Entry<String, String[]> parameter : request.getParameterMap().entrySet()) {
105            builder.paramValues(parameter.getKey(), (Object[]) parameter.getValue());
106        }
107        // attributes
108        Enumeration<String> attributeNames = request.getAttributeNames();
109        while (attributeNames.hasMoreElements()) {
110            String attributeName = attributeNames.nextElement();
111            Object attributeValue = request.getAttribute(attributeName);
112            builder.param(attributeName, attributeValue);
113        }
114        return builder;
115    }
116
117}