Search in sources :

Example 1 with ApiEndpointValidationResponseDTO

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();
}
Also used : MalformedURLException(java.net.MalformedURLException) ServerConfiguration(org.wso2.carbon.base.ServerConfiguration) ApiEndpointValidationResponseDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ApiEndpointValidationResponseDTO) URL(java.net.URL) JSONException(org.json.JSONException) APIImportExportException(org.wso2.carbon.apimgt.impl.importexport.APIImportExportException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) SdkClientException(com.amazonaws.SdkClientException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) URISyntaxException(java.net.URISyntaxException) BadRequestException(org.wso2.carbon.apimgt.rest.api.util.exception.BadRequestException) CryptoException(org.wso2.carbon.core.util.CryptoException) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException) FaultGatewaysException(org.wso2.carbon.apimgt.api.FaultGatewaysException) APIMgtResourceAlreadyExistsException(org.wso2.carbon.apimgt.api.APIMgtResourceAlreadyExistsException) MonetizationException(org.wso2.carbon.apimgt.api.MonetizationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ParseException(org.json.simple.parser.ParseException) MalformedURLException(java.net.MalformedURLException)

Example 2 with ApiEndpointValidationResponseDTO

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;
}
Also used : UnknownHostException(java.net.UnknownHostException) HttpResponse(org.apache.http.HttpResponse) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) ApiEndpointValidationResponseDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ApiEndpointValidationResponseDTO) IOException(java.io.IOException) HttpHead(org.apache.http.client.methods.HttpHead)

Aggregations

IOException (java.io.IOException)2 UnknownHostException (java.net.UnknownHostException)2 ApiEndpointValidationResponseDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ApiEndpointValidationResponseDTO)2 SdkClientException (com.amazonaws.SdkClientException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 HttpResponse (org.apache.http.HttpResponse)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 HttpHead (org.apache.http.client.methods.HttpHead)1 JSONException (org.json.JSONException)1 ParseException (org.json.simple.parser.ParseException)1 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)1 APIMgtResourceAlreadyExistsException (org.wso2.carbon.apimgt.api.APIMgtResourceAlreadyExistsException)1 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)1 FaultGatewaysException (org.wso2.carbon.apimgt.api.FaultGatewaysException)1 MonetizationException (org.wso2.carbon.apimgt.api.MonetizationException)1 APIImportExportException (org.wso2.carbon.apimgt.impl.importexport.APIImportExportException)1 BadRequestException (org.wso2.carbon.apimgt.rest.api.util.exception.BadRequestException)1