Search in sources :

Example 1 with PartSource

use of org.apache.commons.httpclient.methods.multipart.PartSource in project pinot by linkedin.

the class FileUploadUtils method sendFile.

public static int sendFile(final String host, final String port, final String path, final String fileName, final InputStream inputStream, final long lengthInBytes, SendFileMethod httpMethod) {
    EntityEnclosingMethod method = null;
    try {
        method = httpMethod.forUri("http://" + host + ":" + port + "/" + path);
        Part[] parts = { new FilePart(fileName, new PartSource() {

            @Override
            public long getLength() {
                return lengthInBytes;
            }

            @Override
            public String getFileName() {
                return fileName;
            }

            @Override
            public InputStream createInputStream() throws IOException {
                return new BufferedInputStream(inputStream);
            }
        }) };
        method.setRequestEntity(new MultipartRequestEntity(parts, new HttpMethodParams()));
        FILE_UPLOAD_HTTP_CLIENT.executeMethod(method);
        if (method.getStatusCode() >= 400) {
            String errorString = "POST Status Code: " + method.getStatusCode() + "\n";
            if (method.getResponseHeader("Error") != null) {
                errorString += "ServletException: " + method.getResponseHeader("Error").getValue();
            }
            throw new HttpException(errorString);
        }
        return method.getStatusCode();
    } catch (Exception e) {
        LOGGER.error("Caught exception while sending file: {}", fileName, e);
        Utils.rethrowException(e);
        throw new AssertionError("Should not reach this");
    } finally {
        if (method != null) {
            method.releaseConnection();
        }
    }
}
Also used : PartSource(org.apache.commons.httpclient.methods.multipart.PartSource) BufferedInputStream(java.io.BufferedInputStream) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) Part(org.apache.commons.httpclient.methods.multipart.Part) EntityEnclosingMethod(org.apache.commons.httpclient.methods.EntityEnclosingMethod) HttpException(org.apache.commons.httpclient.HttpException) HttpMethodParams(org.apache.commons.httpclient.params.HttpMethodParams) MultipartRequestEntity(org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) HttpException(org.apache.commons.httpclient.HttpException) IOException(java.io.IOException)

Example 2 with PartSource

use of org.apache.commons.httpclient.methods.multipart.PartSource in project pinot by linkedin.

the class ServerSegmentCompletionProtocolHandler method segmentCommit.

public SegmentCompletionProtocol.Response segmentCommit(long offset, final String segmentName, final File segmentTarFile) throws FileNotFoundException {
    SegmentCompletionProtocol.Request.Params params = new SegmentCompletionProtocol.Request.Params();
    params.withInstanceId(_instance).withOffset(offset).withSegmentName(segmentName);
    SegmentCompletionProtocol.SegmentCommitRequest request = new SegmentCompletionProtocol.SegmentCommitRequest(params);
    final InputStream inputStream = new FileInputStream(segmentTarFile);
    Part[] parts = { new FilePart(segmentName, new PartSource() {

        @Override
        public long getLength() {
            return segmentTarFile.length();
        }

        @Override
        public String getFileName() {
            return "fileName";
        }

        @Override
        public InputStream createInputStream() throws IOException {
            return new BufferedInputStream(inputStream);
        }
    }) };
    return doHttp(request, parts);
}
Also used : PartSource(org.apache.commons.httpclient.methods.multipart.PartSource) SegmentCompletionProtocol(com.linkedin.pinot.common.protocols.SegmentCompletionProtocol) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) HttpMethodParams(org.apache.commons.httpclient.params.HttpMethodParams) FileInputStream(java.io.FileInputStream) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) BufferedInputStream(java.io.BufferedInputStream) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) Part(org.apache.commons.httpclient.methods.multipart.Part)

Example 3 with PartSource

use of org.apache.commons.httpclient.methods.multipart.PartSource in project sling by apache.

the class HttpOsgiClient method installBundle.

@Override
public void installBundle(InputStream in, String fileName) throws OsgiClientException {
    if (in == null) {
        throw new IllegalArgumentException("in may not be null");
    }
    if (fileName == null) {
        throw new IllegalArgumentException("fileName may not be null");
    }
    // append pseudo path after root URL to not get redirected for nothing
    final PostMethod filePost = new PostMethod(repositoryInfo.appendPath("system/console/install"));
    try {
        // set referrer
        filePost.setRequestHeader("referer", "about:blank");
        List<Part> partList = new ArrayList<>();
        partList.add(new StringPart("action", "install"));
        partList.add(new StringPart("_noredir_", "_noredir_"));
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        IOUtils.copy(in, baos);
        PartSource partSource = new ByteArrayPartSource(fileName, baos.toByteArray());
        partList.add(new FilePart("bundlefile", partSource));
        partList.add(new StringPart("bundlestart", "start"));
        Part[] parts = partList.toArray(new Part[partList.size()]);
        filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
        int status = getHttpClient().executeMethod(filePost);
        if (status != 200) {
            throw new OsgiClientException("Method execution returned status " + status);
        }
    } catch (IOException e) {
        throw new OsgiClientException(e);
    } finally {
        filePost.releaseConnection();
    }
}
Also used : ByteArrayPartSource(org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource) PartSource(org.apache.commons.httpclient.methods.multipart.PartSource) PostMethod(org.apache.commons.httpclient.methods.PostMethod) ArrayList(java.util.ArrayList) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) MultipartRequestEntity(org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) ByteArrayPartSource(org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) Part(org.apache.commons.httpclient.methods.multipart.Part) OsgiClientException(org.apache.sling.ide.osgi.OsgiClientException)

Aggregations

FilePart (org.apache.commons.httpclient.methods.multipart.FilePart)3 Part (org.apache.commons.httpclient.methods.multipart.Part)3 PartSource (org.apache.commons.httpclient.methods.multipart.PartSource)3 BufferedInputStream (java.io.BufferedInputStream)2 IOException (java.io.IOException)2 MultipartRequestEntity (org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity)2 HttpMethodParams (org.apache.commons.httpclient.params.HttpMethodParams)2 SegmentCompletionProtocol (com.linkedin.pinot.common.protocols.SegmentCompletionProtocol)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 HttpException (org.apache.commons.httpclient.HttpException)1 EntityEnclosingMethod (org.apache.commons.httpclient.methods.EntityEnclosingMethod)1 PostMethod (org.apache.commons.httpclient.methods.PostMethod)1 ByteArrayPartSource (org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource)1 StringPart (org.apache.commons.httpclient.methods.multipart.StringPart)1 OsgiClientException (org.apache.sling.ide.osgi.OsgiClientException)1