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