Skip to content

Commit

Permalink
Merge pull request #2893 from atlanhq/enable-maintenance
Browse files Browse the repository at this point in the history
nit: refactoring the code for better readability
  • Loading branch information
sumandas0 authored Mar 27, 2024
2 parents fdc10f9 + 1f1543e commit edac716
Showing 1 changed file with 36 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,32 +86,15 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo
// If maintenance mode is enabled, return a 503
if (AtlasConfiguration.ATLAS_MAINTENANCE_MODE.getBoolean()) {
// Block all the POST, PUT, DELETE operations
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
if (httpServletRequest.getMethod().equals(HttpMethod.POST) || httpServletRequest.getMethod().equals(HttpMethod.PUT) || httpServletRequest.getMethod().equals(HttpMethod.DELETE)) {
// If API path contains indexsearch, then allow the request
if (httpServletRequest.getRequestURI().contains("indexsearch")) {
filterChain.doFilter(servletRequest, servletResponse);
} else {
LOG.error("Maintenance mode enabled. Blocking request: {}", httpServletRequest.getRequestURI());
// Properly handle the maintenance mode and send properly formatted response
httpServletResponse.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
httpServletResponse.setContentType("application/json");
httpServletResponse.setCharacterEncoding("UTF-8");
AtlasBaseException serverException = new AtlasBaseException(AtlasErrorCode.MAINTENANCE_MODE_ENABLED,
AtlasErrorCode.MAINTENANCE_MODE_ENABLED.getFormattedErrorMessage());
HashMap<String, Object> errorMap = new HashMap<>();
errorMap.put("errorCode", serverException.getAtlasErrorCode().getErrorCode());
errorMap.put("errorMessage", serverException.getMessage());
String jsonResp = AtlasType.toJson(errorMap);
httpServletResponse.getOutputStream().write(jsonResp.getBytes());
httpServletResponse.getOutputStream().flush();
return;
}
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
if (isBlockedMethod(request.getMethod()) && !request.getRequestURI().contains("indexsearch")) {
LOG.error("Maintenance mode enabled. Blocking request: {}", request.getRequestURI());
sendMaintenanceModeResponse(response);
return; // Stop further processing
}
}



if (isFilteredURI(servletRequest)) {
LOG.debug("Is a filtered URI: {}. Passing request downstream.",
((HttpServletRequest)servletRequest).getRequestURI());
Expand Down Expand Up @@ -145,6 +128,7 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo

final String adminUriNotFiltered[] = { "/admin/export", "/admin/import", "/admin/importfile", "/admin/audits",
"/admin/purge", "/admin/expimp/audit", "/admin/metrics", "/admin/server", "/admin/audit/", "admin/tasks"};

private boolean isFilteredURI(ServletRequest servletRequest) {
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
String requestURI = httpServletRequest.getRequestURI();
Expand All @@ -163,6 +147,34 @@ private boolean isFilteredURI(ServletRequest servletRequest) {
}
}

private void sendMaintenanceModeResponse(HttpServletResponse response) throws IOException {
response.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");

AtlasBaseException serverException = new AtlasBaseException(AtlasErrorCode.MAINTENANCE_MODE_ENABLED,
AtlasErrorCode.MAINTENANCE_MODE_ENABLED.getFormattedErrorMessage());

HashMap<String, Object> errorMap = new HashMap<>();
errorMap.put("errorCode", serverException.getAtlasErrorCode().getErrorCode());
errorMap.put("errorMessage", serverException.getMessage());

String jsonResponse = AtlasType.toJson(errorMap);
response.getOutputStream().write(jsonResponse.getBytes());
response.getOutputStream().flush();
}

private boolean isBlockedMethod(String method) {
switch (method) {
case HttpMethod.POST:
case HttpMethod.PUT:
case HttpMethod.DELETE:
return true;
default:
return false;
}
}

private boolean isRootURI(ServletRequest servletRequest) {
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
String requestURI = httpServletRequest.getRequestURI();
Expand Down

0 comments on commit edac716

Please sign in to comment.