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.util;
020
021import java.io.IOException;
022import java.util.List;
023
024import javax.activation.DataHandler;
025import javax.mail.MessagingException;
026import javax.mail.internet.MimeBodyPart;
027import javax.mail.internet.MimeMultipart;
028
029import org.nuxeo.ecm.automation.client.Constants;
030import org.nuxeo.ecm.automation.client.model.Blob;
031import org.nuxeo.ecm.automation.client.model.HasFile;
032
033/**
034 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
035 */
036public class MultipartInput extends MimeMultipart {
037
038    public MultipartInput() {
039        super("related");
040    }
041
042    public void setRequest(String content) throws IOException {
043        MimeBodyPart part = new MimeBodyPart();
044        try {
045            part.setText(content, "UTF-8");
046            part.setContentID("request");
047            part.setHeader("Content-Type", Constants.CTYPE_REQUEST);
048            part.setHeader("Content-Transfer-Encoding", "8bit");
049            part.setHeader("Content-Length", Integer.toString(content.length()));
050            addBodyPart(part);
051        } catch (MessagingException e) {
052            throw new IOException(e);
053        }
054    }
055
056    public void setBlob(Blob blob) throws IOException {
057        setBlob(blob, "input");
058    }
059
060    protected void setBlob(Blob blob, String id) throws IOException {
061        try {
062            MimeBodyPart part = new MimeBodyPart();
063            if (blob instanceof HasFile) {
064                part.attachFile(((HasFile) blob).getFile());
065            } else {
066                part.setDataHandler(new DataHandler(new BlobDataSource(blob)));
067                if (blob.getFileName() != null) {
068                    part.setFileName(blob.getFileName());
069                }
070            }
071            part.setHeader("Content-Type", blob.getMimeType());
072            part.setHeader("Content-Transfer-Encoding", "binary");
073            int length = blob.getLength();
074            if (length > -1) {
075                part.setHeader("Content-Length", Integer.toString(length));
076            }
077            part.setContentID(id);
078            addBodyPart(part);
079        } catch (MessagingException e) {
080            throw new IOException(e);
081        }
082    }
083
084    public void setBlobs(List<Blob> blobs) throws IOException {
085        for (int i = 0, size = blobs.size(); i < size; i++) {
086            setBlob(blobs.get(i), "input#" + i);
087        }
088    }
089
090}