Search in sources :

Example 6 with RequestEntity

use of org.apache.commons.httpclient.methods.RequestEntity in project zeppelin by apache.

the class AbstractTestRestApi method httpPut.

protected static PutMethod httpPut(String path, String body, String user, String pwd) throws IOException {
    LOG.info("Connecting to {}", url + path);
    HttpClient httpClient = new HttpClient();
    PutMethod putMethod = new PutMethod(url + path);
    putMethod.addRequestHeader("Origin", url);
    RequestEntity entity = new ByteArrayRequestEntity(body.getBytes("UTF-8"));
    putMethod.setRequestEntity(entity);
    if (userAndPasswordAreNotBlank(user, pwd)) {
        putMethod.setRequestHeader("Cookie", "JSESSIONID=" + getCookie(user, pwd));
    }
    httpClient.executeMethod(putMethod);
    LOG.info("{} - {}", putMethod.getStatusCode(), putMethod.getStatusText());
    return putMethod;
}
Also used : HttpClient(org.apache.commons.httpclient.HttpClient) PutMethod(org.apache.commons.httpclient.methods.PutMethod) ByteArrayRequestEntity(org.apache.commons.httpclient.methods.ByteArrayRequestEntity) RequestEntity(org.apache.commons.httpclient.methods.RequestEntity) ByteArrayRequestEntity(org.apache.commons.httpclient.methods.ByteArrayRequestEntity)

Example 7 with RequestEntity

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

the class WebHdfsV1Client method uploadSegment.

// This method is based on:
// https://hadoop.apache.org/docs/r1.0.4/webhdfs.html#CREATE
public synchronized boolean uploadSegment(String webHdfsPath, String localFilePath) {
    // Step 1: Submit a HTTP PUT request without automatically following
    // redirects and without sending the file data.
    String firstPutReqString = String.format(WEB_HDFS_UPLOAD_PATH_TEMPLATE, _protocol, _host, _port, webHdfsPath, _overwrite, _permission);
    HttpMethod firstPutReq = new PutMethod(firstPutReqString);
    try {
        LOGGER.info("Trying to send request: {}.", firstPutReqString);
        int firstResponseCode = _httpClient.executeMethod(firstPutReq);
        if (firstResponseCode != 307) {
            LOGGER.error(String.format("Failed to execute the first PUT request to upload segment to webhdfs: %s. " + "Expected response code 307, but get %s. Response body: %s", firstPutReqString, firstResponseCode, firstPutReq.getResponseBodyAsString()));
            return false;
        }
    } catch (Exception e) {
        LOGGER.error(String.format("Failed to execute the first request to upload segment to webhdfs: %s.", firstPutReqString), e);
        return false;
    } finally {
        firstPutReq.releaseConnection();
    }
    // Step 2: Submit another HTTP PUT request using the URL in the Location
    // header with the file data to be written.
    String redirectedReqString = firstPutReq.getResponseHeader(LOCATION).getValue();
    PutMethod redirectedReq = new PutMethod(redirectedReqString);
    File localFile = new File(localFilePath);
    RequestEntity requestEntity = new FileRequestEntity(localFile, "application/binary");
    redirectedReq.setRequestEntity(requestEntity);
    try {
        LOGGER.info("Trying to send request: {}.", redirectedReqString);
        int redirectedResponseCode = _httpClient.executeMethod(redirectedReq);
        if (redirectedResponseCode != 201) {
            LOGGER.error(String.format("Failed to execute the redirected PUT request to upload segment to webhdfs: %s. " + "Expected response code 201, but get %s. Response: %s", redirectedReqString, redirectedResponseCode, redirectedReq.getResponseBodyAsString()));
        }
        return true;
    } catch (IOException e) {
        LOGGER.error(String.format("Failed to execute the redirected request to upload segment to webhdfs: %s.", redirectedReqString), e);
        return false;
    } finally {
        redirectedReq.releaseConnection();
    }
}
Also used : FileRequestEntity(org.apache.commons.httpclient.methods.FileRequestEntity) PutMethod(org.apache.commons.httpclient.methods.PutMethod) IOException(java.io.IOException) FileRequestEntity(org.apache.commons.httpclient.methods.FileRequestEntity) RequestEntity(org.apache.commons.httpclient.methods.RequestEntity) File(java.io.File) HttpMethod(org.apache.commons.httpclient.HttpMethod) IOException(java.io.IOException)

Example 8 with RequestEntity

use of org.apache.commons.httpclient.methods.RequestEntity in project openhab1-addons by openhab.

the class CcuClient method sendScript.

/**
     * Main method for sending a TclRega script and parsing the XML result.
     */
