Search in sources :

Example 91 with Tenant

use of org.wso2.carbon.user.api.Tenant 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 92 with Tenant

use of org.wso2.carbon.user.api.Tenant in project carbon-apimgt by wso2.

the class ServiceCatalogDAO method getServiceByNameAndVersion.

/**
 * Get service information by name and version
 *
 * @param name          Service name
 * @param version       Service version
 * @param tenantId     ID of the owner's tenant
 * @return ServiceEntry
 * throws APIManagementException if failed to retrieve
 */
public ServiceEntry getServiceByNameAndVersion(String name, String version, int tenantId) throws APIManagementException {
    try (Connection connection = APIMgtDBUtil.getConnection();
        PreparedStatement ps = connection.prepareStatement(SQLConstants.ServiceCatalogConstants.GET_SERVICE_BY_NAME_AND_VERSION)) {
        ps.setString(1, name);
        ps.setString(2, version);
        ps.setInt(3, tenantId);
        try (ResultSet rs = ps.executeQuery()) {
            if (rs.next()) {
                ServiceEntry serviceEntry = getServiceParams(rs, false);
                return serviceEntry;
            }
        }
    } catch (SQLException e) {
        handleException("Error while executing SQL for getting catalog entry resources", e);
    }
    return null;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) ServiceEntry(org.wso2.carbon.apimgt.api.model.ServiceEntry)

Example 93 with Tenant

use of org.wso2.carbon.user.api.Tenant in project carbon-apimgt by wso2.

the class SystemApplicationDAO method getApplications.

/**
 * Method to retrieve all the system Applications for the given tenant
 *
 * @param tenantDomain required parameter
 * @return SystemApplicationDTO which hold the retrieved client credentials
 * @throws APIMgtDAOException
 */
public SystemApplicationDTO[] getApplications(String tenantDomain) throws APIMgtDAOException {
    Connection connection = null;
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;
    SystemApplicationDTO systemApplicationDTO = null;
    List<SystemApplicationDTO> systemApplicationDTOS = new ArrayList<>();
    String getCredentialsQuery = SQLConstants.SystemApplicationConstants.GET_APPLICATIONS;
    try {
        connection = APIMgtDBUtil.getConnection();
        connection.setAutoCommit(false);
        connection.commit();
        preparedStatement = connection.prepareStatement(getCredentialsQuery);
        preparedStatement.setString(1, tenantDomain);
        resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            systemApplicationDTO = new SystemApplicationDTO();
            systemApplicationDTO.setName(resultSet.getString("NAME"));
            systemApplicationDTO.setConsumerKey(resultSet.getString("CONSUMER_KEY"));
            systemApplicationDTO.setConsumerSecret(resultSet.getString("CONSUMER_SECRET"));
            systemApplicationDTOS.add(systemApplicationDTO);
        }
    } catch (SQLException e) {
        if (log.isDebugEnabled()) {
            log.debug("Error while retrieving system applications for tenant: " + tenantDomain);
        }
        handleException("Error while retrieving system applications for tenant: " + tenantDomain, e);
    } finally {
        APIMgtDBUtil.closeAllConnections(preparedStatement, connection, resultSet);
    }
    return systemApplicationDTOS.toArray(new SystemApplicationDTO[systemApplicationDTOS.size()]);
}
Also used : SQLException(java.sql.SQLException) SystemApplicationDTO(org.wso2.carbon.apimgt.impl.dto.SystemApplicationDTO) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement)

Example 94 with Tenant

use of org.wso2.carbon.user.api.Tenant in project carbon-apimgt by wso2.

the class CertificateMgtDAO method getCertificates.

/**
 * Method to retrieve certificate metadata from db for specific tenant which matches alias or endpoint.
 * From alias and endpoint, only one parameter is required.
 *
 * @param tenantId : The id of the tenant which the certificate belongs to.
 * @param alias    : Alias for the certificate. (Optional)
 * @param endpoint : The endpoint/ server url which the certificate is mapped to. (Optional)
 * @return : A CertificateMetadataDTO object if the certificate is retrieved successfully, null otherwise.
 */
