Search in sources :

Example 1 with Builder

use of io.airlift.http.client.Request.Builder in project airlift by airlift.

the class ServiceInventory method updateServiceInventory.

@Managed
public final void updateServiceInventory() {
    if (serviceInventoryUri == null) {
        return;
    }
    try {
        ServiceDescriptorsRepresentation serviceDescriptorsRepresentation;
        if (serviceInventoryUri.getScheme().toLowerCase().startsWith("http")) {
            Builder requestBuilder = prepareGet().setUri(serviceInventoryUri).setHeader("User-Agent", nodeInfo.getNodeId());
            serviceDescriptorsRepresentation = httpClient.execute(requestBuilder.build(), createJsonResponseHandler(serviceDescriptorsCodec));
        } else {
            File file = new File(serviceInventoryUri);
            serviceDescriptorsRepresentation = serviceDescriptorsCodec.fromJson(readAllBytes(file.toPath()));
        }
        if (!environment.equals(serviceDescriptorsRepresentation.getEnvironment())) {
            logServerError("Expected environment to be %s, but was %s", environment, serviceDescriptorsRepresentation.getEnvironment());
        }
        List<ServiceDescriptor> descriptors = new ArrayList<>(serviceDescriptorsRepresentation.getServiceDescriptors());
        Collections.shuffle(descriptors);
        serviceDescriptors.set(ImmutableList.copyOf(descriptors));
        if (serverUp.compareAndSet(false, true)) {
            log.info("ServiceInventory connect succeeded");
        }
    } catch (Exception e) {
        logServerError("Error loading service inventory from %s", serviceInventoryUri.toASCIIString());
    }
}
Also used : Builder(io.airlift.http.client.Request.Builder) ArrayList(java.util.ArrayList) File(java.io.File) Managed(org.weakref.jmx.Managed)

Example 2 with Builder

use of io.airlift.http.client.Request.Builder in project airlift by airlift.

the class HttpDiscoveryLookupClient method lookup.

private ListenableFuture<ServiceDescriptors> lookup(final String type, final String pool, final ServiceDescriptors serviceDescriptors) {
    requireNonNull(type, "type is null");
    URI uri = discoveryServiceURI.get();
    if (uri == null) {
        return immediateFailedFuture(new DiscoveryException("No discovery servers are available"));
    }
    Builder requestBuilder = prepareGet().setUri(createServiceLocation(uri, type, Optional.ofNullable(pool))).setHeader("User-Agent", nodeInfo.getNodeId());
    if (serviceDescriptors != null && serviceDescriptors.getETag() != null) {
        requestBuilder.setHeader(HttpHeaders.ETAG, serviceDescriptors.getETag());
    }
    return httpClient.executeAsync(requestBuilder.build(), new DiscoveryResponseHandler<ServiceDescriptors>(format("Lookup of %s", type), uri) {

        @Override
        public ServiceDescriptors handle(Request request, Response response) {
            Duration maxAge = extractMaxAge(response);
            String eTag = response.getHeader(HttpHeaders.ETAG);
            if (NOT_MODIFIED.code() == response.getStatusCode() && serviceDescriptors != null) {
                return new ServiceDescriptors(serviceDescriptors, maxAge, eTag);
            }
            if (OK.code() != response.getStatusCode()) {
                throw new DiscoveryException(format("Lookup of %s failed with status code %s", type, response.getStatusCode()));
            }
            byte[] json;
            try {
                json = ByteStreams.toByteArray(response.getInputStream());
            } catch (IOException e) {
                throw new DiscoveryException(format("Lookup of %s failed", type), e);
            }
            ServiceDescriptorsRepresentation serviceDescriptorsRepresentation = serviceDescriptorsCodec.fromJson(json);
            if (!environment.equals(serviceDescriptorsRepresentation.getEnvironment())) {
                throw new DiscoveryException(format("Expected environment to be %s, but was %s", environment, serviceDescriptorsRepresentation.getEnvironment()));
            }
            return new ServiceDescriptors(type, pool, serviceDescriptorsRepresentation.getServiceDescriptors(), maxAge, eTag);
        }
    });
}
Also used : Response(io.airlift.http.client.Response) HttpUriBuilder(io.airlift.http.client.HttpUriBuilder) Builder(io.airlift.http.client.Request.Builder) Request(io.airlift.http.client.Request) Duration(io.airlift.units.Duration) IOException(java.io.IOException) URI(java.net.URI)

Example 3 with Builder

use of io.airlift.http.client.Request.Builder in project airlift by airlift.

the class TestHttpServerProvider method assertForward.

private void assertForward(ForwardedServlet servlet, Optional<String> proto, Optional<String> host, Optional<String> remoteHost) {
    servlet.reset();
    HttpUriBuilder uriBuilder = HttpUriBuilder.uriBuilderFrom(httpServerInfo.getHttpUri()).replacePath("/some/path");
    try (HttpClient client = new JettyHttpClient()) {
        Builder builder = prepareGet().setUri(uriBuilder.build());
        proto.ifPresent(value -> builder.addHeader(X_FORWARDED_PROTO, value));
        host.ifPresent(value -> builder.addHeader(X_FORWARDED_HOST, value));
        remoteHost.ifPresent(value -> builder.addHeader(X_FORWARDED_FOR, value));
        StringResponse response = client.execute(builder.build(), createStringResponseHandler());
        assertEquals(response.getStatusCode(), 200);
    }
    proto.ifPresent(uriBuilder::scheme);
    host.map(HostAndPort::fromString).ifPresent(uriBuilder::hostAndPort);
    URI forwardedUri = uriBuilder.build();
    assertEquals(servlet.getRequestUrl(), forwardedUri.toString());
    assertEquals(servlet.getScheme(), forwardedUri.getScheme());
    assertEquals(servlet.getIsSecure(), (Boolean) forwardedUri.getScheme().equals("https"));
    remoteHost.ifPresent(value -> assertEquals(servlet.getRemoteAddress(), value));
}
Also used : JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) HttpUriBuilder(io.airlift.http.client.HttpUriBuilder) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) HttpClient(io.airlift.http.client.HttpClient) HttpUriBuilder(io.airlift.http.client.HttpUriBuilder) Builder(io.airlift.http.client.Request.Builder) JcaContentSignerBuilder(org.bouncycastle.operator.jcajce.JcaContentSignerBuilder) JcaX509v3CertificateBuilder(org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder) StringResponse(io.airlift.http.client.StringResponseHandler.StringResponse) URI(java.net.URI)

Aggregations

Builder (io.airlift.http.client.Request.Builder)3 HttpUriBuilder (io.airlift.http.client.HttpUriBuilder)2 URI (java.net.URI)2 HttpClient (io.airlift.http.client.HttpClient)1 Request (io.airlift.http.client.Request)1 Response (io.airlift.http.client.Response)1 StringResponse (io.airlift.http.client.StringResponseHandler.StringResponse)1 JettyHttpClient (io.airlift.http.client.jetty.JettyHttpClient)1 Duration (io.airlift.units.Duration)1 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 JcaX509v3CertificateBuilder (org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder)1 JcaContentSignerBuilder (org.bouncycastle.operator.jcajce.JcaContentSignerBuilder)1 Managed (org.weakref.jmx.Managed)1