001/*
002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and others.
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 *
016 * Contributors:
017 *     Florent Guillaume
018 */
019package org.nuxeo.ecm.platform.web.common;
020
021import javax.servlet.ServletContext;
022import javax.servlet.http.HttpServletRequest;
023
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026import org.nuxeo.ecm.core.io.download.DownloadHelper;
027import org.nuxeo.runtime.transaction.TransactionHelper;
028
029/**
030 * Helpers for servlets.
031 *
032 * @since 5.6
033 */
034public class ServletHelper {
035
036    private static final Log log = LogFactory.getLog(ServletHelper.class);
037
038    public static final String TX_TIMEOUT_HEADER_KEY = "Nuxeo-Transaction-Timeout";
039
040    private static final ThreadLocal<ServletContext> SERVLET_CONTEXT = new ThreadLocal<>();
041
042    private ServletHelper() {
043        // utility class
044    }
045
046    public static boolean startTransaction(HttpServletRequest request) {
047        String header = request.getHeader(TX_TIMEOUT_HEADER_KEY);
048        int timeout = 0; // default
049        if (header != null) {
050            try {
051                timeout = Integer.parseInt(header);
052            } catch (NumberFormatException e) {
053                // bad header
054                log.warn("Invalid request transaction timeout: " + header);
055            }
056        }
057        return TransactionHelper.startTransaction(timeout);
058    }
059
060    /**
061     * Generate a Content-Disposition string based on the servlet request for a given filename. The value follows
062     * RFC2231
063     *
064     * @return a full string to set as value of a {@code Content-Disposition} header
065     * @since 5.7.2
066     */
067    public static String getRFC2231ContentDisposition(HttpServletRequest request, String filename) {
068        return DownloadHelper.getRFC2231ContentDisposition(request, filename);
069    }
070
071    /**
072     * @since 8.3
073     */
074    public static void setServletContext(ServletContext servletContext) {
075        SERVLET_CONTEXT.set(servletContext);
076    }
077
078    /**
079     * @since 8.3
080     */
081    public static ServletContext getServletContext() {
082        return SERVLET_CONTEXT.get();
083    }
084
085    /**
086     * @since 8.3
087     */
088    public static void removeServletContext() {
089        SERVLET_CONTEXT.remove();
090    }
091
092}