001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     bstefanescu
011 *
012 * $Id$
013 */
014
015package org.nuxeo.ecm.automation.jaxrs.io.documents;
016
017import java.io.IOException;
018import java.io.OutputStream;
019import java.lang.annotation.Annotation;
020import java.lang.reflect.Type;
021
022import javax.mail.MessagingException;
023import javax.ws.rs.Produces;
024import javax.ws.rs.core.MediaType;
025import javax.ws.rs.core.MultivaluedMap;
026import javax.ws.rs.ext.MessageBodyWriter;
027import javax.ws.rs.ext.Provider;
028
029/**
030 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
031 */
032@Provider
033@Produces("multipart/mixed")
034public class BlobsWriter implements MessageBodyWriter<MultipartBlobs> {
035
036    @Override
037    public void writeTo(MultipartBlobs blobs, Class<?> type, Type genericType, Annotation[] annotations,
038            MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
039            throws IOException {
040        try {
041            httpHeaders.putSingle("Content-Type", blobs.getContentType());
042            blobs.writeTo(entityStream);
043            entityStream.flush();
044        } catch (MessagingException e) {
045            throw new IOException("Failed to return blobs", e);
046        }
047    }
048
049    @Override
050    public long getSize(MultipartBlobs arg0, Class<?> arg1, Type arg2, Annotation[] arg3, MediaType arg4) {
051        return -1;
052    }
053
054    @Override
055    public boolean isWriteable(Class<?> arg0, Type type, Annotation[] arg2, MediaType arg3) {
056        return MultipartBlobs.class == arg0;
057    }
058
059}