Search in sources :

Example 16 with ByteArrayRequestEntity

use of org.apache.commons.httpclient.methods.ByteArrayRequestEntity 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 17 with ByteArrayRequestEntity

use of org.apache.commons.httpclient.methods.ByteArrayRequestEntity 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 18 with ByteArrayRequestEntity

use of org.apache.commons.httpclient.methods.ByteArrayRequestEntity in project zaproxy by zaproxy.

the class HttpMethodHelper method createRequestMethodNew.

// Not used - all abstract using Generic method but GET cannot be used.
public HttpMethod createRequestMethodNew(HttpRequestHeader header, HttpBody body) throws URIException {
    HttpMethod httpMethod = null;
    String method = header.getMethod();
    URI uri = header.getURI();
    String version = header.getVersion();
    httpMethod = new GenericMethod(method);
    httpMethod.setURI(uri);
    HttpMethodParams httpParams = httpMethod.getParams();
    // default to use HTTP 1.0
    httpParams.setVersion(HttpVersion.HTTP_1_0);
    if (version.equalsIgnoreCase(HttpHeader.HTTP11)) {
        httpParams.setVersion(HttpVersion.HTTP_1_1);
    }
    // set various headers
    int pos = 0;
    // ZAP: FindBugs fix - always initialise pattern
    Pattern pattern = patternCRLF;
    String delimiter = CRLF;
    String msg = header.getHeadersAsString();
    if ((pos = msg.indexOf(CRLF)) < 0) {
        if ((pos = msg.indexOf(LF)) < 0) {
            delimiter = LF;
            pattern = patternLF;
        }
    } else {
        delimiter = CRLF;
        pattern = patternCRLF;
    }
    String[] split = pattern.split(msg);
    String token = null;
    String name = null;
    String value = null;
    for (int i = 0; i < split.length; i++) {
        token = split[i];
        if (token.equals("")) {
            continue;
        }
        if ((pos = token.indexOf(":")) < 0) {
            return null;
        }
        name = token.substring(0, pos).trim();
        value = token.substring(pos + 1).trim();
        httpMethod.addRequestHeader(name, value);
    }
    // set body if post method or put method
    if (body != null && body.length() > 0) {
        EntityEnclosingMethod generic = (EntityEnclosingMethod) httpMethod;
        //			generic.setRequestEntity(new StringRequestEntity(body.toString()));
        generic.setRequestEntity(new ByteArrayRequestEntity(body.getBytes()));
    }
    httpMethod.setFollowRedirects(false);
    return httpMethod;
}
Also used : Pattern(java.util.regex.Pattern) EntityEnclosingMethod(org.apache.commons.httpclient.methods.EntityEnclosingMethod) HttpMethodParams(org.apache.commons.httpclient.params.HttpMethodParams) URI(org.apache.commons.httpclient.URI) HttpMethod(org.apache.commons.httpclient.HttpMethod) ByteArrayRequestEntity(org.apache.commons.httpclient.methods.ByteArrayRequestEntity)

Example 19 with ByteArrayRequestEntity

use of org.apache.commons.httpclient.methods.ByteArrayRequestEntity in project zaproxy by zaproxy.

the class GenericMethod method generateRequestEntity.

/**
     * Generates a request entity from the post parameters, if present.  Calls
     * {@link EntityEnclosingMethod#generateRequestBody()} if parameters have not been set.
     * 
     * @since 3.0
     */
@Override
protected RequestEntity generateRequestEntity() {
    if (!this.params.isEmpty()) {
        // Use a ByteArrayRequestEntity instead of a StringRequestEntity.
        // This is to avoid potential encoding issues.  Form url encoded strings
        // are ASCII by definition but the content type may not be.  Treating the content
        // as bytes allows us to keep the current charset without worrying about how
        // this charset will effect the encoding of the form url encoded string.
        String content = EncodingUtil.formUrlEncode(getParameters(), getRequestCharSet());
        ByteArrayRequestEntity entity = new ByteArrayRequestEntity(EncodingUtil.getAsciiBytes(content), FORM_URL_ENCODED_CONTENT_TYPE);
        return entity;
    }
    return super.generateRequestEntity();
}
Also used : ByteArrayRequestEntity(org.apache.commons.httpclient.methods.ByteArrayRequestEntity)

Example 20 with ByteArrayRequestEntity

use of org.apache.commons.httpclient.methods.ByteArrayRequestEntity 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

ByteArrayRequestEntity (org.apache.commons.httpclient.methods.ByteArrayRequestEntity)30 HttpClient (org.apache.commons.httpclient.HttpClient)20 PostMethod (org.apache.commons.httpclient.methods.PostMethod)11 Account (com.zimbra.cs.account.Account)9 PutMethod (org.apache.commons.httpclient.methods.PutMethod)8 Test (org.junit.Test)8 Header (org.apache.commons.httpclient.Header)5 RequestEntity (org.apache.commons.httpclient.methods.RequestEntity)5 Document (org.w3c.dom.Document)5 Element (com.zimbra.common.soap.Element)4 GetMethod (org.apache.commons.httpclient.methods.GetMethod)4 ZMailbox (com.zimbra.client.ZMailbox)3 HttpMethod (org.apache.commons.httpclient.HttpMethod)3 EntityEnclosingMethod (org.apache.commons.httpclient.methods.EntityEnclosingMethod)3 SearchRequest (com.zimbra.soap.mail.message.SearchRequest)2 SearchResponse (com.zimbra.soap.mail.message.SearchResponse)2 SearchHit (com.zimbra.soap.type.SearchHit)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2