use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ApiEndpointValidationResponseDTO in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method validateEndpoint.
@Override
public Response validateEndpoint(String endpointUrl, String apiId, MessageContext messageContext) {
ApiEndpointValidationResponseDTO apiEndpointValidationResponseDTO = new ApiEndpointValidationResponseDTO();
apiEndpointValidationResponseDTO.setError("");
try {
URL url = new URL(endpointUrl);
if (url.getProtocol().matches("https")) {
ServerConfiguration serverConfig = CarbonUtils.getServerConfiguration();
String trustStorePath = serverConfig.getFirstProperty("Security.TrustStore.Location");
String trustStorePassword = serverConfig.getFirstProperty("Security.TrustStore.Password");
System.setProperty("javax.net.ssl.trustStore", trustStorePath);
System.setProperty("javax.net.ssl.trustStorePassword", trustStorePassword);
String keyStore = serverConfig.getFirstProperty("Security.KeyStore.Location");
String keyStoreType = serverConfig.getFirstProperty("Security.KeyStore.Type");
String keyStorePassword = serverConfig.getFirstProperty("Security.KeyStore.Password");
System.setProperty("javax.net.ssl.keyStoreType", keyStoreType);
System.setProperty("javax.net.ssl.keyStore", keyStore);
System.setProperty("javax.net.ssl.keyStorePassword", keyStorePassword);
/* apiId can be used to get the related API's uriTemplates. These uriTemplates can be used to extract
the API operations and append those operations separately to the API endpoint url. This edited url can
be used to test the endpoint, in case their is no valid url for the sole endpoint url provided. */
apiEndpointValidationResponseDTO = sendHttpHEADRequest(endpointUrl);
return Response.status(Response.Status.OK).entity(apiEndpointValidationResponseDTO).build();
} else if (url.getProtocol().matches("http")) {
apiEndpointValidationResponseDTO = sendHttpHEADRequest(endpointUrl);
return Response.status(Response.Status.OK).entity(apiEndpointValidationResponseDTO).build();
}
} catch (MalformedURLException e) {
log.error("Malformed Url error occurred while sending the HEAD request to the given endpoint url:", e);
apiEndpointValidationResponseDTO.setError(e.getMessage());
} catch (Exception e) {
RestApiUtil.handleInternalServerError("Error while testing the validity of API endpoint url " + "existence", e, log);
}
return Response.status(Response.Status.OK).entity(apiEndpointValidationResponseDTO).build();
}
use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ApiEndpointValidationResponseDTO in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method sendHttpHEADRequest.
/**
* Send HTTP HEAD request to test the endpoint url
*
* @param urlVal url for which the HEAD request is sent
* @return ApiEndpointValidationResponseDTO Response DTO containing validity information of the HEAD request made
* to test the endpoint url
*/
public static ApiEndpointValidationResponseDTO sendHttpHEADRequest(String urlVal) throws APIManagementException {
ApiEndpointValidationResponseDTO apiEndpointValidationResponseDTO = new ApiEndpointValidationResponseDTO();
org.apache.http.client.HttpClient client = APIUtil.getHttpClient(urlVal);
HttpHead method = new HttpHead(urlVal);
try {
HttpResponse response = client.execute(method);
apiEndpointValidationResponseDTO.setStatusCode(response.getStatusLine().getStatusCode());
apiEndpointValidationResponseDTO.setStatusMessage(HttpStatus.getStatusText(response.getStatusLine().getStatusCode()));
} catch (UnknownHostException e) {
log.error("UnknownHostException occurred while sending the HEAD request to the given endpoint url:", e);
apiEndpointValidationResponseDTO.setError("Unknown Host");
} catch (IOException e) {
log.error("Error occurred while sending the HEAD request to the given endpoint url:", e);
apiEndpointValidationResponseDTO.setError("Connection error");
} finally {
method.releaseConnection();
}
return apiEndpointValidationResponseDTO;
}
Aggregations