Search in sources :

Example 1 with AsyncHttpClient

use of org.apache.apex.shaded.ning19.com.ning.http.client.AsyncHttpClient in project protools by SeanDragon.

the class ToolHttp method sendHttp.

/**
 * 用于请求http
 *
 * @param send
 *         里面包含请求的信息
 *
 * @return 响应的信息
 */
public static HttpReceive sendHttp(HttpSend send, HttpBuilder httpBuilder) {
    HttpReceive httpReceive = new HttpReceive();
    httpReceive.setHaveError(true);
    String url = send.getUrl();
    URI uri;
    try {
        uri = new URL(url).toURI();
    } catch (MalformedURLException | URISyntaxException e) {
        httpReceive.setErrMsg("不是一个有效的URL").setThrowable(e);
        return httpReceive;
    }
    String scheme = uri.getScheme();
    String host = uri.getHost();
    int port = uri.getPort();
    if (port == -1) {
        if (HttpScheme.HTTP.equalsIgnoreCase(scheme)) {
            port = 80;
        } else if (HttpScheme.HTTPS.equalsIgnoreCase(scheme)) {
            port = 443;
        }
    }
    Map<String, Object> param = send.getParams();
    Map<String, Object> sendHeaders = send.getHeaders();
    HttpMethod method = send.getMethod();
    Charset charset = send.getCharset();
    AsyncHttpClient asyncHttpClient = httpBuilder.buildDefaultClient();
    RequestBuilder builder = new RequestBuilder(method.name());
    AsyncHttpClient.BoundRequestBuilder requestBuilder = asyncHttpClient.prepareRequest(builder.build());
    // 设置编码
    requestBuilder.setBodyEncoding(charset.toString());
    if (param != null) {
        param.forEach((key, value) -> requestBuilder.addQueryParam(key, value.toString()));
    }
    // 设置基本请求头
    requestBuilder.addHeader(HttpHeaderNames.CONTENT_TYPE.toString(), HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED + ";charset" + charset.toString()).addHeader(HttpHeaderNames.CONNECTION.toString(), HttpHeaderValues.KEEP_ALIVE.toString()).addHeader(HttpHeaderNames.ACCEPT_ENCODING.toString(), HttpHeaderValues.GZIP_DEFLATE.toString()).addHeader(HttpHeaderNames.USER_AGENT.toString(), "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)").addHeader(HttpHeaderNames.CACHE_CONTROL.toString(), "max-age=0").addHeader(HttpHeaderNames.HOST.toString(), host + ":" + port).addHeader("DNT", "1");
    if (sendHeaders != null) {
        sendHeaders.forEach((key, value) -> requestBuilder.addHeader(key, value.toString()));
    }
    // TODO: 2017/7/27 Cookie未加入
    ListenableFuture<Response> future = requestBuilder.execute();
    try {
        Response response = future.get();
        Map<String, String> responseHeaderMap = Maps.newHashMap();
        if (send.getNeedReceiveHeaders()) {
            FluentCaseInsensitiveStringsMap responseHeaders = response.getHeaders();
            responseHeaders.forEach((k, v) -> {
                if (v.size() == 1) {
                    responseHeaderMap.put(k, v.get(0));
                } else {
                    responseHeaderMap.put(k, ToolJson.anyToJson(v));
                }
            });
        }
        int responseStatusCode = response.getStatusCode();
        if (responseStatusCode != 200) {
            throw new HttpException("本次请求响应码不是200,是" + responseStatusCode);
        }
        String responseBody = response.getResponseBody();
        if (log.isDebugEnabled()) {
            log.debug(responseBody);
        }
        httpReceive.setStatusCode(responseStatusCode).setStatusText(response.getStatusText()).setResponseBody(responseBody).setResponseHeader(responseHeaderMap).setHaveError(false);
    } catch (InterruptedException e) {
        httpReceive.setErrMsg("http组件出现问题!").setThrowable(e);
    } catch (IOException e) {
        httpReceive.setErrMsg("获取返回内容失败!").setThrowable(e);
    } catch (ExecutionException e) {
        httpReceive.setErrMsg("访问URL失败!").setThrowable(e);
    } catch (HttpException e) {
        httpReceive.setErrMsg(e.getMessage()).setThrowable(e);
    }
    if (httpReceive.getHaveError()) {
        if (log.isWarnEnabled()) {
            Throwable throwable = httpReceive.getThrowable();
            log.warn(ToolFormat.toException(throwable), throwable);
        }
    }
    httpReceive.setIsDone(true);
    return httpReceive;
}
Also used : HttpReceive(pro.tools.http.pojo.HttpReceive) MalformedURLException(java.net.MalformedURLException) RequestBuilder(com.ning.http.client.RequestBuilder) Charset(java.nio.charset.Charset) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) URL(java.net.URL) Response(com.ning.http.client.Response) FluentCaseInsensitiveStringsMap(com.ning.http.client.FluentCaseInsensitiveStringsMap) HttpException(pro.tools.http.pojo.HttpException) ExecutionException(java.util.concurrent.ExecutionException) HttpMethod(pro.tools.http.pojo.HttpMethod) AsyncHttpClient(com.ning.http.client.AsyncHttpClient)

