Search in sources :

Example 46 with Artifacts

use of org.wso2.ei.dashboard.core.rest.model.Artifacts in project carbon-apimgt by wso2.

the class GatewayStartupListener method deployArtifactsAtStartup.

private boolean deployArtifactsAtStartup(String tenantDomain) throws ArtifactSynchronizerException {
    GatewayArtifactSynchronizerProperties gatewayArtifactSynchronizerProperties = ServiceReferenceHolder.getInstance().getAPIManagerConfiguration().getGatewayArtifactSynchronizerProperties();
    boolean flag = false;
    long waitTime = System.currentTimeMillis() + 60 * 1000;
    long retryDuration = 5000;
    if (gatewayArtifactSynchronizerProperties.isRetrieveFromStorageEnabled()) {
        InMemoryAPIDeployer inMemoryAPIDeployer = new InMemoryAPIDeployer();
        while (waitTime > System.currentTimeMillis() && !flag) {
            flag = inMemoryAPIDeployer.deployAllAPIsAtGatewayStartup(gatewayArtifactSynchronizerProperties.getGatewayLabels(), tenantDomain);
            if (!flag) {
                log.error("Unable to deploy synapse artifacts at gateway. Next retry in " + (retryDuration / 1000) + " seconds");
                try {
                    Thread.sleep(retryDuration);
                } catch (InterruptedException ignore) {
                }
            }
        }
    }
    return flag;
}
Also used : GatewayArtifactSynchronizerProperties(org.wso2.carbon.apimgt.impl.dto.GatewayArtifactSynchronizerProperties) InMemoryAPIDeployer(org.wso2.carbon.apimgt.gateway.InMemoryAPIDeployer)

Example 47 with Artifacts

use of org.wso2.ei.dashboard.core.rest.model.Artifacts in project carbon-apimgt by wso2.

the class APIArtifactGeneratorUtil method generateAPIArtifact.

public static RuntimeArtifactDto generateAPIArtifact(List<String> apiUuids, String name, String version, String gatewayLabel, String type, String tenantDomain) throws APIManagementException {
    GatewayArtifactGenerator gatewayArtifactGenerator = ServiceReferenceHolder.getInstance().getGatewayArtifactGenerator(type);
    if (gatewayArtifactGenerator != null) {
        List<APIRuntimeArtifactDto> gatewayArtifacts;
        if (StringUtils.isNotEmpty(gatewayLabel)) {
            byte[] decodedValue = Base64.decodeBase64(gatewayLabel.getBytes());
            String[] gatewayLabels = new String(decodedValue).split("\\|");
            if (!apiUuids.isEmpty()) {
                gatewayArtifacts = gatewayArtifactsMgtDAO.retrieveGatewayArtifactsByAPIIDs(apiUuids, gatewayLabels, tenantDomain);
            } else {
                gatewayArtifacts = gatewayArtifactsMgtDAO.retrieveGatewayArtifactsByLabel(gatewayLabels, tenantDomain);
            }
        } else {
            gatewayArtifacts = gatewayArtifactsMgtDAO.retrieveGatewayArtifacts(tenantDomain);
        }
        if (gatewayArtifacts != null) {
            if (gatewayArtifacts.isEmpty()) {
                throw new APIManagementException("No API Artifacts", ExceptionCodes.NO_API_ARTIFACT_FOUND);
            }
            for (APIRuntimeArtifactDto apiRuntimeArtifactDto : gatewayArtifacts) {
                String organizationId = gatewayArtifactsMgtDAO.retrieveOrganization(apiRuntimeArtifactDto.getApiId());
                if (organizationId != null) {
                    apiRuntimeArtifactDto.setOrganization(organizationId);
                }
            }
        }
        if (gatewayArtifacts == null || gatewayArtifacts.isEmpty()) {
            return null;
        }
        return gatewayArtifactGenerator.generateGatewayArtifact(gatewayArtifacts);
    } else {
        Set<String> gatewayArtifactGeneratorTypes = ServiceReferenceHolder.getInstance().getGatewayArtifactGeneratorTypes();
        throw new APIManagementException("Couldn't find gateway Type", ExceptionCodes.from(ExceptionCodes.GATEWAY_TYPE_NOT_FOUND, String.join(",", gatewayArtifactGeneratorTypes)));
    }
}
Also used : APIRuntimeArtifactDto(org.wso2.carbon.apimgt.impl.dto.APIRuntimeArtifactDto) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException)

Example 48 with Artifacts

