brooklyn-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From neykov <...@git.apache.org>
Subject [GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Date Mon, 05 Sep 2016 10:17:19 GMT
Github user neykov commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/285#discussion_r77500324
  
    --- Diff: core/src/main/java/org/apache/brooklyn/feed/http/HttpFeed.java ---
    @@ -331,52 +366,62 @@ protected void preStart() {
                 }
     
                 Callable<HttpToolResponse> pollJob;
    -            
    -            if (pollInfo.method.equals("get")) {
    -                pollJob = new Callable<HttpToolResponse>() {
    -                    public HttpToolResponse call() throws Exception {
    -                        if (log.isTraceEnabled()) log.trace("http polling for {} sensors
at {}", entity, pollInfo);
    -                        return HttpTool.httpGet(httpClient, pollInfo.uriProvider.get(),
pollInfo.headers);
    -                    }};
    -            } else if (pollInfo.method.equals("post")) {
    -                pollJob = new Callable<HttpToolResponse>() {
    -                    public HttpToolResponse call() throws Exception {
    -                        if (log.isTraceEnabled()) log.trace("http polling for {} sensors
at {}", entity, pollInfo);
    -                        return HttpTool.httpPost(httpClient, pollInfo.uriProvider.get(),
pollInfo.headers, pollInfo.body);
    -                    }};
    -            } else if (pollInfo.method.equals("head")) {
    -                pollJob = new Callable<HttpToolResponse>() {
    -                    public HttpToolResponse call() throws Exception {
    -                        if (log.isTraceEnabled()) log.trace("http polling for {} sensors
at {}", entity, pollInfo);
    -                        return HttpTool.httpHead(httpClient, pollInfo.uriProvider.get(),
pollInfo.headers);
    -                    }};
    -            } else {
    -                throw new IllegalStateException("Unexpected http method: "+pollInfo.method);
    -            }
    -            
    -            getPoller().scheduleAtFixedRate(pollJob, new DelegatingPollHandler<HttpToolResponse>(handlers),
minPeriod);
    -        }
    -    }
    +            pollJob = new Callable<HttpToolResponse>() {
    +                public HttpToolResponse call() throws Exception {
    +                    if (log.isTraceEnabled()) log.trace("http polling for {} sensors
at {}", entity, pollInfo);
     
    -    // TODO Should we really trustAll for https? Make configurable?
    -    private HttpClient createHttpClient(HttpPollIdentifier pollIdentifier) {
    -        URI uri = pollIdentifier.uriProvider.get();
    -        HttpClientBuilder builder = HttpTool.httpClientBuilder()
    -                .trustAll()
    -                .laxRedirect(true);
    -        if (uri != null) builder.uri(uri);
    -        if (uri != null) builder.credential(pollIdentifier.credentials);
    -        if (pollIdentifier.connectionTimeout != null) {
    -            builder.connectionTimeout(pollIdentifier.connectionTimeout);
    -        }
    -        if (pollIdentifier.socketTimeout != null) {
    -            builder.socketTimeout(pollIdentifier.socketTimeout);
    +                    BasicAuth creds = null;
    +                    if (pollInfo.credentials.isPresent()) {
    +                        creds =  org.apache.brooklyn.util.http.executor.Credentials.basic(
    +                                pollInfo.credentials.get().getUserPrincipal().getName(),
    +                                pollInfo.credentials.get().getPassword());
    +                    }
    +
    +                    HttpResponse response =  pollInfo.httpExecutor.execute(new HttpRequest.Builder()
    +                            .headers(pollInfo.headers)
    +                            .uri(pollInfo.uriProvider.get())
    +                            .credentials(creds)
    +                            .method(pollInfo.method)
    +                            .body(pollInfo.body)
    +                            .config(HttpConfig.builder()
    +                                    .trustSelfSigned(true)
    +                                    .trustAll(true)
    +                                    .laxRedirect(true)
    +                                    .build())
    +                            .build());
    +                    return createHttpToolRespose(response);
    +                }};
    +                getPoller().scheduleAtFixedRate(pollJob, new DelegatingPollHandler(handlers),
minPeriod);
             }
    -        return builder.build();
         }
     
         @SuppressWarnings("unchecked")
         protected Poller<HttpToolResponse> getPoller() {
    -        return (Poller<HttpToolResponse>) super.getPoller();
    +        return  (Poller<HttpToolResponse>) super.getPoller();
    +    }
    +
    +    @SuppressWarnings("unchecked")
    +    private HttpToolResponse createHttpToolRespose(HttpResponse response) throws IOException
{
    +        int responseCode = response.code();
    +
    +        /* From https://tools.ietf.org/html/rfc4918#section-11.2
    +        The 422 (Unprocessable Entity) status code means the server
    +        understands the content type of the request entity (hence a
    +        415(Unsupported Media Type) status code is inappropriate), and the
    +        syntax of the request entity is correct (thus a 400 (Bad Request)
    +        status code is inappropriate) but was unable to process the contained
    +        instructions. */
    +        if (responseCode == 422) {
    +            throw new IOException(" Unprocessable Entity: " + response.reasonPhrase());
    +        }
    --- End diff --
    
    Still don't see why we need special handling of 422? (to discuss).


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message