public List<CertificateMetadataDTO> getCertificates(String alias, String endpoint, int tenantId) throws CertificateManagementException {
    String getCertQuery;
    CertificateMetadataDTO certificateMetadataDTO;
    List<CertificateMetadataDTO> certificateMetadataList = new ArrayList<>();
    if (StringUtils.isNotEmpty(alias) || StringUtils.isNotEmpty(endpoint)) {
        if (log.isDebugEnabled()) {
            log.debug("The alias and endpoint are not empty. Invoking the search query with parameters " + "alias = " + alias + " endpoint = " + endpoint);
        }
        getCertQuery = SQLConstants.CertificateConstants.GET_CERTIFICATE_TENANT;
    } else {
        if (log.isDebugEnabled()) {
            log.debug("The alias and endpoint are empty. Invoking the get all certificates for tenant " + tenantId);
        }
        getCertQuery = SQLConstants.CertificateConstants.GET_CERTIFICATES;
    }
    try (Connection connection = APIMgtDBUtil.getConnection()) {
        try (PreparedStatement preparedStatement = connection.prepareStatement(getCertQuery)) {
            preparedStatement.setInt(1, tenantId);
            if (StringUtils.isNotEmpty(alias) || StringUtils.isNotEmpty(endpoint)) {
                preparedStatement.setString(2, alias);
                preparedStatement.setString(3, endpoint);
            }
            try (ResultSet resultSet = preparedStatement.executeQuery()) {
                while (resultSet.next()) {
                    certificateMetadataDTO = new CertificateMetadataDTO();
                    certificateMetadataDTO.setAlias(resultSet.getString("ALIAS"));
                    certificateMetadataDTO.setEndpoint(resultSet.getString("END_POINT"));
                    try (InputStream certificate = resultSet.getBinaryStream("CERTIFICATE")) {
                        certificateMetadataDTO.setCertificate(APIMgtDBUtil.getStringFromInputStream(certificate));
                    }
                    certificateMetadataList.add(certificateMetadataDTO);
                }
            }
        }
    } catch (SQLException | IOException e) {
        handleException("Error while retrieving certificate metadata.", e);
    }
    return certificateMetadataList;
}
Also used : CertificateMetadataDTO(org.wso2.carbon.apimgt.api.dto.CertificateMetadataDTO) SQLException(java.sql.SQLException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException)

Example 95 with Tenant

use of org.wso2.carbon.user.api.Tenant in project carbon-apimgt by wso2.

the class ServiceCatalogDAO method getServiceByKey.

/**
 * Get service information by service key
 *
 * @param key          Service key of service
 * @param tenantId     ID of the owner's tenant
 * @return ServiceEntry
 * throws APIManagementException if failed to retrieve
 */
public ServiceEntry getServiceByKey(String key, int tenantId) throws APIManagementException {
    try (Connection connection = APIMgtDBUtil.getConnection();
        PreparedStatement ps = connection.prepareStatement(SQLConstants.ServiceCatalogConstants.GET_SERVICE_BY_SERVICE_KEY)) {
        ps.setString(1, key);
        ps.setInt(2, tenantId);
        try (ResultSet rs = ps.executeQuery()) {
            if (rs.next()) {
                ServiceEntry serviceEntry = getServiceParams(rs, false);
                return serviceEntry;
            }
        }
    } catch (SQLException e) {
        handleException("Error while executing SQL for getting service information", e);
    }
    return null;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) ServiceEntry(org.wso2.carbon.apimgt.api.model.ServiceEntry)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)180 UserStoreException (org.wso2.carbon.user.api.UserStoreException)88 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)83 ArrayList (java.util.ArrayList)79 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)70 PreparedStatement (java.sql.PreparedStatement)51 SQLException (java.sql.SQLException)50 IOException (java.io.IOException)49 Connection (java.sql.Connection)49 HashMap (java.util.HashMap)44 ResultSet (java.sql.ResultSet)43 JSONObject (org.json.simple.JSONObject)41 Resource (org.wso2.carbon.registry.core.Resource)40 Registry (org.wso2.carbon.registry.core.Registry)38 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)34 API (org.wso2.carbon.apimgt.api.model.API)34 Test (org.junit.Test)33 File (java.io.File)32 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)32 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)30