use of org.wso2.ei.dashboard.core.rest.model.Artifacts in project carbon-apimgt by wso2.

the class GatewayArtifactsMgtDAO method retrieveGatewayArtifactsByLabel.

public List<APIRuntimeArtifactDto> retrieveGatewayArtifactsByLabel(String[] labels, String tenantDomain) throws APIManagementException {
    String query = SQLConstants.RETRIEVE_ARTIFACTS_BY_LABEL;
    query = query.replaceAll(SQLConstants.GATEWAY_LABEL_REGEX, String.join(",", Collections.nCopies(labels.length, "?")));
    List<APIRuntimeArtifactDto> apiRuntimeArtifactDtoList = new ArrayList<>();
    try (Connection connection = GatewayArtifactsMgtDBUtil.getArtifactSynchronizerConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(query)) {
        int index = 1;
        for (String label : labels) {
            preparedStatement.setString(index, label);
            index++;
        }
        preparedStatement.setString(index, tenantDomain);
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                String apiId = resultSet.getString("API_ID");
                String label = resultSet.getString("LABEL");
                try {
                    APIRuntimeArtifactDto apiRuntimeArtifactDto = new APIRuntimeArtifactDto();
                    apiRuntimeArtifactDto.setTenantDomain(resultSet.getString("TENANT_DOMAIN"));
                    apiRuntimeArtifactDto.setApiId(apiId);
                    String resolvedVhost = VHostUtils.resolveIfNullToDefaultVhost(label, resultSet.getString("VHOST"));
                    apiRuntimeArtifactDto.setLabel(label);
                    apiRuntimeArtifactDto.setVhost(resolvedVhost);
                    apiRuntimeArtifactDto.setName(resultSet.getString("API_NAME"));
                    apiRuntimeArtifactDto.setVersion(resultSet.getString("API_VERSION"));
                    apiRuntimeArtifactDto.setProvider(resultSet.getString("API_PROVIDER"));
                    apiRuntimeArtifactDto.setRevision(resultSet.getString("REVISION_ID"));
                    apiRuntimeArtifactDto.setType(resultSet.getString("API_TYPE"));
                    apiRuntimeArtifactDto.setContext(resultSet.getString("CONTEXT"));
                    InputStream artifact = resultSet.getBinaryStream("ARTIFACT");
                    if (artifact != null) {
                        byte[] artifactByte = APIMgtDBUtil.getBytesFromInputStream(artifact);
                        try (InputStream newArtifact = new ByteArrayInputStream(artifactByte)) {
                            apiRuntimeArtifactDto.setArtifact(newArtifact);
                        }
                    }
                    apiRuntimeArtifactDto.setFile(true);
                    apiRuntimeArtifactDtoList.add(apiRuntimeArtifactDto);
                } catch (APIManagementException e) {
                    // handle exception inside the loop and continue with other API artifacts
                    log.error(String.format("Error resolving vhost while retrieving runtime artifact for API %s, " + "gateway environment \"%s\", tenant: \"%s\"." + "Skipping runtime artifact for the API.", apiId, label, tenantDomain), e);
                } catch (IOException e) {
                    // handle exception inside the loop and continue with other API artifacts
                    log.error(String.format("Error occurred retrieving input stream from byte array of " + "API: %s, gateway environment \"%s\", tenant: \"%s\".", apiId, label, tenantDomain), e);
                } catch (SQLException e) {
                    // handle exception inside the loop and continue with other API artifacts
                    log.error(String.format("Failed to retrieve Gateway Artifact of API: %s, " + "gateway environment \"%s\", tenant: \"%s\".", apiId, label, tenantDomain), e);
                }
            }
        }
    } catch (SQLException e) {
        handleException("Failed to retrieve Gateway Artifact for labels : " + StringUtils.join(",", labels), e);
    }
    return apiRuntimeArtifactDtoList;
}
Also used : SQLException(java.sql.SQLException) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) APIRuntimeArtifactDto(org.wso2.carbon.apimgt.impl.dto.APIRuntimeArtifactDto) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ByteArrayInputStream(java.io.ByteArrayInputStream) ResultSet(java.sql.ResultSet)

Example 49 with Artifacts

use of org.wso2.ei.dashboard.core.rest.model.Artifacts in project carbon-apimgt by wso2.

the class GatewayArtifactsMgtDAO method retrieveGatewayArtifactsByAPIIDAndLabel.

