From 12e2206f9d24b29c8276b2743603635620643444 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 18 Jan 2023 20:39:41 +0300 Subject: RestTemplate -> OkHttpClient --- .../config/ActivityPubClientErrorHandler.java | 42 ++++++++++++---------- 1 file changed, 24 insertions(+), 18 deletions(-) (limited to 'src/main/java/com/juick/config/ActivityPubClientErrorHandler.java') diff --git a/src/main/java/com/juick/config/ActivityPubClientErrorHandler.java b/src/main/java/com/juick/config/ActivityPubClientErrorHandler.java index 08b98c82..65b67110 100644 --- a/src/main/java/com/juick/config/ActivityPubClientErrorHandler.java +++ b/src/main/java/com/juick/config/ActivityPubClientErrorHandler.java @@ -18,33 +18,39 @@ package com.juick.config; import com.juick.service.activities.DeleteUserEvent; +import okhttp3.Interceptor; +import okhttp3.Response; +import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.ApplicationEventPublisher; -import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; -import org.springframework.http.client.ClientHttpResponse; -import org.springframework.stereotype.Component; -import org.springframework.web.client.DefaultResponseErrorHandler; -import javax.inject.Inject; import java.io.IOException; -import java.net.URI; -@Component -public class ActivityPubClientErrorHandler extends DefaultResponseErrorHandler { +public class ActivityPubClientErrorHandler implements Interceptor { private static final Logger logger = LoggerFactory.getLogger("ActivityPub"); - @Inject - private ApplicationEventPublisher applicationEventPublisher; + private final ApplicationEventPublisher applicationEventPublisher; + + public ActivityPubClientErrorHandler(ApplicationEventPublisher applicationEventPublisher) { + this.applicationEventPublisher = applicationEventPublisher; + } + + @NotNull @Override - public void handleError(URI contextUri, HttpMethod method, ClientHttpResponse response) - throws IOException { - if (response.getStatusCode().equals(HttpStatus.GONE)) { - logger.warn("Server report {} is gone, deleting", contextUri.toASCIIString()); - applicationEventPublisher.publishEvent(new DeleteUserEvent(this, contextUri.toASCIIString())); - } else { - logger.warn("HTTP ERROR {} on {} : {}", response.getStatusCode().value(), - contextUri.toASCIIString(), response.getStatusText()); + public Response intercept(@NotNull Interceptor.Chain chain) throws IOException { + var request = chain.request(); + var response = chain.proceed(request); + var url = request.url(); + if (!response.isSuccessful()) { + if (response.code() == HttpStatus.GONE.value()) { + logger.warn("Server report {} is gone, deleting", url); + applicationEventPublisher.publishEvent(new DeleteUserEvent(this, url.toString())); + } else { + logger.warn("HTTP ERROR {} on {} : {}", response.code(), + url, response.body() != null ? response.body().string() : ""); + } } + return response; } } -- cgit v1.2.3