|
49 | 49 | import org.apache.http.client.methods.HttpGet; |
50 | 50 | import org.apache.http.client.methods.HttpPost; |
51 | 51 | import org.apache.http.client.methods.HttpUriRequest; |
| 52 | +import org.apache.http.client.methods.RequestBuilder; |
52 | 53 | import org.apache.http.client.protocol.HttpClientContext; |
53 | 54 | import org.apache.http.client.utils.URIBuilder; |
54 | 55 | import org.apache.http.entity.ContentType; |
@@ -1058,44 +1059,61 @@ protected void executeNoContent(HttpUriRequest method) throws IOException, RDF4J |
1058 | 1059 | } |
1059 | 1060 |
|
1060 | 1061 | protected HttpResponse execute(HttpUriRequest method) throws IOException, RDF4JException { |
| 1062 | + return executeWithRedirects(method, 5); |
| 1063 | + } |
| 1064 | + |
| 1065 | + private HttpResponse executeWithRedirects(HttpUriRequest method, int redirectsLeft) |
| 1066 | + throws IOException, RDF4JException { |
1061 | 1067 | boolean consume = true; |
1062 | 1068 | if (params != null) { |
1063 | 1069 | method.setParams(params); |
1064 | 1070 | } |
1065 | 1071 | HttpResponse response = httpClient.execute(method, httpContext); |
1066 | | - |
1067 | 1072 | try { |
1068 | 1073 | int httpCode = response.getStatusLine().getStatusCode(); |
1069 | 1074 | if (httpCode >= 200 && httpCode < 300 || httpCode == HttpURLConnection.HTTP_NOT_FOUND) { |
1070 | 1075 | consume = false; |
1071 | 1076 | return response; // everything OK, control flow can continue |
1072 | | - } else { |
1073 | | - switch (httpCode) { |
1074 | | - case HttpURLConnection.HTTP_UNAUTHORIZED: // 401 |
1075 | | - throw new UnauthorizedException(); |
1076 | | - case HttpURLConnection.HTTP_UNAVAILABLE: // 503 |
1077 | | - throw new QueryInterruptedException(); |
1078 | | - default: |
1079 | | - ErrorInfo errInfo = getErrorInfo(response); |
1080 | | - // Throw appropriate exception |
1081 | | - if (errInfo.getErrorType() == ErrorType.MALFORMED_DATA) { |
1082 | | - throw new RDFParseException(errInfo.getErrorMessage()); |
1083 | | - } else if (errInfo.getErrorType() == ErrorType.UNSUPPORTED_FILE_FORMAT) { |
1084 | | - throw new UnsupportedRDFormatException(errInfo.getErrorMessage()); |
1085 | | - } else if (errInfo.getErrorType() == ErrorType.MALFORMED_QUERY) { |
1086 | | - throw new MalformedQueryException(errInfo.getErrorMessage()); |
1087 | | - } else if (errInfo.getErrorType() == ErrorType.UNSUPPORTED_QUERY_LANGUAGE) { |
1088 | | - throw new UnsupportedQueryLanguageException(errInfo.getErrorMessage()); |
1089 | | - } else if (contentTypeIs(response, "application/shacl-validation-report")) { |
1090 | | - RDFFormat format = getContentTypeSerialisation(response); |
1091 | | - throw new RepositoryException(new RemoteShaclValidationException( |
1092 | | - new StringReader(errInfo.toString()), "", format)); |
1093 | | - |
1094 | | - } else if (!errInfo.toString().isEmpty()) { |
1095 | | - throw new RepositoryException(errInfo.toString()); |
1096 | | - } else { |
1097 | | - throw new RepositoryException(response.getStatusLine().getReasonPhrase()); |
1098 | | - } |
| 1077 | + } |
| 1078 | + |
| 1079 | + // Follow redirects for any method (preserving method and entity) |
| 1080 | + if (redirectsLeft > 0 && (httpCode == HttpURLConnection.HTTP_MOVED_PERM |
| 1081 | + || httpCode == HttpURLConnection.HTTP_MOVED_TEMP || httpCode == 307 || httpCode == 308)) { |
| 1082 | + Header location = response.getFirstHeader("Location"); |
| 1083 | + if (location != null) { |
| 1084 | + // consume and follow |
| 1085 | + EntityUtils.consumeQuietly(response.getEntity()); |
| 1086 | + java.net.URI uri = java.net.URI.create(location.getValue()); |
| 1087 | + HttpUriRequest redirect = RequestBuilder.copy(method).setUri(uri).build(); |
| 1088 | + return executeWithRedirects(redirect, redirectsLeft - 1); |
| 1089 | + } |
| 1090 | + } |
| 1091 | + |
| 1092 | + switch (httpCode) { |
| 1093 | + case HttpURLConnection.HTTP_UNAUTHORIZED: // 401 |
| 1094 | + throw new UnauthorizedException(); |
| 1095 | + case HttpURLConnection.HTTP_UNAVAILABLE: // 503 |
| 1096 | + throw new QueryInterruptedException(); |
| 1097 | + default: |
| 1098 | + ErrorInfo errInfo = getErrorInfo(response); |
| 1099 | + // Throw appropriate exception |
| 1100 | + if (errInfo.getErrorType() == ErrorType.MALFORMED_DATA) { |
| 1101 | + throw new RDFParseException(errInfo.getErrorMessage()); |
| 1102 | + } else if (errInfo.getErrorType() == ErrorType.UNSUPPORTED_FILE_FORMAT) { |
| 1103 | + throw new UnsupportedRDFormatException(errInfo.getErrorMessage()); |
| 1104 | + } else if (errInfo.getErrorType() == ErrorType.MALFORMED_QUERY) { |
| 1105 | + throw new MalformedQueryException(errInfo.getErrorMessage()); |
| 1106 | + } else if (errInfo.getErrorType() == ErrorType.UNSUPPORTED_QUERY_LANGUAGE) { |
| 1107 | + throw new UnsupportedQueryLanguageException(errInfo.getErrorMessage()); |
| 1108 | + } else if (contentTypeIs(response, "application/shacl-validation-report")) { |
| 1109 | + RDFFormat format = getContentTypeSerialisation(response); |
| 1110 | + throw new RepositoryException(new RemoteShaclValidationException( |
| 1111 | + new StringReader(errInfo.toString()), "", format)); |
| 1112 | + |
| 1113 | + } else if (!errInfo.toString().isEmpty()) { |
| 1114 | + throw new RepositoryException(errInfo.toString()); |
| 1115 | + } else { |
| 1116 | + throw new RepositoryException(response.getStatusLine().getReasonPhrase()); |
1099 | 1117 | } |
1100 | 1118 | } |
1101 | 1119 | } finally { |
|
0 commit comments