001/* 002 * (C) Copyright 2006-2008 Nuxeo SAS (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 * bstefanescu 016 * 017 * $Id$ 018 */ 019 020package org.nuxeo.ecm.webengine.model.io; 021 022import static org.apache.commons.logging.LogFactory.getLog; 023 024import java.io.IOException; 025import java.io.OutputStream; 026import java.lang.annotation.Annotation; 027import java.lang.reflect.Type; 028 029import javax.servlet.ServletContext; 030import javax.servlet.http.HttpServletRequest; 031import javax.servlet.http.HttpServletResponse; 032import javax.ws.rs.Produces; 033import javax.ws.rs.core.Context; 034import javax.ws.rs.core.MediaType; 035import javax.ws.rs.core.MultivaluedMap; 036import javax.ws.rs.ext.MessageBodyWriter; 037import javax.ws.rs.ext.Provider; 038 039import org.apache.commons.logging.Log; 040import org.nuxeo.ecm.core.api.Blob; 041import org.nuxeo.ecm.core.io.download.BufferingServletOutputStream; 042import org.nuxeo.ecm.core.io.download.DownloadService; 043import org.nuxeo.runtime.api.Framework; 044import org.nuxeo.runtime.transaction.TransactionHelper; 045 046/** 047 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 048 */ 049@Provider 050@Produces({ "*/*", "text/plain" }) 051public class BlobWriter implements MessageBodyWriter<Blob> { 052 053 private static final Log log = getLog(BlobWriter.class); 054 055 public static final String BLOB_ID = "blobId"; 056 057 @Context 058 private HttpServletRequest request; 059 060 @Context 061 private HttpServletResponse response; 062 063 @Context 064 private ServletContext servletContext; 065 066 @Override 067 public void writeTo(Blob blob, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, 068 MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException { 069 // Ensure transaction is committed before writing blob to response 070 commitAndReopenTransaction(); 071 if (Framework.isTestModeSet()) { 072 transferBlob(blob, entityStream); 073 } else { 074 DownloadService downloadService = Framework.getService(DownloadService.class); 075 downloadService.downloadBlob(request, response, null, null, blob, blob.getFilename(), "webengine"); 076 } 077 } 078 079 protected void commitAndReopenTransaction() { 080 if (TransactionHelper.isTransactionActiveOrMarkedRollback()) { 081 TransactionHelper.commitOrRollbackTransaction(); 082 TransactionHelper.startTransaction(); 083 } 084 } 085 086 protected void transferBlob(Blob blob, OutputStream entityStream) throws IOException { 087 BufferingServletOutputStream.stopBufferingThread(); 088 blob.transferTo(entityStream); 089 entityStream.flush(); 090 } 091 092 @Override 093 public long getSize(Blob arg0, Class<?> arg1, Type arg2, Annotation[] arg3, MediaType arg4) { 094 long n = arg0.getLength(); 095 return n <= 0 ? -1 : n; 096 } 097 098 @Override 099 public boolean isWriteable(Class<?> arg0, Type type, Annotation[] arg2, MediaType arg3) { 100 return Blob.class.isAssignableFrom(arg0); 101 } 102 103}