-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added functionality to use a SkipInertia annotation for controllers a…
…nd/or actions that should not be handled by the InertiaInterceptor.
- Loading branch information
Showing
5 changed files
with
197 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/main/java/grails/plugin/inertia/annotation/AnnotationExcluder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package grails.plugin.inertia.annotation; | ||
|
||
import grails.artefact.Interceptor; | ||
import grails.core.GrailsApplication; | ||
import grails.core.GrailsClass; | ||
import grails.core.GrailsControllerClass; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Modifier; | ||
import java.util.Arrays; | ||
import java.util.LinkedHashMap; | ||
import java.util.Objects; | ||
|
||
public class AnnotationExcluder { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(AnnotationExcluder.class); | ||
|
||
/** | ||
* Excludes all Controllers and/or Actions that have the provided annotation applied to them from | ||
* triggering the provided interceptor. | ||
* @param interceptor The interceptor to exclude the matches for | ||
* @param grailsApplication The current grails application | ||
* @param annotation The annotation to search for | ||
*/ | ||
public static void excludeAnnotations(final Interceptor interceptor, final GrailsApplication grailsApplication, final Class<? extends Annotation> annotation) { | ||
final GrailsClass[] controllers = grailsApplication.getArtefacts("Controller"); | ||
|
||
for (GrailsClass controller : controllers) { | ||
|
||
final String controllerName = controller.getLogicalPropertyName(); | ||
final Class<?> controllerClazz = controller.getClazz(); | ||
final Object namespace = getControllerNamespace(controllerClazz); | ||
final Annotation classAnnotation = controllerClazz.getAnnotation(annotation); | ||
|
||
if (classAnnotation != null) { | ||
handleAction("*", namespace, controllerName, interceptor); | ||
} else{ | ||
Arrays.stream(controllerClazz.getMethods()) | ||
.filter(method -> method.getAnnotation(annotation) != null && Modifier.isPublic(method.getModifiers())) | ||
.forEach(methodAction -> handleAction(methodAction.getName(), namespace, controllerName, interceptor)); | ||
|
||
Arrays.stream(controllerClazz.getDeclaredFields()) | ||
.filter( field -> field.getAnnotation(annotation) != null ) | ||
.forEach( fieldAction -> handleAction(fieldAction.getName(), namespace, controllerName, interceptor)); | ||
} | ||
} | ||
} | ||
|
||
private static void handleAction(String actionName, Object namespace, String controllerName, Interceptor interceptor) { | ||
if(log.isDebugEnabled()) log.debug("Excluding namespace: {}, controller: {}, action: {} from interceptor: {}", namespace, controllerName, actionName, interceptor.getClass().getName()); | ||
LinkedHashMap<String,Object> args = new LinkedHashMap<>(); | ||
args.put("namespace", namespace); | ||
args.put("controller", controllerName); | ||
args.put("action", actionName); | ||
interceptor.getMatchers().forEach(matcher -> matcher.except(args)); | ||
} | ||
|
||
private static Object getControllerNamespace(Class<?> controllerClazz) { | ||
return Arrays.stream(controllerClazz.getDeclaredFields()) | ||
.filter(field -> Objects.equals(GrailsControllerClass.NAMESPACE_PROPERTY, field.getName()) && Modifier.isStatic(field.getModifiers())) | ||
.findFirst() | ||
.map(field -> { try { return field.get(null); } catch (IllegalAccessException e) { return null; } }) | ||
.orElse(null); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/grails/plugin/inertia/annotation/SkipInertia.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package grails.plugin.inertia.annotation; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.METHOD, ElementType.TYPE, ElementType.FIELD}) | ||
public @interface SkipInertia { | ||
String value() default ""; | ||
} |
98 changes: 98 additions & 0 deletions
98
src/test/groovy/grails/plugin/inertia/annotation/AnnotationExcluderSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package grails.plugin.inertia.annotation | ||
|
||
import grails.artefact.Interceptor | ||
import grails.testing.web.interceptor.InterceptorUnitTest | ||
import grails.web.Controller | ||
import spock.lang.Specification | ||
|
||
import java.lang.annotation.ElementType | ||
import java.lang.annotation.Retention | ||
import java.lang.annotation.RetentionPolicy | ||
import java.lang.annotation.Target | ||
|
||
class AnnotationExcluderSpec extends Specification implements InterceptorUnitTest<AllMatchingInterceptor> { | ||
|
||
def 'it excludes controller classes with annotation'() { | ||
given: | ||
grailsApplication.addArtefact('Controller', SkipClassController) | ||
|
||
when: | ||
withRequest(controller: 'skipClass', action: 'action') | ||
|
||
then: | ||
interceptor.doesMatch() | ||
|
||
when: | ||
AnnotationExcluder.excludeAnnotations(interceptor, grailsApplication, SkipAnnotation) | ||
|
||
then: | ||
!interceptor.doesMatch() | ||
} | ||
|
||
def 'it excludes controller method actions with annotation'() { | ||
given: | ||
grailsApplication.addArtefact('Controller', SkipMethodActionController) | ||
|
||
when: | ||
withRequest(controller: 'skipMethodAction', action: 'action') | ||
|
||
then: | ||
interceptor.doesMatch() | ||
|
||
when: | ||
AnnotationExcluder.excludeAnnotations(interceptor, grailsApplication, SkipAnnotation) | ||
|
||
then: | ||
!interceptor.doesMatch() | ||
} | ||
|
||
def 'it excludes controller closure actions with annotation'() { | ||
given: | ||
grailsApplication.addArtefact('Controller', SkipClosureActionController) | ||
|
||
when: | ||
withRequest(controller: 'skipClosureAction', action: 'action') | ||
|
||
then: | ||
interceptor.doesMatch() | ||
|
||
when: | ||
AnnotationExcluder.excludeAnnotations(interceptor, grailsApplication, SkipAnnotation) | ||
|
||
then: | ||
!interceptor.doesMatch() | ||
} | ||
} | ||
|
||
@Controller | ||
@SkipAnnotation | ||
class SkipClassController { | ||
|
||
@SuppressWarnings('unused') | ||
def action() {} | ||
} | ||
|
||
@Controller | ||
class SkipMethodActionController { | ||
|
||
@SkipAnnotation | ||
@SuppressWarnings('unused') | ||
def action() {} | ||
} | ||
|
||
@Controller | ||
class SkipClosureActionController { | ||
@SkipAnnotation | ||
@SuppressWarnings('unused') | ||
def action = {} | ||
} | ||
|
||
class AllMatchingInterceptor implements Interceptor { | ||
AllMatchingInterceptor() { matchAll() } | ||
} | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target([ElementType.METHOD, ElementType.TYPE, ElementType.FIELD]) | ||
@interface SkipAnnotation { | ||
String value() default ""; | ||
} |