Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.englishtown.vertx.promises.impl;
  
 
 import java.net.URI;

 
 public class DefaultWhenHttpClient implements WhenHttpClient {
 
     public static int CONNECT_TIMEOUT = 10000;
 
     private final Vertx vertx;
     private final When<HttpClientResponseVoidwhen = new When<>();
 
     @Inject
     public DefaultWhenHttpClient(Vertx vertx) {
         this. = vertx;
     }

    
Send a Vertx http client request and returns a promise

Parameters:
method the http method (GET, PUT, POST etc.)
url the java.net.URI to send the request to
Returns:
a promise for the HttpClientResponse
 
     @Override
     public Promise<HttpClientResponseVoidrequest(String methodURI url) {
         return request(methodurlnull);
     }

    
Send a Vertx http client request and returns a promise

Parameters:
method the http method (GET, PUT, POST etc.)
url the java.net.URI to send the request to
setupHandler optional setup handler. If provided it must call end() on the org.vertx.java.core.http.HttpClientRequest
Returns:
a promise for the HttpClientResponse
 
     @Override
     public Promise<HttpClientResponseVoidrequest(String methodURI urlHandler<HttpClientRequestsetupHandler) {
 
         final Deferred<HttpClientResponseVoidd = .defer();
 
         try {
             HttpClient client = createClient(urld);
             return request(dmethodgetPath(url), clientsetupHandler);
 
         } catch (Throwable t) {
             reject(dt);
             return d.getPromise();
         }
 
     }

    
Send a Vertx http client request and returns a promise

Parameters:
method the http method (GET, PUT, POST etc.)
url the url to send the request to
client the vertx http client to use
Returns:
a promise for the HttpClientResponse
 
     @Override
     public Promise<HttpClientResponseVoidrequest(String methodString urlHttpClient client) {
         return request(methodurlclientnull);
     }

    
Send a Vertx http client request and returns a promise

Parameters:
method the http method (GET, PUT, POST etc.)
url the url to send the request to
client the vertx http client to use
setupHandler optional setup handler. If provided it must call end() on the org.vertx.java.core.http.HttpClientRequest
Returns:
a promise for the HttpClientResponse
 
     @Override
     public Promise<HttpClientResponseVoidrequest(String methodString urlHttpClient clientHandler<HttpClientRequestsetupHandler) {
         final Deferred<HttpClientResponseVoidd = .defer();
         return request(dmethodurlclientsetupHandler);
     }
 
     protected Promise<HttpClientResponseVoidrequest(
             final Deferred<HttpClientResponseVoidd,
             String method,
             String url,
            HttpClient client,
            Handler<HttpClientRequestsetupHandler) {
        try {
            HttpClientRequest request = client.request(methodurlnew Handler<HttpClientResponse>() {
                @Override
                public void handle(HttpClientResponse response) {
                    if (response.statusCode() == 200) {
                        d.getResolver().resolve(response);
                    } else {
                        d.getResolver().reject(response);
                    }
                }
            });
            request.exceptionHandler(new Handler<Throwable>() {
                @Override
                public void handle(Throwable t) {
                    reject(dt);
                }
            });
            if (setupHandler != null) {
                setupHandler.handle(request);
            } else {
                request.end();
            }
        } catch (Throwable t) {
            reject(dt);
        }
        return d.getPromise();
    }
    protected HttpClient createClient(URI urlfinal Deferred<HttpClientResponseVoidd) {
        if (url == nullthrow new IllegalArgumentException("url is null");
        if (!url.isAbsolute()) throw new IllegalArgumentException("url must be absolute");
        int port = (url.getPort() > 0) ? url.getPort() : 80;
        return .createHttpClient()
                .setHost(url.getHost())
                .setPort(port)
                .setConnectTimeout()
                .exceptionHandler(new Handler<Throwable>() {
                    @Override
                    public void handle(Throwable t) {
                        reject(dt);
                    }
                });
    }
    protected String getPath(URI url) {
        final StringBuilder path = new StringBuilder();
        path.append(url.getPath());
        if (url.getQuery() != null && url.getQuery().length() > 0) {
            path.append("?");
            path.append(url.getQuery());
        }
        return path.toString();
    }
    protected void reject(Deferred<HttpClientResponseVoiddThrowable t) {
        RuntimeException e = (t instanceof RuntimeException ? (RuntimeExceptiont : new RuntimeException(t));
        d.getResolver().reject(new Value<HttpClientResponse>(nulle));
    }
New to GrepCode? Check out our FAQ X