Example usage for org.apache.commons.httpclient.methods ByteArrayRequestEntity ByteArrayRequestEntity

List of usage examples for org.apache.commons.httpclient.methods ByteArrayRequestEntity ByteArrayRequestEntity

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.methods ByteArrayRequestEntity ByteArrayRequestEntity.

Prototype

public ByteArrayRequestEntity(byte[] paramArrayOfByte, String paramString) 

Source Link

Usage

From source file:org.ws4d.pipes.modules.http.BinaryPost.java

public void doWork() {

    // execute base module doWork()
    super.doWork();

    final PortValue bodyV = getWiredOrConfiguredValue(PORT_BODY);
    final PortValue contentTypeV = getWiredOrConfiguredValue(PORT_CONTENTTYPE);

    // check if we can terminate
    if (canClose()) {
        closeAllPorts();/* ww  w  . ja  va2 s.  c o  m*/
        return;
    }

    // if body is string
    if (bodyV.isString()) {
        try {
            body = bodyV.getString().getBytes("utf-8");
        } catch (UnsupportedEncodingException e) {
            getLogger().log(Level.SEVERE, "Can't convert string", e);
            body = null;
        }
    }

    // if body is byte array
    if (bodyV.isByteArray()) {
        body = bodyV.getByteArray();
    }

    // get content type
    if (contentTypeV.isString()) {
        contentType = contentTypeV.getString();
    }

    if ((getUrl() != null) && (body != null)) {
        final PostMethod method = new PostMethod(getUrl());
        try {
            method.setRequestEntity(new ByteArrayRequestEntity(body, contentType));
        } catch (Exception e) {
            getLogger().log(Level.SEVERE, "Can't create request entity", e);
            return;
        }

        try {
            getClient().executeMethod(method);

            setOutData(PORT_RESPONSE, method.getResponseBodyAsString());
            setOutData(PORT_STATUSCODE, method.getStatusCode());
            setOutData(PORT_STATUSLINE, method.getStatusLine());

        } catch (Exception e) {
            getLogger().log(Level.SEVERE, "Can't execute http post request", e);
        }
        method.releaseConnection();
    }
}

From source file:org.ws4d.pipes.modules.http.Put.java

public void doWork() {

    // execute base module doWork()
    super.doWork();

    /* wait for input */
    final PortValue bodyV = getWiredOrConfiguredValue(PORT_BODY);
    final PortValue contentTypeV = getWiredOrConfiguredValue(PORT_CONTENTTYPE);

    // check if we can terminate
    if (canClose()) {
        closeAllPorts();//w ww  .jav a  2s.com
        return;
    }

    // if body is string
    if (bodyV.isString()) {
        try {
            body = bodyV.getString().getBytes("utf-8");
        } catch (UnsupportedEncodingException e) {
            getLogger().log(Level.SEVERE, "Can't convert string", e);
            body = null;
        }
    }

    // if body is byte array
    if (bodyV.isByteArray()) {
        body = bodyV.getByteArray();
    }

    // get content type
    if (contentTypeV.isString()) {
        contentType = contentTypeV.getString();
    }

    if ((getUrl() != null) && (body != null)) {
        final PutMethod method = new PutMethod(getUrl());
        try {
            method.setRequestEntity(new ByteArrayRequestEntity(body, contentType));
        } catch (Exception e) {
            getLogger().log(Level.SEVERE, "Can't create request entity", e);
            return;
        }

        try {
            getClient().executeMethod(method);

            setOutData(BaseModule.PORT_STATUSCODE, method.getStatusCode());
            setOutData(BaseModule.PORT_STATUSLINE, method.getStatusLine());

        } catch (Exception e) {
            getLogger().log(Level.SEVERE, "Can't execute http put request", e);
        } finally {
            method.releaseConnection();
        }
    }
}

From source file:test.integ.be.fedict.trust.TSATest.java

@Test
public void testTSA() throws Exception {

    // setup/*www . j a  v  a 2  s . c om*/
    TimeStampRequestGenerator requestGen = new TimeStampRequestGenerator();
    requestGen.setCertReq(true);
    TimeStampRequest request = requestGen.generate(TSPAlgorithms.SHA1, new byte[20], BigInteger.valueOf(100));
    byte[] requestData = request.getEncoded();

    HttpClient httpClient = new HttpClient();
    httpClient.getHostConfiguration().setProxy("proxy.yourict.net", 8080);
    PostMethod postMethod = new PostMethod(tsa_location);
    postMethod.setRequestEntity(new ByteArrayRequestEntity(requestData, "application/timestamp-query"));

    // operate
    int statusCode = httpClient.executeMethod(postMethod);
    if (statusCode != HttpStatus.SC_OK) {
        LOG.error("Error contacting TSP server " + tsa_location);
        throw new Exception("Error contacting TSP server " + tsa_location);
    }

    TimeStampResponse tspResponse = new TimeStampResponse(postMethod.getResponseBodyAsStream());
    postMethod.releaseConnection();

    CertStore certStore = tspResponse.getTimeStampToken().getCertificatesAndCRLs("Collection", "BC");

    Collection<? extends Certificate> certificates = certStore.getCertificates(null);
    List<X509Certificate> certificateChain = new LinkedList<X509Certificate>();
    for (Certificate certificate : certificates) {
        LOG.debug("certificate: " + certificate.toString());
        certificateChain.add(0, (X509Certificate) certificate);
    }

    LOG.debug("token received");
    // send token to trust service
    XKMS2Client client = new XKMS2Client("https://www.e-contract.be/eid-trust-service-ws/xkms2");
    client.setProxy("proxy.yourict.net", 8080);
    client.validate(TrustServiceDomains.BELGIAN_TSA_TRUST_DOMAIN, certificateChain, true);
}