Skip to content

Commit

Permalink
Add: NPE handling
Browse files Browse the repository at this point in the history
  • Loading branch information
BLasan committed Sep 17, 2024
1 parent 9c40f67 commit 444f62e
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2918,13 +2918,15 @@ public static void validateAPIContext(String context, String apiName) throws API
*/
public static void validateAPIEndpointConfig(Object endpointConfigObject, String apiType, String apiName)
throws APIManagementException {
Map endpointConfigMap = (Map) endpointConfigObject;

if (endpointConfigMap.containsKey("endpoint_type") && APIConstants.ENDPOINT_TYPE_SEQUENCE.equals(
endpointConfigMap.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE))
&& !APIConstants.API_TYPE_HTTP.equalsIgnoreCase(apiType)
&& !APIConstants.API_TYPE_SOAPTOREST.equalsIgnoreCase(apiType)) {
throw new APIManagementException("Invalid endpoint configuration provided for the API " + apiName);
if (endpointConfigObject != null) {
Map endpointConfigMap = (Map) endpointConfigObject;
if (endpointConfigMap != null && endpointConfigMap.containsKey("endpoint_type")
&& APIConstants.ENDPOINT_TYPE_SEQUENCE.equals(
endpointConfigMap.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE))
&& !APIConstants.API_TYPE_HTTP.equalsIgnoreCase(apiType)
&& !APIConstants.API_TYPE_SOAPTOREST.equalsIgnoreCase(apiType)) {
throw new APIManagementException("Invalid endpoint configuration provided for the API " + apiName);
}

Check warning on line 2929 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java#L2929

Added line #L2929 was not covered by tests
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ public static void setCustomSequencesToBeRemoved(API api, GatewayAPIDTO gatewayA
}

public static void setCustomBackendToBeRemoved(GatewayAPIDTO gatewayAPIDTO) {
String sandBoxBackend = APIUtil.getCustomBackendName(gatewayAPIDTO.getApiId(), "SANDBOX");
String sandBoxBackend = APIUtil.getCustomBackendName(gatewayAPIDTO.getApiId(),
APIConstants.API_KEY_TYPE_SANDBOX);
gatewayAPIDTO.setSequencesToBeRemove(addStringToList(sandBoxBackend, gatewayAPIDTO.getSequencesToBeRemove()));
String productionBackend = APIUtil.getCustomBackendName(gatewayAPIDTO.getApiId(), "PRODUCTION");
String productionBackend = APIUtil.getCustomBackendName(gatewayAPIDTO.getApiId(),
APIConstants.API_KEY_TYPE_PRODUCTION);
gatewayAPIDTO.setSequencesToBeRemove(
addStringToList(productionBackend, gatewayAPIDTO.getSequencesToBeRemove()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,15 @@ public static File exportApi(APIProvider apiProvider, APIIdentifier apiIdentifie
addOperationPoliciesToArchive(archivePath, tenantDomain, exportFormat, apiProvider,
api, currentApiUuid);

JsonObject endpointConfig = JsonParser.parseString(api.getEndpointConfig()).getAsJsonObject();
if (APIConstants.ENDPOINT_TYPE_SEQUENCE.equals(
endpointConfig.get(API_ENDPOINT_CONFIG_PROTOCOL_TYPE).getAsString()) && StringUtils.equals(
apiDtoToReturn.getType().toString().toLowerCase(), APIConstants.API_TYPE_HTTP.toLowerCase())
|| StringUtils.equals(apiDtoToReturn.getType().toString().toLowerCase(),
APIConstants.API_TYPE_SOAPTOREST.toLowerCase())) {
addCustomBackendToArchive(archivePath, apiProvider, currentApiUuid);
if (api != null && !StringUtils.isEmpty(api.getEndpointConfig())) {
JsonObject endpointConfig = JsonParser.parseString(api.getEndpointConfig()).getAsJsonObject();
if (APIConstants.ENDPOINT_TYPE_SEQUENCE.equals(
endpointConfig.get(API_ENDPOINT_CONFIG_PROTOCOL_TYPE).getAsString()) && StringUtils.equals(
apiDtoToReturn.getType().toString().toLowerCase(), APIConstants.API_TYPE_HTTP.toLowerCase())

Check warning on line 225 in components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/ExportUtils.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/ExportUtils.java#L225

Added line #L225 was not covered by tests
|| StringUtils.equals(apiDtoToReturn.getType().toString().toLowerCase(),
APIConstants.API_TYPE_SOAPTOREST.toLowerCase())) {
addCustomBackendToArchive(archivePath, apiProvider, currentApiUuid);
}
}

addGatewayEnvironmentsToArchive(archivePath, apiDtoToReturn.getId(), exportFormat, apiProvider);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -678,22 +678,24 @@ public static void validateAppliedPolicy(OperationPolicy appliedPolicy,
public static void updateAPIWithCustomBackend(API api, String extractedFolderPath, APIProvider apiProvider)
throws APIManagementException {
String customBackendDir = extractedFolderPath + File.separator + ImportExportConstants.CUSTOM_BACKEND_DIRECTORY;

Check warning on line 680 in components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/ImportUtils.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/ImportUtils.java#L680

Added line #L680 was not covered by tests
JsonObject endpointConfig = JsonParser.parseString(api.getEndpointConfig()).getAsJsonObject();

if (endpointConfig != null) {
if (endpointConfig.get("sandbox") != null) {
String seqFile = endpointConfig.get("sandbox").getAsString();
String seqId = UUID.randomUUID().toString();
InputStream seq = APIUtil.getCustomBackendSequence(customBackendDir, seqFile, ".xml");
apiProvider.updateCustomBackend(api.getUuid(), APIConstants.API_KEY_TYPE_SANDBOX, seq, seqFile + ".xml",
seqId);
}
if (endpointConfig.get("production") != null) {
String seqFile = endpointConfig.get("production").getAsString();
String seqId = UUID.randomUUID().toString();
InputStream seq = APIUtil.getCustomBackendSequence(customBackendDir, seqFile, ".xml");
apiProvider.updateCustomBackend(api.getUuid(), APIConstants.API_KEY_TYPE_PRODUCTION, seq,
seqFile + ".xml", seqId);
if (api != null && !StringUtils.isEmpty(api.getEndpointConfig())) {
JsonObject endpointConfig = JsonParser.parseString(api.getEndpointConfig()).getAsJsonObject();

Check warning on line 682 in components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/ImportUtils.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/ImportUtils.java#L682

Added line #L682 was not covered by tests

if (endpointConfig != null) {
if (endpointConfig.get("sandbox") != null) {
String seqFile = endpointConfig.get("sandbox").getAsString();
String seqId = UUID.randomUUID().toString();
InputStream seq = APIUtil.getCustomBackendSequence(customBackendDir, seqFile, ".xml");
apiProvider.updateCustomBackend(api.getUuid(), APIConstants.API_KEY_TYPE_SANDBOX, seq,

Check warning on line 689 in components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/ImportUtils.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/ImportUtils.java#L686-L689

Added lines #L686 - L689 were not covered by tests
seqFile + ".xml", seqId);
}
if (endpointConfig.get("production") != null) {
String seqFile = endpointConfig.get("production").getAsString();
String seqId = UUID.randomUUID().toString();
InputStream seq = APIUtil.getCustomBackendSequence(customBackendDir, seqFile, ".xml");
apiProvider.updateCustomBackend(api.getUuid(), APIConstants.API_KEY_TYPE_PRODUCTION, seq,

Check warning on line 696 in components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/ImportUtils.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/ImportUtils.java#L693-L696

Added lines #L693 - L696 were not covered by tests
seqFile + ".xml", seqId);
}
}
}
}

Check warning on line 701 in components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/ImportUtils.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/ImportUtils.java#L701

Added line #L701 was not covered by tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,14 @@ public static API updateApi(API originalAPI, APIDTO apiDtoToUpdate, APIProvider
API apiToUpdate = prepareForUpdateApi(originalAPI, apiDtoToUpdate, apiProvider, tokenScopes);
apiProvider.updateAPI(apiToUpdate, originalAPI);
API apiUpdated = apiProvider.getAPIbyUUID(originalAPI.getUuid(), originalAPI.getOrganization());
JsonObject endpointConfig = JsonParser.parseString(apiUpdated.getEndpointConfig()).getAsJsonObject();
if (!APIConstants.ENDPOINT_TYPE_SEQUENCE.equals(
endpointConfig.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE).getAsString())) {
apiProvider.deleteSequenceBackendByRevision(apiUpdated.getUuid(), "0");
if (apiUpdated != null && !StringUtils.isEmpty(apiUpdated.getEndpointConfig())) {
JsonObject endpointConfig = JsonParser.parseString(apiUpdated.getEndpointConfig()).getAsJsonObject();
if (!APIConstants.ENDPOINT_TYPE_SEQUENCE.equals(
endpointConfig.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE).getAsString()) && (
APIConstants.API_TYPE_HTTP.equals(apiUpdated.getType()) || APIConstants.API_TYPE_SOAPTOREST.equals(
apiUpdated.getType()))) {
apiProvider.deleteSequenceBackendByRevision(apiUpdated.getUuid(), "0");
}
}
return apiUpdated;
// TODO use returend api
Expand Down Expand Up @@ -253,6 +257,8 @@ private static API prepareForUpdateApi(API originalAPI, APIDTO apiDtoToUpdate, A
+ " as the token information hasn't been correctly set internally",
ExceptionCodes.TOKEN_SCOPES_NOT_SET);
}
APIUtil.validateAPIEndpointConfig(apiDtoToUpdate.getEndpointConfig(), apiDtoToUpdate.getType().toString(),
apiDtoToUpdate.getName());
if (apiDtoToUpdate.getVisibility() == APIDTO.VisibilityEnum.RESTRICTED && apiDtoToUpdate.getVisibleRoles()
.isEmpty()) {
throw new APIManagementException("Access control roles cannot be empty when visibility is restricted",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3898,7 +3898,7 @@ public Response deployAPIRevision(String apiId, String revisionId,

// Cannot deploy an API with custom sequence to the APK gateway
Map endpointConfigMap = (Map) apiDto.getEndpointConfig();
if (APIConstants.WSO2_APK_GATEWAY.equals(apiDto.getGatewayType()) && APIConstants.ENDPOINT_TYPE_SEQUENCE.equals(
if (!APIConstants.WSO2_SYNAPSE_GATEWAY.equals(apiDto.getGatewayType()) && APIConstants.ENDPOINT_TYPE_SEQUENCE.equals(
endpointConfigMap.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE))) {
return Response.status(Response.Status.BAD_REQUEST)
.entity("Cannot Deploy an API with a Custom Sequence to APK Gateway: " + apiId).build();

Check warning on line 3904 in components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java#L3902-L3904

Added lines #L3902 - L3904 were not covered by tests
Expand Down

0 comments on commit 444f62e

Please sign in to comment.