public List<APIRuntimeArtifactDto> retrieveGatewayArtifactsByAPIIDAndLabel(String apiId, String[] labels, String tenantDomain) throws APIManagementException {
    String query = SQLConstants.RETRIEVE_ARTIFACTS_BY_APIID_AND_LABEL;
    query = query.replaceAll(SQLConstants.GATEWAY_LABEL_REGEX, String.join(",", Collections.nCopies(labels.length, "?")));
    List<APIRuntimeArtifactDto> apiRuntimeArtifactDtoList = new ArrayList<>();
    try (Connection connection = GatewayArtifactsMgtDBUtil.getArtifactSynchronizerConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(query)) {
        preparedStatement.setString(1, apiId);
        int index = 2;
        for (String label : labels) {
            preparedStatement.setString(index, label);
            index++;
        }
        preparedStatement.setString(index, tenantDomain);
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                APIRuntimeArtifactDto apiRuntimeArtifactDto = new APIRuntimeArtifactDto();
                apiRuntimeArtifactDto.setTenantDomain(resultSet.getString("TENANT_DOMAIN"));
                apiRuntimeArtifactDto.setApiId(apiId);
                String label = resultSet.getString("LABEL");
                // Do not handle the exception here since runtime artifacts are retrieved by API UUID
                // throw the exception here.
                String resolvedVhost = VHostUtils.resolveIfNullToDefaultVhost(label, resultSet.getString("VHOST"));
                apiRuntimeArtifactDto.setLabel(label);
                apiRuntimeArtifactDto.setVhost(resolvedVhost);
                apiRuntimeArtifactDto.setName(resultSet.getString("API_NAME"));
                apiRuntimeArtifactDto.setVersion(resultSet.getString("API_VERSION"));
                apiRuntimeArtifactDto.setProvider(resultSet.getString("API_PROVIDER"));
                apiRuntimeArtifactDto.setRevision(resultSet.getString("REVISION_ID"));
                apiRuntimeArtifactDto.setType(resultSet.getString("API_TYPE"));
                apiRuntimeArtifactDto.setContext(resultSet.getString("CONTEXT"));
                InputStream artifact = resultSet.getBinaryStream("ARTIFACT");
                if (artifact != null) {
                    byte[] artifactByte = APIMgtDBUtil.getBytesFromInputStream(artifact);
                    try (InputStream newArtifact = new ByteArrayInputStream(artifactByte)) {
                        apiRuntimeArtifactDto.setArtifact(newArtifact);
                    } catch (IOException e) {
                        // Do not handle the exception here since runtime artifacts are retrieved by API UUID
                        // throw the exception here.
                        handleException("Error occurred retrieving input stream from byte array.", e);
                    }
                }
                apiRuntimeArtifactDto.setFile(true);
                apiRuntimeArtifactDtoList.add(apiRuntimeArtifactDto);
            }
        }
    } catch (SQLException e) {
        handleException("Failed to retrieve Gateway Artifact for Api : " + apiId + " and labels: " + StringUtils.join(",", labels), e);
    }
    return apiRuntimeArtifactDtoList;
}
Also used : SQLException(java.sql.SQLException) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) APIRuntimeArtifactDto(org.wso2.carbon.apimgt.impl.dto.APIRuntimeArtifactDto) ByteArrayInputStream(java.io.ByteArrayInputStream) ResultSet(java.sql.ResultSet)

Example 50 with Artifacts

use of org.wso2.ei.dashboard.core.rest.model.Artifacts in project carbon-apimgt by wso2.

the class GatewayArtifactsMgtDAO method retrieveGatewayArtifactsByAPIIDs.

