Search in sources :

Example 1 with APIRuntimeArtifactDto

use of org.wso2.carbon.apimgt.impl.dto.APIRuntimeArtifactDto in project carbon-apimgt by wso2.

the class MicroGatewayArtifactGenerator method generateGatewayArtifact.

@Override
public RuntimeArtifactDto generateGatewayArtifact(List<APIRuntimeArtifactDto> apiRuntimeArtifactDtoList) throws APIManagementException {
    try {
        DeploymentDescriptorDto descriptorDto = new DeploymentDescriptorDto();
        Map<String, ApiProjectDto> deploymentsMap = new HashMap<>();
        // "tempDirectory" is the root artifact directory
        File tempDirectory = CommonUtil.createTempDirectory(null);
        for (APIRuntimeArtifactDto apiRuntimeArtifactDto : apiRuntimeArtifactDtoList) {
            if (apiRuntimeArtifactDto.isFile()) {
                InputStream artifact = (InputStream) apiRuntimeArtifactDto.getArtifact();
                String fileName = apiRuntimeArtifactDto.getApiId().concat("-").concat(apiRuntimeArtifactDto.getRevision()).concat(APIConstants.ZIP_FILE_EXTENSION);
                Path path = Paths.get(tempDirectory.getAbsolutePath(), fileName);
                FileUtils.copyInputStreamToFile(artifact, path.toFile());
                ApiProjectDto apiProjectDto = deploymentsMap.get(fileName);
                if (apiProjectDto == null) {
                    apiProjectDto = new ApiProjectDto();
                    deploymentsMap.put(fileName, apiProjectDto);
                    apiProjectDto.setApiFile(fileName);
                    apiProjectDto.setEnvironments(new HashSet<>());
                    apiProjectDto.setOrganizationId(apiRuntimeArtifactDto.getOrganization());
                }
                // environment is unique for a revision in a deployment
                // create new environment
                EnvironmentDto environment = new EnvironmentDto();
                environment.setName(apiRuntimeArtifactDto.getLabel());
                environment.setVhost(apiRuntimeArtifactDto.getVhost());
                // ignored if the name of the environment is same
                apiProjectDto.getEnvironments().add(environment);
            }
        }
        descriptorDto.setDeployments(new HashSet<>(deploymentsMap.values()));
        String descriptorFile = Paths.get(tempDirectory.getAbsolutePath(), APIConstants.GatewayArtifactConstants.DEPLOYMENT_DESCRIPTOR_FILE).toString();
        CommonUtil.writeDtoToFile(descriptorFile, ExportFormat.JSON, APIConstants.GatewayArtifactConstants.DEPLOYMENT_DESCRIPTOR_FILE_TYPE, descriptorDto);
        // adding env_properties.json
        Map<String, Map<String, Environment>> environmentSpecificAPIProperties = getEnvironmentSpecificAPIProperties(apiRuntimeArtifactDtoList);
        String environmentSpecificAPIPropertyFile = Paths.get(tempDirectory.getAbsolutePath(), APIConstants.GatewayArtifactConstants.ENVIRONMENT_SPECIFIC_API_PROPERTY_FILE).toString();
        CommonUtil.writeDtoToFile(environmentSpecificAPIPropertyFile, ExportFormat.JSON, APIConstants.GatewayArtifactConstants.ENVIRONMENT_SPECIFIC_API_PROPERTY_FILE, APIConstants.GatewayArtifactConstants.ENVIRONMENT_SPECIFIC_API_PROPERTY_KEY_NAME, environmentSpecificAPIProperties);
        CommonUtil.archiveDirectory(tempDirectory.getAbsolutePath());
        FileUtils.deleteQuietly(tempDirectory);
        RuntimeArtifactDto runtimeArtifactDto = new RuntimeArtifactDto();
        runtimeArtifactDto.setArtifact(new File(tempDirectory.getAbsolutePath() + APIConstants.ZIP_FILE_EXTENSION));
        runtimeArtifactDto.setFile(true);
        return runtimeArtifactDto;
    } catch (APIImportExportException | IOException e) {
        throw new APIManagementException("Error while Generating API artifact", e);
    }
}
Also used : Path(java.nio.file.Path) HashMap(java.util.HashMap) InputStream(java.io.InputStream) EnvironmentDto(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.dto.EnvironmentDto) RuntimeArtifactDto(org.wso2.carbon.apimgt.impl.dto.RuntimeArtifactDto) APIRuntimeArtifactDto(org.wso2.carbon.apimgt.impl.dto.APIRuntimeArtifactDto) IOException(java.io.IOException) ApiProjectDto(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.dto.ApiProjectDto) APIRuntimeArtifactDto(org.wso2.carbon.apimgt.impl.dto.APIRuntimeArtifactDto) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIImportExportException(org.wso2.carbon.apimgt.impl.importexport.APIImportExportException) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) DeploymentDescriptorDto(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.dto.DeploymentDescriptorDto)

