use of ee.ria.xroad.common.util.OpenapiDescriptionFiletype in project X-Road by nordic-institute.
the class RestMetadataServiceHandlerImpl method handleGetOpenApi.
private void handleGetOpenApi(ProxyMessage requestProxyMessage) throws IOException, HttpClientCreator.HttpClientCreatorException, URISyntaxException {
List<NameValuePair> pairs = URLEncodedUtils.parse(requestProxyMessage.getRest().getQuery(), Charset.forName("UTF-8"));
String targetServiceCode = null;
for (NameValuePair pair : pairs) {
log.trace("{} : {}", pair.getName(), pair.getValue());
if (pair.getName().equalsIgnoreCase(QUERY_PARAM_SERVICECODE)) {
targetServiceCode = pair.getValue();
}
}
if (targetServiceCode == null || targetServiceCode.isEmpty()) {
throw new CodedException(X_INVALID_REQUEST, "Missing serviceCode in message body");
}
ServiceId targetServiceId = ServiceId.create(requestProxyMessage.getRest().getServiceId().getClientId(), targetServiceCode);
log.trace("targetServiceId={}", targetServiceId);
DescriptionType descriptionType = ServerConf.getDescriptionType(targetServiceId);
if (descriptionType == null) {
throw new CodedException(X_INTERNAL_ERROR, String.format("Service not found: %s", targetServiceId.toString()));
}
if (descriptionType != DescriptionType.OPENAPI3) {
throw new CodedException(X_INTERNAL_ERROR, String.format("Invalid service type: %s", descriptionType.toString()));
}
String serviceDescriptionURL = ServerConf.getServiceDescriptionURL(targetServiceId);
HttpClient client = httpClientCreator.getHttpClient();
HttpContext httpContext = new BasicHttpContext();
// ServerMessageProcessor uses the same method to pass the ServiceId to CustomSSLSocketFactory
httpContext.setAttribute(ServiceId.class.getName(), targetServiceId);
URI uri = new URI(serviceDescriptionURL);
HttpResponse response = client.execute(new HttpGet(uri), httpContext);
StatusLine statusLine = response.getStatusLine();
if (HttpStatus.SC_OK != statusLine.getStatusCode()) {
throw new CodedException(X_INTERNAL_ERROR, String.format("Failed reading service description from %s. Status: %s Reason: %s", serviceDescriptionURL, statusLine.getStatusCode(), statusLine.getReasonPhrase()));
}
InputStream responseContent = response.getEntity().getContent();
try {
OpenapiDescriptionFiletype filetype = getFileType(response, uri);
Openapi3Anonymiser anonymiser = new Openapi3Anonymiser();
if (OpenapiDescriptionFiletype.JSON.equals(filetype)) {
anonymiser.anonymiseJson(responseContent, restResponseBody);
} else {
anonymiser.anonymiseYaml(responseContent, restResponseBody);
}
} catch (IOException e) {
throw new CodedException(X_INTERNAL_ERROR, String.format("Failed overwriting origin URL for the openapi servers for %s", serviceDescriptionURL));
}
if (response.containsHeader(MimeUtils.HEADER_CONTENT_TYPE)) {
restResponse.getHeaders().add(new BasicHeader(MimeUtils.HEADER_CONTENT_TYPE, response.getFirstHeader(MimeUtils.HEADER_CONTENT_TYPE).getValue()));
} else {
restResponse.getHeaders().add(new BasicHeader(MimeUtils.HEADER_CONTENT_TYPE, DEFAULT_GETOPENAPI_CONTENT_TYPE));
}
}
Aggregations