List of usage examples for org.apache.http.nio.entity NStringEntity NStringEntity
public NStringEntity(final String s) throws UnsupportedEncodingException
From source file:org.jclouds.http.httpnio.util.NioHttpUtils.java
public static void addEntityForContent(BasicHttpEntityEnclosingRequest apacheRequest, Object content, String contentType, long length) { if (content instanceof InputStream) { InputStream inputStream = (InputStream) content; if (length == -1) throw new IllegalArgumentException("you must specify size when content is an InputStream"); InputStreamEntity entity = new InputStreamEntity(inputStream, length); entity.setContentType(contentType); apacheRequest.setEntity(entity); } else if (content instanceof String) { NStringEntity nStringEntity = null; try {/*from w w w.ja va 2s . co m*/ nStringEntity = new NStringEntity((String) content); } catch (UnsupportedEncodingException e) { throw new UnsupportedOperationException("Encoding not supported", e); } nStringEntity.setContentType(contentType); apacheRequest.setEntity(nStringEntity); } else if (content instanceof File) { apacheRequest.setEntity(new NFileEntity((File) content, contentType, true)); } else if (content instanceof byte[]) { NByteArrayEntity entity = new NByteArrayEntity((byte[]) content); entity.setContentType(contentType); apacheRequest.setEntity(entity); } else { throw new UnsupportedOperationException("Content class not supported: " + content.getClass().getName()); } }
From source file:org.jhk.pulsing.search.elasticsearch.client.ESRestClient.java
public Optional<String> putDocument(String index, String type, long id, Map<String, String> jsonObject) { String endpoint = new StringJoiner("/").add(index).add(type).add(id + "").toString(); _LOGGER.debug("ESRestClient.putDocument: " + endpoint + " - " + jsonObject); try {/*from w w w . java2 s. c o m*/ Optional<Response> response = performRequest("PUT", endpoint, Collections.EMPTY_MAP, new NStringEntity(_objectMapper.writeValueAsString(jsonObject)), EMPTY_HEADER); if (response.isPresent()) { HttpEntity hEntity = response.get().getEntity(); String result = EntityUtils.toString(hEntity); _LOGGER.debug("ESRestClient.putDocument: result - " + result); return Optional.of(result); } } catch (IOException iException) { iException.printStackTrace(); } return Optional.empty(); }
From source file:org.frameworkset.spi.remote.http.HttpBaseRPCIOHandler.java
public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException { // InputStream instream = request.getInputStream(); // ByteArrayOutputStream // buffer = new ByteArrayOutputStream(); // try { // byte[] tmp = new byte[4096]; // int l; // while((l = instream.read(tmp)) != -1) { // buffer.write(tmp, 0, l); // } // } finally { // instream.close(); // } String method = request.getRequestLine().getMethod().toUpperCase(Locale.ENGLISH); if (!method.equals("GET") && !method.equals("HEAD") && !method.equals("POST")) { throw new MethodNotSupportedException(method + " method not supported"); }// w w w .jav a 2s . c o m try { HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity(); InputStream instream = entity.getContent(); // XStream stream = new XStream(); RPCMessage message = (RPCMessage) ObjectSerializable.toBean(instream, RPCMessage.class); RPCMessage ret = super.messageReceived(message); String ret_str = ObjectSerializable.toXML(ret); response.addHeader("Content-Type", "text/xml;charset=UTF-8"); response.setEntity(new NStringEntity(ret_str)); } catch (Exception e) { throw new HttpException(e.getMessage()); } }
From source file:com.comcast.cns.io.HTTPEndpointAsyncPublisher.java
@Override public void send() throws Exception { HttpAsyncRequester requester = new HttpAsyncRequester(httpProcessor, new DefaultConnectionReuseStrategy(), httpParams);//w w w.ja v a 2s . com final URL url = new URL(endpoint); final HttpHost target = new HttpHost(url.getHost(), url.getPort(), url.getProtocol()); BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST", url.getPath() + (url.getQuery() == null ? "" : "?" + url.getQuery())); composeHeader(request); String msg = null; if (message.getMessageStructure() == CNSMessageStructure.json) { msg = message.getProtocolSpecificMessage(CnsSubscriptionProtocol.http); } else { msg = message.getMessage(); } if (!rawMessageDelivery && message.getMessageType() == CNSMessageType.Notification) { msg = com.comcast.cns.util.Util.generateMessageJson(message, CnsSubscriptionProtocol.http); } logger.debug("event=send_async_http_request endpoint=" + endpoint + "\" message=\"" + msg + "\""); request.setEntity(new NStringEntity(msg)); requester.execute(new BasicAsyncRequestProducer(target, request), new BasicAsyncResponseConsumer(), connectionPool, new BasicHttpContext(), new FutureCallback<HttpResponse>() { public void completed(final HttpResponse response) { int statusCode = response.getStatusLine().getStatusCode(); // accept all 2xx status codes if (statusCode >= 200 && statusCode < 300) { callback.onSuccess(); } else { logger.warn(target + "://" + url.getPath() + "?" + url.getQuery() + " -> " + response.getStatusLine()); callback.onFailure(statusCode); } } public void failed(final Exception ex) { logger.warn(target + " " + url.getPath() + " " + url.getQuery(), ex); callback.onFailure(0); } public void cancelled() { logger.warn(target + " " + url.getPath() + " " + url.getQuery() + " -> " + "cancelled"); callback.onFailure(1); } }); }