From 97c29f69d269c793df7fa59c1f40efccef04bfaf Mon Sep 17 00:00:00 2001 From: Luke Sikina Date: Mon, 23 Oct 2023 15:51:10 -0400 Subject: [PATCH] [ALS-0000] - 500 for 401 on sites --- .../avillach/resource/passthru/HttpClient.java | 4 ++++ .../resource/passthru/PassThroughResourceRS.java | 14 +++++++------- .../dbmi/avillach/util/HttpClientUtil.java | 16 ++++++++++++++++ 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/pic-sure-resources/pic-sure-passthrough-resource/src/main/java/edu/harvard/hms/dbmi/avillach/resource/passthru/HttpClient.java b/pic-sure-resources/pic-sure-passthrough-resource/src/main/java/edu/harvard/hms/dbmi/avillach/resource/passthru/HttpClient.java index 2e186c62..f929f91e 100644 --- a/pic-sure-resources/pic-sure-passthrough-resource/src/main/java/edu/harvard/hms/dbmi/avillach/resource/passthru/HttpClient.java +++ b/pic-sure-resources/pic-sure-passthrough-resource/src/main/java/edu/harvard/hms/dbmi/avillach/resource/passthru/HttpClient.java @@ -56,4 +56,8 @@ public HttpResponse retrievePostResponse(String uri, Header[] headers, String bo public void throwResponseError(HttpResponse response, String baseURL) { edu.harvard.dbmi.avillach.util.HttpClientUtil.throwResponseError(response, baseURL); } + + public void throwInternalResponseError(HttpResponse response, String baseURL) { + edu.harvard.dbmi.avillach.util.HttpClientUtil.throwInternalResponseError(response, baseURL); + } } diff --git a/pic-sure-resources/pic-sure-passthrough-resource/src/main/java/edu/harvard/hms/dbmi/avillach/resource/passthru/PassThroughResourceRS.java b/pic-sure-resources/pic-sure-passthrough-resource/src/main/java/edu/harvard/hms/dbmi/avillach/resource/passthru/PassThroughResourceRS.java index e251a759..67b2a13a 100644 --- a/pic-sure-resources/pic-sure-passthrough-resource/src/main/java/edu/harvard/hms/dbmi/avillach/resource/passthru/PassThroughResourceRS.java +++ b/pic-sure-resources/pic-sure-passthrough-resource/src/main/java/edu/harvard/hms/dbmi/avillach/resource/passthru/PassThroughResourceRS.java @@ -66,7 +66,7 @@ public ResourceInfo info(QueryRequest infoRequest) { if (response.getStatusLine().getStatusCode() != 200) { logger.error("{}{} did not return a 200: {} {} ", properties.getTargetPicsureUrl(), pathName, response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase()); - httpClient.throwResponseError(response, properties.getTargetPicsureUrl()); + httpClient.throwInternalResponseError(response, properties.getTargetPicsureUrl()); } ResourceInfo resourceInfo = httpClient.readObjectFromResponse(response, ResourceInfo.class); @@ -110,7 +110,7 @@ public QueryStatus query(QueryRequest queryRequest) { logger.error("{}{} calling resource with id {} did not return a 200: {} {} ", properties.getTargetPicsureUrl(), pathName, chainRequest.getResourceUUID(), response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase()); - httpClient.throwResponseError(response, properties.getTargetPicsureUrl()); + httpClient.throwInternalResponseError(response, properties.getTargetPicsureUrl()); } QueryStatus queryStatus = httpClient.readObjectFromResponse(response, QueryStatus.class); queryStatus.setResourceID(queryRequest.getResourceUUID()); @@ -146,7 +146,7 @@ public Response queryResult(@PathParam("resourceQueryId") String queryId, QueryR logger.error("{}{} calling resource with id {} did not return a 200: {} {} ", properties.getTargetPicsureUrl(), pathName, chainRequest.getResourceUUID(), response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase()); - httpClient.throwResponseError(response, properties.getTargetPicsureUrl()); + httpClient.throwInternalResponseError(response, properties.getTargetPicsureUrl()); } return Response.ok(response.getEntity().getContent()).build(); @@ -182,7 +182,7 @@ public QueryStatus queryStatus(@PathParam("resourceQueryId") String queryId, Que logger.error("{}{} calling resource with id {} did not return a 200: {} {} ", properties.getTargetPicsureUrl(), pathName, chainRequest.getResourceUUID(), response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase()); - httpClient.throwResponseError(response, properties.getTargetPicsureUrl()); + httpClient.throwInternalResponseError(response, properties.getTargetPicsureUrl()); } QueryStatus queryStatus = httpClient.readObjectFromResponse(response, QueryStatus.class); queryStatus.setResourceID(statusRequest.getResourceUUID()); @@ -223,7 +223,7 @@ public Response querySync(QueryRequest queryRequest) { logger.error("{}{} calling resource with id {} did not return a 200: {} {} ", properties.getTargetPicsureUrl(), pathName, chainRequest.getResourceUUID(), response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase()); - httpClient.throwResponseError(response, properties.getTargetPicsureUrl()); + httpClient.throwInternalResponseError(response, properties.getTargetPicsureUrl()); } if (response.containsHeader(QUERY_METADATA_FIELD)) { @@ -267,7 +267,7 @@ public SearchResults search(QueryRequest searchRequest) { if (response.getStatusLine().getStatusCode() != 200) { logger.error("{}{} did not return a 200: {} {} ", properties.getTargetPicsureUrl(), pathName, response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase()); - httpClient.throwResponseError(response, properties.getTargetPicsureUrl()); + httpClient.throwInternalResponseError(response, properties.getTargetPicsureUrl()); } return httpClient.readObjectFromResponse(response, SearchResults.class); } catch (IOException e) { @@ -303,7 +303,7 @@ public Response queryFormat(QueryRequest queryRequest) { logger.error("{}{} calling resource with id {} did not return a 200: {} {} ", properties.getTargetPicsureUrl(), pathName, chainRequest.getResourceUUID(), response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase()); - httpClient.throwResponseError(response, properties.getTargetPicsureUrl()); + httpClient.throwInternalResponseError(response, properties.getTargetPicsureUrl()); } return Response.ok(response.getEntity().getContent()).build(); diff --git a/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/HttpClientUtil.java b/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/HttpClientUtil.java index b912ed33..a506b94d 100644 --- a/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/HttpClientUtil.java +++ b/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/HttpClientUtil.java @@ -186,6 +186,22 @@ public static void throwResponseError(HttpResponse response, String baseURL) { throw new ResourceInterfaceException(errorMessage); } + public static void throwInternalResponseError(HttpResponse response, String baseURL) { + // We don't want to propagate 401s. A site 401ing is a server side error and should + // 500 in the common area. + String errorMessage = baseURL + " " + response.getStatusLine().getStatusCode() + " " + + response.getStatusLine().getReasonPhrase(); + try { + JsonNode responseNode = json.readTree(response.getEntity().getContent()); + if (responseNode != null && responseNode.has("message")) { + errorMessage += "/n" + responseNode.get("message").asText(); + } + } catch (IOException e) { + // That's fine, there's no message + } + throw new ResourceInterfaceException(errorMessage); + } + /** * Basic and general post function using Apache Http Client *