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