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:org.apache.brooklyn.util.core.http.AuthHandler.java

@Override
public void handle(HttpRequest request, HttpResponse response, HttpContext context) {
    String creds = (String) context.getAttribute("creds");
    if (creds == null || !creds.equals(getExpectedCredentials())) {
        response.setStatusCode(HttpStatus.SC_UNAUTHORIZED);
    } else {//from  w ww  . j a  v  a2s  .c  om
        response.setEntity(new ByteArrayEntity(responseBody));
    }
}

From source file:org.opencastproject.remotetest.server.IngestZipTest.java

@Test
public void testIngestZip() throws Exception {
    byte[] bytesToPost = IOUtils.toByteArray(getClass().getClassLoader().getResourceAsStream("ingest.zip"));
    HttpPost post = new HttpPost(BASE_URL + "/ingest/addZippedMediaPackage");
    post.setEntity(new ByteArrayEntity(bytesToPost));
    HttpResponse response = client.execute(post);
    Assert.assertEquals(200, response.getStatusLine().getStatusCode());
}

From source file:com.deployd.Deployd.java

public static JSONObject post(JSONObject input, String uri)
        throws ClientProtocolException, IOException, JSONException {

    HttpPost post = new HttpPost(endpoint + uri);
    HttpClient client = new DefaultHttpClient();
    post.setEntity(new ByteArrayEntity(input.toString().getBytes("UTF8")));
    HttpResponse response = client.execute(post);
    ByteArrayEntity e = (ByteArrayEntity) response.getEntity();
    InputStream is = e.getContent();
    String data = new Scanner(is).next();
    JSONObject result = new JSONObject(data);
    return result;
}

From source file:org.cloudsimulator.utility.RestAPI.java

public static ResponseMessageString sendString(final String requestMethod, final String restAPIURI,
        final String username, final String password, final String stringToSend, final String typeOfString,
        final String charset) throws IOException {
    CloseableHttpClient httpClient = HttpClients.createDefault();
    CloseableHttpResponse httpResponse = null;

    ResponseMessageString responseMessageString = null;
    if ("PUT".equals(requestMethod)) {
        httpResponse = putRequestBasicAuth(httpClient, escapeURI(restAPIURI), username, password, typeOfString,
                new ByteArrayEntity(stringToSend.getBytes(charset)));

    }/*from   w w  w . j a v  a 2 s  .co m*/

    if ("POST".equals(requestMethod)) {
        httpResponse = postRequestBasicAuth(httpClient, escapeURI(restAPIURI), username, password, typeOfString,
                new ByteArrayEntity(stringToSend.getBytes(charset)));
    }

    if (httpResponse != null) {
        if (httpResponse.getStatusLine() != null) {
            if (httpResponse.getEntity() != null) {
                responseMessageString = new ResponseMessageString(httpResponse.getStatusLine().getStatusCode(),
                        httpResponse.getStatusLine().getReasonPhrase(),
                        IOUtils.toString(httpResponse.getEntity().getContent(), charset));
            } else {
                responseMessageString = new ResponseMessageString(httpResponse.getStatusLine().getStatusCode(),
                        httpResponse.getStatusLine().getReasonPhrase(), null);
            }

        } else {
            if (httpResponse.getEntity() != null) {
                responseMessageString = new ResponseMessageString(null, null,
                        IOUtils.toString(httpResponse.getEntity().getContent(), charset));
            }
        }

        httpResponse.close();
    }

    httpClient.close();
    return responseMessageString;

}

From source file:com.eviware.soapui.impl.wsdl.support.CompressionSupport.java

public static byte[] decompress(String alg, byte[] content) throws Exception {
    // Use the excellent content encoding handling that exists in HTTP Client
    HttpResponse response = new BasicHttpResponse(new BasicStatusLine(new HttpVersion(1, 0), 0, null));
    ByteArrayEntity entity = new ByteArrayEntity(content);
    entity.setContentEncoding(alg);/*from   w  w w . j a va  2s.c  om*/
    response.setEntity(entity);
    new ResponseContentEncoding().process(response, null);
    return IOUtils.toByteArray(response.getEntity().getContent());
}

From source file:org.chaplib.TestHttpResource.java

@Before
public void setUp() throws Exception {
    uri = new URI("http://www.example.com/");
    entity = new ByteArrayEntity(new byte[] {});
    response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
    response.setEntity(entity);/* w  w  w . j  a va 2  s .c  om*/
    parsed = new Object();
    impl = new HttpResource(uri, mockHttpClient);
}

From source file:org.fcrepo.camel.FcrepoClientAuthTest.java

@Test
public void testAuthNoHost() throws IOException, FcrepoOperationFailedException {
    final int status = 200;
    final URI uri = create(baseUrl);
    final ByteArrayEntity entity = new ByteArrayEntity(rdfXml.getBytes());

    testClient = new FcrepoClient("user", "pass", null, true);
    setField(testClient, "httpclient", mockHttpclient);
    entity.setContentType(RDF_XML);//from w w w  . j  a  va 2s .com
    doSetupMockRequest(RDF_XML, entity, status);

    final FcrepoResponse response = testClient.get(uri, RDF_XML, null);

    assertEquals(response.getUrl(), uri);
    assertEquals(response.getStatusCode(), status);
    assertEquals(response.getContentType(), RDF_XML);
    assertEquals(response.getLocation(), null);
    assertEquals(IOUtils.toString(response.getBody()), rdfXml);
}

From source file:org.openvoters.android.tasks.RemoteAPIVoteTask.java

public static HttpResponse makeRequest(String path, Item item, String uniqueID) throws Exception {
    JSONObject holder = new JSONObject();
    holder.put("candidate", item.getID());
    holder.put("ID", uniqueID);

    int TIMEOUT_MILLISEC = 10000;
    HttpParams httpParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);
    HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
    HttpClient client = new DefaultHttpClient(httpParams);

    HttpPost request = new HttpPost(path);
    request.setEntity(new ByteArrayEntity(holder.toString().getBytes("UTF8")));
    HttpResponse response = client.execute(request);
    return response;
}

From source file:com.nominanuda.web.http.FormEncodingUtf8Test.java

private String g1(byte[] v, String ct) throws IOException {
    byte[] x = new byte[2 + v.length];
    x[0] = 'f';/*from  ww w.  j a v a 2 s  .  c  om*/
    x[1] = '=';
    System.arraycopy(v, 0, x, 2, v.length);
    ByteArrayEntity se = new ByteArrayEntity(x);
    if (ct != null) {
        se.setContentType(ct);
    }
    List<NameValuePair> pairs = h.parseEntityWithDefaultUtf8(se);
    String vv = pairs.get(0).getValue();
    return vv;
}