Skip to content

Commit

Permalink
fix: stacktrace on unreachable swagger and remove handling for deprec…
Browse files Browse the repository at this point in the history
…a… (#3699)

fix: stacktrace on unreachable swagger and remove deprecated z/OSMF swagger workaround

Signed-off-by: Richard Salač <richard.salac@broadcom.com>
Co-authored-by: Pablo Carle <pablocarle@users.noreply.github.com>
  • Loading branch information
richard-salac and pablocarle authored Aug 29, 2024
1 parent 78e91d9 commit 3606dd6
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.*;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
Expand All @@ -35,9 +33,11 @@
import org.zowe.apiml.apicatalog.swagger.SubstituteSwaggerGenerator;
import org.zowe.apiml.config.ApiInfo;
import org.zowe.apiml.eurekaservice.client.util.EurekaMetadataParser;
import org.zowe.apiml.message.log.ApimlLogger;
import org.zowe.apiml.product.gateway.GatewayClient;
import org.zowe.apiml.product.instance.ServiceAddress;
import org.zowe.apiml.product.instance.InstanceInitializationException;
import org.zowe.apiml.product.logging.annotations.InjectApimlLogger;
import org.zowe.apiml.product.routing.RoutedService;
import org.zowe.apiml.product.routing.RoutedServices;

Expand Down Expand Up @@ -70,6 +70,9 @@ public class APIDocRetrievalService {
private final EurekaMetadataParser metadataParser = new EurekaMetadataParser();
private final SubstituteSwaggerGenerator swaggerGenerator = new SubstituteSwaggerGenerator();

@InjectApimlLogger
private ApimlLogger apimlLogger = ApimlLogger.empty();

/**
* Retrieves the available API versions for a registered service.
* Takes the versions available in each 'apiml.service.apiInfo' element.
Expand Down Expand Up @@ -175,26 +178,12 @@ private ApiDocInfo buildApiDocInfo(String serviceId, ApiInfo apiInfo, InstanceIn
return getApiDocInfoBySubstituteSwagger(instanceInfo, routes, apiInfo);
}

// The Swagger generated by zOSMF is invalid because it has null in enum values for boolean.
// Remove once we know that zOSMF APARs for zOS2.2 and 2.3 is prerequisite
if (serviceId.equals("zosmf") || serviceId.equals("ibmzosmf")) {
try {
String apiDocContent = getApiDocContentByUrl(serviceId, apiDocUrl);
if (apiDocContent != null) {
apiDocContent = apiDocContent.replace("null, null", "true, false");
}
return new ApiDocInfo(apiInfo, apiDocContent, routes);
} catch (Exception e) {
log.error("Error retrieving api doc for '{}'", serviceId, e);
return getApiDocInfoBySubstituteSwagger(instanceInfo, routes, apiInfo);
}
}

String apiDocContent = "";
try {
apiDocContent = getApiDocContentByUrl(serviceId, apiDocUrl);
} catch (IOException | ParseException e) { //TODO: Is it handled correctly?
log.error("Error retrieving api doc for '{}'", serviceId, e);
} catch (IOException e) {
apimlLogger.log("org.zowe.apiml.apicatalog.apiDocHostCommunication", serviceId, e.getMessage());
log.debug("Error retrieving api doc for '{}'", serviceId, e);
}
return new ApiDocInfo(apiInfo, apiDocContent, routes);
}
Expand Down Expand Up @@ -310,7 +299,6 @@ private String getApiDocUrl(ApiInfo apiInfo, InstanceInfo instanceInfo, RoutedSe
return apiDocUrl;
}


/**
* Get ApiDoc content by Url
*
Expand All @@ -319,26 +307,21 @@ private String getApiDocUrl(ApiInfo apiInfo, InstanceInfo instanceInfo, RoutedSe
* @return the information about ApiDoc content as application/json
* @throws ApiDocNotFoundException if the response is error
*/
private String getApiDocContentByUrl(@NonNull String serviceId, String apiDocUrl) throws IOException, ParseException {
private String getApiDocContentByUrl(@NonNull String serviceId, String apiDocUrl) throws IOException {
HttpGet httpGet = new HttpGet(apiDocUrl);
httpGet.setHeader(org.apache.http.HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE);

String responseBody = null;
try {
CloseableHttpResponse response = secureHttpClientWithoutKeystore.execute(httpGet);
final HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
responseBody = EntityUtils.toString(responseEntity, StandardCharsets.UTF_8);
var responseCodeBodyPair = secureHttpClientWithoutKeystore.execute(httpGet, response -> {
var responseBody = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
return Pair.of(response.getCode(), responseBody);
}
if (response.getCode() != HttpStatus.SC_OK) {
throw new ApiDocNotFoundException("No API Documentation was retrieved due to " + serviceId +
" server error: '" + responseBody + "'.");
}
} catch (Exception e) {
log.error("Error retrieving api doc by url for {} at {}", serviceId, apiDocUrl, e);
throw e;
);

if (responseCodeBodyPair.getLeft() != HttpStatus.SC_OK) {
throw new ApiDocNotFoundException("No API Documentation was retrieved due to " + serviceId +
" server error: '" + responseCodeBodyPair.getRight() + "'.");
}
return responseBody;
return responseCodeBodyPair.getRight();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ messages:
reason: "The status of one or more containers could not be retrieved."
action: "Check the status of the message for more information and the health of the Discovery Service."

- key: org.zowe.apiml.apicatalog.apiDocHostCommunication
number: ZWEAC105
type: WARNING
text: "API Documentation not retrieved for service '%s' due to communication error, %s"
reason: "Unable to fetch API documentation."
action: "Make sure the service documentation url or server transport encoding is configured correctly."

# HTTP, Protocol messages
# 400-499\
# TLS, Certificate messages
Expand Down
Loading

0 comments on commit 3606dd6

Please sign in to comment.