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.core.operations.blob;
013
014import java.io.IOException;
015import java.io.InputStream;
016import java.io.OutputStream;
017import java.net.URL;
018import java.net.URLConnection;
019
020import org.nuxeo.common.utils.FileUtils;
021import org.nuxeo.ecm.automation.core.Constants;
022import org.nuxeo.ecm.automation.core.annotations.Operation;
023import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
024import org.nuxeo.ecm.automation.core.annotations.Param;
025import org.nuxeo.ecm.automation.core.collectors.BlobCollector;
026import org.nuxeo.ecm.core.api.Blob;
027
028/**
029 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
030 */
031@Operation(id = PostBlob.ID, category = Constants.CAT_BLOB, label = "HTTP Post", description = "Post the input file to a target HTTP URL. Returns back the input file.", aliases = { "Blob.Post" })
032public class PostBlob {
033
034    public static final String ID = "Blob.PostToURL";
035
036    @Param(name = "url")
037    protected String url;
038
039    @OperationMethod(collector = BlobCollector.class)
040    public Blob run(Blob blob) throws IOException {
041        URL target = new URL(url);
042        URLConnection conn = target.openConnection();
043        conn.setDoOutput(true);
044        InputStream in = blob.getStream();
045        OutputStream out = conn.getOutputStream();
046        try {
047            FileUtils.copy(in, out);
048            out.flush();
049        } finally {
050            in.close();
051            out.close();
052        }
053        return blob;
054    }
055
056}