Example usage for org.apache.http.nio.client.methods HttpAsyncMethods createPost

List of usage examples for org.apache.http.nio.client.methods HttpAsyncMethods createPost

Introduction

In this page you can find the example usage for org.apache.http.nio.client.methods HttpAsyncMethods createPost.

Prototype

public static HttpAsyncRequestProducer createPost(final String requestURI, final byte[] content,
        final ContentType contentType) 

Source Link

Document

Creates asynchronous POST request generator.

Usage

From source file:org.opendaylight.fpc.notification.HTTPNotifier.java

@Override
public void run() {
    this.run = true;
    LOG.info("HTTPNotifier RUN started");
    try {// w  w w . j  a va2 s. c  o  m
        while (run) {
            SimpleEntry<Uri, Notification> obj = (AbstractMap.SimpleEntry<Uri, Notification>) blockingNotificationQueue
                    .take();
            String postBody = null;
            try {
                if (obj.getValue() instanceof ConfigResultNotification) {
                    postBody = fpcCodecUtils.notificationToJsonString(ConfigResultNotification.class,
                            (DataObject) obj.getValue(), true);
                } else if (obj.getValue() instanceof Notify) {
                    postBody = fpcCodecUtils.notificationToJsonString(Notify.class, (DataObject) obj.getValue(),
                            true);
                }
            } catch (Exception e) {
                ErrorLog.logError(e.getStackTrace());
            }
            if (obj.getKey() != null && postBody != null) {
                String url = obj.getKey().getValue();
                try {
                    client.start();
                    HttpRequest post = HttpAsyncMethods.createPost(url, postBody, ContentType.APPLICATION_JSON)
                            .generateRequest();
                    post.setHeader("User-Agent", "ODL Notification Agent");
                    post.setHeader("charset", "utf-8");
                    client.execute((HttpUriRequest) post, new FutureCallback<HttpResponse>() {
                        @Override
                        public void cancelled() {
                            LOG.error(post.getRequestLine() + "-> Cancelled");
                        }

                        @Override
                        public void completed(HttpResponse resp) {
                            try {
                                if (obj.getValue() instanceof Notify) {
                                    if (((Notify) obj.getValue())
                                            .getValue() instanceof DownlinkDataNotification) {
                                        String body = handler.handleResponse(resp);
                                        JSONObject json_body = new JSONObject(body);
                                        api.ddnAck(json_body);
                                        LOG.info("Response Body: " + body);
                                    }
                                }
                            } catch (IOException e) {
                                ErrorLog.logError(e.getStackTrace());
                            }
                        }

                        @Override
                        public void failed(Exception e) {
                            ErrorLog.logError(post.getRequestLine() + "->" + e.getMessage(), e.getStackTrace());
                        }
                    });
                } catch (UnsupportedEncodingException e) {
                    ErrorLog.logError(e.getStackTrace());
                } catch (IOException e) {
                    ErrorLog.logError(e.getStackTrace());
                } catch (HttpException e) {
                    ErrorLog.logError(e.getStackTrace());
                } catch (Exception e) {
                    ErrorLog.logError(e.getStackTrace());
                }
            }
        }
    } catch (InterruptedException e) {
        ErrorLog.logError(e.getStackTrace());
    } catch (Exception e) {
        ErrorLog.logError(e.getStackTrace());
    }
}

From source file:org.opennms.newts.gsod.ImportRunner.java

public static Func1<String, Observable<ObservableHttpResponse>> postJSON(final String baseURL,
        final CloseableHttpAsyncClient httpClient) {

    final URI baseURI = URI.create(baseURL);

    return new Func1<String, Observable<ObservableHttpResponse>>() {
        @Override//from w  w  w.ja  v  a  2s  .c o m
        public Observable<ObservableHttpResponse> call(String json) {
            try {
                return ObservableHttp
                        .createRequest(HttpAsyncMethods.createPost(baseURI, json, ContentType.APPLICATION_JSON),
                                httpClient)
                        .toObservable();
            } catch (UnsupportedEncodingException e) {
                throw Exceptions.propagate(e);
            }
        }
    };
}