Skip to content

Commit

Permalink
Fix: Input Stream Closing Issue
Browse files Browse the repository at this point in the history
  • Loading branch information
BLasan committed Sep 23, 2024
1 parent f2cb6d3 commit 00d6c1c
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,14 @@ Comment getComment(ApiTypeWrapper apiTypeWrapper, String commentId, Integer repl
* This method is to delete Sequence Backend by type
*
* @param apiUUID API Id
* @param type Key type
* @param type Key type
* @throws APIManagementException If failed to delete Sequence Backend
*/
void deleteCustomBackendByID(String apiUUID, String type) throws APIManagementException;

/**
* This method is to delete all Sequence Backends by APIID
*
* @param apiUUID API ID
* @throws APIManagementException If failed to delete Sequence Backend
*/
Expand Down Expand Up @@ -343,21 +344,21 @@ List<SubscribedAPI> getSubscriptionsOfAPI(String apiName, String apiVersion, Str
/**
* This method is to update Sequence Backend
*
* @param api API
* @param type Key Type
* @param sequence Sequence Content
* @param seqName Sequence Name
* @param api API
* @param type Key Type
* @param sequence Sequence Content
* @param seqName Sequence Name
* @param customBackendUUID Sequence Id
* @throws APIManagementException If not updated
*/
void updateCustomBackend(String api, String type, InputStream sequence, String seqName, String customBackendUUID)
void updateCustomBackend(String api, String type, String sequence, String seqName, String customBackendUUID)
throws APIManagementException;

/**
* THis method is to retrieve Sequence Backend data
*
* @param apiUUID API Id
* @param type Key Type
* @param type Key Type
* @return SequenceBackendData object
* @throws APIManagementException If data is not properly retrieved
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1045,7 +1045,7 @@ private void updateAPIPolicies(API api, String tenantDomain) throws APIManagemen
}

@Override
public void updateCustomBackend(String apiUUID, String type, InputStream sequence, String seqName,
public void updateCustomBackend(String apiUUID, String type, String sequence, String seqName,
String customBackendUUID) throws APIManagementException {
apiMgtDAO.updateCustomBackend(apiUUID, seqName, sequence, type, customBackendUUID);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20950,7 +20950,17 @@ public void updateAPIPoliciesMapping(String apiUUID, Set<URITemplate> uriTemplat
}
}

public void updateCustomBackend(String apiUUID, String sequenceName, InputStream sequence, String type,
/**
* This method is to update Sequence Backend data
*
* @param apiUUID API Id
* @param sequenceName Sequence Name
* @param sequence Sequence Content
* @param type Key type
* @param backendUUID Sequence Id
* @throws APIManagementException If not properly updated
*/
public void updateCustomBackend(String apiUUID, String sequenceName, String sequence, String type,
String backendUUID) throws APIManagementException {
// delete current working copy
String deleteCustomBackedQuery = SQLConstants.CustomBackendConstants.DELETE_CUSTOM_BACKEND_BY_API_AND_TYPE;
Expand Down Expand Up @@ -21025,22 +21035,24 @@ public void addNewCustomBackendForAPIRevision(String apiUUID, String revisionID,
}
}

public void addCustomBackend(String apiUUID, String sequenceName, String revision, InputStream sequence,
public void addCustomBackend(String apiUUID, String sequenceName, String revision, String sequence,
String type, Connection connection, String backendUUID) throws APIManagementException {
String insertCustomBackendQuery = SQLConstants.CustomBackendConstants.ADD_CUSTOM_BACKEND;
try (PreparedStatement prepStmt = connection.prepareStatement(insertCustomBackendQuery)) {
connection.setAutoCommit(false);
prepStmt.setString(1, backendUUID);
prepStmt.setString(2, apiUUID);
prepStmt.setBinaryStream(3, sequence);
try (InputStream seqStream = new ByteArrayInputStream(sequence.getBytes())) {
prepStmt.setBinaryStream(3, seqStream);
}
prepStmt.setString(4, type);
if (revision == null) {
revision = "0";
}
prepStmt.setString(5, revision);
prepStmt.setString(6, sequenceName);
prepStmt.executeUpdate();
} catch (SQLException e) {
} catch (SQLException | IOException e) {
handleException("Error while adding Custom Backend for API : " + apiUUID, e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10037,7 +10037,7 @@ public static void loadCommonOperationPolicies(String organization) {
* @return The Sequence of the Custom Backend as an Input Stream
* @throws APIManagementException If an error occurs while reading, throws an error
*/
public static InputStream getCustomBackendSequence(String extractedFolderPath, String customBackendFileName,
public static String getCustomBackendSequence(String extractedFolderPath, String customBackendFileName,
String fileExtension) throws APIManagementException {
if (!StringUtils.isEmpty(customBackendFileName) && !customBackendFileName.contains(fileExtension)) {
customBackendFileName = customBackendFileName + fileExtension;
Expand All @@ -10046,7 +10046,7 @@ public static InputStream getCustomBackendSequence(String extractedFolderPath, S
if (checkFileExistence(fileName)) {
try {
try (InputStream inputStream = new FileInputStream(fileName)) {
return inputStream;
return IOUtils.toString(inputStream);
}
} catch (IOException ex) {
handleException("Error reading Custom Backend " + customBackendFileName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -701,24 +701,28 @@ public static void updateAPIWithCustomBackend(API api, String extractedFolderPat
if (endpointConfig.get("sandbox") != null) {
String seqFile = endpointConfig.get("sandbox").getAsString();
String seqId = UUID.randomUUID().toString();
InputStream seq = APIUtil.getCustomBackendSequence(customBackendDir, seqFile, ".xml");
String seq = APIUtil.getCustomBackendSequence(customBackendDir, seqFile, ".xml");
if (!StringUtils.isEmpty(seqFile) && !seqFile.contains(
APIConstants.SYNAPSE_POLICY_DEFINITION_EXTENSION_XML)) {
seqFile = seqFile + APIConstants.SYNAPSE_POLICY_DEFINITION_EXTENSION_XML;
}
apiProvider.updateCustomBackend(api.getUuid(), APIConstants.API_KEY_TYPE_SANDBOX, seq, seqFile,
seqId);
if (seq != null) {
apiProvider.updateCustomBackend(api.getUuid(), APIConstants.API_KEY_TYPE_SANDBOX, seq, seqFile,
seqId);
}
}
if (endpointConfig.get("production") != null) {
String seqFile = endpointConfig.get("production").getAsString();
String seqId = UUID.randomUUID().toString();
InputStream seq = APIUtil.getCustomBackendSequence(customBackendDir, seqFile, ".xml");
String seq = APIUtil.getCustomBackendSequence(customBackendDir, seqFile, ".xml");
if (!StringUtils.isEmpty(seqFile) && !seqFile.contains(
APIConstants.SYNAPSE_POLICY_DEFINITION_EXTENSION_XML)) {
seqFile = seqFile + APIConstants.SYNAPSE_POLICY_DEFINITION_EXTENSION_XML;
}
apiProvider.updateCustomBackend(api.getUuid(), APIConstants.API_KEY_TYPE_PRODUCTION, seq, seqFile,
seqId);
if (seq != null) {
apiProvider.updateCustomBackend(api.getUuid(), APIConstants.API_KEY_TYPE_PRODUCTION, seq,
seqFile, seqId);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import graphql.schema.validation.SchemaValidationError;
import graphql.schema.validation.SchemaValidator;
import io.swagger.v3.parser.ObjectMapperFactory;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
Expand Down Expand Up @@ -216,9 +217,13 @@ public static void updateCustomBackend(API api, APIProvider apiProvider, String
throw new APIManagementException(
"Error when retrieving Custom Backend file name of API: " + api.getId().getApiName());
}
String seqName = APIUtil.getCustomBackendName(api.getUuid(), endpointType);
String customBackendUUID = UUID.randomUUID().toString();
apiProvider.updateCustomBackend(api.getUuid(), endpointType, customBackend, fileName, customBackendUUID);
try {
String customBackendStr = IOUtils.toString(customBackend);
apiProvider.updateCustomBackend(api.getUuid(), endpointType, customBackendStr, fileName, customBackendUUID);
} catch (IOException ex) {
throw new APIManagementException("Error retrieving sequence backend of API: " + api.getUuid(), ex);
}
}

private static String getFileNameFromContentDisposition(String contentDisposition) {
Expand Down

0 comments on commit 00d6c1c

Please sign in to comment.