From 74635514fe557f0d9eed72ad3cc12a74e3039382 Mon Sep 17 00:00:00 2001 From: "Brian M. Long" Date: Thu, 14 Nov 2024 11:19:33 -0500 Subject: [PATCH] added WebScriptContext request scope support --- .../annotations/aspect/WebScriptAspect.java | 50 +++++++++++++++++++ .../annotations/context/WebScriptContext.java | 31 ++++++++++++ core/src/main/resources/META-INF/aop.xml | 2 + 3 files changed, 83 insertions(+) create mode 100644 core/src/main/java/com/inteligr8/alfresco/annotations/aspect/WebScriptAspect.java create mode 100644 core/src/main/java/com/inteligr8/alfresco/annotations/context/WebScriptContext.java diff --git a/core/src/main/java/com/inteligr8/alfresco/annotations/aspect/WebScriptAspect.java b/core/src/main/java/com/inteligr8/alfresco/annotations/aspect/WebScriptAspect.java new file mode 100644 index 0000000..db9483c --- /dev/null +++ b/core/src/main/java/com/inteligr8/alfresco/annotations/aspect/WebScriptAspect.java @@ -0,0 +1,50 @@ +package com.inteligr8.alfresco.annotations.aspect; + +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Pointcut; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Scope; +import org.springframework.extensions.webscripts.WebScript; +import org.springframework.extensions.webscripts.WebScriptRequest; +import org.springframework.extensions.webscripts.WebScriptResponse; +import org.springframework.web.context.WebApplicationContext; + +import com.inteligr8.alfresco.annotations.context.WebScriptContext; + +/** + * This aspect captures the WebScript execution context. + */ +@Aspect +public class WebScriptAspect { + + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + private ThreadLocal context = new ThreadLocal<>(); + + @Pointcut("execution(public void org.springframework.extensions.webscripts.WebScript.execute(..))") + public void isWebScriptExecute() { + } + + @Around("isWebScriptExecute()") + public Object execute(ProceedingJoinPoint joinPoint) throws Throwable { + this.logger.trace("execute({})", joinPoint); + + WebScript ws = (WebScript) joinPoint.getTarget(); + WebScriptRequest req = (WebScriptRequest) joinPoint.getArgs()[0]; + WebScriptResponse res = (WebScriptResponse) joinPoint.getArgs()[1]; + this.context.set(new WebScriptContext(ws, req, res)); + + return joinPoint.proceed(); + } + + @Bean + @Scope(WebApplicationContext.SCOPE_REQUEST) + public WebScriptContext getContext() { + return this.context.get(); + } + +} diff --git a/core/src/main/java/com/inteligr8/alfresco/annotations/context/WebScriptContext.java b/core/src/main/java/com/inteligr8/alfresco/annotations/context/WebScriptContext.java new file mode 100644 index 0000000..336d0a4 --- /dev/null +++ b/core/src/main/java/com/inteligr8/alfresco/annotations/context/WebScriptContext.java @@ -0,0 +1,31 @@ +package com.inteligr8.alfresco.annotations.context; + +import org.springframework.extensions.webscripts.WebScript; +import org.springframework.extensions.webscripts.WebScriptRequest; +import org.springframework.extensions.webscripts.WebScriptResponse; + +public class WebScriptContext { + + private final WebScript webscript; + private final WebScriptRequest request; + private final WebScriptResponse response; + + public WebScriptContext(WebScript webscript, WebScriptRequest request, WebScriptResponse response) { + this.webscript = webscript; + this.request = request; + this.response = response; + } + + public WebScript getWebscript() { + return webscript; + } + + public WebScriptRequest getRequest() { + return request; + } + + public WebScriptResponse getResponse() { + return response; + } + +} diff --git a/core/src/main/resources/META-INF/aop.xml b/core/src/main/resources/META-INF/aop.xml index 71b86ab..e1e685a 100644 --- a/core/src/main/resources/META-INF/aop.xml +++ b/core/src/main/resources/META-INF/aop.xml @@ -13,5 +13,7 @@ + + \ No newline at end of file