use of org.wso2.carbon.apimgt.api.model.APIProductIdentifier in project carbon-apimgt by wso2.
the class ApiMgtDAO method getExternalWorkflowReferenceForSubscription.
/**
* Retries the WorkflowExternalReference for a subscription.
*
* @param identifier Identifier to find the subscribed api
* @param appID ID of the application which has the subscription
* @param organization organization
* @return External workflow reference for the subscription identified
* @throws APIManagementException
*/
public String getExternalWorkflowReferenceForSubscription(Identifier identifier, int appID, String organization) throws APIManagementException {
String workflowExtRef = null;
int id = -1;
int subscriptionID = -1;
String sqlQuery = SQLConstants.GET_EXTERNAL_WORKFLOW_REFERENCE_FOR_SUBSCRIPTION_SQL;
String postgreSQL = SQLConstants.GET_EXTERNAL_WORKFLOW_REFERENCE_FOR_SUBSCRIPTION_POSTGRE_SQL;
try (Connection conn = APIMgtDBUtil.getConnection()) {
if (identifier instanceof APIIdentifier) {
String apiUuid;
if (identifier.getUUID() != null) {
apiUuid = identifier.getUUID();
} else {
apiUuid = getUUIDFromIdentifier((APIIdentifier) identifier, organization);
}
id = getAPIID(apiUuid, conn);
} else if (identifier instanceof APIProductIdentifier) {
id = ((APIProductIdentifier) identifier).getProductId();
}
if (conn.getMetaData().getDriverName().contains("PostgreSQL")) {
sqlQuery = postgreSQL;
}
try (PreparedStatement ps = conn.prepareStatement(sqlQuery)) {
ps.setInt(1, id);
ps.setInt(2, appID);
ps.setString(3, WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_CREATION);
try (ResultSet rs = ps.executeQuery()) {
// returns only one row
if (rs.next()) {
workflowExtRef = rs.getString("WF_EXTERNAL_REFERENCE");
}
}
}
} catch (SQLException e) {
handleException("Error occurred while getting workflow entry for " + "Subscription : " + subscriptionID, e);
}
return workflowExtRef;
}
use of org.wso2.carbon.apimgt.api.model.APIProductIdentifier in project carbon-apimgt by wso2.
the class ApiMgtDAO method getProductMappingsForAPI.
public List<APIProductResource> getProductMappingsForAPI(API api) throws APIManagementException {
List<APIProductResource> productMappings = new ArrayList<>();
Set<URITemplate> uriTemplatesOfAPI = getURITemplatesOfAPI(api.getUuid());
for (URITemplate uriTemplate : uriTemplatesOfAPI) {
Set<APIProductIdentifier> apiProductIdentifiers = uriTemplate.retrieveUsedByProducts();
for (APIProductIdentifier apiProductIdentifier : apiProductIdentifiers) {
APIProductResource productMapping = new APIProductResource();
productMapping.setProductIdentifier(apiProductIdentifier);
productMapping.setUriTemplate(uriTemplate);
productMappings.add(productMapping);
}
}
return productMappings;
}
use of org.wso2.carbon.apimgt.api.model.APIProductIdentifier in project carbon-apimgt by wso2.
the class ApiMgtDAO method restoreAPIProductRevision.
/**
* Restore API Product revision database records as the Current API Product of an API Product
*
* @param apiRevision content of the revision
* @throws APIManagementException if an error occurs when restoring an API revision
*/
public void restoreAPIProductRevision(APIRevision apiRevision) throws APIManagementException {
try (Connection connection = APIMgtDBUtil.getConnection()) {
try {
connection.setAutoCommit(false);
// Retrieve API ID
APIProductIdentifier apiProductIdentifier = APIUtil.getAPIProductIdentifierFromUUID(apiRevision.getApiUUID());
int apiId = getAPIID(apiRevision.getApiUUID(), connection);
int tenantId = APIUtil.getTenantId(APIUtil.replaceEmailDomainBack(apiProductIdentifier.getProviderName()));
String tenantDomain = APIUtil.getTenantDomainFromTenantId(tenantId);
// Remove Current API Product entries from AM_API_URL_MAPPING table
PreparedStatement removeURLMappingsFromCurrentAPIProduct = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.REMOVE_CURRENT_API_PRODUCT_ENTRIES_IN_AM_API_URL_MAPPING);
removeURLMappingsFromCurrentAPIProduct.setString(1, Integer.toString(apiId));
removeURLMappingsFromCurrentAPIProduct.executeUpdate();
// Copy Revision resources
PreparedStatement getURLMappingsFromRevisionedAPIProduct = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.GET_API_PRODUCT_REVISION_URL_MAPPINGS_BY_REVISION_UUID);
getURLMappingsFromRevisionedAPIProduct.setString(1, apiRevision.getRevisionUUID());
Map<String, URITemplate> urlMappingList = new HashMap<>();
try (ResultSet rs = getURLMappingsFromRevisionedAPIProduct.executeQuery()) {
String key, httpMethod, urlPattern;
while (rs.next()) {
String script = null;
URITemplate uriTemplate = new URITemplate();
httpMethod = rs.getString("HTTP_METHOD");
urlPattern = rs.getString("URL_PATTERN");
uriTemplate.setHTTPVerb(httpMethod);
uriTemplate.setAuthType(rs.getString("AUTH_SCHEME"));
uriTemplate.setUriTemplate(rs.getString("URL_PATTERN"));
uriTemplate.setThrottlingTier(rs.getString("THROTTLING_TIER"));
InputStream mediationScriptBlob = rs.getBinaryStream("MEDIATION_SCRIPT");
if (mediationScriptBlob != null) {
script = APIMgtDBUtil.getStringFromInputStream(mediationScriptBlob);
}
uriTemplate.setMediationScript(script);
if (rs.getInt("API_ID") != 0) {
// Adding product id to uri template id just to store value
uriTemplate.setId(rs.getInt("API_ID"));
}
key = urlPattern + httpMethod;
urlMappingList.put(key, uriTemplate);
}
}
// Populate Scope Mappings
PreparedStatement getScopeMappingsFromRevisionedAPIProduct = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.GET_API_PRODUCT_REVISION_SCOPE_MAPPINGS_BY_REVISION_UUID);
getScopeMappingsFromRevisionedAPIProduct.setString(1, apiRevision.getRevisionUUID());
try (ResultSet rs = getScopeMappingsFromRevisionedAPIProduct.executeQuery()) {
while (rs.next()) {
String key = rs.getString("URL_PATTERN") + rs.getString("HTTP_METHOD");
if (urlMappingList.containsKey(key)) {
URITemplate uriTemplate = urlMappingList.get(key);
Scope scope = new Scope();
scope.setKey(rs.getString("SCOPE_NAME"));
uriTemplate.setScope(scope);
uriTemplate.setScopes(scope);
}
}
}
setAPIProductOperationPoliciesToURITemplatesMap(apiRevision.getRevisionUUID(), urlMappingList);
PreparedStatement insertURLMappingsStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.INSERT_URL_MAPPINGS);
for (URITemplate urlMapping : urlMappingList.values()) {
insertURLMappingsStatement.setInt(1, urlMapping.getId());
insertURLMappingsStatement.setString(2, urlMapping.getHTTPVerb());
insertURLMappingsStatement.setString(3, urlMapping.getAuthType());
insertURLMappingsStatement.setString(4, urlMapping.getUriTemplate());
insertURLMappingsStatement.setString(5, urlMapping.getThrottlingTier());
insertURLMappingsStatement.setString(6, Integer.toString(apiId));
insertURLMappingsStatement.addBatch();
}
insertURLMappingsStatement.executeBatch();
// Insert Scope Mappings and operation policy mappings
PreparedStatement getRevisionedURLMappingsStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.GET_REVISIONED_URL_MAPPINGS_ID);
PreparedStatement addResourceScopeMapping = connection.prepareStatement(SQLConstants.ADD_API_RESOURCE_SCOPE_MAPPING);
PreparedStatement addOperationPolicyStatement = connection.prepareStatement(SQLConstants.OperationPolicyConstants.ADD_API_OPERATION_POLICY_MAPPING);
Map<String, String> clonedPoliciesMap = new HashMap<>();
Set<String> usedClonedPolicies = new HashSet<String>();
for (URITemplate urlMapping : urlMappingList.values()) {
getRevisionedURLMappingsStatement.setInt(1, urlMapping.getId());
getRevisionedURLMappingsStatement.setString(2, Integer.toString(apiId));
getRevisionedURLMappingsStatement.setString(3, urlMapping.getHTTPVerb());
getRevisionedURLMappingsStatement.setString(4, urlMapping.getAuthType());
getRevisionedURLMappingsStatement.setString(5, urlMapping.getUriTemplate());
getRevisionedURLMappingsStatement.setString(6, urlMapping.getThrottlingTier());
try (ResultSet rs = getRevisionedURLMappingsStatement.executeQuery()) {
if (rs.next()) {
int newURLMappingId = rs.getInt("URL_MAPPING_ID");
if (urlMapping.getScopes() != null && urlMapping.getScopes().size() > 0) {
for (Scope scope : urlMapping.getScopes()) {
addResourceScopeMapping.setString(1, scope.getKey());
addResourceScopeMapping.setInt(2, newURLMappingId);
addResourceScopeMapping.setInt(3, tenantId);
addResourceScopeMapping.addBatch();
}
}
if (urlMapping.getOperationPolicies().size() > 0) {
for (OperationPolicy policy : urlMapping.getOperationPolicies()) {
if (!clonedPoliciesMap.keySet().contains(policy.getPolicyName())) {
String policyId = restoreOperationPolicyRevision(connection, apiRevision.getApiUUID(), policy.getPolicyId(), apiRevision.getId(), tenantDomain);
clonedPoliciesMap.put(policy.getPolicyName(), policyId);
usedClonedPolicies.add(policyId);
}
Gson gson = new Gson();
String paramJSON = gson.toJson(policy.getParameters());
addOperationPolicyStatement.setInt(1, rs.getInt(1));
addOperationPolicyStatement.setString(2, clonedPoliciesMap.get(policy.getPolicyName()));
addOperationPolicyStatement.setString(3, policy.getDirection());
addOperationPolicyStatement.setString(4, paramJSON);
addOperationPolicyStatement.setInt(5, policy.getOrder());
addOperationPolicyStatement.executeUpdate();
}
}
}
}
}
addResourceScopeMapping.executeBatch();
cleanUnusedClonedOperationPolicies(connection, usedClonedPolicies, apiRevision.getApiUUID());
// Get URL_MAPPING_IDs from table and add records to product mapping table
PreparedStatement getURLMappingOfAPIProduct = connection.prepareStatement(SQLConstants.GET_URL_MAPPING_IDS_OF_API_PRODUCT_SQL);
PreparedStatement insertProductResourceMappingStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.INSERT_PRODUCT_REVISION_RESOURCE_MAPPING);
getURLMappingOfAPIProduct.setString(1, Integer.toString(apiId));
try (ResultSet rs = getURLMappingOfAPIProduct.executeQuery()) {
while (rs.next()) {
insertProductResourceMappingStatement.setInt(1, apiId);
insertProductResourceMappingStatement.setInt(2, rs.getInt("URL_MAPPING_ID"));
insertProductResourceMappingStatement.setString(3, "Current API");
insertProductResourceMappingStatement.addBatch();
}
insertProductResourceMappingStatement.executeBatch();
}
// Restoring AM_API_CLIENT_CERTIFICATE table entries
PreparedStatement removeClientCertificatesStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.REMOVE_CURRENT_API_ENTRIES_IN_AM_API_CLIENT_CERTIFICATE_BY_API_ID);
removeClientCertificatesStatement.setInt(1, apiId);
removeClientCertificatesStatement.executeUpdate();
PreparedStatement getClientCertificatesStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.GET_CLIENT_CERTIFICATES_BY_REVISION_UUID);
getClientCertificatesStatement.setInt(1, apiId);
getClientCertificatesStatement.setString(2, apiRevision.getRevisionUUID());
List<ClientCertificateDTO> clientCertificateDTOS = new ArrayList<>();
try (ResultSet rs = getClientCertificatesStatement.executeQuery()) {
while (rs.next()) {
ClientCertificateDTO clientCertificateDTO = new ClientCertificateDTO();
clientCertificateDTO.setAlias(rs.getString(1));
clientCertificateDTO.setCertificate(APIMgtDBUtil.getStringFromInputStream(rs.getBinaryStream(2)));
clientCertificateDTO.setTierName(rs.getString(3));
clientCertificateDTOS.add(clientCertificateDTO);
}
}
PreparedStatement insertClientCertificateStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.INSERT_CLIENT_CERTIFICATES_AS_CURRENT_API);
for (ClientCertificateDTO clientCertificateDTO : clientCertificateDTOS) {
insertClientCertificateStatement.setInt(1, tenantId);
insertClientCertificateStatement.setString(2, clientCertificateDTO.getAlias());
insertClientCertificateStatement.setInt(3, apiId);
insertClientCertificateStatement.setBinaryStream(4, getInputStream(clientCertificateDTO.getCertificate()));
insertClientCertificateStatement.setBoolean(5, false);
insertClientCertificateStatement.setString(6, clientCertificateDTO.getTierName());
insertClientCertificateStatement.setString(7, "Current API");
insertClientCertificateStatement.addBatch();
}
insertClientCertificateStatement.executeBatch();
// Restoring AM_GRAPHQL_COMPLEXITY table
PreparedStatement removeGraphQLComplexityStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.REMOVE_CURRENT_API_ENTRIES_IN_AM_GRAPHQL_COMPLEXITY_BY_API_ID);
removeGraphQLComplexityStatement.setInt(1, apiId);
removeGraphQLComplexityStatement.executeUpdate();
PreparedStatement getGraphQLComplexityStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.GET_GRAPHQL_COMPLEXITY_BY_REVISION_UUID);
List<CustomComplexityDetails> customComplexityDetailsList = new ArrayList<>();
getGraphQLComplexityStatement.setInt(1, apiId);
getGraphQLComplexityStatement.setString(2, apiRevision.getRevisionUUID());
try (ResultSet rs1 = getGraphQLComplexityStatement.executeQuery()) {
while (rs1.next()) {
CustomComplexityDetails customComplexityDetails = new CustomComplexityDetails();
customComplexityDetails.setType(rs1.getString("TYPE"));
customComplexityDetails.setField(rs1.getString("FIELD"));
customComplexityDetails.setComplexityValue(rs1.getInt("COMPLEXITY_VALUE"));
customComplexityDetailsList.add(customComplexityDetails);
}
}
PreparedStatement insertGraphQLComplexityStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.INSERT_GRAPHQL_COMPLEXITY_AS_CURRENT_API);
for (CustomComplexityDetails customComplexityDetails : customComplexityDetailsList) {
insertGraphQLComplexityStatement.setString(1, UUID.randomUUID().toString());
insertGraphQLComplexityStatement.setInt(2, apiId);
insertGraphQLComplexityStatement.setString(3, customComplexityDetails.getType());
insertGraphQLComplexityStatement.setString(4, customComplexityDetails.getField());
insertGraphQLComplexityStatement.setInt(5, customComplexityDetails.getComplexityValue());
insertGraphQLComplexityStatement.addBatch();
}
insertGraphQLComplexityStatement.executeBatch();
connection.commit();
} catch (SQLException e) {
connection.rollback();
handleException("Failed to restore API Revision entry of API UUID " + apiRevision.getApiUUID(), e);
}
} catch (SQLException e) {
handleException("Failed to restore API Revision entry of API UUID " + apiRevision.getApiUUID(), e);
}
}
use of org.wso2.carbon.apimgt.api.model.APIProductIdentifier in project carbon-apimgt by wso2.
the class ApiMgtDAO method getSubscriptionById.
/**
* returns the SubscribedAPI object which is related to the subscriptionId
*
* @param subscriptionId subscription id
* @return {@link SubscribedAPI} Object which contains the subscribed API information.
* @throws APIManagementException
*/
public SubscribedAPI getSubscriptionById(int subscriptionId) throws APIManagementException {
Connection conn = null;
ResultSet resultSet = null;
PreparedStatement ps = null;
try {
conn = APIMgtDBUtil.getConnection();
String getSubscriptionQuery = SQLConstants.GET_SUBSCRIPTION_BY_ID_SQL;
ps = conn.prepareStatement(getSubscriptionQuery);
ps.setInt(1, subscriptionId);
resultSet = ps.executeQuery();
SubscribedAPI subscribedAPI = null;
if (resultSet.next()) {
int applicationId = resultSet.getInt("APPLICATION_ID");
Application application = getLightweightApplicationById(conn, applicationId);
if (APIConstants.API_PRODUCT.equals(resultSet.getString("API_TYPE"))) {
APIProductIdentifier apiProductIdentifier = new APIProductIdentifier(APIUtil.replaceEmailDomain(resultSet.getString("API_PROVIDER")), resultSet.getString("API_NAME"), resultSet.getString("API_VERSION"));
apiProductIdentifier.setProductId(resultSet.getInt("API_ID"));
apiProductIdentifier.setUUID(resultSet.getString("API_UUID"));
subscribedAPI = new SubscribedAPI(application.getSubscriber(), apiProductIdentifier);
} else {
APIIdentifier apiIdentifier = new APIIdentifier(APIUtil.replaceEmailDomain(resultSet.getString("API_PROVIDER")), resultSet.getString("API_NAME"), resultSet.getString("API_VERSION"));
apiIdentifier.setId(resultSet.getInt("API_ID"));
apiIdentifier.setUuid(resultSet.getString("API_UUID"));
subscribedAPI = new SubscribedAPI(application.getSubscriber(), apiIdentifier);
}
subscribedAPI.setSubscriptionId(resultSet.getInt("SUBSCRIPTION_ID"));
subscribedAPI.setSubStatus(resultSet.getString("SUB_STATUS"));
subscribedAPI.setSubCreatedStatus(resultSet.getString("SUBS_CREATE_STATE"));
subscribedAPI.setTier(new Tier(resultSet.getString("TIER_ID")));
subscribedAPI.setRequestedTier(new Tier(resultSet.getString("TIER_ID_PENDING")));
subscribedAPI.setUUID(resultSet.getString("UUID"));
subscribedAPI.setApplication(application);
}
return subscribedAPI;
} catch (SQLException e) {
handleException("Failed to retrieve subscription from subscription id", e);
} finally {
APIMgtDBUtil.closeAllConnections(ps, conn, resultSet);
}
return null;
}
use of org.wso2.carbon.apimgt.api.model.APIProductIdentifier in project carbon-apimgt by wso2.
the class ApiMgtDAO method getAPIProductId.
public int getAPIProductId(APIProductIdentifier identifier) throws APIManagementException {
Connection conn = null;
String queryGetProductId = SQLConstants.GET_PRODUCT_ID;
PreparedStatement preparedStatement = null;
ResultSet rs = null;
int productId = -1;
try {
conn = APIMgtDBUtil.getConnection();
preparedStatement = conn.prepareStatement(queryGetProductId);
preparedStatement.setString(1, identifier.getName());
preparedStatement.setString(2, APIUtil.replaceEmailDomainBack(identifier.getProviderName()));
// versioning is not supported atm
preparedStatement.setString(3, APIConstants.API_PRODUCT_VERSION);
rs = preparedStatement.executeQuery();
if (rs.next()) {
productId = rs.getInt("API_ID");
}
if (productId == -1) {
String msg = "Unable to find the API Product : " + productId + " in the database";
log.error(msg);
throw new APIManagementException(msg);
}
} catch (SQLException e) {
handleException("Error while retrieving api product id for product " + identifier.getName() + " by " + APIUtil.replaceEmailDomainBack(identifier.getProviderName()), e);
} finally {
APIMgtDBUtil.closeAllConnections(preparedStatement, conn, rs);
}
return productId;
}
Aggregations