Example usage for org.apache.http.entity ByteArrayEntity ByteArrayEntity

List of usage examples for org.apache.http.entity ByteArrayEntity ByteArrayEntity

Introduction

In this page you can find the example usage for org.apache.http.entity ByteArrayEntity ByteArrayEntity.

Prototype

public ByteArrayEntity(byte[] bArr) 

Source Link

Usage

From source file:com.nominanuda.web.mvc.Favicon.java

@Override
public HttpResponse handle(HttpRequest request) throws Exception {
    HttpResponse resp = HTTP.createBasicResponse(200);
    ByteArrayEntity e = new ByteArrayEntity(favicon);
    e.setContentType(CT_IMAGE_X_ICON);/*from   ww  w.  j  ava  2s  .c  o  m*/
    resp.setEntity(e);
    return resp;
}

From source file:net.gcolin.httpquery.RequestWithPayloadImpl.java

public Request serializeWith(Serializer s) {
    if (s != null) {
        try {/*from ww  w  .ja  v  a 2s . c o  m*/
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            s.write(out, obj);
            delegate.setEntity(new ByteArrayEntity(out.toByteArray()));
            String type = IO.contentType(s);
            if (type != null) {
                delegate.addHeader("Content-Type", type);
            }
        } catch (IOException e) {
            Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, e.getMessage(), e);
        }

    }
    return new RequestImpl(delegate);
}

From source file:de.utkast.encoding.EncodingTestClient.java

@Test
public void testEncoding() throws Exception {

    String input = "?? ?";
    String url = "http://localhost:8080/restful-encoding/enc";

    EncodingDTO dto = new EncodingDTO(input);

    JAXBContext ctx = JAXBContext.newInstance(EncodingDTO.class);
    StringWriter writer = new StringWriter();
    ctx.createMarshaller().marshal(dto, writer);

    System.out.println(String.format("Will send: %s", writer.toString()));

    HttpClient client = HttpClientBuilder.create().build();
    HttpPost post = new HttpPost(url);
    HttpEntity entity = new ByteArrayEntity(writer.toString().getBytes("UTF-8"));
    post.setEntity(entity);/*from  w w  w.j a v a2 s  .  co  m*/
    post.setHeader("Content-Type", "application/xml;charset=UTF-8");
    post.setHeader("Accept", "application/xml;charset=UTF-8");

    HttpResponse response = client.execute(post);
    String output = EntityUtils.toString(response.getEntity(), "UTF-8");

    assertEquals(writer.toString(), output);
}

From source file:com.messagemedia.restapi.client.v1.internal.RestRequest.java

/**
 * Builds an apache http request/*from w w  w .  j av a2 s .  c om*/
 *
 * @return the apache http request
 */
HttpUriRequest getHttpRequest() {
    HttpUriRequest request;
    switch (method) {
    case GET:
        request = new HttpGet(url);
        break;
    case DELETE:
        request = new HttpDelete(url);
        break;
    case HEAD:
        request = new HttpHead(url);
        break;
    case POST:
        HttpPost post = new HttpPost(url);
        post.setEntity(new ByteArrayEntity(body));
        request = post;
        break;
    case PUT:
        HttpPut put = new HttpPut(url);
        put.setEntity(new ByteArrayEntity(body));
        request = put;
        break;
    case PATCH:
        HttpPatch patch = new HttpPatch(url);
        patch.setEntity(new ByteArrayEntity(body));
        request = patch;
        break;
    default:
        throw new RuntimeException("Method not supported");
    }

    addHeaders(request);

    return request;
}

From source file:org.fao.geonet.MockCloseableHttpResponse.java

public MockCloseableHttpResponse(int responseCode, String statusReason, byte[] response) {
    _response = new BasicHttpResponse(HttpVersion.HTTP_1_1, responseCode, statusReason);
    _response.setEntity(new ByteArrayEntity(response));
}

From source file:com.ctg.itrdc.janus.rpc.protocol.hessian.HttpClientConnection.java

public void sendRequest() throws IOException {
    request.setEntity(new ByteArrayEntity(output.toByteArray()));
    this.response = httpClient.execute(request);
}

From source file:email.mandrill.SendMail.java

public static void checkPingPong() {
    try {/*  w  w w . j  a  va  2s. c om*/
        HttpClient httpclient = HttpClients.createDefault();
        HttpPost httppost = new HttpPost("https://mandrillapp.com/api/1.0/users/ping.json");

        JSONObject obj = new JSONObject();
        obj.put("key", SendEmail.MANDRILL_KEY);
        String request = obj.toString();
        HttpEntity entity = new ByteArrayEntity(request.getBytes("UTF-8"));
        httppost.setEntity(entity);
        //Execute and get the response.
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity responseEntity = response.getEntity();

        if (responseEntity != null) {
            InputStream instream = responseEntity.getContent();
            try {
                StringWriter writer = new StringWriter();
                IOUtils.copy(instream, writer, "UTF-8");
                String theString = writer.toString();
                logger.log(Level.INFO, theString);
            } finally {
                instream.close();
            }
        }
    } catch (Exception e) {
        logger.log(Level.SEVERE, util.Utility.logMessage(e, "Exception while updating org name:", null));
    }
}

From source file:de.l3s.archivepig.enrich.StringContent.java

@Override
public void enrich(Tuple data, Tuple enrichment, Object... params) throws Exception {
    byte[] payload = get(data, enrichField);
    HttpEntity entity = new ByteArrayEntity(payload);
    enrichment.append(EntityUtils.toString(entity).trim());
}

From source file:com.subgraph.vega.ui.httpviewer.entity.HttpEntityBinaryViewer.java

private HttpEntity createEntity(byte[] content, String contentType, String contentEncoding) {
    final ByteArrayEntity entity = new ByteArrayEntity(content);
    if (contentType != null && !contentType.isEmpty()) {
        entity.setContentType(contentType);
    }//w ww .j a v a  2s  . c  o m
    if (contentEncoding != null && !contentEncoding.isEmpty()) {
        entity.setContentEncoding(contentEncoding);
    }
    return entity;
}

From source file:org.n52.shetland.util.HTTP.java

public static void post(URI uri, byte[] bytes, OutputStream out) throws IOException {
    HttpPost request = new HttpPost(uri);
    request.setEntity(new ByteArrayEntity(bytes));
    execute(request, out);/*  ww  w  .j  av a  2 s . c o  m*/
}