Example 2 with APIRuntimeArtifactDto

use of org.wso2.carbon.apimgt.impl.dto.APIRuntimeArtifactDto in project carbon-apimgt by wso2.

the class RuntimeArtifactGeneratorUtil method getRuntimeArtifacts.

private static List<APIRuntimeArtifactDto> getRuntimeArtifacts(String apiId, String gatewayLabel, String tenantDomain) throws APIManagementException {
    List<APIRuntimeArtifactDto> gatewayArtifacts;
    if (StringUtils.isNotEmpty(gatewayLabel)) {
        byte[] decodedValue = Base64.decodeBase64(gatewayLabel.getBytes());
        String[] gatewayLabels = new String(decodedValue).split("\\|");
        if (StringUtils.isNotEmpty(apiId)) {
            gatewayArtifacts = gatewayArtifactsMgtDAO.retrieveGatewayArtifactsByAPIIDAndLabel(apiId, 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 gatewayArtifacts;
}
Also used : APIRuntimeArtifactDto(org.wso2.carbon.apimgt.impl.dto.APIRuntimeArtifactDto) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException)

Example 3 with APIRuntimeArtifactDto

use of org.wso2.carbon.apimgt.impl.dto.APIRuntimeArtifactDto in project carbon-apimgt by wso2.

the class GatewayArtifactsMgtDAO method retrieveGatewayArtifacts.

public List<APIRuntimeArtifactDto> retrieveGatewayArtifacts(String tenantDomain) throws APIManagementException {
    String query = SQLConstants.RETRIEVE_ARTIFACTS;
    List<APIRuntimeArtifactDto> apiRuntimeArtifactDtoList = new ArrayList<>();
    try (Connection connection = GatewayArtifactsMgtDBUtil.getArtifactSynchronizerConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(query)) {
        preparedStatement.setString(1, 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 Artifacts.", 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 4 with APIRuntimeArtifactDto

use of org.wso2.carbon.apimgt.impl.dto.APIRuntimeArtifactDto 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 5 with APIRuntimeArtifactDto

use of org.wso2.carbon.apimgt.impl.dto.APIRuntimeArtifactDto 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)

Aggregations

APIRuntimeArtifactDto (org.wso2.carbon.apimgt.impl.dto.APIRuntimeArtifactDto)11 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)7 IOException (java.io.IOException)6 InputStream (java.io.InputStream)6 File (java.io.File)5 ArrayList (java.util.ArrayList)5 RuntimeArtifactDto (org.wso2.carbon.apimgt.impl.dto.RuntimeArtifactDto)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 FileInputStream (java.io.FileInputStream)4 Connection (java.sql.Connection)4 PreparedStatement (java.sql.PreparedStatement)4 ResultSet (java.sql.ResultSet)4 SQLException (java.sql.SQLException)4 HashMap (java.util.HashMap)4 API (org.wso2.carbon.apimgt.api.model.API)3 URL (java.net.URL)2 Test (org.junit.Test)2 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)2 EnvironmentDto (org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.dto.EnvironmentDto)2 APIImportExportException (org.wso2.carbon.apimgt.impl.importexport.APIImportExportException)2