Example 2 with AsyncHttpClient

use of org.apache.apex.shaded.ning19.com.ning.http.client.AsyncHttpClient in project tez by apache.

the class AsyncHttpConnection method initClient.

private void initClient(HttpConnectionParams httpConnParams) throws IOException {
    if (httpAsyncClient != null) {
        return;
    }
    if (httpAsyncClient == null) {
        synchronized (AsyncHttpConnection.class) {
            if (httpAsyncClient == null) {
                LOG.info("Initializing AsyncClient (TezBodyDeferringAsyncHandler)");
                AsyncHttpClientConfig.Builder builder = new AsyncHttpClientConfig.Builder();
                if (httpConnParams.isSslShuffle()) {
                    // Configure SSL
                    SSLFactory sslFactory = httpConnParams.getSslFactory();
                    Preconditions.checkArgument(sslFactory != null, "SSLFactory can not be null");
                    sslFactory.configure(builder);
                }
                /**
                 * TODO : following settings need fine tuning.
                 * Change following config to accept common thread pool later.
                 * Change max connections based on the total inputs (ordered & unordered). Need to tune
                 * setMaxConnections & addRequestFilter.
                 */
                builder.setAllowPoolingConnection(httpConnParams.isKeepAlive()).setAllowSslConnectionPool(httpConnParams.isKeepAlive()).setCompressionEnabled(false).setMaximumConnectionsPerHost(1).setConnectionTimeoutInMs(httpConnParams.getConnectionTimeout()).setRequestTimeoutInMs(httpConnParams.getReadTimeout()).setUseRawUrl(true).build();
                httpAsyncClient = new AsyncHttpClient(builder.build());
            }
        }
    }
}
Also used : SSLFactory(org.apache.tez.http.SSLFactory) RequestBuilder(com.ning.http.client.RequestBuilder) AsyncHttpClientConfig(com.ning.http.client.AsyncHttpClientConfig) AsyncHttpClient(com.ning.http.client.AsyncHttpClient)

Example 3 with AsyncHttpClient

use of org.apache.apex.shaded.ning19.com.ning.http.client.AsyncHttpClient in project scheduling by ow2-proactive.

the class SchedulerEventReceiver method openAndReceive.

@SuppressWarnings("rawtypes")
private void openAndReceive(final SchedulerEventListener eventListener, boolean myEventsOnly, SchedulerEvent... events) throws IOException {
    Client client = ClientFactory.getDefault().newClient();
    AsyncHttpClientConfig.Builder builder = new AsyncHttpClientConfig.Builder();
    if (insecure) {
        builder = builder.setAcceptAnyCertificate(true).setHostnameVerifier(SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    }
    AsyncHttpClientConfig httpClientConfig = builder.build();
    AsyncHttpClient httpClient = new AsyncHttpClient(httpClientConfig);
    socket = client.create(client.newOptionsBuilder().runtime(httpClient).reconnect(false).build());
    EventNotificationHandler notificationHandler = new EventNotificationHandler(eventListener);
    socket.on(Event.MESSAGE, notificationHandler);
    socket.on(Event.CLOSE, new Function() {

        public void on(Object t) {
            SchedulerEventReceiver.logger.info("#### Websocket connection is closed ####");
            if (eventListener instanceof DisconnectionAwareSchedulerEventListener) {
                ((DisconnectionAwareSchedulerEventListener) eventListener).notifyDisconnection();
            }
        }
    });
    // initialize the connection
    RequestBuilder requestBuilder = client.newRequestBuilder();
    requestBuilder.method(Request.METHOD.GET);
    requestBuilder.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
    // authentication header
    requestBuilder.header("sessionid", sessionId);
    requestBuilder.uri(eventingUrl(restServerUrl));
    requestBuilder.encoder(new EventSubscriptionEncoder());
    requestBuilder.decoder(new EventNotificationDecoder());
    requestBuilder.transport(Request.TRANSPORT.WEBSOCKET);
    socket.open(requestBuilder.build());
    // submit subscription request
    EventSubscription eventSubscription = new EventSubscription(myEventsOnly, asStringArray(events));
    socket.fire(EventCodecUtil.toJsonString(eventSubscription));
}
Also used : RequestBuilder(org.atmosphere.wasync.RequestBuilder) RequestBuilder(org.atmosphere.wasync.RequestBuilder) Function(org.atmosphere.wasync.Function) EventSubscription(org.ow2.proactive_grid_cloud_portal.scheduler.dto.eventing.EventSubscription) AsyncHttpClientConfig(com.ning.http.client.AsyncHttpClientConfig) Client(org.atmosphere.wasync.Client) AsyncHttpClient(com.ning.http.client.AsyncHttpClient) AsyncHttpClient(com.ning.http.client.AsyncHttpClient)

Example 4 with AsyncHttpClient

use of org.apache.apex.shaded.ning19.com.ning.http.client.AsyncHttpClient in project jersey by jersey.

the class UnderlyingHttpClientAccessTest method testHttpClientInstanceAccess.

/**
     * Verifier of JERSEY-2424 fix.
     */
@Test
public void testHttpClientInstanceAccess() {
    final Client client = ClientBuilder.newClient(new ClientConfig().connectorProvider(new GrizzlyConnectorProvider()));
    final AsyncHttpClient hcOnClient = GrizzlyConnectorProvider.getHttpClient(client);
    // important: the web target instance in this test must be only created AFTER the client has been pre-initialized
    // (see org.glassfish.jersey.client.Initializable.preInitialize method). This is here achieved by calling the
    // connector provider's static getHttpClient method above.
    final WebTarget target = client.target("http://localhost/");
    final AsyncHttpClient hcOnTarget = GrizzlyConnectorProvider.getHttpClient(target);
    assertNotNull("HTTP client instance set on JerseyClient should not be null.", hcOnClient);
    assertNotNull("HTTP client instance set on JerseyWebTarget should not be null.", hcOnTarget);
    assertSame("HTTP client instance set on JerseyClient should be the same instance as the one set on JerseyWebTarget" + "(provided the target instance has not been further configured).", hcOnClient, hcOnTarget);
}
Also used : WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) AsyncHttpClient(com.ning.http.client.AsyncHttpClient) ClientConfig(org.glassfish.jersey.client.ClientConfig) AsyncHttpClient(com.ning.http.client.AsyncHttpClient) Test(org.junit.Test)

