Search in sources :

Example 6 with APIRuntimeArtifactDto

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

the class RuntimeArtifactGeneratorUtil method generateMetadataArtifact.

public static RuntimeArtifactDto generateMetadataArtifact(String tenantDomain, String apiId, String gatewayLabel) throws APIManagementException {
    List<APIRuntimeArtifactDto> gatewayArtifacts = getRuntimeArtifacts(apiId, gatewayLabel, tenantDomain);
    if (gatewayArtifacts != null) {
        try {
            MetadataDescriptorDto metadataDescriptorDto = new MetadataDescriptorDto();
            Map<String, ApiMetadataProjectDto> deploymentsMap = new HashMap<>();
            // "tempDirectory" is the root artifact directory
            File tempDirectory = CommonUtil.createTempDirectory(null);
            for (APIRuntimeArtifactDto apiRuntimeArtifactDto : gatewayArtifacts) {
                if (apiRuntimeArtifactDto.isFile()) {
                    String fileName = apiRuntimeArtifactDto.getApiId().concat("-").concat(apiRuntimeArtifactDto.getRevision());
                    ApiMetadataProjectDto apiProjectDto = deploymentsMap.get(fileName);
                    if (apiProjectDto == null) {
                        apiProjectDto = new ApiMetadataProjectDto();
                        deploymentsMap.put(fileName, apiProjectDto);
                        apiProjectDto.setApiFile(fileName);
                        apiProjectDto.setEnvironments(new HashSet<>());
                        apiProjectDto.setOrganizationId(apiRuntimeArtifactDto.getOrganization());
                        apiProjectDto.setVersion(apiRuntimeArtifactDto.getVersion());
                        apiProjectDto.setApiContext(apiRuntimeArtifactDto.getContext());
                    }
                    EnvironmentDto environment = new EnvironmentDto();
                    environment.setName(apiRuntimeArtifactDto.getLabel());
                    environment.setVhost(apiRuntimeArtifactDto.getVhost());
                    apiProjectDto.getEnvironments().add(environment);
                }
            }
            metadataDescriptorDto.setMetadataDescriptor(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, metadataDescriptorDto);
            RuntimeArtifactDto runtimeArtifactDto = new RuntimeArtifactDto();
            runtimeArtifactDto.setArtifact(new File(descriptorFile.concat(APIConstants.JSON_FILE_EXTENSION)));
            runtimeArtifactDto.setFile(true);
            return runtimeArtifactDto;
        } catch (APIImportExportException | IOException e) {
            throw new APIManagementException("Error while Generating API artifact", e);
        }
    } else {
        throw new APIManagementException("No API Artifacts", ExceptionCodes.NO_API_ARTIFACT_FOUND);
    }
}
Also used : MetadataDescriptorDto(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.dto.MetadataDescriptorDto) HashMap(java.util.HashMap) 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) ApiMetadataProjectDto(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.dto.ApiMetadataProjectDto) 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)

Example 7 with APIRuntimeArtifactDto

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

Example 10 with APIRuntimeArtifactDto

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

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