@SuppressWarnings("unchecked")
private synchronized <T> T sendScript(String script, Class<T> clazz) throws HomematicClientException {
    PostMethod post = null;
    try {
        script = StringUtils.trim(script);
        if (StringUtils.isEmpty(script)) {
            throw new RuntimeException("Homematic TclRegaScript is empty!");
        }
        if (TRACE_ENABLED) {
            logger.trace("TclRegaScript: {}", script);
        }
        post = new PostMethod(context.getConfig().getTclRegaUrl());
        RequestEntity re = new ByteArrayRequestEntity(script.getBytes("ISO-8859-1"));
        post.setRequestEntity(re);
        httpClient.executeMethod(post);
        String result = post.getResponseBodyAsString();
        result = StringUtils.substringBeforeLast(result, "<xml><exec>");
        if (TRACE_ENABLED) {
            logger.trace("Result TclRegaScript: {}", result);
        }
        Unmarshaller um = JAXBContext.newInstance(clazz).createUnmarshaller();
        um.setListener(new CommonUnmarshallerListener());
        return (T) um.unmarshal(new StringReader(result));
    } catch (Exception ex) {
        throw new HomematicClientException(ex.getMessage(), ex);
    } finally {
        if (post != null) {
            post.releaseConnection();
        }
    }
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) StringReader(java.io.StringReader) CommonUnmarshallerListener(org.openhab.binding.homematic.internal.model.CommonUnmarshallerListener) ByteArrayRequestEntity(org.apache.commons.httpclient.methods.ByteArrayRequestEntity) RequestEntity(org.apache.commons.httpclient.methods.RequestEntity) Unmarshaller(javax.xml.bind.Unmarshaller) JAXBException(javax.xml.bind.JAXBException) ByteArrayRequestEntity(org.apache.commons.httpclient.methods.ByteArrayRequestEntity)

Example 9 with RequestEntity

use of org.apache.commons.httpclient.methods.RequestEntity in project zm-mailbox by Zimbra.

the class DavRequest method getHttpMethod.

public HttpMethod getHttpMethod(String baseUrl) {
    String url = mRedirectUrl;
    if (url == null) {
        // schedule-outbox-URL in the same report.
        try {
            // This will throw an exception when the URL is a relative one.
            new URL(mUri);
            url = mUri;
        } catch (MalformedURLException e) {
            url = baseUrl + mUri;
        }
    }
    if (mDoc == null)
        return new GetMethod(url) {

            @Override
            public String getName() {
                return mMethod;
            }
        };
    PutMethod m = new PutMethod(url) {

        RequestEntity re;

        @Override
        public String getName() {
            return mMethod;
        }

        @Override
        protected RequestEntity generateRequestEntity() {
            return re;
        }

        @Override
        public void setRequestEntity(RequestEntity requestEntity) {
            re = requestEntity;
            super.setRequestEntity(requestEntity);
        }
    };
    DocumentRequestEntity re = new DocumentRequestEntity(mDoc);
    m.setRequestEntity(re);
    return m;
}
Also used : MalformedURLException(java.net.MalformedURLException) GetMethod(org.apache.commons.httpclient.methods.GetMethod) PutMethod(org.apache.commons.httpclient.methods.PutMethod) RequestEntity(org.apache.commons.httpclient.methods.RequestEntity) URL(java.net.URL)

Example 10 with RequestEntity

use of org.apache.commons.httpclient.methods.RequestEntity in project zm-mailbox by Zimbra.

the class DavMethod method toHttpMethod.

public HttpMethod toHttpMethod(DavContext ctxt, String targetUrl) throws IOException, DavException {
    if (ctxt.getUpload() != null && ctxt.getUpload().getSize() > 0) {
        PostMethod method = new PostMethod(targetUrl) {

            @Override
            public String getName() {
                return getMethodName();
            }
        };
        RequestEntity reqEntry;
        if (ctxt.hasRequestMessage()) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            XMLWriter writer = new XMLWriter(baos);
            writer.write(ctxt.getRequestMessage());
            reqEntry = new ByteArrayRequestEntity(baos.toByteArray());
        } else {
            // this could be a huge upload
            reqEntry = new InputStreamRequestEntity(ctxt.getUpload().getInputStream(), ctxt.getUpload().getSize());
        }
        method.setRequestEntity(reqEntry);
        return method;
    }
    return new GetMethod(targetUrl) {

        @Override
        public String getName() {
            return getMethodName();
        }
    };
}
Also used : InputStreamRequestEntity(org.apache.commons.httpclient.methods.InputStreamRequestEntity) PostMethod(org.apache.commons.httpclient.methods.PostMethod) GetMethod(org.apache.commons.httpclient.methods.GetMethod) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayRequestEntity(org.apache.commons.httpclient.methods.ByteArrayRequestEntity) InputStreamRequestEntity(org.apache.commons.httpclient.methods.InputStreamRequestEntity) RequestEntity(org.apache.commons.httpclient.methods.RequestEntity) XMLWriter(org.dom4j.io.XMLWriter) ByteArrayRequestEntity(org.apache.commons.httpclient.methods.ByteArrayRequestEntity)

Aggregations

RequestEntity (org.apache.commons.httpclient.methods.RequestEntity)11 ByteArrayRequestEntity (org.apache.commons.httpclient.methods.ByteArrayRequestEntity)6 StringRequestEntity (org.apache.commons.httpclient.methods.StringRequestEntity)5 IOException (java.io.IOException)3 EntityEnclosingMethod (org.apache.commons.httpclient.methods.EntityEnclosingMethod)3 FileRequestEntity (org.apache.commons.httpclient.methods.FileRequestEntity)3 InputStreamRequestEntity (org.apache.commons.httpclient.methods.InputStreamRequestEntity)3 PostMethod (org.apache.commons.httpclient.methods.PostMethod)3 PutMethod (org.apache.commons.httpclient.methods.PutMethod)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 InputStream (java.io.InputStream)2 Message (org.apache.camel.Message)2 HttpClient (org.apache.commons.httpclient.HttpClient)2 HttpMethod (org.apache.commons.httpclient.HttpMethod)2 GetMethod (org.apache.commons.httpclient.methods.GetMethod)2 SpanEventRecorder (com.navercorp.pinpoint.bootstrap.context.SpanEventRecorder)1 Serializable (java.io.Serializable)1 StringReader (java.io.StringReader)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1