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.api.impl.blob.JSONBlob;
044import org.nuxeo.ecm.core.io.download.BufferingServletOutputStream;
045import org.nuxeo.ecm.core.io.download.DownloadService;
046import org.nuxeo.runtime.api.Framework;
047import org.nuxeo.runtime.transaction.TransactionHelper;
048
049/**
050 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
051 */
052@Provider
053@Produces({ "*/*", "text/plain" })
054public class BlobWriter implements MessageBodyWriter<Blob> {
055
056    private static final Log log = getLog(BlobWriter.class);
057
058    public static final String BLOB_ID = "blobId";
059
060    @Context
061    private HttpServletRequest request;
062
063    @Context
064    private HttpServletResponse response;
065
066    @Context
067    private ServletContext servletContext;
068
069    @Override
070    public void writeTo(Blob blob, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
071            MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException {
072        // Ensure transaction is committed before writing blob to response
073        commitAndReopenTransaction();
074        if (Framework.isTestModeSet()) {
075            transferBlob(blob, entityStream);
076        } else {
077            DownloadService downloadService = Framework.getService(DownloadService.class);
078            String reason = blob instanceof JSONBlob ? "webengine" : "download";
079            downloadService.downloadBlob(request, response, null, null, blob, blob.getFilename(), reason);
080        }
081    }
082
083    protected void commitAndReopenTransaction() {
084        if (TransactionHelper.isTransactionActiveOrMarkedRollback()) {
085            TransactionHelper.commitOrRollbackTransaction();
086            TransactionHelper.startTransaction();
087        }
088    }
089
090    protected void transferBlob(Blob blob, OutputStream entityStream) throws IOException {
091        if (entityStream instanceof BufferingServletOutputStream) {
092            ((BufferingServletOutputStream)entityStream).stopBuffering();
093        }
094        blob.transferTo(entityStream);
095        entityStream.flush();
096    }
097
098    @Override
099    public long getSize(Blob arg0, Class<?> arg1, Type arg2, Annotation[] arg3, MediaType arg4) {
100        long n = arg0.getLength();
101        return n <= 0 ? -1 : n;
102    }
103
104    @Override
105    public boolean isWriteable(Class<?> arg0, Type type, Annotation[] arg2, MediaType arg3) {
106        return Blob.class.isAssignableFrom(arg0);
107    }
108
109}