List of usage examples for org.apache.http.entity BasicHttpEntity setContent
public void setContent(InputStream inputStream)
From source file:org.fcrepo.integration.http.api.html.FedoraHtmlResponsesIT.java
License:asdf
private void postSparqlUpdateUsingHttpClient(final String sparql, final String pid) throws IOException { final HttpPatch method = new HttpPatch(serverAddress + pid); method.addHeader("Content-Type", "application/sparql-update"); final BasicHttpEntity entity = new BasicHttpEntity(); entity.setContent(new ByteArrayInputStream(sparql.getBytes())); method.setEntity(entity);/* ww w.java2s .c o m*/ final HttpResponse response = client.execute(method); assertEquals("Expected successful response.", 204, response.getStatusLine().getStatusCode()); }
From source file:org.sahli.asciidoc.confluence.publisher.client.http.HttpRequestFactory.java
HttpPut updatePageRequest(String contentId, String ancestorId, String title, String content, int newVersion) { assertMandatoryParameter(isNotBlank(contentId), "contentId"); assertMandatoryParameter(isNotBlank(ancestorId), "ancestorId"); assertMandatoryParameter(isNotBlank(title), "title"); PagePayload pagePayload = pagePayloadBuilder().ancestorId(ancestorId).title(title).content(content) .version(newVersion).build(); String jsonPayload = toJsonString(pagePayload); BasicHttpEntity entity = new BasicHttpEntity(); entity.setContent(new ByteArrayInputStream(jsonPayload.getBytes())); HttpPut updatePageRequest = new HttpPut(this.confluenceRestApiEndpoint + "/content/" + contentId); updatePageRequest.setEntity(entity); updatePageRequest.addHeader(APPLICATION_JSON_UTF8_HEADER); return updatePageRequest; }
From source file:org.zenoss.zep.rest.RestClient.java
private HttpEntity createProtobufEntity(Message msg) { BasicHttpEntity entity = new BasicHttpEntity(); entity.setContentType(ProtobufConstants.CONTENT_TYPE_PROTOBUF); entity.setContentLength(msg.getSerializedSize()); entity.setContent(new ByteArrayInputStream(msg.toByteArray())); return entity; }
From source file:org.docx4j.services.client.ConverterHttp.java
/** * Convert InputStream fromFormat to toFormat, streaming result to OutputStream os. * //w w w . j ava 2s . c o m * fromFormat supported: DOC, DOCX * * toFormat supported: PDF * * Note this uses a non-repeatable request entity, so it may not be suitable * (depending on the endpoint config). * * @param instream * @param fromFormat * @param toFormat * @param os * @throws IOException * @throws ConversionException */ public void convert(InputStream instream, Format fromFormat, Format toFormat, OutputStream os) throws IOException, ConversionException { checkParameters(fromFormat, toFormat); CloseableHttpClient httpclient = HttpClients.createDefault(); try { HttpPost httppost = getUrlForFormat(toFormat); BasicHttpEntity reqEntity = new BasicHttpEntity(); reqEntity.setContentType(map(fromFormat).getMimeType()); // messy that API is different to FileEntity reqEntity.setContent(instream); httppost.setEntity(reqEntity); execute(httpclient, httppost, os); } finally { httpclient.close(); } }
From source file:at.uni_salzburg.cs.ckgroup.cpcc.mapper.algorithms.RandomTaskGenerator.java
private void addActionPointToVirtualVehicle(IVirtualVehicleInfo vehicleInfo, ActionPoint ap) throws IOException { String url = vehicleInfo.getEngineUrl() + "/vehicle/text/vehicleAddTask/" + vehicleInfo.getVehicleName(); HttpPost httppost = new HttpPost(url); BasicHttpEntity entity1 = new BasicHttpEntity(); entity1.setContent(ap.getInputStream()); httppost.setEntity(entity1);/*www . j a va 2 s . c o m*/ HttpClient httpclient = new DefaultHttpClient(); HttpResponse response = httpclient.execute(httppost); LOG.info("Add AP: " + response.getStatusLine()); }
From source file:com.lion328.xenonlauncher.minecraft.api.authentication.yggdrasil.YggdrasilMinecraftAuthenticator.java
private ResponseState sendRequest(String endpoint, String data) throws IOException, YggdrasilAPIException { URL url = new URL(serverURL, endpoint); // HttpURLConnection can only handle 2xx response code for headers // so it need to use HttpCore instead // maybe I could use an alternative like HttpClient // but for lightweight, I think is not a good idea BasicHttpEntity entity = new BasicHttpEntity(); byte[] dataBytes = data.getBytes(StandardCharsets.UTF_8); entity.setContent(new ByteArrayInputStream(dataBytes)); entity.setContentLength(dataBytes.length); HttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST", url.getFile(), HttpVersion.HTTP_1_1);/*from w w w.j a v a2s .co m*/ request.setHeader(new BasicHeader("Host", url.getHost())); request.setHeader(new BasicHeader("Content-Type", "application/json")); request.setHeader(new BasicHeader("Content-Length", Integer.toString(dataBytes.length))); request.setEntity(entity); Socket s; int port = url.getPort(); if (url.getProtocol().equals("https")) { if (port == -1) { port = 443; } s = SSLSocketFactory.getDefault().createSocket(url.getHost(), port); } else { if (port == -1) { port = 80; } s = new Socket(url.getHost(), port); } DefaultBHttpClientConnection connection = new DefaultBHttpClientConnection(8192); connection.bind(s); try { connection.sendRequestHeader(request); connection.sendRequestEntity(request); HttpResponse response = connection.receiveResponseHeader(); connection.receiveResponseEntity(response); if (!response.getFirstHeader("Content-Type").getValue().startsWith("application/json")) { throw new InvalidImplementationException("Invalid content type"); } InputStream stream = response.getEntity().getContent(); StringBuilder sb = new StringBuilder(); int b; while ((b = stream.read()) != -1) { sb.append((char) b); } return new ResponseState(response.getStatusLine().getStatusCode(), sb.toString()); } catch (HttpException e) { throw new IOException(e); } }
From source file:org.zenoss.zep.rest.RestClient.java
private HttpEntity createJsonEntity(Message msg) throws IOException { BasicHttpEntity entity = new BasicHttpEntity(); entity.setContentType(MediaType.APPLICATION_JSON); ByteArrayOutputStream baos = new ByteArrayOutputStream(); JsonFormat.writeTo(msg, baos);/*from w w w .ja v a 2 s . c o m*/ entity.setContent(new ByteArrayInputStream(baos.toByteArray())); return entity; }
From source file:com.joyent.manta.client.multipart.ServerSideMultipartManagerTest.java
private ServerSideMultipartManager buildMockManager(final StatusLine statusLine, final String responseBody, final Consumer<CloseableHttpResponse> responseConfigCallback) throws IOException { final ConfigContext config = testConfigContext(); final CloseableHttpResponse response = mock(CloseableHttpResponse.class); when(response.getStatusLine()).thenReturn(statusLine); if (responseConfigCallback != null) { responseConfigCallback.accept(response); }/*from www. j a v a2s . c om*/ if (responseBody != null) { final BasicHttpEntity entity = new BasicHttpEntity(); entity.setContent(IOUtils.toInputStream(responseBody, StandardCharsets.UTF_8)); when(response.getEntity()).thenReturn(entity); } final MantaConnectionContext connectionContext = mock(MantaConnectionContext.class); final CloseableHttpClient fakeClient = new FakeCloseableHttpClient(response); when(connectionContext.getHttpClient()).thenReturn(fakeClient); final HttpHelper httpHelper = mock(HttpHelper.class); when(httpHelper.getConnectionContext()).thenReturn(connectionContext); when(httpHelper.getRequestFactory()).thenReturn(new MantaHttpRequestFactory(config.getMantaURL())); when(httpHelper.executeRequest(any(HttpUriRequest.class), any())).thenReturn(response); final MantaClient client = mock(MantaClient.class); return new ServerSideMultipartManager(config, httpHelper, client); }
From source file:at.deder.ybr.test.cukes.HttpServerSimulator.java
@Override public Object answer(InvocationOnMock invocation) throws Throwable { Object[] args = invocation.getArguments(); HttpResponse response = null;/*from w w w. ja v a 2 s . c om*/ if (args.length != 1 && !(args[0] instanceof HttpGet)) { response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_INTERNAL_SERVER_ERROR, "wrong arguments"); return response; } HttpGet request = (HttpGet) args[0]; BasicHttpEntity entity = new BasicHttpEntity(); String requestedPath = request.getURI().getPath(); VirtualResource requestedResource = getResource(requestedPath); if (requestedResource == null) { response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_NOT_FOUND, "not found"); } else { entity.setContent(new ByteArrayInputStream(requestedResource.content)); entity.setContentType(requestedResource.contentType); response = new BasicHttpResponse(HttpVersion.HTTP_1_1, requestedResource.httpStatus, requestedResource.httpStatusText); response.setEntity(entity); } return response; }