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