Skip to content

Commit

Permalink
Reduce cyclomatic complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
Niek Raaijmakers committed Oct 24, 2024
1 parent bc4e9e5 commit b37eb0b
Showing 1 changed file with 30 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,42 +104,39 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo

ValueMap parameters = ValueMap.EMPTY;

if(servletRequest instanceof SlingHttpServletRequest){

SlingHttpServletRequest request = (SlingHttpServletRequest) servletRequest;

Predicate<String> typeCheckFn = (resourceType) -> request.getResourceResolver().isResourceType(request.getResource(), resourceType);

if(typeCheckFn.test(RESOURCE_TYPE)){
Object ignoreResourceType = request.getAttribute(REQ_ATTR_IGNORE_CHILDREN_RESOURCE_TYPE);
Object namespace = request.getAttribute(REQ_ATTR_NAMESPACE);
//if children ignore is active, but we have a new include, we de-activate the ignore children.
if(ignoreResourceType != null){
request.removeAttribute(REQ_ATTR_IGNORE_CHILDREN_RESOURCE_TYPE);
request.removeAttribute(REQ_ATTR_NAMESPACE);
}
performFilter(request, servletResponse, chain, parameters);
// we are now out of the nested include context. re-activate the ignore children if it was active before.
if(ignoreResourceType != null){
request.setAttribute(REQ_ATTR_IGNORE_CHILDREN_RESOURCE_TYPE, ignoreResourceType);
request.setAttribute(REQ_ATTR_NAMESPACE, namespace);
}
return;
}else if(resourceTypesIgnoreChildren.stream().anyMatch(typeCheckFn)){
boolean ignoreChildren = resourceTypesIgnoreChildren.stream().anyMatch(typeCheckFn);
if(ignoreChildren){
request.setAttribute(REQ_ATTR_IGNORE_CHILDREN_RESOURCE_TYPE, request.getResource().getResourceType());
}
chain.doFilter(servletRequest, servletResponse);
if(ignoreChildren){
request.removeAttribute(REQ_ATTR_IGNORE_CHILDREN_RESOURCE_TYPE);
}
return;
}
if(!(servletRequest instanceof SlingHttpServletRequest)){
chain.doFilter(servletRequest, servletResponse);
return;
}
SlingHttpServletRequest request = (SlingHttpServletRequest) servletRequest;

Predicate<String> typeCheckFn = (resourceType) -> request.getResourceResolver().isResourceType(request.getResource(), resourceType);

if(typeCheckFn.test(RESOURCE_TYPE)){
Object ignoreResourceType = request.getAttribute(REQ_ATTR_IGNORE_CHILDREN_RESOURCE_TYPE);
Object namespace = request.getAttribute(REQ_ATTR_NAMESPACE);
//if children ignore is active, but we have a new include, we de-activate the ignore children.
if(ignoreResourceType != null){
request.removeAttribute(REQ_ATTR_IGNORE_CHILDREN_RESOURCE_TYPE);
request.removeAttribute(REQ_ATTR_NAMESPACE);
}
performFilter(request, servletResponse, chain, parameters);
// we are now out of the nested include context. re-activate the ignore children if it was active before.
if(ignoreResourceType != null){
request.setAttribute(REQ_ATTR_IGNORE_CHILDREN_RESOURCE_TYPE, ignoreResourceType);
request.setAttribute(REQ_ATTR_NAMESPACE, namespace);
}
}else if(resourceTypesIgnoreChildren.stream().anyMatch(typeCheckFn)){
boolean ignoreChildren = resourceTypesIgnoreChildren.stream().anyMatch(typeCheckFn);
if(ignoreChildren){
request.setAttribute(REQ_ATTR_IGNORE_CHILDREN_RESOURCE_TYPE, request.getResource().getResourceType());
}
chain.doFilter(servletRequest, servletResponse);
if(ignoreChildren){
request.removeAttribute(REQ_ATTR_IGNORE_CHILDREN_RESOURCE_TYPE);
}
}

chain.doFilter(servletRequest, servletResponse);
}

private void performFilter(SlingHttpServletRequest request, ServletResponse servletResponse, FilterChain chain, ValueMap parameters) throws IOException, ServletException {
Expand Down

0 comments on commit b37eb0b

Please sign in to comment.