Skip to content

Commit

Permalink
Fix minor issue in API Level logging
Browse files Browse the repository at this point in the history
  • Loading branch information
AnuGayan committed Oct 22, 2024
1 parent c938dc1 commit 18a8db7
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ?";
Expand Down

0 comments on commit 18a8db7

Please sign in to comment.