Search in sources :

Example 1 with ArtifactSynchronizerException

use of org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException in project carbon-apimgt by wso2.

the class DBRetriever method retrieveAllArtifacts.

@Override
public List<String> retrieveAllArtifacts(String label, String tenantDomain) throws ArtifactSynchronizerException {
    List<String> gatewayRuntimeArtifactsArray = new ArrayList<>();
    try {
        String endcodedgatewayLabel = URLEncoder.encode(label, APIConstants.DigestAuthConstants.CHARSET);
        String path = APIConstants.GatewayArtifactSynchronizer.GATEAY_SYNAPSE_ARTIFACTS + "?gatewayLabel=" + endcodedgatewayLabel + "&type=Synapse";
        String endpoint = baseURL + path;
        try (CloseableHttpResponse httpResponse = invokeService(endpoint, tenantDomain)) {
            JSONArray jsonArray = retrieveArtifact(httpResponse);
            if (jsonArray != null) {
                for (int i = 0; i < jsonArray.length(); i++) {
                    gatewayRuntimeArtifactsArray.add(jsonArray.getString(i));
                }
            }
        }
        return gatewayRuntimeArtifactsArray;
    } catch (IOException e) {
        String msg = "Error while executing the http client";
        log.error(msg, e);
        throw new ArtifactSynchronizerException(msg, e);
    }
}
Also used : ArtifactSynchronizerException(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException) ArrayList(java.util.ArrayList) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) JSONArray(org.json.JSONArray) IOException(java.io.IOException)

Example 2 with ArtifactSynchronizerException

use of org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException in project carbon-apimgt by wso2.

the class DBRetriever method retrieveArtifact.

@Override
public String retrieveArtifact(String apiId, String gatewayLabel) throws ArtifactSynchronizerException {
    String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
    try {
        Thread.sleep(gatewayArtifactSynchronizerProperties.getEventWaitingTime());
    } catch (InterruptedException e) {
        log.error("Error occurred while waiting to retrieve artifacts from event hub");
    }
    try {
        String encodedGatewayLabel = URLEncoder.encode(gatewayLabel, APIConstants.DigestAuthConstants.CHARSET);
        encodedGatewayLabel = encodedGatewayLabel.replace("\\+", "%20");
        String path = APIConstants.GatewayArtifactSynchronizer.GATEAY_SYNAPSE_ARTIFACTS + "?apiId=" + apiId + "&gatewayLabel=" + encodedGatewayLabel + "&type=Synapse";
        String endpoint = baseURL + path;
        try (CloseableHttpResponse httpResponse = invokeService(endpoint, tenantDomain)) {
            JSONArray jsonArray = retrieveArtifact(httpResponse);
            if (jsonArray != null && jsonArray.length() > 0) {
                return jsonArray.getString(0);
            }
        }
    } catch (IOException e) {
        String msg = "Error while executing the http client";
        log.error(msg, e);
        throw new ArtifactSynchronizerException(msg, e);
    }
    return null;
}
Also used : ArtifactSynchronizerException(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) JSONArray(org.json.JSONArray) IOException(java.io.IOException)

Example 3 with ArtifactSynchronizerException

use of org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException in project carbon-apimgt by wso2.

the class DBRetriever method invokeService.

