use of okhttp3.Request in project azure-sdk-for-java by Azure.
the class KeyVaultCredentials method applyCredentialsFilter.
@Override
public void applyCredentialsFilter(OkHttpClient.Builder clientBuilder) {
clientBuilder.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
HttpUrl url = chain.request().url();
Map<String, String> challengeMap = cache.getCachedChallenge(url);
if (challengeMap != null) {
// Get the bearer token
String credential = getAuthenticationCredentials(challengeMap);
Request newRequest = chain.request().newBuilder().header(AUTHENTICATE, BEARER_TOKEP_REFIX + credential).build();
return chain.proceed(newRequest);
} else {
// response
return chain.proceed(chain.request());
}
}
});
// Caches the challenge for failed request and re-send the request with
// access token.
clientBuilder.authenticator(new Authenticator() {
@Override
public Request authenticate(Route route, Response response) throws IOException {
// if challenge is not cached then extract and cache it
String authenticateHeader = response.header(WWW_AUTHENTICATE);
Map<String, String> challengeMap = extractChallenge(authenticateHeader, BEARER_TOKEP_REFIX);
// Cache the challenge
cache.addCachedChallenge(response.request().url(), challengeMap);
// Get the bearer token from the callback by providing the
// challenges
String credential = getAuthenticationCredentials(challengeMap);
if (credential == null) {
return null;
}
// be cached anywhere in our code.
return response.request().newBuilder().header(AUTHENTICATE, BEARER_TOKEP_REFIX + credential).build();
}
});
}
use of okhttp3.Request in project azure-sdk-for-java by Azure.
the class MockIntegrationTestBase method recordRequestAndResponse.
private Response recordRequestAndResponse(Interceptor.Chain chain) throws IOException {
Request request = chain.request();
NetworkCallRecord networkCallRecord = new NetworkCallRecord();
networkCallRecord.Headers = new HashMap<>();
try {
if (request.header("Content-Type") != null) {
networkCallRecord.Headers.put("Content-Type", request.header("Content-Type"));
}
if (request.header("x-ms-version") != null) {
networkCallRecord.Headers.put("x-ms-version", request.header("x-ms-version"));
}
if (request.header("User-Agent") != null) {
networkCallRecord.Headers.put("User-Agent", request.header("User-Agent"));
}
networkCallRecord.Method = request.method();
networkCallRecord.Uri = applyRegex(request.url().toString().replaceAll("\\?$", ""));
} catch (Exception e) {
}
Response response = chain.proceed(chain.request());
networkCallRecord.Response = new HashMap<>();
try {
networkCallRecord.Response.put("StatusCode", Integer.toString(response.code()));
extractResponseData(networkCallRecord.Response, response);
// remove pre-added header if this is a waiting or redirection
if (networkCallRecord.Response.get("Body").contains("<Status>InProgress</Status>") || Integer.parseInt(networkCallRecord.Response.get("StatusCode")) == HttpStatus.SC_TEMPORARY_REDIRECT) {
} else {
synchronized (testRecord.networkCallRecords) {
testRecord.networkCallRecords.add(networkCallRecord);
}
}
} catch (Exception e) {
}
return response;
}
use of okhttp3.Request in project azure-sdk-for-java by Azure.
the class MockIntegrationTestBase method registerRecordedResponse.
private synchronized Response registerRecordedResponse(Interceptor.Chain chain) throws IOException {
Request request = chain.request();
String url = request.url().toString();
url = applyRegex(url);
try {
synchronized (testRecord.networkCallRecords) {
registerStub(request.method(), url);
}
} catch (Exception e) {
e.printStackTrace();
}
return chain.proceed(chain.request());
}
use of okhttp3.Request in project Gradle-demo by Arisono.
the class OkhttpUtils method sendGetHttp.
/**
* get http
* @param url
* @param tag
*/
public static void sendGetHttp(String url, Map<String, Object> params, String cookies, String tag) {
StringBuilder buf = new StringBuilder(url);
if (params != null) {
if (!params.isEmpty()) {
if (url.indexOf("?") == -1)
buf.append("?");
else if (!url.endsWith("&"))
buf.append("&");
Iterator<Map.Entry<String, Object>> entries = params.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry<String, Object> entry = entries.next();
buf.append(String.valueOf(entry.getKey())).append("=").append(String.valueOf(entry.getValue())).append("&");
}
buf.deleteCharAt(buf.length() - 1);
}
}
Request request = new Request.Builder().url(buf.toString()).addHeader("content-type", "text/html;charset:utf-8").addHeader("Cookie", "12").build();
OkhttpUtils.println(tag + ":" + buf.toString());
OkhttpUtils.client.newCall(request).enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) throws IOException {
String requestJson;
requestJson = OkhttpUtils.getResponseString(response);
OkhttpUtils.println(requestJson);
RxBus.getInstance().send(tag + ":" + requestJson);
}
@Override
public void onFailure(Call call, IOException e) {
OkhttpUtils.onFailurePrintln(call, e, this);
}
});
}
use of okhttp3.Request in project Gradle-demo by Arisono.
the class RetryIntercepter method intercept.
@SuppressWarnings("resource")
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Response response = chain.proceed(request);
try {
// OkhttpUtils.println("重试拦截器:"+response.isSuccessful());
while (!response.isSuccessful() && retryNum < maxRetry) {
retryNum++;
response = chain.proceed(request);
}
} catch (ConnectException e) {
OkhttpUtils.println("服务器拒绝连接.....");
} catch (IOException e) {
OkhttpUtils.println("服务器拒绝连接.....");
}
return response;
}
Aggregations