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());
}
}
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);
}
});
}
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));
}
Aggregations