use of okhttp3.HttpUrl.Builder in project mbed-cloud-sdk-java by ARMmbed.
the class OAuth method retryingIntercept.
private Response retryingIntercept(Chain chain, boolean updateTokenAndRetryOnAuthorizationFailure) throws IOException {
Request request = chain.request();
// If the request already have an authorization (eg. Basic auth), do nothing
if (request.header("Authorization") != null) {
return chain.proceed(request);
}
// If first time, get the token
OAuthClientRequest oAuthRequest;
if (getAccessToken() == null) {
updateAccessToken(null);
}
if (getAccessToken() != null) {
// Build the request
Builder rb = request.newBuilder();
String requestAccessToken = new String(getAccessToken());
try {
oAuthRequest = new OAuthBearerClientRequest(request.url().toString()).setAccessToken(requestAccessToken).buildHeaderMessage();
} catch (OAuthSystemException e) {
throw new IOException(e);
}
for (Map.Entry<String, String> header : oAuthRequest.getHeaders().entrySet()) {
rb.addHeader(header.getKey(), header.getValue());
}
rb.url(oAuthRequest.getLocationUri());
// Execute the request
Response response = chain.proceed(rb.build());
// 401/403 most likely indicates that access token has expired. Unless it happens two times in a row.
if (response != null && (response.code() == HTTP_UNAUTHORIZED || response.code() == HTTP_FORBIDDEN) && updateTokenAndRetryOnAuthorizationFailure) {
if (updateAccessToken(requestAccessToken)) {
return retryingIntercept(chain, false);
}
}
return response;
} else {
return chain.proceed(chain.request());
}
}
use of okhttp3.HttpUrl.Builder in project mbed-cloud-sdk-java by ARMmbed.
the class OAuth method retryingIntercept.
private Response retryingIntercept(Chain chain, boolean updateTokenAndRetryOnAuthorizationFailure) throws IOException {
Request request = chain.request();
// If the request already have an authorization (eg. Basic auth), do nothing
if (request.header("Authorization") != null) {
return chain.proceed(request);
}
// If first time, get the token
OAuthClientRequest oAuthRequest;
if (getAccessToken() == null) {
updateAccessToken(null);
}
if (getAccessToken() != null) {
// Build the request
Builder rb = request.newBuilder();
String requestAccessToken = new String(getAccessToken());
try {
oAuthRequest = new OAuthBearerClientRequest(request.url().toString()).setAccessToken(requestAccessToken).buildHeaderMessage();
} catch (OAuthSystemException e) {
throw new IOException(e);
}
for (Map.Entry<String, String> header : oAuthRequest.getHeaders().entrySet()) {
rb.addHeader(header.getKey(), header.getValue());
}
rb.url(oAuthRequest.getLocationUri());
// Execute the request
Response response = chain.proceed(rb.build());
// 401/403 most likely indicates that access token has expired. Unless it happens two times in a row.
if (response != null && (response.code() == HTTP_UNAUTHORIZED || response.code() == HTTP_FORBIDDEN) && updateTokenAndRetryOnAuthorizationFailure) {
if (updateAccessToken(requestAccessToken)) {
return retryingIntercept(chain, false);
}
}
return response;
} else {
return chain.proceed(chain.request());
}
}
use of okhttp3.HttpUrl.Builder in project spring-cloud-netflix by spring-cloud.
the class OkHttpRibbonRequestTests method testEntity.
void testEntity(String entityValue, ByteArrayInputStream requestEntity, boolean addContentLengthHeader, String method) throws IOException {
String lengthString = String.valueOf(entityValue.length());
Long length = null;
String uri = "http://example.com";
LinkedMultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
if (addContentLengthHeader) {
headers.add("Content-Length", lengthString);
length = (long) entityValue.length();
}
RibbonRequestCustomizer requestCustomizer = new RibbonRequestCustomizer<Request.Builder>() {
@Override
public boolean accepts(Class builderClass) {
return builderClass == Request.Builder.class;
}
@Override
public void customize(Request.Builder builder) {
builder.addHeader("from-customizer", "foo");
}
};
RibbonCommandContext context = new RibbonCommandContext("example", method, uri, false, headers, new LinkedMultiValueMap<String, String>(), requestEntity, Collections.singletonList(requestCustomizer));
context.setContentLength(length);
OkHttpRibbonRequest httpRequest = new OkHttpRibbonRequest(context);
Request request = httpRequest.toRequest();
assertThat("uri is wrong", request.url().toString(), startsWith(uri));
if (addContentLengthHeader) {
assertThat("Content-Length is wrong", request.header("Content-Length"), is(equalTo(lengthString)));
}
assertThat("from-customizer is wrong", request.header("from-customizer"), is(equalTo("foo")));
if (!method.equalsIgnoreCase("get")) {
assertThat("body is null", request.body(), is(notNullValue()));
RequestBody body = request.body();
assertThat("contentLength is wrong", body.contentLength(), is(equalTo((long) entityValue.length())));
Buffer content = new Buffer();
body.writeTo(content);
String string = content.readByteString().utf8();
assertThat("content is wrong", string, is(equalTo(entityValue)));
}
}
use of okhttp3.HttpUrl.Builder in project spring-cloud-netflix by spring-cloud.
the class OkHttpLoadBalancingClient method getOkHttpClient.
OkHttpClient getOkHttpClient(IClientConfig configOverride, boolean secure) {
IClientConfig config = configOverride != null ? configOverride : this.config;
RibbonProperties ribbon = RibbonProperties.from(config);
OkHttpClient.Builder builder = this.delegate.newBuilder().connectTimeout(ribbon.connectTimeout(this.connectTimeout), TimeUnit.MILLISECONDS).readTimeout(ribbon.readTimeout(this.readTimeout), TimeUnit.MILLISECONDS).followRedirects(ribbon.isFollowRedirects(this.followRedirects));
if (secure) {
builder.followSslRedirects(ribbon.isFollowRedirects(this.followRedirects));
}
return builder.build();
}
use of okhttp3.HttpUrl.Builder in project spring-cloud-netflix by spring-cloud.
the class RibbonCommandContextTest method givenRibbonCommandContextIsSetup.
private void givenRibbonCommandContextIsSetup() {
LinkedMultiValueMap headers = new LinkedMultiValueMap();
LinkedMultiValueMap params = new LinkedMultiValueMap();
RibbonRequestCustomizer requestCustomizer = new RibbonRequestCustomizer<Request.Builder>() {
@Override
public boolean accepts(Class builderClass) {
return builderClass == Request.Builder.class;
}
@Override
public void customize(Request.Builder builder) {
builder.addHeader("from-customizer", "foo");
}
};
ribbonCommandContext = new RibbonCommandContext("serviceId", HttpMethod.POST.toString(), "/my/route", true, headers, params, new ByteArrayInputStream(TEST_CONTENT), Lists.newArrayList(requestCustomizer));
}
Aggregations