Search in sources :

Example 1 with ArtifactRetriever

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

the class InMemoryAPIDeployer method deployAllAPIsAtGatewayStartup.

/**
 * Deploy an API in the gateway using the deployAPI method in gateway admin.
 *
 * @param assignedGatewayLabels - The labels which the gateway subscribed to
 * @param tenantDomain          tenantDomain of API.
 * @return True if all API artifacts retrieved from the storage and successfully deployed without any error. else
 * false
 */
public boolean deployAllAPIsAtGatewayStartup(Set<String> assignedGatewayLabels, String tenantDomain) throws ArtifactSynchronizerException {
    boolean result = false;
    if (gatewayArtifactSynchronizerProperties.isRetrieveFromStorageEnabled()) {
        if (artifactRetriever != null) {
            try {
                int errorCount = 0;
                String labelString = String.join("|", assignedGatewayLabels);
                String encodedString = Base64.encodeBase64URLSafeString(labelString.getBytes());
                APIGatewayAdmin apiGatewayAdmin = new APIGatewayAdmin();
                MessageContext.setCurrentMessageContext(org.wso2.carbon.apimgt.gateway.utils.GatewayUtils.createAxis2MessageContext());
                PrivilegedCarbonContext.startTenantFlow();
                PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
                List<String> gatewayRuntimeArtifacts = ServiceReferenceHolder.getInstance().getArtifactRetriever().retrieveAllArtifacts(encodedString, tenantDomain);
                if (gatewayRuntimeArtifacts.size() == 0) {
                    return true;
                }
                for (String runtimeArtifact : gatewayRuntimeArtifacts) {
                    GatewayAPIDTO gatewayAPIDTO = null;
                    try {
                        if (StringUtils.isNotEmpty(runtimeArtifact)) {
                            gatewayAPIDTO = new Gson().fromJson(runtimeArtifact, GatewayAPIDTO.class);
                            log.info("Deploying synapse artifacts of " + gatewayAPIDTO.getName());
                            apiGatewayAdmin.deployAPI(gatewayAPIDTO);
                            addDeployedCertificatesToAPIAssociation(gatewayAPIDTO);
                            addDeployedGraphqlQLToAPI(gatewayAPIDTO);
                            DataHolder.getInstance().addKeyManagerToAPIMapping(gatewayAPIDTO.getApiId(), gatewayAPIDTO.getKeyManagers());
                        }
                    } catch (AxisFault axisFault) {
                        log.error("Error in deploying " + gatewayAPIDTO.getName() + " to the Gateway ", axisFault);
                        errorCount++;
                    }
                }
                // reload dynamic profiles to avoid delays in loading certs in mutual ssl enabled APIs upon
                // server restart
                DynamicProfileReloaderHolder.getInstance().reloadAllHandlers();
                if (debugEnabled) {
                    log.debug("APIs deployed in gateway with the labels of " + labelString);
                }
                result = true;
                // Setting the result to false only if all the API deployments are failed
                if (gatewayRuntimeArtifacts.size() == errorCount) {
                    return false;
                }
            } catch (ArtifactSynchronizerException | AxisFault e) {
                String msg = "Error deploying APIs to the Gateway ";
                log.error(msg, e);
                return false;
            } finally {
                MessageContext.destroyCurrentMessageContext();
                PrivilegedCarbonContext.endTenantFlow();
            }
        } else {
            String msg = "Artifact retriever not found";
            log.error(msg);
            throw new ArtifactSynchronizerException(msg);
        }
    }
    return result;
}
Also used : GatewayAPIDTO(org.wso2.carbon.apimgt.api.gateway.GatewayAPIDTO) AxisFault(org.apache.axis2.AxisFault) ArtifactSynchronizerException(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException) Gson(com.google.gson.Gson) APIGatewayAdmin(org.wso2.carbon.apimgt.gateway.service.APIGatewayAdmin)

Example 2 with ArtifactRetriever

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

the class InMemoryAPIDeployer method retrieveArtifact.

private GatewayAPIDTO retrieveArtifact(String apiId, Set<String> gatewayLabels) throws ArtifactSynchronizerException {
    GatewayAPIDTO result;
    String labelString = String.join("|", gatewayLabels);
    String encodedString = Base64.encodeBase64URLSafeString(labelString.getBytes());
    if (artifactRetriever != null) {
        try {
            String gatewayRuntimeArtifact = artifactRetriever.retrieveArtifact(apiId, encodedString);
            if (StringUtils.isNotEmpty(gatewayRuntimeArtifact)) {
                result = new Gson().fromJson(gatewayRuntimeArtifact, GatewayAPIDTO.class);
            } else {
                String msg = "Error retrieving artifacts for API " + apiId + ". Storage returned null";
                log.error(msg);
                throw new ArtifactSynchronizerException(msg);
            }
        } catch (ArtifactSynchronizerException e) {
            String msg = "Error deploying " + apiId + " in Gateway";
            log.error(msg, e);
            throw new ArtifactSynchronizerException(msg, e);
        }
    } else {
        String msg = "Artifact retriever not found";
        log.error(msg);
        throw new ArtifactSynchronizerException(msg);
    }
    return result;
}
Also used : GatewayAPIDTO(org.wso2.carbon.apimgt.api.gateway.GatewayAPIDTO) ArtifactSynchronizerException(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException) Gson(com.google.gson.Gson)

Aggregations

Gson (com.google.gson.Gson)2 GatewayAPIDTO (org.wso2.carbon.apimgt.api.gateway.GatewayAPIDTO)2 ArtifactSynchronizerException (org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException)2 AxisFault (org.apache.axis2.AxisFault)1 APIGatewayAdmin (org.wso2.carbon.apimgt.gateway.service.APIGatewayAdmin)1