List of usage examples for org.apache.http.entity ByteArrayEntity ByteArrayEntity
public ByteArrayEntity(byte[] bArr, ContentType contentType)
From source file:com.net.plus.common.http.transport.PostBytesHttpTransport.java
public byte[] send(HttpClient httpClient, Object obj) { byte[] bytes = (byte[]) obj; HttpPost post = new HttpPost(getSendUrl()); ByteArrayEntity entity = new ByteArrayEntity(bytes, ContentType.create(mimeType, charset)); entity.setChunked(true);/*from w w w .j a v a 2s . c o m*/ post.setEntity(entity); try { byte[] response = httpClient.execute(post, resHandler); return response; } catch (ConnectException ce) { post.abort(); log.error("Http transport error." + getSendUrl().toString(), ce); /*CommunicationException cce = new CommunicationException("pe.connect_failed"); cce.setDefaultMessage(getSendUrl().toString()); throw cce;*/ } catch (Exception ex) { post.abort(); /*log.error("Http transport error."+getSendUrl().toString(), ex); throw new CommunicationException("pe.error.undefined", ex);*/ } return null; }
From source file:juzu.impl.bridge.request.AbstractRequestEntityReader.java
@Test public void testPost() throws Exception { driver.get(applicationURL().toString()); WebElement elt = driver.findElement(By.id("post")); String url = elt.getText();/*from ww w. jav a 2 s .co m*/ data = null; HttpPost post = new HttpPost(url); post.setEntity(new ByteArrayEntity("<foo></foo>".getBytes(Tools.ISO_8859_1), ContentType.create("text/foo", Tools.ISO_8859_1))); HttpClient client = HttpClientBuilder.create().build(); client.execute(post); assertNotNull(data); assertEquals("<foo></foo>", new String(data, Tools.ISO_8859_1)); }
From source file:org.sentilo.platform.server.response.SentiloResponse.java
public void setBody(final ByteArrayOutputStream out, final ContentType contentType) { httpResponse.setEntity(new ByteArrayEntity(out.toByteArray(), contentType)); }
From source file:org.esigate.HttpErrorPage.java
private static HttpEntity toMemoryEntity(HttpEntity httpEntity) { if (httpEntity == null) { return null; }/*from w ww . j a va2 s .c om*/ HttpEntity memoryEntity; try { byte[] content = EntityUtils.toByteArray(httpEntity); ByteArrayEntity byteArrayEntity = new ByteArrayEntity(content, ContentType.get(httpEntity)); Header contentEncoding = httpEntity.getContentEncoding(); if (contentEncoding != null) { byteArrayEntity.setContentEncoding(contentEncoding); } memoryEntity = byteArrayEntity; } catch (IOException e) { StringBuilderWriter out = new StringBuilderWriter(Parameters.DEFAULT_BUFFER_SIZE); PrintWriter pw = new PrintWriter(out); e.printStackTrace(pw); pw.close(); memoryEntity = new StringEntity(out.toString(), ContentType.getOrDefault(httpEntity)); } return memoryEntity; }
From source file:io.github.bckfnn.reactstreams.arangodb.AsyncHttpClient.java
@Override public <T extends Result> Stream<T> process(Operation<T> req) { LOG.debug("req -> " + req.getUri()); return Stream.asOne(subscription -> { HttpRequest request = null;//from www . j av a 2 s. com if (req.getMethod().equals("POST")) { HttpPost post = new HttpPost(req.getUri()); post.setEntity( new ByteArrayEntity(mapper.writeValueAsBytes(req.getBody()), ContentType.APPLICATION_JSON)); request = post; } else if (req.getMethod().equals("GET")) { request = new HttpGet(req.getUri()); } httpClient.execute(new HttpHost(host, port), request, new FutureCallback<HttpResponse>() { public void completed(HttpResponse result) { LOG.debug("res <- " + result.getStatusLine().getStatusCode()); HttpEntity ent = result.getEntity(); System.out.println(result.getStatusLine().getStatusCode() + " " + ent.isRepeatable()); try { T val = mapper.readValue(ent.getContent(), req.getResponseClass()); subscription.sendNext(val); subscription.sendComplete(); } catch (Exception e) { subscription.sendError(e); } } public void failed(Exception ex) { subscription.sendError(ex); } public void cancelled() { subscription.sendError(new Throwable("cancelled")); } }); }); }
From source file:com.nextdoor.bender.ipc.es.ElasticSearchTransporterTest.java
private HttpClient getMockClientWithResponse(byte[] respPayload, ContentType contentType, int status) throws IOException { HttpClient mockClient = mock(HttpClient.class); HttpResponse mockResponse = mock(HttpResponse.class); StatusLine mockStatusLine = mock(StatusLine.class); doReturn("expected failure").when(mockStatusLine).getReasonPhrase(); doReturn(status).when(mockStatusLine).getStatusCode(); doReturn(mockStatusLine).when(mockResponse).getStatusLine(); HttpEntity entity = new ByteArrayEntity(respPayload, contentType); doReturn(entity).when(mockResponse).getEntity(); doReturn(mockResponse).when(mockClient).execute(any(HttpPost.class)); return mockClient; }
From source file:org.Cherry.Modules.Web.Engine.BadJujuRequestHandlerService.java
@Override public void handle(final RequestCommand command) throws HttpException, IOException { if (command.isBadJuju()) { final WeakReference<FastStringWriter> writer = new WeakReference<FastStringWriter>( new FastStringWriter()); getFreeMarkerService().process(normalizeTemplatePath(command.getStatusCode().asURI()), createModel(), writer.get());// w ww. j a va 2 s .c om final AbstractHttpEntity entity = new ByteArrayEntity(writer.get().getBuffer().toString().getBytes(), APPLICATION_HTML_CONTENT_TYPE); command.getResponse().setStatusCode(HttpStatus.SC_OK); command.getResponse().setEntity(entity); } }
From source file:org.apache.hyracks.tests.integration.ApplicationDeploymentAPIIntegrationTest.java
protected void deployApplicationFile(int dataSize, String fileName) throws URISyntaxException, IOException { final String deployid = "testApp"; String path = "/applications/" + deployid + "&" + fileName; URI uri = uri(path);//from w w w . java 2s.co m byte[] data = new byte[dataSize]; for (int i = 0; i < data.length; ++i) { data[i] = (byte) i; } HttpClient client = HttpClients.createMinimal(); // Put the data HttpPut put = new HttpPut(uri); HttpEntity entity = new ByteArrayEntity(data, ContentType.APPLICATION_OCTET_STREAM); put.setEntity(entity); client.execute(put); // Get it back HttpGet get = new HttpGet(uri); HttpResponse response = client.execute(get); HttpEntity respEntity = response.getEntity(); Header contentType = respEntity.getContentType(); // compare results Assert.assertEquals(ContentType.APPLICATION_OCTET_STREAM.getMimeType(), contentType.getValue()); InputStream is = respEntity.getContent(); for (int i = 0; i < dataSize; ++i) { Assert.assertEquals(data[i], (byte) is.read()); } Assert.assertEquals(-1, is.read()); is.close(); }
From source file:com.nextdoor.bender.ipc.http.HttpTransport.java
protected HttpResponse sendBatchUncompressed(HttpPost httpPost, byte[] raw) throws TransportException { HttpEntity entity = new ByteArrayEntity(raw, getUncompressedContentType()); httpPost.setEntity(entity);/* www . j av a 2 s . com*/ /* * Make call */ HttpResponse resp = null; try { resp = this.client.execute(httpPost); } catch (IOException e) { throw new TransportException("failed to make call", e); } return resp; }
From source file:org.sonatype.nexus.repositories.metadata.Hc4RawTransport.java
@Override public void writeRawData(final String path, final byte[] bytes) throws IOException { final HttpPut put = new HttpPut(createUrlWithPath(path)); put.setEntity(new ByteArrayEntity(bytes, ContentType.APPLICATION_XML)); final HttpResponse response = httpClient.execute(put); try {//from w w w . j a v a2 s. com if (response.getStatusLine().getStatusCode() > 299) { throw new IOException("The response was not successful: " + response.getStatusLine()); } } finally { EntityUtils.consume(response.getEntity()); } }