use of io.fabric8.kubernetes.client.http.HttpRequest in project kubernetes-client by fabric8io.
the class JdkHttpClientImpl method sendAsync.
@Override
public <T> CompletableFuture<HttpResponse<T>> sendAsync(HttpRequest request, Class<T> type) {
JdkHttpRequestImpl jdkRequest = (JdkHttpRequestImpl) request;
JdkHttpRequestImpl.BuilderImpl builderImpl = jdkRequest.newBuilder();
for (Interceptor interceptor : builder.interceptors.values()) {
interceptor.before(builderImpl, jdkRequest);
jdkRequest = builderImpl.build();
}
BodyHandler<T> bodyHandler;
if (type == null) {
bodyHandler = (BodyHandler<T>) BodyHandlers.discarding();
} else if (type == InputStream.class) {
bodyHandler = (BodyHandler<T>) BodyHandlers.ofInputStream();
} else if (type == String.class) {
bodyHandler = (BodyHandler<T>) BodyHandlers.ofString();
} else {
bodyHandler = responseInfo -> {
BodySubscriber<InputStream> upstream = BodyHandlers.ofInputStream().apply(responseInfo);
BodySubscriber<Reader> downstream = BodySubscribers.mapping(upstream, (InputStream is) -> new InputStreamReader(is, StandardCharsets.UTF_8));
return (BodySubscriber<T>) downstream;
};
}
CompletableFuture<java.net.http.HttpResponse<T>> cf = this.httpClient.sendAsync(builderImpl.build().request, bodyHandler);
for (Interceptor interceptor : builder.interceptors.values()) {
cf = cf.thenCompose(response -> {
if (response != null && !HttpResponse.isSuccessful(response.statusCode()) && interceptor.afterFailure(builderImpl, new JdkHttpResponseImpl<>(response))) {
return this.httpClient.sendAsync(builderImpl.build().request, bodyHandler);
}
return CompletableFuture.completedFuture(response);
});
}
return cf.thenApply(JdkHttpResponseImpl::new);
}
use of io.fabric8.kubernetes.client.http.HttpRequest in project kubernetes-client by fabric8io.
the class OkHttpClientImpl method sendAsync.
@Override
public <T> CompletableFuture<HttpResponse<T>> sendAsync(HttpRequest request, Class<T> type) {
CompletableFuture<HttpResponse<T>> future = new CompletableFuture<>();
Call call = httpClient.newCall(((OkHttpRequestImpl) request).getRequest());
call.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) throws IOException {
future.complete(new OkHttpResponseImpl<>(response, type));
}
@Override
public void onFailure(Call call, IOException e) {
future.completeExceptionally(e);
}
});
future.whenComplete((r, t) -> {
if (future.isCancelled()) {
call.cancel();
}
});
return future;
}
use of io.fabric8.kubernetes.client.http.HttpRequest in project kubernetes-client by fabric8io.
the class BackwardsCompatibilityInterceptor method afterFailure.
@Override
public boolean afterFailure(Builder builder, HttpResponse<?> response) {
ResourceKey target = findNewTarget(builder, response);
if (target == null) {
return false;
}
HttpRequest request = response.request();
if (request.bodyString() != null && !request.method().equalsIgnoreCase(PATCH)) {
Object object = Serialization.unmarshal(request.bodyString());
if (object instanceof HasMetadata) {
HasMetadata h = (HasMetadata) object;
h.setApiVersion(target.group + "/" + target.version);
switch(request.method()) {
case "POST":
builder.post(JSON, Serialization.asJson(h));
break;
case "PUT":
builder.put(JSON, Serialization.asJson(h));
break;
case "DELETE":
builder.delete(JSON, Serialization.asJson(h));
break;
default:
return false;
}
}
}
return true;
}
use of io.fabric8.kubernetes.client.http.HttpRequest in project kubernetes-client by fabric8io.
the class OpenIDConnectionUtils method getOIDCDiscoveryDocumentAsMap.
/**
* OpenID providers publish their metadata at a well-known URL OpenID providers publish their metadata
* at a well-known URL which looks like this: https://[base-server-url]/.well-known/openid-configuration
* This method performs an Http Get at this public URL and fetches response as a HashMap
*
* @param client HttpClient for doing HTTP Get to well known URL of OpenID provider
* @param issuer OpenID Connect provider issuer URL
* @return a HashMap of Discovery document
*/
static Map<String, Object> getOIDCDiscoveryDocumentAsMap(HttpClient client, String issuer) {
HttpRequest request = client.newHttpRequestBuilder().uri(getWellKnownUrlForOpenIDIssuer(issuer)).build();
try {
HttpResponse<String> response = client.send(request, String.class);
if (response.isSuccessful() && response.body() != null) {
return convertJsonStringToMap(response.body());
} else {
// Don't produce an error that's too huge (e.g. if we get HTML back for some reason).
String responseBody = response.body();
LOGGER.warn("oidc: failed to query metadata endpoint: {} {}", response.code(), responseBody);
}
} catch (IOException e) {
LOGGER.warn("Could not refresh OIDC token, failure in getting refresh URL", e);
}
return Collections.emptyMap();
}
use of io.fabric8.kubernetes.client.http.HttpRequest in project kubernetes-client by fabric8io.
the class OpenIDConnectionUtils method refreshOidcToken.
/**
* Issue Token Refresh HTTP Request to OIDC Provider
*
* @param client Http Client for issuing HTTP request
* @param clientId client id
* @param refreshToken refresh token
* @param clientSecret client secret
* @param tokenURL OpenID Connection provider's token refresh url
* @return response as HashMap
* @throws IOException in case of any error in contacting OIDC provider
*/
static Map<String, Object> refreshOidcToken(HttpClient client, String clientId, String refreshToken, String clientSecret, String tokenURL) throws IOException {
HttpRequest request = getTokenRefreshHttpRequest(client, tokenURL, clientId, refreshToken, clientSecret);
HttpResponse<String> response = client.send(request, String.class);
String body = response.body();
if (body != null) {
// Get response body as string
if (response.isSuccessful()) {
// Deserialize response body into a Map and return
return convertJsonStringToMap(body);
} else {
// Log error response body
LOGGER.warn("Response: {}", body);
}
}
return Collections.emptyMap();
}
Aggregations