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     * @param request
065     * @param filename
066     * @return a full string to set as value of a {@code Content-Disposition} header
067     * @since 5.7.2
068     */
069    public static String getRFC2231ContentDisposition(HttpServletRequest request, String filename) {
070        return DownloadHelper.getRFC2231ContentDisposition(request, filename);
071    }
072
073    /**
074     * @since 8.3
075     */
076    public static void setServletContext(ServletContext servletContext) {
077        SERVLET_CONTEXT.set(servletContext);
078    }
079
080    /**
081     * @since 8.3
082     */
083    public static ServletContext getServletContext() {
084        return SERVLET_CONTEXT.get();
085    }
086
087    /**
088     * @since 8.3
089     */
090    public static void removeServletContext() {
091        SERVLET_CONTEXT.remove();
092    }
093
094}