Search in sources :

Example 21 with VHost

use of org.wso2.carbon.apimgt.api.model.VHost 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 22 with VHost

use of org.wso2.carbon.apimgt.api.model.VHost 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)

Example 23 with VHost

use of org.wso2.carbon.apimgt.api.model.VHost in project carbon-apimgt by wso2.

the class ExportUtils method addGatewayEnvironmentsToArchive.

/**
 * Retrieve the deployed gateway environments and store those in the archive directory.
 *
 * @param archivePath  File path to export the endpoint certificates
 * @param apiID        UUID of the API/ API Product
 * @param exportFormat Export format of file
 * @param apiProvider  API Provider
 * @throws APIImportExportException If an error occurs while exporting gateway environments
 */
public static void addGatewayEnvironmentsToArchive(String archivePath, String apiID, ExportFormat exportFormat, APIProvider apiProvider) throws APIManagementException {
    try {
        List<APIRevisionDeployment> deploymentsList = apiProvider.getAPIRevisionDeploymentList(apiID);
        JsonArray deploymentsArray = new JsonArray();
        for (APIRevisionDeployment deployment : deploymentsList) {
            JsonObject deploymentObject = new JsonObject();
            // Do not set vhost in deployment environment file when export API (or API Project)
            // So when importing the exported API, the default vhost of the new environment is selected.
            deploymentObject.addProperty(ImportExportConstants.DEPLOYMENT_NAME, deployment.getDeployment());
            deploymentObject.addProperty(ImportExportConstants.DISPLAY_ON_DEVPORTAL_OPTION, deployment.isDisplayOnDevportal());
            deploymentsArray.add(deploymentObject);
        }
        if (deploymentsArray.size() > 0) {
            CommonUtil.writeDtoToFile(archivePath + ImportExportConstants.DEPLOYMENT_INFO_LOCATION, exportFormat, ImportExportConstants.TYPE_DEPLOYMENT_ENVIRONMENTS, deploymentsArray);
        }
    } catch (APIImportExportException e) {
        throw new APIManagementException("Error in converting deployment environment details to JSON object in API: " + apiID, e);
    } catch (IOException e) {
        throw new APIManagementException("Error while saving deployment environment details for API: " + apiID + " as YAML", e);
    }
}
Also used : JsonArray(com.google.gson.JsonArray) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIImportExportException(org.wso2.carbon.apimgt.impl.importexport.APIImportExportException) JsonObject(com.google.gson.JsonObject) APIRevisionDeployment(org.wso2.carbon.apimgt.api.model.APIRevisionDeployment) IOException(java.io.IOException)

Example 24 with VHost

use of org.wso2.carbon.apimgt.api.model.VHost in project carbon-apimgt by wso2.

the class ApiMgtDAO method addGatewayVhosts.

/**
 * Add VHost assigned to gateway environment
 *
 * @param connection connection
 * @param id         Environment ID in the databse
 * @param vhosts     list of VHosts assigned to the environment
 * @throws APIManagementException if falied to add VHosts
 */
private void addGatewayVhosts(Connection connection, int id, List<VHost> vhosts) throws APIManagementException {
    try (PreparedStatement prepStmt = connection.prepareStatement(SQLConstants.INSERT_GATEWAY_VHOSTS_SQL)) {
        for (VHost vhost : vhosts) {
            prepStmt.setInt(1, id);
            prepStmt.setString(2, vhost.getHost());
            prepStmt.setString(3, vhost.getHttpContext());
            prepStmt.setString(4, vhost.getHttpPort().toString());
            prepStmt.setString(5, vhost.getHttpsPort().toString());
            prepStmt.setString(6, vhost.getWsPort().toString());
            prepStmt.setString(7, vhost.getWssPort().toString());
            prepStmt.addBatch();
        }
        prepStmt.executeBatch();
    } catch (SQLException e) {
        handleException("Failed to add VHosts for environment ID: " + id, e);
    }
}
Also used : VHost(org.wso2.carbon.apimgt.api.model.VHost) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement)

Example 25 with VHost

use of org.wso2.carbon.apimgt.api.model.VHost in project carbon-apimgt by wso2.

the class ApiMgtDAO method addDeployedAPIRevision.

/**
 * Adds an deployed API revision to the database
 *
 * @param deployedAPIRevisionList content of the revision deployment mapping objects
 * @throws APIManagementException if an error occurs when adding a new API revision
 */
public void addDeployedAPIRevision(String apiRevisionId, List<DeployedAPIRevision> deployedAPIRevisionList) throws APIManagementException {
    if (deployedAPIRevisionList.size() > 0) {
        try (Connection connection = APIMgtDBUtil.getConnection()) {
            connection.setAutoCommit(false);
            // Adding to AM_DEPLOYED_REVISION table
            try (PreparedStatement statement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.ADD_DEPLOYED_API_REVISION)) {
                for (DeployedAPIRevision deployedAPIRevision : deployedAPIRevisionList) {
                    String envName = deployedAPIRevision.getDeployment();
                    String vhost = deployedAPIRevision.getVhost();
                    // set VHost as null, if it is the default vhost of the read only environment
                    statement.setString(1, deployedAPIRevision.getDeployment());
                    statement.setString(2, VHostUtils.resolveIfDefaultVhostToNull(envName, vhost));
                    statement.setString(3, apiRevisionId);
                    statement.setTimestamp(4, new Timestamp(System.currentTimeMillis()));
                    statement.addBatch();
                }
                statement.executeBatch();
                connection.commit();
            } catch (SQLException e) {
                connection.rollback();
                // handle concurrent db entry update. Fix duplicate primary key issue.
                if (e.getMessage().toLowerCase().contains("primary key violation") || e.getMessage().toLowerCase().contains("duplicate entry")) {
                    log.debug("Duplicate entries detected for Revision UUID " + apiRevisionId + " while adding deployed API revisions", e);
                } else {
                    handleException("Failed to add deployed API Revision for Revision UUID " + apiRevisionId, e);
                }
            }
        } catch (SQLException e) {
            handleException("Failed to add deployed API Revision for Revision UUID " + apiRevisionId, e);
        }
    }
}
Also used : DeployedAPIRevision(org.wso2.carbon.apimgt.api.model.DeployedAPIRevision) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp)

Aggregations

SQLException (java.sql.SQLException)14 PreparedStatement (java.sql.PreparedStatement)13 ArrayList (java.util.ArrayList)13 Connection (java.sql.Connection)12 APIRevisionDeployment (org.wso2.carbon.apimgt.api.model.APIRevisionDeployment)11 ResultSet (java.sql.ResultSet)9 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)8 Environment (org.wso2.carbon.apimgt.api.model.Environment)8 VHost (org.wso2.carbon.apimgt.api.model.VHost)7 IOException (java.io.IOException)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 FileInputStream (java.io.FileInputStream)4 InputStream (java.io.InputStream)4 APIRuntimeArtifactDto (org.wso2.carbon.apimgt.impl.dto.APIRuntimeArtifactDto)4 HashMap (java.util.HashMap)3 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)3 DeployedAPIRevision (org.wso2.carbon.apimgt.api.model.DeployedAPIRevision)3 JsonObject (com.google.gson.JsonObject)2 Timestamp (java.sql.Timestamp)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2