use of org.wso2.carbon.apimgt.api.model.APIRevision in project carbon-apimgt by wso2.
the class ApiMgtDAO method getRevisionsListByAPIUUID.
/**
* Get revision details by providing revision UUID
*
* @return revisions List object
* @throws APIManagementException if an error occurs while retrieving revision details
*/
public List<APIRevision> getRevisionsListByAPIUUID(String apiUUID) throws APIManagementException {
List<APIRevision> revisionList = new ArrayList<>();
try (Connection connection = APIMgtDBUtil.getConnection();
PreparedStatement statement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.GET_REVISIONS_BY_API_UUID)) {
statement.setString(1, apiUUID);
try (ResultSet rs = statement.executeQuery()) {
while (rs.next()) {
APIRevision apiRevision = new APIRevision();
apiRevision.setId(rs.getInt("ID"));
apiRevision.setApiUUID(apiUUID);
apiRevision.setRevisionUUID(rs.getString("REVISION_UUID"));
apiRevision.setDescription(rs.getString("DESCRIPTION"));
apiRevision.setCreatedTime(rs.getString("CREATED_TIME"));
apiRevision.setCreatedBy(rs.getString("CREATED_BY"));
apiRevision.setApiRevisionDeploymentList(new ArrayList<>());
revisionList.add(apiRevision);
}
}
} catch (SQLException e) {
handleException("Failed to get revision details for API UUID: " + apiUUID, e);
}
// adding deployment info to revision objects
List<APIRevisionDeployment> allAPIRevisionDeploymentList = getAPIRevisionDeploymentByApiUUID(apiUUID);
for (APIRevisionDeployment apiRevisionDeployment : allAPIRevisionDeploymentList) {
for (APIRevision apiRevision : revisionList) {
if (apiRevision.getRevisionUUID().equals(apiRevisionDeployment.getRevisionUUID())) {
apiRevision.getApiRevisionDeploymentList().add(apiRevisionDeployment);
break;
}
}
}
return revisionList;
}
use of org.wso2.carbon.apimgt.api.model.APIRevision in project carbon-apimgt by wso2.
the class ApiMgtDAO method addAPIProductRevision.
/**
* Adds an API Product revision record to the database
*
* @param apiRevision content of the revision
* @throws APIManagementException if an error occurs when adding a new API revision
*/
public void addAPIProductRevision(APIRevision apiRevision) throws APIManagementException {
try (Connection connection = APIMgtDBUtil.getConnection()) {
try {
connection.setAutoCommit(false);
// Adding to AM_REVISION table
PreparedStatement statement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.ADD_API_REVISION);
statement.setInt(1, apiRevision.getId());
statement.setString(2, apiRevision.getApiUUID());
statement.setString(3, apiRevision.getRevisionUUID());
statement.setString(4, apiRevision.getDescription());
statement.setString(5, apiRevision.getCreatedBy());
statement.setTimestamp(6, new Timestamp(System.currentTimeMillis()));
statement.executeUpdate();
// Retrieve API Product 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);
// Adding to AM_API_URL_MAPPING table
PreparedStatement getURLMappingsStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.GET_URL_MAPPINGS_WITH_SCOPE_AND_PRODUCT_ID_BY_PRODUCT_ID);
getURLMappingsStatement.setInt(1, apiId);
List<URITemplate> urlMappingList = new ArrayList<>();
try (ResultSet rs = getURLMappingsStatement.executeQuery()) {
while (rs.next()) {
String script = null;
URITemplate uriTemplate = new URITemplate();
uriTemplate.setHTTPVerb(rs.getString(1));
uriTemplate.setAuthType(rs.getString(2));
uriTemplate.setUriTemplate(rs.getString(3));
uriTemplate.setThrottlingTier(rs.getString(4));
InputStream mediationScriptBlob = rs.getBinaryStream(5);
if (mediationScriptBlob != null) {
script = APIMgtDBUtil.getStringFromInputStream(mediationScriptBlob);
}
uriTemplate.setMediationScript(script);
if (!StringUtils.isEmpty(rs.getString(6))) {
Scope scope = new Scope();
scope.setKey(rs.getString(6));
uriTemplate.setScope(scope);
}
if (rs.getInt(7) != 0) {
// Adding api id to uri template id just to store value
uriTemplate.setId(rs.getInt(7));
}
urlMappingList.add(uriTemplate);
}
}
Map<String, URITemplate> uriTemplateMap = new HashMap<>();
for (URITemplate urlMapping : urlMappingList) {
if (urlMapping.getScope() != null) {
URITemplate urlMappingNew = urlMapping;
URITemplate urlMappingExisting = uriTemplateMap.get(urlMapping.getUriTemplate() + urlMapping.getHTTPVerb());
if (urlMappingExisting != null && urlMappingExisting.getScopes() != null) {
if (!urlMappingExisting.getScopes().contains(urlMapping.getScope())) {
urlMappingExisting.setScopes(urlMapping.getScope());
uriTemplateMap.put(urlMappingExisting.getUriTemplate() + urlMappingExisting.getHTTPVerb(), urlMappingExisting);
}
} else {
urlMappingNew.setScopes(urlMapping.getScope());
uriTemplateMap.put(urlMappingNew.getUriTemplate() + urlMappingNew.getHTTPVerb(), urlMappingNew);
}
} else if (urlMapping.getId() != 0) {
URITemplate urlMappingExisting = uriTemplateMap.get(urlMapping.getUriTemplate() + urlMapping.getHTTPVerb());
if (urlMappingExisting == null) {
uriTemplateMap.put(urlMapping.getUriTemplate() + urlMapping.getHTTPVerb(), urlMapping);
}
} else {
uriTemplateMap.put(urlMapping.getUriTemplate() + urlMapping.getHTTPVerb(), urlMapping);
}
}
setAPIProductOperationPoliciesToURITemplatesMap(new Integer(apiId).toString(), uriTemplateMap);
PreparedStatement insertURLMappingsStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.INSERT_URL_MAPPINGS);
for (URITemplate urlMapping : uriTemplateMap.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, apiRevision.getRevisionUUID());
insertURLMappingsStatement.addBatch();
}
insertURLMappingsStatement.executeBatch();
// Add to AM_API_RESOURCE_SCOPE_MAPPING table and to AM_API_PRODUCT_MAPPING
PreparedStatement getRevisionedURLMappingsStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.GET_REVISIONED_URL_MAPPINGS_ID);
PreparedStatement insertScopeResourceMappingStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.INSERT_SCOPE_RESOURCE_MAPPING);
PreparedStatement insertProductResourceMappingStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.INSERT_PRODUCT_REVISION_RESOURCE_MAPPING);
String dbProductName = connection.getMetaData().getDatabaseProductName();
PreparedStatement insertOperationPolicyMappingStatement = connection.prepareStatement(SQLConstants.OperationPolicyConstants.ADD_API_OPERATION_POLICY_MAPPING, new String[] { DBUtils.getConvertedAutoGeneratedColumnName(dbProductName, "OPERATION_POLICY_MAPPING_ID") });
Map<String, String> clonedPoliciesMap = new HashMap<>();
for (URITemplate urlMapping : uriTemplateMap.values()) {
getRevisionedURLMappingsStatement.setInt(1, urlMapping.getId());
getRevisionedURLMappingsStatement.setString(2, apiRevision.getRevisionUUID());
getRevisionedURLMappingsStatement.setString(3, urlMapping.getHTTPVerb());
getRevisionedURLMappingsStatement.setString(4, urlMapping.getAuthType());
getRevisionedURLMappingsStatement.setString(5, urlMapping.getUriTemplate());
getRevisionedURLMappingsStatement.setString(6, urlMapping.getThrottlingTier());
if (urlMapping.getScopes() != null) {
try (ResultSet rs = getRevisionedURLMappingsStatement.executeQuery()) {
while (rs.next()) {
for (Scope scope : urlMapping.getScopes()) {
insertScopeResourceMappingStatement.setString(1, scope.getKey());
insertScopeResourceMappingStatement.setInt(2, rs.getInt(1));
insertScopeResourceMappingStatement.setInt(3, tenantId);
insertScopeResourceMappingStatement.addBatch();
}
}
}
}
try (ResultSet rs = getRevisionedURLMappingsStatement.executeQuery()) {
while (rs.next()) {
insertProductResourceMappingStatement.setInt(1, apiId);
insertProductResourceMappingStatement.setInt(2, rs.getInt(1));
insertProductResourceMappingStatement.setString(3, apiRevision.getRevisionUUID());
insertProductResourceMappingStatement.addBatch();
}
}
try (ResultSet rs = getRevisionedURLMappingsStatement.executeQuery()) {
while (rs.next()) {
for (OperationPolicy policy : urlMapping.getOperationPolicies()) {
String clonedPolicyId = null;
if (!clonedPoliciesMap.keySet().contains(policy.getPolicyId())) {
// Since we are creating a new revision, we need to clone all the policies from current status.
// If the policy is not cloned from a previous policy, we have to clone.
clonedPolicyId = revisionOperationPolicy(connection, policy.getPolicyId(), apiRevision.getApiUUID(), apiRevision.getRevisionUUID(), tenantDomain);
clonedPoliciesMap.put(policy.getPolicyId(), clonedPolicyId);
}
Gson gson = new Gson();
String paramJSON = gson.toJson(policy.getParameters());
insertOperationPolicyMappingStatement.setInt(1, rs.getInt(1));
insertOperationPolicyMappingStatement.setString(2, clonedPoliciesMap.get(policy.getPolicyId()));
insertOperationPolicyMappingStatement.setString(3, policy.getDirection());
insertOperationPolicyMappingStatement.setString(4, paramJSON);
insertOperationPolicyMappingStatement.setInt(5, policy.getOrder());
insertOperationPolicyMappingStatement.executeUpdate();
}
}
}
}
insertScopeResourceMappingStatement.executeBatch();
insertProductResourceMappingStatement.executeBatch();
// Adding to AM_API_CLIENT_CERTIFICATE
PreparedStatement getClientCertificatesStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.GET_CLIENT_CERTIFICATES);
getClientCertificatesStatement.setInt(1, apiId);
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);
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, apiRevision.getRevisionUUID());
insertClientCertificateStatement.addBatch();
}
insertClientCertificateStatement.executeBatch();
// Adding to AM_GRAPHQL_COMPLEXITY table
PreparedStatement getGraphQLComplexityStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.GET_GRAPHQL_COMPLEXITY);
List<CustomComplexityDetails> customComplexityDetailsList = new ArrayList<>();
getGraphQLComplexityStatement.setInt(1, apiId);
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);
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.setString(6, apiRevision.getRevisionUUID());
insertGraphQLComplexityStatement.addBatch();
}
insertGraphQLComplexityStatement.executeBatch();
updateLatestRevisionNumber(connection, apiRevision.getApiUUID(), apiRevision.getId());
addAPIRevisionMetaData(connection, apiRevision.getApiUUID(), apiRevision.getRevisionUUID());
connection.commit();
} catch (SQLException e) {
connection.rollback();
handleException("Failed to add API Revision entry of API Product UUID " + apiRevision.getApiUUID(), e);
}
} catch (SQLException e) {
handleException("Failed to add API Revision entry of API Product UUID " + apiRevision.getApiUUID(), e);
}
}
use of org.wso2.carbon.apimgt.api.model.APIRevision in project carbon-apimgt by wso2.
the class ApiMgtDAO method setOperationPolicies.
/**
* Sets operation policies to uriTemplates map
*
* @param uuid UUID of API or API Revision
* @param uriTemplates URI Templates map with URL_MAPPING_ID as the map key
* @throws SQLException
* @throws APIManagementException
*/
private void setOperationPolicies(String uuid, Map<Integer, URITemplate> uriTemplates) throws SQLException, APIManagementException {
String currentApiUuid;
String query;
boolean isRevision = false;
APIRevision apiRevision = checkAPIUUIDIsARevisionUUID(uuid);
if (apiRevision != null && apiRevision.getApiUUID() != null) {
currentApiUuid = apiRevision.getApiUUID();
query = SQLConstants.OperationPolicyConstants.GET_OPERATION_POLICIES_FOR_API_REVISION_SQL;
isRevision = true;
} else {
query = SQLConstants.OperationPolicyConstants.GET_OPERATION_POLICIES_OF_API_SQL;
currentApiUuid = uuid;
}
try (Connection conn = APIMgtDBUtil.getConnection();
PreparedStatement ps = conn.prepareStatement(query)) {
int apiId = getAPIID(currentApiUuid);
ps.setInt(1, apiId);
if (isRevision) {
ps.setString(2, uuid);
}
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
int uriTemplateId = rs.getInt("URL_MAPPING_ID");
URITemplate uriTemplate = uriTemplates.get(uriTemplateId);
if (uriTemplate != null) {
OperationPolicy operationPolicy = populateOperationPolicyWithRS(rs);
uriTemplate.addOperationPolicy(operationPolicy);
}
}
}
}
}
use of org.wso2.carbon.apimgt.api.model.APIRevision in project carbon-apimgt by wso2.
the class ApiMgtDAO method setOperationPoliciesToURITemplatesMap.
/**
* Sets operation policies to uriTemplates map
*
* @param uuid UUID of API or API Revision
* @param uriTemplates URI Templates map with 'URL_PATTERN + HTTP_METHOD' as the map key
* @throws SQLException
* @throws APIManagementException
*/
private void setOperationPoliciesToURITemplatesMap(String uuid, Map<String, URITemplate> uriTemplates) throws SQLException, APIManagementException {
String currentApiUuid;
String query;
boolean isRevision = false;
APIRevision apiRevision = checkAPIUUIDIsARevisionUUID(uuid);
if (apiRevision != null && apiRevision.getApiUUID() != null) {
currentApiUuid = apiRevision.getApiUUID();
query = SQLConstants.OperationPolicyConstants.GET_OPERATION_POLICIES_FOR_API_REVISION_SQL;
isRevision = true;
} else {
query = SQLConstants.OperationPolicyConstants.GET_OPERATION_POLICIES_OF_API_SQL;
currentApiUuid = uuid;
}
try (Connection conn = APIMgtDBUtil.getConnection();
PreparedStatement ps = conn.prepareStatement(query)) {
int apiId = getAPIID(currentApiUuid);
ps.setInt(1, apiId);
if (isRevision) {
ps.setString(2, uuid);
}
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
String key = rs.getString("URL_PATTERN") + rs.getString("HTTP_METHOD");
URITemplate uriTemplate = uriTemplates.get(key);
if (uriTemplate != null) {
OperationPolicy operationPolicy = populateOperationPolicyWithRS(rs);
uriTemplate.addOperationPolicy(operationPolicy);
}
}
}
}
}
use of org.wso2.carbon.apimgt.api.model.APIRevision in project carbon-apimgt by wso2.
the class APIMappingUtil method fromAPIRevisiontoDTO.
public static APIRevisionDTO fromAPIRevisiontoDTO(APIRevision model) throws APIManagementException {
APIRevisionDTO apiRevisionDTO = new APIRevisionDTO();
apiRevisionDTO.setId(model.getRevisionUUID());
String key = "Revision " + model.getId();
apiRevisionDTO.setDisplayName(key);
apiRevisionDTO.setDescription(model.getDescription());
if (model.getCreatedTime() != null) {
try {
apiRevisionDTO.setCreatedTime(parseStringToDate(model.getCreatedTime()));
} catch (java.text.ParseException e) {
throw new APIManagementException("Error while parsing the created time:" + model.getCreatedTime(), e);
}
}
APIRevisionAPIInfoDTO apiRevisionAPIInfoDTO = new APIRevisionAPIInfoDTO();
apiRevisionAPIInfoDTO.setId(model.getApiUUID());
apiRevisionDTO.setApiInfo(apiRevisionAPIInfoDTO);
List<APIRevisionDeploymentDTO> apiRevisionDeploymentDTOS = new ArrayList<>();
if (model.getApiRevisionDeploymentList() != null) {
for (APIRevisionDeployment apiRevisionDeployment : model.getApiRevisionDeploymentList()) {
apiRevisionDeploymentDTOS.add(fromAPIRevisionDeploymenttoDTO(apiRevisionDeployment));
}
}
apiRevisionDTO.setDeploymentInfo(apiRevisionDeploymentDTOS);
return apiRevisionDTO;
}
Aggregations