From 120b26c55069f89cc60ef862514d5cf09566f348 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 4 Jan 2023 22:57:31 +0300 Subject: Fix class cast exception in tests --- src/main/java/com/juick/SignatureManager.java | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/com/juick/SignatureManager.java b/src/main/java/com/juick/SignatureManager.java index 92cb7fd9..792cc8cd 100644 --- a/src/main/java/com/juick/SignatureManager.java +++ b/src/main/java/com/juick/SignatureManager.java @@ -90,12 +90,12 @@ public class SignatureManager { } public String addSignature(Actor from, String host, String method, String path, String dateString, - String digestHeader) throws IOException { + String digestHeader) throws IOException { return addSignature(from, host, method, path, dateString, digestHeader, keystoreManager); } public String addSignature(Actor from, String host, String method, String path, String dateString, - String digestHeader, KeystoreManager keystoreManager) throws IOException { + String digestHeader, KeystoreManager keystoreManager) throws IOException { List requiredHeaders = StringUtils.isEmpty(digestHeader) ? Arrays.asList("(request-target)", "host", "date") : Arrays.asList("(request-target)", "host", "date", "digest"); @@ -138,7 +138,7 @@ public class SignatureManager { return AnonymousUser.INSTANCE; } } catch (NoSuchAlgorithmException | SignatureException | MissingRequiredHeaderException - | IOException e) { + | IOException e) { logger.warn("Verification error for {}: {}", signature.getKeyId(), e.getMessage()); } } else { @@ -156,12 +156,15 @@ public class SignatureManager { HttpHeaders headers = new HttpHeaders(); headers.setAccept(Collections.singletonList(MediaType.valueOf(ACTIVITY_MEDIA_TYPE))); HttpEntity activityRequest = new HttpEntity<>(headers); - Context context = apClient.exchange(contextUri, HttpMethod.GET, activityRequest, Context.class).getBody(); - if (context == null) { - logger.warn("Cannot identify {}", contextUri); - return Optional.empty(); + var response = apClient.exchange(contextUri, HttpMethod.GET, activityRequest, Context.class); + if (response.getStatusCode().is2xxSuccessful() && response.hasBody()) { + var context = response.getBody(); + if (context == null) { + logger.warn("Cannot identify {}", contextUri); + return Optional.empty(); + } + return Optional.of(context); } - return Optional.of(context); } catch (Exception e) { logger.warn("{}", e.getMessage()); } -- cgit v1.2.3