brooklyn-dev mailing list archives

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

    https://github.com/apache/brooklyn-server/pull/285#discussion_r76594668
  
    --- Diff: utils/common/src/main/java/org/apache/brooklyn/util/http/executor/apacheclient/HttpExecutorImpl.java
---
    @@ -0,0 +1,106 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied.  See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + */
    +package org.apache.brooklyn.util.http.executor.apacheclient;
    +
    +import java.io.IOException;
    +
    +import org.apache.brooklyn.util.http.HttpTool;
    +import org.apache.brooklyn.util.http.HttpTool.HttpClientBuilder;
    +import org.apache.brooklyn.util.http.HttpToolResponse;
    +import org.apache.brooklyn.util.http.executor.HttpConfig;
    +import org.apache.brooklyn.util.http.executor.HttpExecutor;
    +import org.apache.brooklyn.util.http.executor.HttpRequest;
    +import org.apache.brooklyn.util.http.executor.HttpResponse;
    +import org.apache.http.auth.Credentials;
    +import org.apache.http.auth.UsernamePasswordCredentials;
    +import org.apache.http.client.HttpClient;
    +
    +import com.google.common.annotations.Beta;
    +import com.google.common.base.Optional;
    +
    +@Beta
    +public class HttpExecutorImpl implements HttpExecutor {
    +
    +    private static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
    +    
    +    private static final HttpConfig DEFAULT_CONFIG = HttpConfig.builder()
    +            .laxRedirect(false)
    +            .trustAll(false)
    +            .trustSelfSigned(false)
    +            .build();
    +
    +    public static HttpExecutorImpl newInstance() {
    +        return newInstance(HttpTool.httpClientBuilder());
    +    }
    +    
    +    /**
    +     * This {@code baseBuilder} is used to construct a new {@link HttpClient} for each
call to {@link #execute(HttpRequest)}.
    +     * Callers are strongly discouraged from modifying the baseBuilder after passing
it in!
    +     */
    +    public static HttpExecutorImpl newInstance(HttpTool.HttpClientBuilder baseBuilder)
{
    +        return new HttpExecutorImpl(baseBuilder);
    +    }
    +
    +    private final HttpClientBuilder baseBuilder;
    +    
    +    protected HttpExecutorImpl(HttpClientBuilder baseBuilder) {
    +        this.baseBuilder = baseBuilder;
    +    }
    +
    +    @Override
    +    public HttpResponse execute(HttpRequest request) throws IOException {
    +        HttpConfig config = (request.config() != null) ? request.config() : DEFAULT_CONFIG;
    +        Credentials creds = (request.credentials() != null) ? new UsernamePasswordCredentials(request.credentials().username(),
request.credentials().password()) : null;
    +        HttpClient httpClient = HttpTool.HttpClientBuilder.fromBuilder(baseBuilder)
    +                .uri(request.uri())
    +                .https(request.isHttps())
    +                .credential(Optional.fromNullable(creds))
    +                .laxRedirect(config.laxRedirect())
    +                .trustSelfSigned(config.trustSelfSigned())
    +                .trustAll(config.trustAll())
    +                .build();
    --- End diff --
    
    The baseBuilder provides configuration for several more parameters which are implementation
dependent (e.g. apache client)


---
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