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;
}
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;
}
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()]);
}
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;
}
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;
}
Aggregations