public List<APIRuntimeArtifactDto> retrieveGatewayArtifactsByAPIIDs(List<String> apiIds, String[] labels, String tenantDomain) throws APIManagementException {
    // Split apiId list into smaller list of size 25
    List<List<String>> apiIdsChunk = new ArrayList<>();
    int apiIdListSize = apiIds.size();
    int apiIdArrayIndex = 0;
    int apiIdsChunkSize = SQLConstants.API_ID_CHUNK_SIZE;
    while (apiIdArrayIndex < apiIdListSize) {
        apiIdsChunk.add(apiIds.subList(apiIdArrayIndex, Math.min(apiIdArrayIndex + apiIdsChunkSize, apiIdListSize)));
        apiIdArrayIndex += apiIdsChunkSize;
    }
    List<APIRuntimeArtifactDto> apiRuntimeArtifactDtoList = new ArrayList<>();
    for (List<String> apiIdList : apiIdsChunk) {
        String query = SQLConstants.RETRIEVE_ARTIFACTS_BY_MULTIPLE_APIIDs_AND_LABEL;
        query = query.replaceAll(SQLConstants.GATEWAY_LABEL_REGEX, String.join(",", Collections.nCopies(labels.length, "?")));
        query = query.replaceAll(SQLConstants.API_ID_REGEX, String.join(",", Collections.nCopies(apiIdList.size(), "?")));
        try (Connection connection = GatewayArtifactsMgtDBUtil.getArtifactSynchronizerConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(query)) {
            int index = 1;
            for (String apiId : apiIdList) {
                preparedStatement.setString(index, apiId);
                index++;
            }
            for (String label : labels) {
                preparedStatement.setString(index, label);
                index++;
            }
            preparedStatement.setString(index, tenantDomain);
            try (ResultSet resultSet = preparedStatement.executeQuery()) {
                while (resultSet.next()) {
                    APIRuntimeArtifactDto apiRuntimeArtifactDto = new APIRuntimeArtifactDto();
                    apiRuntimeArtifactDto.setTenantDomain(resultSet.getString("TENANT_DOMAIN"));
                    String apiId = resultSet.getString("API_ID");
                    apiRuntimeArtifactDto.setApiId(apiId);
                    String label = resultSet.getString("LABEL");
                    String resolvedVhost = VHostUtils.resolveIfNullToDefaultVhost(label, resultSet.getString("VHOST"));
                    apiRuntimeArtifactDto.setLabel(label);
                    apiRuntimeArtifactDto.setVhost(resolvedVhost);
                    apiRuntimeArtifactDto.setName(resultSet.getString("API_NAME"));
                    apiRuntimeArtifactDto.setVersion(resultSet.getString("API_VERSION"));
                    apiRuntimeArtifactDto.setProvider(resultSet.getString("API_PROVIDER"));
                    apiRuntimeArtifactDto.setRevision(resultSet.getString("REVISION_ID"));
                    apiRuntimeArtifactDto.setType(resultSet.getString("API_TYPE"));
                    apiRuntimeArtifactDto.setContext(resultSet.getString("CONTEXT"));
                    InputStream artifact = resultSet.getBinaryStream("ARTIFACT");
                    if (artifact != null) {
                        byte[] artifactByte = APIMgtDBUtil.getBytesFromInputStream(artifact);
                        try (InputStream newArtifact = new ByteArrayInputStream(artifactByte)) {
                            apiRuntimeArtifactDto.setArtifact(newArtifact);
                        } catch (IOException e) {
                            // Do not handle the exception here since runtime artifacts are retrieved by API UUID
                            // throw the exception here.
                            handleException("Error occurred retrieving input stream from byte array.", e);
                        }
                    }
                    apiRuntimeArtifactDto.setFile(true);
                    apiRuntimeArtifactDtoList.add(apiRuntimeArtifactDto);
                }
            }
        } catch (SQLException e) {
            handleException("Failed to retrieve Gateway Artifact for Apis : " + apiIdList + " and labels: " + StringUtils.join(",", labels), e);
        }
    }
    return apiRuntimeArtifactDtoList;
}
Also used : SQLException(java.sql.SQLException) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) APIRuntimeArtifactDto(org.wso2.carbon.apimgt.impl.dto.APIRuntimeArtifactDto) ByteArrayInputStream(java.io.ByteArrayInputStream) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

File (java.io.File)23 IOException (java.io.IOException)18 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)18 ArrayList (java.util.ArrayList)17 CAppArtifacts (org.wso2.ei.dashboard.core.rest.model.CAppArtifacts)16 Operation (io.swagger.v3.oas.annotations.Operation)15 ApiResponse (io.swagger.v3.oas.annotations.responses.ApiResponse)15 ApiResponses (io.swagger.v3.oas.annotations.responses.ApiResponses)15 Response (javax.ws.rs.core.Response)15 Artifact (org.wso2.carbon.application.deployer.config.Artifact)15 Artifacts (org.wso2.ei.dashboard.core.rest.model.Artifacts)15 DeploymentException (org.apache.axis2.deployment.DeploymentException)11 CappFile (org.wso2.carbon.application.deployer.config.CappFile)11 Deployer (org.apache.axis2.deployment.Deployer)10 ArtifactSynchronizerException (org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException)10 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)10 InputStream (java.io.InputStream)9 FileInputStream (java.io.FileInputStream)7 APIRuntimeArtifactDto (org.wso2.carbon.apimgt.impl.dto.APIRuntimeArtifactDto)7 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)7