List of usage examples for org.apache.http.entity ContentType create
public static ContentType create(String str, String str2) throws UnsupportedCharsetException
From source file:org.n52.ses.services.wps.WPSConnector.java
/** * Sends a request to a web service. Can deal with SOAP messages and http-authentification. * /* w ww .j a v a 2 s .c o m*/ * @param xmlRequest * the request * @return * the response from the web service * @throws Exception */ private XmlObject sendHttpPost(XmlObject xmlRequest) throws Exception { String request = xmlRequest.xmlText(requestOptions); // authentication if (userNamePW != null) { String username = userNamePW.get(USER_KEY); String password = userNamePW.get(PASSWORD_KEY); client.setAuthentication(new BasicAuthenticator(username, password)); } SESHttpResponse resp = client.sendPost(new URL(wpsURL), request, ContentType.create("text/xml", "utf-8")); if (resp.getContentType().contains("xml")) { return XmlObject.Factory.parse(resp.getContent()); } return null; }
From source file:org.elasticsearch.test.rest.yaml.ClientYamlTestExecutionContext.java
private HttpEntity createEntity(List<Map<String, Object>> bodies, Map<String, String> headers) throws IOException { if (bodies.isEmpty()) { return null; }//from w w w .ja v a2 s .c o m if (bodies.size() == 1) { XContentType xContentType = getContentType(headers, XContentType.values()); BytesRef bytesRef = bodyAsBytesRef(bodies.get(0), xContentType); return new ByteArrayEntity(bytesRef.bytes, bytesRef.offset, bytesRef.length, ContentType.create(xContentType.mediaTypeWithoutParameters(), StandardCharsets.UTF_8)); } else { XContentType xContentType = getContentType(headers, STREAMING_CONTENT_TYPES); List<BytesRef> bytesRefList = new ArrayList<>(); int totalBytesLength = 0; for (Map<String, Object> body : bodies) { BytesRef bytesRef = bodyAsBytesRef(body, xContentType); bytesRefList.add(bytesRef); totalBytesLength += bytesRef.length - bytesRef.offset + 1; } byte[] bytes = new byte[totalBytesLength]; int position = 0; for (BytesRef bytesRef : bytesRefList) { for (int i = bytesRef.offset; i < bytesRef.length; i++) { bytes[position++] = bytesRef.bytes[i]; } bytes[position++] = xContentType.xContent().streamSeparator(); } return new ByteArrayEntity(bytes, ContentType.create(xContentType.mediaTypeWithoutParameters(), StandardCharsets.UTF_8)); } }
From source file:co.mafiagame.telegraminterface.outputhandler.TelegramChannel.java
private void deliverMessage(SendMessage sendMessage) throws IOException { logger.info("deliver {}", sendMessage); HttpPost post = new HttpPost(url); post.setHeader("Content-Type", "application/json; charset=UTF-8"); StringEntity entity = new StringEntity(objectMapper.writeValueAsString(sendMessage), ContentType.create("application/json", "UTF-8")); post.setEntity(entity);/*w ww . j a v a 2 s . c o m*/ HttpResponse response = client.execute(post); SendMessageResult result = objectMapper.readValue(response.getEntity().getContent(), SendMessageResult.class); post.releaseConnection(); if (!result.isOk()) { logger.error("could not deliver message: {} - {}", result.getErrorCode(), result.getDescription()); throw new CouldNotSendMessageException(); } }
From source file:com.autonomy.aci.client.transport.AciParameter.java
@Override public void addToEntity(final MultipartEntityBuilder builder, final Charset charset) { builder.addPart(name, new StringBody(value, ContentType.create("text/plain", charset))); }
From source file:org.n52.ses.common.environment.SESSoapClient.java
private Element sendHTTPPost(EndpointReference dest, String soapString) throws IllegalStateException, IOException, URISyntaxException, Exception { SESHttpResponse postResponse = this.httpClient.sendPost(getDestinationURL(dest), soapString, ContentType.create("application/soap+xml", "UTF-8")); Element soapResponse = null;/*from w ww. j a v a2 s .c om*/ // // read in the response and build an XML document from it // if (postResponse != null && postResponse == SESHttpResponse.NO_CONTENT_RESPONSE) { return null; } else if (postResponse != null && postResponse.getContentType().contains("xml")) { Document responseDoc = XmlUtils.createDocument(postResponse.getContent()); soapResponse = XmlUtils.getFirstElement(responseDoc); } else { logger.warn("received a null or unsupported response."); } postResponse.getContent().close(); return soapResponse; }
From source file:com.oakhole.voa.utils.HttpClientUtils.java
/** * post/*w w w. ja v a 2s . co m*/ * * @param uri * @param map * @return */ public static String post(String uri, Map<String, Object> map) { CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(uri); HttpEntity httpEntity = null; MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create(); for (String key : map.keySet()) { Object value = map.get(key); if (value != null && value instanceof String) { String param = (String) value; multipartEntityBuilder.addTextBody(key, param, ContentType.create("text/plain", CHARSET)); } if (value != null && value instanceof File) { multipartEntityBuilder.addBinaryBody(key, (File) value); } } try { httpPost.setEntity(multipartEntityBuilder.build()); HttpResponse httpResponse = httpClient.execute(httpPost); if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { httpEntity = httpResponse.getEntity(); } } catch (IOException e) { logger.error(":{}", e.getMessage()); } try { return EntityUtils.toString(httpEntity, CHARSET); } catch (IOException e) { logger.error(":{}", e.getMessage()); } return ""; }
From source file:io.liveoak.pgsql.BasePgSqlHttpTest.java
protected String putRequest(HttpPut put, String json) throws IOException { StringEntity entity = new StringEntity(json, ContentType.create(APPLICATION_JSON, "UTF-8")); put.setEntity(entity);/*from w w w . j av a 2 s . c o m*/ System.err.println("DO PUT - " + put.getURI()); System.out.println("\n" + json); CloseableHttpResponse result = httpClient.execute(put); System.err.println("=============>>>"); System.err.println(result); HttpEntity resultEntity = result.getEntity(); assertThat(resultEntity.getContentLength()).isGreaterThan(0); ByteArrayOutputStream baos = new ByteArrayOutputStream(); resultEntity.writeTo(baos); String resultStr = new String(baos.toByteArray()); System.err.println(resultStr); System.err.println("\n<<<============="); return resultStr; }
From source file:com.swisscom.refimpl.boundary.MIB3Client.java
public EasypayResponse modifyAuthSubscription(String merchantId, String easypayAuthId, String operation) throws IOException, HttpException { HttpPut method = null;// w w w .j a v a2s. co m try { method = new HttpPut(MIB3Client.AUTHSUBSCRIPTIONS_URL + "/" + easypayAuthId); String entity = "{\"operation\": \"" + operation + "\"}"; method.setEntity(new StringEntity(entity, ContentType.create("application/vnd.ch.swisscom.easypay.authsubscription+json", "UTF-8"))); addSignature(method, "PUT", "/authsubscriptions/" + easypayAuthId, merchantId, "application/vnd.ch.swisscom.easypay.authsubscription+json", entity.getBytes("UTF-8")); HttpResponse response = httpClient.execute(method); return new EasypayResponse(new String(EntityUtils.toByteArray(response.getEntity())), response.getStatusLine().getStatusCode()); } finally { if (method != null) { method.releaseConnection(); } ; } }