private CloseableHttpResponse invokeService(String endpoint, String tenantDomain) throws IOException, ArtifactSynchronizerException {
    HttpGet method = new HttpGet(endpoint);
    URL url = new URL(endpoint);
    String username = eventHubConfigurationDto.getUsername();
    String password = eventHubConfigurationDto.getPassword();
    byte[] credentials = Base64.encodeBase64((username + APIConstants.DELEM_COLON + password).getBytes(APIConstants.DigestAuthConstants.CHARSET));
    int port = url.getPort();
    String protocol = url.getProtocol();
    method.setHeader(APIConstants.AUTHORIZATION_HEADER_DEFAULT, APIConstants.AUTHORIZATION_BASIC + new String(credentials, APIConstants.DigestAuthConstants.CHARSET));
    if (tenantDomain != null) {
        method.setHeader(APIConstants.HEADER_TENANT, tenantDomain);
    }
    HttpClient httpClient = APIUtil.getHttpClient(port, protocol);
    try {
        return APIUtil.executeHTTPRequest(method, httpClient);
    } catch (APIManagementException e) {
        throw new ArtifactSynchronizerException(e);
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ArtifactSynchronizerException(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException) HttpGet(org.apache.http.client.methods.HttpGet) HttpClient(org.apache.http.client.HttpClient) URL(java.net.URL)

Example 4 with ArtifactSynchronizerException

use of org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException in project carbon-apimgt by wso2.

the class DBRetriever method retrieveAttributes.

@Override
public Map<String, String> retrieveAttributes(String apiName, String version, String tenantDomain) throws ArtifactSynchronizerException {
    CloseableHttpResponse httpResponse = null;
    try {
        String endcodedVersion = URLEncoder.encode(version, APIConstants.DigestAuthConstants.CHARSET);
        String path = APIConstants.GatewayArtifactSynchronizer.SYNAPSE_ATTRIBUTES + "?apiName=" + apiName + "&tenantDomain=" + tenantDomain + "&version=" + endcodedVersion;
        String endpoint = baseURL + path;
        httpResponse = invokeService(endpoint, tenantDomain);
        String responseString;
        if (httpResponse.getEntity() != null) {
            responseString = EntityUtils.toString(httpResponse.getEntity(), APIConstants.DigestAuthConstants.CHARSET);
            httpResponse.close();
        } else {
            throw new ArtifactSynchronizerException("HTTP response is empty");
        }
        Map<String, String> apiAttribute = new HashMap<>();
        JSONObject artifactObject = new JSONObject(responseString);
        String label = null;
        String apiId = null;
        try {
            apiId = (String) artifactObject.get(APIConstants.GatewayArtifactSynchronizer.API_ID);
            String labelsStr = artifactObject.get(APIConstants.GatewayArtifactSynchronizer.LABELS).toString();
            Set<String> labelsSet = new Gson().fromJson(labelsStr, new TypeToken<HashSet<String>>() {
            }.getType());
            Set<String> gatewaySubscribedLabel = gatewayArtifactSynchronizerProperties.getGatewayLabels();
            if (!labelsSet.isEmpty() || !gatewaySubscribedLabel.isEmpty()) {
                labelsSet.retainAll(gatewaySubscribedLabel);
                if (!labelsSet.isEmpty()) {
                    label = labelsSet.iterator().next();
                }
            }
        } catch (ClassCastException e) {
            log.error("Unexpected response received from the storage." + e.getMessage());
        }
        apiAttribute.put(APIConstants.GatewayArtifactSynchronizer.API_ID, apiId);
        apiAttribute.put(APIConstants.GatewayArtifactSynchronizer.LABEL, label);
        return apiAttribute;
    } catch (IOException e) {
        String msg = "Error while executing the http client";
        log.error(msg, e);
        throw new ArtifactSynchronizerException(msg, e);
    }
}
Also used : JSONObject(org.json.JSONObject) ArtifactSynchronizerException(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException) HashMap(java.util.HashMap) TypeToken(com.google.gson.reflect.TypeToken) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Gson(com.google.gson.Gson) IOException(java.io.IOException)

Example 5 with ArtifactSynchronizerException

use of org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException in project carbon-apimgt by wso2.

the class EndpointCertificateDeployer method invokeService.

private CloseableHttpResponse invokeService(String endpoint, String tenantDomain) throws IOException, ArtifactSynchronizerException {
    HttpGet method = new HttpGet(endpoint);
    URL url = new URL(endpoint);
    String username = eventHubConfigurationDto.getUsername();
    String password = eventHubConfigurationDto.getPassword();
    byte[] credentials = Base64.encodeBase64((username + APIConstants.DELEM_COLON + password).getBytes(APIConstants.DigestAuthConstants.CHARSET));
    int port = url.getPort();
    String protocol = url.getProtocol();
    method.setHeader(APIConstants.AUTHORIZATION_HEADER_DEFAULT, APIConstants.AUTHORIZATION_BASIC + new String(credentials, APIConstants.DigestAuthConstants.CHARSET));
    if (tenantDomain != null) {
        method.setHeader(APIConstants.HEADER_TENANT, tenantDomain);
    }
    HttpClient httpClient = APIUtil.getHttpClient(port, protocol);
    try {
        return APIUtil.executeHTTPRequest(method, httpClient);
    } catch (APIManagementException e) {
        throw new ArtifactSynchronizerException(e);
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ArtifactSynchronizerException(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException) HttpGet(org.apache.http.client.methods.HttpGet) HttpClient(org.apache.http.client.HttpClient) URL(java.net.URL)

Aggregations

ArtifactSynchronizerException (org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException)22 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)12 IOException (java.io.IOException)7 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)6 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)6 Gson (com.google.gson.Gson)4 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)4 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)4 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)4 APIRevision (org.wso2.carbon.apimgt.api.model.APIRevision)4 APIImportExportException (org.wso2.carbon.apimgt.impl.importexport.APIImportExportException)4 File (java.io.File)3 URL (java.net.URL)3 HttpClient (org.apache.http.client.HttpClient)3 HttpGet (org.apache.http.client.methods.HttpGet)3 GatewayAPIDTO (org.wso2.carbon.apimgt.api.gateway.GatewayAPIDTO)3 APIProductIdentifier (org.wso2.carbon.apimgt.api.model.APIProductIdentifier)3 InMemoryAPIDeployer (org.wso2.carbon.apimgt.gateway.InMemoryAPIDeployer)3 APIGatewayAdmin (org.wso2.carbon.apimgt.gateway.service.APIGatewayAdmin)3 DeployAPIInGatewayEvent (org.wso2.carbon.apimgt.impl.notifier.events.DeployAPIInGatewayEvent)3