BAEL-4204 - Adding Interceptors in OkHTTP (#10539)
* BAEL-4706 - Spring Boot with Spring Batch * BAEL-3948 - Fix test(s) in spring-batch which leaves repository.sqlite changed * BAEL-4736 - Convert JSONArray to List of Object using camel-jackson * BAEL-4756 - Mockito MockSettings * BAEL-4756 - Mockito MockSettings - fix spelling * BAEL-2674 - Upgrade the Okhttp article * BAEL-4204 - Adding Interceptors in OkHTTP Co-authored-by: Jonathan Cook <jcook@sciops.esa.int>
This commit is contained in:
+18
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.okhttp.interceptors;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import okhttp3.Interceptor;
|
||||
import okhttp3.Response;
|
||||
|
||||
public class CacheControlResponeInterceptor implements Interceptor {
|
||||
|
||||
@Override
|
||||
public Response intercept(Chain chain) throws IOException {
|
||||
Response response = chain.proceed(chain.request());
|
||||
return response.newBuilder()
|
||||
.header("Cache-Control", "no-store")
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.okhttp.interceptors;
|
||||
|
||||
public class ErrorMessage {
|
||||
|
||||
private final int status;
|
||||
private final String detail;
|
||||
|
||||
public ErrorMessage(int status, String detail) {
|
||||
this.status = status;
|
||||
this.detail = detail;
|
||||
}
|
||||
|
||||
public int getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public String getDetail() {
|
||||
return detail;
|
||||
}
|
||||
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.okhttp.interceptors;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
|
||||
import okhttp3.Interceptor;
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.Response;
|
||||
import okhttp3.ResponseBody;
|
||||
|
||||
public class ErrorResponseInterceptor implements Interceptor {
|
||||
|
||||
public static final MediaType APPLICATION_JSON = MediaType.get("application/json; charset=utf-8");
|
||||
|
||||
@Override
|
||||
public Response intercept(Chain chain) throws IOException {
|
||||
Response response = chain.proceed(chain.request());
|
||||
|
||||
if(!response.isSuccessful()) {
|
||||
Gson gson = new Gson();
|
||||
String body = gson.toJson(new ErrorMessage(response.code(), "The response from the server was not OK"));
|
||||
ResponseBody responseBody = ResponseBody.create(body, APPLICATION_JSON);
|
||||
|
||||
return response.newBuilder()
|
||||
.body(responseBody)
|
||||
.build();
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.okhttp.interceptors;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import okhttp3.Interceptor;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
|
||||
public class SimpleLoggingInterceptor implements Interceptor {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(SimpleLoggingInterceptor.class);
|
||||
|
||||
@Override
|
||||
public Response intercept(Chain chain) throws IOException {
|
||||
Request request = chain.request();
|
||||
|
||||
LOGGER.info("Intercepted headers: {} from URL: {}", request.headers(), request.url());
|
||||
|
||||
return chain.proceed(request);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user