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