Example 5 with AsyncHttpClient

use of org.apache.apex.shaded.ning19.com.ning.http.client.AsyncHttpClient in project cdap by caskdata.

the class NettyRouterPipelineTest method testChunkRequestSuccess.

@Test
public void testChunkRequestSuccess() throws Exception {
    AsyncHttpClientConfig.Builder configBuilder = new AsyncHttpClientConfig.Builder();
    final AsyncHttpClient asyncHttpClient = new AsyncHttpClient(new NettyAsyncHttpProvider(configBuilder.build()), configBuilder.build());
    byte[] requestBody = generatePostData();
    final Request request = new RequestBuilder("POST").setUrl(String.format("http://%s:%d%s", HOSTNAME, ROUTER.getServiceMap().get(GATEWAY_NAME), "/v1/upload")).setContentLength(requestBody.length).setBody(new ByteEntityWriter(requestBody)).build();
    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    Future<Void> future = asyncHttpClient.executeRequest(request, new AsyncCompletionHandler<Void>() {

        @Override
        public Void onCompleted(Response response) throws Exception {
            return null;
        }

        @Override
        public STATE onBodyPartReceived(HttpResponseBodyPart content) throws Exception {
            // TimeUnit.MILLISECONDS.sleep(RANDOM.nextInt(10));
            content.writeTo(byteArrayOutputStream);
            return super.onBodyPartReceived(content);
        }
    });
    future.get();
    Assert.assertArrayEquals(requestBody, byteArrayOutputStream.toByteArray());
}
Also used : RequestBuilder(com.ning.http.client.RequestBuilder) RequestBuilder(com.ning.http.client.RequestBuilder) Request(com.ning.http.client.Request) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) NettyAsyncHttpProvider(com.ning.http.client.providers.netty.NettyAsyncHttpProvider) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) HttpResponse(io.netty.handler.codec.http.HttpResponse) Response(com.ning.http.client.Response) AsyncHttpClientConfig(com.ning.http.client.AsyncHttpClientConfig) HttpResponseBodyPart(com.ning.http.client.HttpResponseBodyPart) AsyncHttpClient(com.ning.http.client.AsyncHttpClient) Test(org.junit.Test)

Aggregations

AsyncHttpClient (com.ning.http.client.AsyncHttpClient)12 AsyncHttpClientConfig (com.ning.http.client.AsyncHttpClientConfig)6 RequestBuilder (com.ning.http.client.RequestBuilder)5 Response (com.ning.http.client.Response)5 IOException (java.io.IOException)5 Test (org.junit.Test)5 Request (com.ning.http.client.Request)3 NettyAsyncHttpProvider (com.ning.http.client.providers.netty.NettyAsyncHttpProvider)3 HttpRequest (io.netty.handler.codec.http.HttpRequest)3 URISyntaxException (java.net.URISyntaxException)3 DefaultChannelFactory (com.meltwater.rxrabbit.impl.DefaultChannelFactory)2 HttpResponseBodyPart (com.ning.http.client.HttpResponseBodyPart)2 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ExecutionException (java.util.concurrent.ExecutionException)2 ThreadFactory (java.util.concurrent.ThreadFactory)2 AsyncHttpClient (org.apache.apex.shaded.ning19.com.ning.http.client.AsyncHttpClient)2 AsyncHttpClientConfigBean (org.apache.apex.shaded.ning19.com.ning.http.client.AsyncHttpClientConfigBean)2 WebSocket (org.apache.apex.shaded.ning19.com.ning.http.client.ws.WebSocket)2 WebSocketTextListener (org.apache.apex.shaded.ning19.com.ning.http.client.ws.WebSocketTextListener)2