From 2b9d2478833a060df76c855e78c46e33c032db49 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 1 Nov 2018 13:29:29 +0300 Subject: ActivityPub: fix 410 error handler --- .../java/com/juick/server/ActivityPubManager.java | 11 ++----- .../java/com/juick/server/SignatureManager.java | 12 +++----- .../configuration/ActivityPubClientConfig.java | 3 +- .../ActivityPubClientErrorHandler.java | 35 ++++++++++++++++++++++ 4 files changed, 43 insertions(+), 18 deletions(-) create mode 100644 juick-server/src/main/java/com/juick/server/configuration/ActivityPubClientErrorHandler.java diff --git a/juick-server/src/main/java/com/juick/server/ActivityPubManager.java b/juick-server/src/main/java/com/juick/server/ActivityPubManager.java index 692d1107..0cb76a03 100644 --- a/juick-server/src/main/java/com/juick/server/ActivityPubManager.java +++ b/juick-server/src/main/java/com/juick/server/ActivityPubManager.java @@ -22,6 +22,9 @@ import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; +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; @@ -317,12 +320,4 @@ public class ActivityPubManager implements ActivityListener, NotificationListene } }); } - - public static class ErrorHandler extends DefaultResponseErrorHandler { - @Override - public void handleError(ClientHttpResponse response) throws IOException { - logger.warn("HTTP ERROR {} {} : {}", response.getStatusCode().value(), - response.getStatusText(), IOUtils.toString(response.getBody(), StandardCharsets.UTF_8)); - } - } } diff --git a/juick-server/src/main/java/com/juick/server/SignatureManager.java b/juick-server/src/main/java/com/juick/server/SignatureManager.java index d43efd4a..927a62a7 100644 --- a/juick-server/src/main/java/com/juick/server/SignatureManager.java +++ b/juick-server/src/main/java/com/juick/server/SignatureManager.java @@ -89,15 +89,11 @@ public class SignatureManager { return false; } public Optional getContext(URI contextUri) { - try { - return Optional.of(apClient.getForEntity(contextUri, Context.class).getBody()); - } catch (HttpClientErrorException e) { + Context context = apClient.getForEntity(contextUri, Context.class).getBody(); + if (context == null) { logger.warn("Cannot identify {}", contextUri); - if (e.getStatusCode().equals(HttpStatus.GONE)) { - logger.warn("Server report {} is gone, deleting", contextUri); - applicationEventPublisher.publishEvent(new DeleteUserEvent(this, contextUri.toASCIIString())); - } + return Optional.empty(); } - return Optional.empty(); + return Optional.of(context); } } diff --git a/juick-server/src/main/java/com/juick/server/configuration/ActivityPubClientConfig.java b/juick-server/src/main/java/com/juick/server/configuration/ActivityPubClientConfig.java index 0e8eaf02..ad26582a 100644 --- a/juick-server/src/main/java/com/juick/server/configuration/ActivityPubClientConfig.java +++ b/juick-server/src/main/java/com/juick/server/configuration/ActivityPubClientConfig.java @@ -1,6 +1,5 @@ package com.juick.server.configuration; -import com.juick.server.ActivityPubManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.client.SimpleClientHttpRequestFactory; @@ -13,7 +12,7 @@ public class ActivityPubClientConfig { SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); requestFactory.setOutputStreaming(false); RestTemplate restTemplate = new RestTemplate(requestFactory); - restTemplate.setErrorHandler(new ActivityPubManager.ErrorHandler()); + restTemplate.setErrorHandler(new ActivityPubClientErrorHandler()); return restTemplate; } } \ No newline at end of file diff --git a/juick-server/src/main/java/com/juick/server/configuration/ActivityPubClientErrorHandler.java b/juick-server/src/main/java/com/juick/server/configuration/ActivityPubClientErrorHandler.java new file mode 100644 index 00000000..ad8e9b2f --- /dev/null +++ b/juick-server/src/main/java/com/juick/server/configuration/ActivityPubClientErrorHandler.java @@ -0,0 +1,35 @@ +package com.juick.server.configuration; + +import com.juick.service.activities.DeleteUserEvent; +import org.apache.commons.io.IOUtils; +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.annotation.Nonnull; +import javax.inject.Inject; +import java.io.IOException; +import java.net.URI; +import java.nio.charset.StandardCharsets; + +@Component +public class ActivityPubClientErrorHandler extends DefaultResponseErrorHandler { + private static final Logger logger = LoggerFactory.getLogger(ActivityPubClientErrorHandler.class); + + @Inject + private ApplicationEventPublisher applicationEventPublisher; + @Override + public void handleError(URI contextUri, HttpMethod method, @Nonnull ClientHttpResponse response) throws IOException { + logger.warn("HTTP ERROR {} {} : {}", response.getStatusCode().value(), + response.getStatusText(), IOUtils.toString(response.getBody(), StandardCharsets.UTF_8)); + if (response.getStatusCode().equals(HttpStatus.GONE)) { + logger.warn("Server report {} is gone, deleting", response); + applicationEventPublisher.publishEvent(new DeleteUserEvent(this, contextUri.toASCIIString())); + } + } +} -- cgit v1.2.3