Skip to content

Commit

Permalink
Remove: Unnecessary Codes
Browse files Browse the repository at this point in the history
  • Loading branch information
BLasan committed Sep 5, 2024
1 parent 2d71961 commit 2f67b3b
Show file tree
Hide file tree
Showing 12 changed files with 387 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public interface APIProvider extends APIManager {
*/
Comment getComment(ApiTypeWrapper apiTypeWrapper, String commentId, Integer replyLimit, Integer replyOffset) throws
APIManagementException;
void deleteCustomBackendByID(String backendUUID, String apiUUID, String type) throws APIManagementException;

/**
* @param apiTypeWrapper Api type wrapper
Expand Down Expand Up @@ -317,7 +318,8 @@ List<SubscribedAPI> getSubscriptionsOfAPI(String apiName, String apiVersion, Str
API updateAPI(API api, API existingAPI) throws APIManagementException, FaultGatewaysException;

void updateCustomBackend(API api,String type, InputStream sequence, String fileName, String customBackendUUID) throws APIManagementException;
Map<String, String> getCustomBackendOfAPIByUUID(String customBackendUUID, String apiUUID, boolean isInfoOnly) throws APIManagementException;
Map<String, Object> getCustomBackendOfAPIByUUID(String customBackendUUID, String apiUUID, String type, boolean isInfoOnly) throws APIManagementException;
InputStream getCustomBackendSequenceOfAPIByUUID(String apiUUID, String backendUUID, String type) throws APIManagementException;

void updateCustomBackendByRevisionID(String apiUUID, String type, String revision,
String seqName, String backendUUID) throws APIManagementException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import jdk.internal.util.xml.impl.Input;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.util.AXIOMUtil;
import org.apache.axis2.Constants;
Expand All @@ -35,6 +34,7 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.solr.client.solrj.util.ClientUtils;
import org.apache.solr.common.util.Hash;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
Expand Down Expand Up @@ -1050,23 +1050,50 @@ public void updateCustomBackend(API api, String type, InputStream sequence, Stri
}

@Override
public Map<String, String> getCustomBackendOfAPIByUUID(String customBackendUUID, String apiUUID, boolean isInfoOnly) throws APIManagementException {
return apiMgtDAO.getCustomBackendOfAPIByUUID(customBackendUUID, apiUUID, isInfoOnly);
public Map<String, Object> getCustomBackendOfAPIByUUID(String customBackendUUID, String apiUUID, String type, boolean isInfoOnly) throws APIManagementException {
return apiMgtDAO.getCustomBackendOfAPIByUUID(customBackendUUID, apiUUID, type, isInfoOnly);
}

@Override
public void updateCustomBackendByRevisionID(String apiUUID, String type, String revision,
String seqName, String backendUUID) throws APIManagementException {
@Override public void updateCustomBackendByRevisionID(String apiUUID, String type, String revision, String seqName,
String backendUUID) throws APIManagementException {
String customBackendUUID = UUID.randomUUID().toString();
InputStream sequence = apiMgtDAO.getCustomBackendSequenceOfAPIByUUID(backendUUID, apiUUID);
InputStream sequence = apiMgtDAO.getCustomBackendSequenceOfAPIByUUID(backendUUID, apiUUID, type);
apiMgtDAO.updateCustomBackendByRevision(apiUUID, seqName, sequence, type, revision, customBackendUUID);
}

@Override public InputStream getCustomBackendSequenceOfAPIByUUID(String apiUUID, String backendUUID, String type)
throws APIManagementException {
return apiMgtDAO.getCustomBackendSequenceOfAPIByUUID(backendUUID, apiUUID, type);
}

@Override public void addNewCustomBackendForRevision(String revisionUUID, String updatedBackendUUID, String apiUUID,
Map<String, Object> config) throws APIManagementException {
InputStream sequence = apiMgtDAO.getCustomBackendSequenceOfAPIByUUID(updatedBackendUUID, apiUUID);
config.put("sequence", sequence);
apiMgtDAO.addNewCustomBackendForAPIRevision(apiUUID, revisionUUID, config);
if (config.get("production") != null) {
InputStream sequence = apiMgtDAO.getCustomBackendSequenceOfAPIByUUID(updatedBackendUUID, apiUUID,
"PRODUCTION");
if (config.get("production") instanceof HashMap) {
String seqName = ((HashMap) config.get("production")).get("sequence_name").toString();
String backendUUID = ((HashMap) config.get("production")).get("sequence_id").toString();
apiMgtDAO.addNewCustomBackendForAPIRevision(apiUUID, revisionUUID, "PRODUCTION", seqName, sequence,
backendUUID);
}
}

if (config.get("SANDBOX") != null) {
InputStream sequence = apiMgtDAO.getCustomBackendSequenceOfAPIByUUID(updatedBackendUUID, apiUUID,
"SANDBOX");
if (config.get("sandbox") instanceof HashMap) {
String seqName = ((HashMap) config.get("sandbox")).get("sequence_name").toString();
String backendUUID = ((HashMap) config.get("sandbox")).get("sequence_id").toString();
apiMgtDAO.addNewCustomBackendForAPIRevision(apiUUID, revisionUUID, "SANDBOX", seqName, sequence,
backendUUID);
}
}
}

@Override public void deleteCustomBackendByID(String backendUUID, String apiUUID, String type)
throws APIManagementException {
apiMgtDAO.deleteCustomBackend(apiUUID, backendUUID, type);
}

private void validateKeyManagers(API api) throws APIManagementException {
Expand Down Expand Up @@ -6494,7 +6521,7 @@ public void deleteAPIRevision(String apiId, String apiRevisionId, String organiz
ERROR_DELETING_API_REVISION,apiRevision.getApiUUID()));
}
apiMgtDAO.deleteAPIRevision(apiRevision);
apiMgtDAO.deleteCustomBackend(apiId, apiRevisionId);
apiMgtDAO.deleteCustomBackendByRevision(apiId, apiRevisionId);
gatewayArtifactsMgtDAO.deleteGatewayArtifact(apiRevision.getApiUUID(), apiRevision.getRevisionUUID());
if (artifactSaver != null) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import jdk.internal.util.xml.impl.Input;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
Expand Down Expand Up @@ -11194,7 +11193,7 @@ public Map<String, Object> retrieveCustomBackendOfAPIRevision(String apiUUID, St
return map;
}

public InputStream getCustomBackendSequenceOfAPIByUUID(String backendUUID, String apiUUID) throws APIManagementException {
public InputStream getCustomBackendSequenceOfAPIByUUID(String backendUUID, String apiUUID, String type) throws APIManagementException {
String sqlQuery = SQLConstants.CustomBackendConstants.GET_API_SPECIFIC_CUSTOM_BACKEND_FROM_SEQUENCE_ID;
ResultSet rs = null;
InputStream sequence = null;
Expand All @@ -11220,27 +11219,31 @@ public InputStream getCustomBackendSequenceOfAPIByUUID(String backendUUID, Strin
return sequence;
}

public Map<String, String> getCustomBackendOfAPIByUUID(String backendUUID, String apiUUID, boolean isInfo) throws APIManagementException {
public Map<String, Object> getCustomBackendOfAPIByUUID(String backendUUID, String apiUUID, String type,
boolean isInfo) throws APIManagementException {
String sqlQuery;
Map<String, String> endpointConfig = new HashMap<>();
Map<String, Object> endpointConfig = new HashMap<>();
boolean isRevisioned = checkAPIUUIDIsARevisionUUID(apiUUID) != null;
if(isRevisioned) {
if (isRevisioned) {
sqlQuery = SQLConstants.CustomBackendConstants.GET_REVISION_SPECIFIC_CUSTOM_BACKEND_FROM_SEQUENCE_ID;
} else {
sqlQuery = SQLConstants.CustomBackendConstants.GET_API_SPECIFIC_CUSTOM_BACKEND_FROM_SEQUENCE_ID;
}
ResultSet resultSet = null;
try (Connection connection = APIMgtDBUtil.getConnection();
PreparedStatement ps = connection.prepareStatement(sqlQuery)) {
PreparedStatement ps = connection.prepareStatement(sqlQuery)) {
ps.setString(1, backendUUID);
ps.setString(2, apiUUID);
ps.setString(3, type);
resultSet = ps.executeQuery();
while (resultSet.next()) {
if(!isInfo) {
if (!isInfo) {
try (InputStream in = resultSet.getBinaryStream("SEQUENCE")) {
endpointConfig.put("sequence", IOUtils.toString(in));
endpointConfig.put("sequence", in);
} catch (IOException ex) {
handleException("Error reading Sequence Content of Custom Backend: " + backendUUID + " API: " + apiUUID, ex);
handleException(
"Error reading Sequence Content of Custom Backend: " + backendUUID + " API: " + apiUUID,
ex);
}
}
endpointConfig.put("type", resultSet.getString("TYPE"));
Expand Down Expand Up @@ -17055,6 +17058,9 @@ public void addAPIRevision(APIRevision apiRevision) throws APIManagementExceptio

// Retrieve API ID
APIIdentifier apiIdentifier = APIUtil.getAPIIdentifierFromUUID(apiRevision.getApiUUID());

// Insert Custom Backend if Provided

int apiId = getAPIID(apiRevision.getApiUUID(), connection);
int tenantId = APIUtil.getTenantId(APIUtil.replaceEmailDomainBack(apiIdentifier.getProviderName()));
String tenantDomain = APIUtil.getTenantDomainFromTenantId(tenantId);
Expand Down Expand Up @@ -17183,6 +17189,8 @@ public void addAPIRevision(APIRevision apiRevision) throws APIManagementExceptio
insertScopeResourceMappingStatement.executeBatch();
insertProductResourceMappingStatement.executeBatch();
revisionAPIPolicies(apiRevision, tenantDomain, uriTemplateMap, connection);
// Add Custom Backend
revisionCustomBackend(apiRevision, connection);

// Adding to AM_API_CLIENT_CERTIFICATE
String getClientCertificatesQuery = SQLConstants.APIRevisionSqlConstants.GET_CLIENT_CERTIFICATES_OF_KEY_TYPE;
Expand Down Expand Up @@ -20861,6 +20869,7 @@ public void updateCustomBackend(String apiUUID, String sequenceName, InputStream
connection.setAutoCommit(false);
prepStmt.setString(1, apiUUID);
prepStmt.setString(2, backendUUID);
prepStmt.setString(3, type);
prepStmt.executeUpdate();
addCustomBackend(apiUUID, sequenceName, null, sequence, type, connection, backendUUID);
connection.commit();
Expand All @@ -20869,20 +20878,39 @@ public void updateCustomBackend(String apiUUID, String sequenceName, InputStream
}
}

public void deleteCustomBackend(String apiUUID, String revisionId) throws APIManagementException {
String deleteCustomBackedQuery = SQLConstants.CustomBackendConstants.DELETE_CUSTOM_BACKEND_BY_REVISION;
public void deleteCustomBackend(String apiUUID, String type, String backendUUID) throws APIManagementException {
String deleteCustomBackedQuery = SQLConstants.CustomBackendConstants.DELETE_CUSTOM_BACKEND;
try (Connection connection = APIMgtDBUtil.getConnection();
PreparedStatement prepStmt = connection.prepareStatement(deleteCustomBackedQuery)) {
connection.setAutoCommit(false);
prepStmt.setString(1, apiUUID);
prepStmt.setString(2, revisionId);
prepStmt.setString(2, backendUUID);
prepStmt.setString(3, type);
prepStmt.executeUpdate();
connection.commit();
} catch (SQLException e) {
handleException("Error while deleting Custom Backend for API : " + apiUUID, e);
}
}

public void deleteCustomBackendByRevision(String apiUUID, String revisionUUID) throws APIManagementException {
boolean isRevisioned = checkAPIUUIDIsARevisionUUID(apiUUID) != null;
String deleteSqlQuery = SQLConstants.CustomBackendConstants.DELETE_CUSTOM_BACKEND_BY_REVISION;
if(isRevisioned) {
try (Connection con = APIMgtDBUtil.getConnection();
PreparedStatement ps = con.prepareStatement(deleteSqlQuery)) {
con.setAutoCommit(false);
ps.setString(1, apiUUID);
ps.executeUpdate();
con.commit();
} catch (SQLException ex) {
handleException("Error deleting Custom Backend for Revision: "+ apiUUID, ex);
}
} else {
throw new APIManagementException("Invalid Revision Id: "+ apiUUID);
}
}

public void updateCustomBackendByRevision(String apiUUID, String sequenceName, InputStream sequence, String type,
String revision, String backendUUID) throws APIManagementException {
String deleteCustomBackedQuery = SQLConstants.CustomBackendConstants.DELETE_CUSTOM_BACKEND_BY_REVISION;
Expand All @@ -20899,22 +20927,19 @@ public void updateCustomBackendByRevision(String apiUUID, String sequenceName, I
}
}

public void addNewCustomBackendForAPIRevision(String apiUUID, String revisionID, Map<String, Object> endpointConfig) throws APIManagementException {
public void addNewCustomBackendForAPIRevision(String apiUUID, String revisionID, String type, String seqName, InputStream sequence, String backendUUID) throws APIManagementException {
String addSqlQuery = SQLConstants.CustomBackendConstants.ADD_CUSTOM_BACKEND;
try (Connection con = APIMgtDBUtil.getConnection();
PreparedStatement ps = con.prepareStatement(addSqlQuery)) {
String backendUUID = endpointConfig.get("sequence_id").toString();
InputStream sequence = (InputStream) endpointConfig.get("sequence");
String type = endpointConfig.get("type").toString();
String sequenceName = endpointConfig.get("sequence_name").toString();
con.setAutoCommit(false);
ps.setString(1, backendUUID);
ps.setString(2, apiUUID);
ps.setBinaryStream(3, sequence);
ps.setString(4, type);
ps.setString(5, revisionID);
ps.setString(6, sequenceName);
ps.setString(6, seqName);
ps.executeUpdate();
con.commit();
} catch (SQLException ex) {
handleException("Error when inserting Custom Backend data for API: " + apiUUID, ex);
}
Expand All @@ -20929,6 +20954,9 @@ public void addCustomBackend(String apiUUID, String sequenceName, String revisio
prepStmt.setString(2, apiUUID);
prepStmt.setBinaryStream(3, sequence);
prepStmt.setString(4, type);
if (revision == null) {
revision = "0";
}
prepStmt.setString(5, revision);
prepStmt.setString(6, sequenceName);
prepStmt.executeUpdate();
Expand Down Expand Up @@ -21121,6 +21149,33 @@ private List<OperationPolicy> getAPIPolicyMapping(String apiUUID, String revisio
return policyList;
}

private void revisionCustomBackend(APIRevision apiRevision, Connection connection)
throws SQLException, APIManagementException {
String addCBSqlQuery = SQLConstants.CustomBackendConstants.ADD_CUSTOM_BACKEND;
String getCBSQLQuery = SQLConstants.CustomBackendConstants.GET_ALL_API_SPECIFIC_CUSTOM_BACKENDS;
ResultSet rs = null;
try (Connection con = APIMgtDBUtil.getConnection();
PreparedStatement getPstmt = con.prepareStatement(getCBSQLQuery);
PreparedStatement addPstmt = con.prepareStatement(addCBSqlQuery)) {
con.setAutoCommit(false);
getPstmt.setString(1, apiRevision.getApiUUID());
rs = getPstmt.executeQuery();
while (rs.next()) {
addPstmt.setString(1, rs.getString("ID"));
addPstmt.setString(2, apiRevision.getApiUUID());
addPstmt.setBinaryStream(3, rs.getBinaryStream("SEQUENCE"));
addPstmt.setString(4, rs.getString("TYPE"));
addPstmt.setString(5, apiRevision.getRevisionUUID());
addPstmt.setString(6, rs.getString("NAME"));
addPstmt.addBatch();
}
addPstmt.executeBatch();
} catch (SQLException ex) {
handleException("Error while adding Custom Backends to the database of API: " + apiRevision.getApiUUID(),
ex);
}
}

/**
* Create a revision of API policis. This will clone the policy and policy mapping with each revision
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4404,13 +4404,14 @@ public static class SystemConfigsConstants {
public static class CustomBackendConstants {
public static final String ADD_CUSTOM_BACKEND =
"INSERT INTO AM_API_CUSTOM_BACKEND (ID,API_UUID,SEQUENCE,TYPE,REVISION_UUID,NAME) " + "VALUES (?,?,?,?,?,?)";
public static final String DELETE_CUSTOM_BACKEND = "DELETE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND ID = ? AND REVISION_UUID IS NULL";
public static final String DELETE_CUSTOM_BACKEND_BY_REVISION = "DELETE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND REVISION_UUID = ?";
public static final String DELETE_CUSTOM_BACKEND = "DELETE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND ID = ? AND TYPE = ? AND REVISION_UUID = 0";
public static final String DELETE_CUSTOM_BACKEND_BY_REVISION = "DELETE FROM AM_API_CUSTOM_BACKEND WHERE REVISION_UUID = ?";
public static final String DELETE_CUSTOM_BACKEND_BY_API = "DELETE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ?";
public static final String GET_CUSTOM_BACKEND_OF_API_REVISION = "SELECT NAME, SEQUENCE, TYPE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND REVISION_UUID = ?";
public static final String GET_CUSTOM_BACKEND_OF_API_DEFAULT_REVISION = "SELECT ACB.NAME, ACB.TYPE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND REVISION_UUID IS NULL";
public static final String GET_REVISION_SPECIFIC_CUSTOM_BACKEND_FROM_SEQUENCE_ID = "SELECT ACB.ID, ACB.NAME, ACB.SEQUENCE, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.ID = ? AND ACB.REVISION_UUID = ?";
public static final String GET_API_SPECIFIC_CUSTOM_BACKEND_FROM_SEQUENCE_ID = "SELECT ACB.ID, ACB.NAME, ACB.SEQUENCE, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.ID = ? AND API_UUID = ? AND REVISION_UUID IS NULL";
public static final String GET_REVISION_SPECIFIC_CUSTOM_BACKEND_FROM_SEQUENCE_ID = "SELECT ACB.ID, ACB.NAME, ACB.SEQUENCE, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.ID = ? AND ACB.REVISION_UUID = ? AND ACB.TYPE = ?";
public static final String GET_API_SPECIFIC_CUSTOM_BACKEND_FROM_SEQUENCE_ID = "SELECT ACB.ID, ACB.NAME, ACB.SEQUENCE, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.ID = ? AND ACB.API_UUID = ? AND ACB.REVISION_UUID = 0 AND ACB.TYPE = ?";
public static final String GET_ALL_API_SPECIFIC_CUSTOM_BACKENDS = "SELECT ACB.ID, ACB.NAME, ACB.SEQUENCE, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.API_UUID = ? AND ACB.REVISION_UUID = 0";
public static final String GET_REVISION_SPECIFIC_CUSTOM_BACKEND_META_DATA_FROM_SEQUENCE_ID = "SELECT ACB.ID, ACB.NAME, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.ID = ? AND ACB.REVISION_UUID = ?";
public static final String GET_API_SPECIFIC_CUSTOM_BACKEND_META_DATA_FROM_SEQUENCE_ID = "SELECT ACB.ID, ACB.NAME, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.ID = ? AND API_UUID = ?";
public static final String GET_API_SPECIFIC_CUSTOM_BACKEND_SEQUENCE_FROM_SEQUENCE_ID = "SELECT ACB.SEQUENCE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.ID = ? AND API_UUID = ?";
Expand Down
Loading

0 comments on commit 2f67b3b

Please sign in to comment.