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 */
012package org.nuxeo.ecm.automation.client.jaxrs.impl;
013
014import java.io.IOException;
015import java.io.InputStream;
016import java.io.OutputStream;
017import java.util.List;
018
019import javax.activation.DataHandler;
020import javax.mail.MessagingException;
021import javax.mail.internet.MimeBodyPart;
022import javax.mail.internet.MimeMultipart;
023
024import org.apache.http.entity.AbstractHttpEntity;
025import org.nuxeo.ecm.automation.client.Constants;
026import org.nuxeo.ecm.automation.client.jaxrs.util.BlobDataSource;
027import org.nuxeo.ecm.automation.client.model.Blob;
028import org.nuxeo.ecm.automation.client.model.HasFile;
029
030/**
031 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
032 */
033public class MultipartRequestEntity extends AbstractHttpEntity {
034
035    protected MimeMultipart mp;
036
037    public MultipartRequestEntity() {
038        this(new MimeMultipart("related"));
039    }
040
041    public MultipartRequestEntity(String subType) {
042        this(new MimeMultipart(subType));
043    }
044
045    public MultipartRequestEntity(MimeMultipart mp) {
046        this.mp = mp;
047        setContentType(mp.getContentType() + "; type=\"" + Constants.CTYPE_REQUEST_NOCHARSET + "\"; start=\"request\"");
048    }
049
050    public void setRequest(String content) throws MessagingException {
051        MimeBodyPart part = new MimeBodyPart();
052        part.setText(content, "UTF-8");
053        part.setContentID("request");
054        part.setHeader("Content-Type", Constants.CTYPE_REQUEST);
055        part.setHeader("Content-Transfer-Encoding", "8bit");
056        part.setHeader("Content-Length", Integer.toString(content.length()));
057        mp.addBodyPart(part);
058    }
059
060    public void setBlob(Blob blob) throws MessagingException, IOException {
061        setBlob(blob, "input");
062    }
063
064    protected void setBlob(Blob blob, String id) throws MessagingException, IOException {
065        MimeBodyPart part = new MimeBodyPart();
066        if (blob instanceof HasFile) {
067            part.attachFile(((HasFile) blob).getFile());
068        } else {
069            part.setDataHandler(new DataHandler(new BlobDataSource(blob)));
070            part.setFileName(blob.getFileName());
071        }
072        part.setHeader("Content-Type", blob.getMimeType());
073        part.setHeader("Content-Transfer-Encoding", "binary");
074        int length = blob.getLength();
075        if (length > -1) {
076            part.setHeader("Content-Length", Integer.toString(length));
077        }
078        part.setContentID(id);
079        mp.addBodyPart(part);
080    }
081
082    public void setBlobs(List<Blob> blobs) throws MessagingException, IOException {
083        for (int i = 0, size = blobs.size(); i < size; i++) {
084            setBlob(blobs.get(i), "input#" + i);
085        }
086    }
087
088    public boolean isRepeatable() {
089        return false;
090    }
091
092    public boolean isStreaming() {
093        return false;
094    }
095
096    public InputStream getContent() throws IOException, IllegalStateException {
097        throw new UnsupportedOperationException("Not a streaming entity");
098    }
099
100    public void writeTo(OutputStream arg0) throws IOException {
101        try {
102            mp.writeTo(arg0);
103        } catch (MessagingException e) {
104            IOException ioe = new IOException(e.getMessage());
105            ioe.initCause(e);
106            throw ioe;
107        }
108    }
109
110    public long getContentLength() {
111        return -1;
112    }
113
114}