001/* 002 * (C) Copyright 2006-2011 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 * Gagnavarslan ehf 018 */ 019package org.nuxeo.ecm.webdav.service; 020 021import java.io.IOException; 022 023import javax.servlet.Filter; 024import javax.servlet.FilterChain; 025import javax.servlet.FilterConfig; 026import javax.servlet.ServletException; 027import javax.servlet.ServletRequest; 028import javax.servlet.ServletResponse; 029import javax.servlet.http.HttpServletRequest; 030import javax.servlet.http.HttpServletResponse; 031 032import org.apache.commons.lang.StringUtils; 033import org.nuxeo.ecm.platform.web.common.requestcontroller.filter.BufferingHttpServletResponse; 034import org.nuxeo.runtime.transaction.TransactionHelper; 035import org.nuxeo.runtime.transaction.TransactionRuntimeException; 036 037/** 038 * Windows Integration Request filter, bound to /nuxeo. Allows Windows user agents to bind to the root as the expect 039 * (not /nuxeo/site/dav) and still work. 040 */ 041public class WIRequestFilter implements Filter { 042 043 public static String WEBDAV_USERAGENT = "Microsoft-WebDAV-MiniRedir"; 044 045 public static String MSOFFICE_USERAGENT = "Microsoft Office Existence Discovery"; 046 047 public static final String BACKEND_KEY = "org.nuxeo.ecm.webdav.service.backend"; 048 049 @Override 050 public void init(FilterConfig filterConfig) throws ServletException { 051 } 052 053 @Override 054 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, 055 ServletException { 056 057 HttpServletRequest httpRequest = (HttpServletRequest) request; 058 HttpServletResponse httpResponse = (HttpServletResponse) response; 059 060 if (!isWIRequest(httpRequest)) { 061 chain.doFilter(request, response); 062 return; 063 } 064 065 // do what WebEngineFilter does: 066 // - start a transaction 067 // - do response buffering 068 boolean txStarted = false; 069 boolean ok = false; 070 try { 071 if (!TransactionHelper.isTransactionActive()) { 072 txStarted = TransactionHelper.startTransaction(); 073 if (!txStarted) { 074 throw new ServletException("A transaction is needed."); 075 } 076 response = new BufferingHttpServletResponse(httpResponse); 077 } 078 chain.doFilter(request, response); 079 ok = true; 080 } finally { 081 if (txStarted) { 082 try { 083 if (!ok) { 084 TransactionHelper.setTransactionRollbackOnly(); 085 } 086 TransactionHelper.commitOrRollbackTransaction(); 087 } catch (TransactionRuntimeException e) { 088 // commit failed, report this to the client before stopping buffering 089 ((HttpServletResponse) response).sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, 090 e.getMessage()); 091 throw e; 092 } finally { 093 ((BufferingHttpServletResponse) response).stopBuffering(); 094 } 095 } 096 } 097 } 098 099 @Override 100 public void destroy() { 101 } 102 103 private boolean isWIRequest(HttpServletRequest request) { 104 String ua = request.getHeader("User-Agent"); 105 return StringUtils.isNotEmpty(ua) && (ua.contains(WEBDAV_USERAGENT) || ua.contains(MSOFFICE_USERAGENT)); 106 } 107 108}