Skip to content

Commit

Permalink
Merge pull request #68 from companieshouse/feature/IDVA6-1930-improve…
Browse files Browse the repository at this point in the history
…-TemplateNameInterceptor

IDVA6-1930 Improve templateName checking
  • Loading branch information
griffithsjd authored Nov 21, 2024
2 parents b637993 + 90c70e7 commit 983c831
Showing 1 changed file with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,16 @@ public void postHandle(HttpServletRequest request, @NonNull HttpServletResponse
@NonNull Object handler, ModelAndView modelAndView) {

// Ensure that a model/view exists
if ((request.getMethod().equalsIgnoreCase("GET") || request.getMethod().equalsIgnoreCase("POST"))
&& modelAndView != null) {
if (modelAndView == null)
return;

// Do nothing if templateName already exists
var model = modelAndView.getModel();
if (model.containsKey("templateName"))
return;

// Look for GET and POST requests only
if ((request.getMethod().equalsIgnoreCase("GET") || request.getMethod().equalsIgnoreCase("POST"))) {

// Exclude redirect responses
var viewName = modelAndView.getViewName();
Expand All @@ -36,10 +44,14 @@ public void postHandle(HttpServletRequest request, @NonNull HttpServletResponse

// Get the last part of the URI (assuming it matches the HTML file name)
String[] uriParts = requestURI.split("/");
String templateName = uriParts[uriParts.length - 1];
if (uriParts.length > 0) {
String templateName = uriParts[uriParts.length - 1];

// Add the template name to the model
modelAndView.addObject("templateName", templateName);
if (!templateName.isBlank()) {
// Add the template name to the model
model.put("templateName", templateName);
}
}
}
}
}

0 comments on commit 983c831

Please sign in to comment.