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