From 18a8db763bf3fde73af0a9c431386bec1255eb47 Mon Sep 17 00:00:00 2001 From: AnuGayan Date: Tue, 22 Oct 2024 23:05:24 +0530 Subject: [PATCH] Fix minor issue in API Level logging --- .../wso2/carbon/apimgt/api/ExceptionCodes.java | 1 + .../devops/impl/logging/APILoggingImpl.java | 9 ++++++++- .../carbon/apimgt/impl/dao/LoggingMgtDAO.java | 15 +++++++++++++++ .../apimgt/impl/dao/constants/SQLConstants.java | 2 ++ 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/ExceptionCodes.java b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/ExceptionCodes.java index 26d6bf917fa9..98161fc60f0c 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/ExceptionCodes.java +++ b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/ExceptionCodes.java @@ -512,6 +512,7 @@ public enum ExceptionCodes implements ErrorHandler { LOGGING_API_INCORRECT_LOG_LEVEL(901401, "Bad Request", 400, "Log level should be either OFF, BASIC, STANDARD or FULL"), LOGGING_API_MISSING_DATA(901402, "Missing data", 400, "API context or log level is missing"), LOGGING_API_RESOURCE_NOT_FOUND(901403, "Requested Resource Not Found", 404, "Requested API Resource Not Found"), + LOGGING_API_NOT_FOUND_IN_TENANT(901404, "Requested API Not Found", 404, "Requested API Not Found"), CORRELATION_CONFIG_BAD_REQUEST(902020, "Bad Request", 400, "Request body can not have empty elements"), CORRELATION_CONFIG_BAD_REQUEST_INVALID_NAME(902021, "Bad Request", 400, "Request body contains invalid correlation component name"), //Service Catalog related error codes diff --git a/components/apimgt/org.wso2.carbon.apimgt.devops.impl/src/main/java/org/wso2/carbon/apimgt/devops/impl/logging/APILoggingImpl.java b/components/apimgt/org.wso2.carbon.apimgt.devops.impl/src/main/java/org/wso2/carbon/apimgt/devops/impl/logging/APILoggingImpl.java index 5d1ae5631ac0..4be2743657ea 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.devops.impl/src/main/java/org/wso2/carbon/apimgt/devops/impl/logging/APILoggingImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.devops.impl/src/main/java/org/wso2/carbon/apimgt/devops/impl/logging/APILoggingImpl.java @@ -40,6 +40,7 @@ public class APILoggingImpl { private static final String INVALID_LOGGING_PERMISSION = "Invalid logging permission"; private static final String INCORRECT_LOGGING_PER_API_RESOURCE_REQUEST = "Resource Method and Resource Path both " + "should be included"; + private static final String LOGGING_API_MISSING_DATA = "Requested API is not available"; private static final String REQUIRED_API_RESOURCE_IS_NOT_AVAILABLE = "Requested resource is not available"; private final ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance(); @@ -64,7 +65,13 @@ public void addUpdateAPILogger(String tenantId, String apiId, String logLevel, S ExceptionCodes.from(ExceptionCodes.LOGGING_API_RESOURCE_NOT_FOUND)); } } else if (resourceMethod == null && resourcePath == null) { - LoggingMgtDAO.getInstance().addAPILogger(tenantId, apiId, logLevel); + boolean isAPIExists = LoggingMgtDAO.getInstance().checkIfAPIExists(tenantId, apiId); + if (isAPIExists) { + LoggingMgtDAO.getInstance().addAPILogger(tenantId, apiId, logLevel); + } else { + throw new APIManagementException(LOGGING_API_MISSING_DATA, + ExceptionCodes.from(ExceptionCodes.LOGGING_API_NOT_FOUND_IN_TENANT)); + } } else { throw new APIManagementException(INCORRECT_LOGGING_PER_API_RESOURCE_REQUEST, ExceptionCodes.from(ExceptionCodes.LOGGING_API_MISSING_DATA)); diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/LoggingMgtDAO.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/LoggingMgtDAO.java index e8ca3e2b5002..e49ffda74d7c 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/LoggingMgtDAO.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/LoggingMgtDAO.java @@ -76,6 +76,21 @@ public void addAPILogger(String organization, String apiId, String logLevel) thr } } + public boolean checkIfAPIExists(String organization, String apiId) throws APIManagementException { + try (Connection connection = APIMgtDBUtil.getConnection(); + PreparedStatement preparedStatement = + connection.prepareStatement(SQLConstants.CHECK_PER_API_IS_AVAILABLE_LOGGING_SQL)) { + preparedStatement.setString(1, apiId); + preparedStatement.setString(2, organization); + try (ResultSet resultSet = preparedStatement.executeQuery()) { + return resultSet.next(); + } + } catch (SQLException e) { + handleException("Failed to check if API exists with the UUID " + apiId + " for tenant " + organization, e); + } + return false; + } + public void addAPILoggerPerResource(String organization, String apiId, String logLevel, String resourceMethod, String resourcePath) throws APIManagementException { try (Connection addLoggingCon = APIMgtDBUtil.getConnection()) { diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SQLConstants.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SQLConstants.java index c92fe740f37a..e616a39900e2 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SQLConstants.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SQLConstants.java @@ -3149,6 +3149,8 @@ public class SQLConstants { public static final String CHECK_PER_API_RESOURCE_IS_AVAILABLE_LOGGING_SQL = "SELECT * FROM AM_API_URL_MAPPING WHERE API_ID = (SELECT API_ID FROM AM_API WHERE API_UUID= ? AND " + "ORGANIZATION= ?) AND HTTP_METHOD = ? AND URL_PATTERN = ?"; + public static final String CHECK_PER_API_IS_AVAILABLE_LOGGING_SQL = + "SELECT * FROM AM_API WHERE API_UUID= ? AND ORGANIZATION= ?"; public static final String RETRIEVE_PER_API_LOGGING_OFF_SQL = "SELECT AM_API.API_UUID, AM_API.LOG_LEVEL, AM_API.API_NAME, AM_API.CONTEXT, AM_API.API_VERSION " + "FROM AM_API WHERE AM_API.LOG_LEVEL = 'OFF' AND AM_API.ORGANIZATION = ?";