From afd2be51f4f2d1168a152ec59c5a794d851aaf92 Mon Sep 17 00:00:00 2001 From: John Griffiths Date: Tue, 3 Sep 2024 15:05:26 +0100 Subject: [PATCH 01/11] IDVA6-1502 common template name interceptor for piwik/matomo events --- pom.xml | 16 +++++++++ .../interceptor/TemplateNameInterceptor.java | 33 +++++++++++++++++++ .../templates/layouts/chsBaseLayout.html | 2 ++ 3 files changed, 51 insertions(+) create mode 100644 src/main/java/uk/gov/companieshouse/common/web/interceptor/TemplateNameInterceptor.java diff --git a/pom.xml b/pom.xml index e3285ce..e5d7fce 100644 --- a/pom.xml +++ b/pom.xml @@ -21,8 +21,20 @@ 21 + 3.2.4 3.3.0 + + + + org.springframework.boot + spring-boot-dependencies + ${spring-boot-dependencies.version} + pom + import + + + @@ -36,6 +48,10 @@ + + org.springframework.boot + spring-boot-starter-web + org.yaml snakeyaml diff --git a/src/main/java/uk/gov/companieshouse/common/web/interceptor/TemplateNameInterceptor.java b/src/main/java/uk/gov/companieshouse/common/web/interceptor/TemplateNameInterceptor.java new file mode 100644 index 0000000..7362e0a --- /dev/null +++ b/src/main/java/uk/gov/companieshouse/common/web/interceptor/TemplateNameInterceptor.java @@ -0,0 +1,33 @@ +package uk.gov.companieshouse.common.web.interceptor; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.HandlerInterceptor; +import org.springframework.web.servlet.ModelAndView; + +/* + * Interceptor to add name of template in get requests to model for use by matomo events + * Infers name of template from final part of request uri + */ + +@Component +public class TemplateNameInterceptor implements HandlerInterceptor { + + @Override + public void postHandle(HttpServletRequest request, HttpServletResponse response, + Object handler, ModelAndView modelAndView) { + // Ensure that this is a GET request and a model/view exists + if (request.getMethod().equalsIgnoreCase("GET") && modelAndView != null) { + // Extract the request URI and remove leading '/' + String requestURI = request.getRequestURI().substring(1); + + // Get the last part of the URI (assuming it matches the HTML file name) + String[] uriParts = requestURI.split("/"); + String templateName = uriParts[uriParts.length - 1]; + + // Add the template name to the model + modelAndView.addObject("templateName", templateName); + } + } +} diff --git a/src/main/resources/templates/layouts/chsBaseLayout.html b/src/main/resources/templates/layouts/chsBaseLayout.html index 6339642..e6a56b6 100644 --- a/src/main/resources/templates/layouts/chsBaseLayout.html +++ b/src/main/resources/templates/layouts/chsBaseLayout.html @@ -34,6 +34,7 @@ + @@ -67,6 +68,7 @@ +

local common-web-java

From 6129368d831567d369a3fa8aa42167862014d185 Mon Sep 17 00:00:00 2001 From: John Griffiths Date: Tue, 3 Sep 2024 15:07:28 +0100 Subject: [PATCH 02/11] IDVA6-1502 common template name interceptor for piwik/matomo events --- src/main/resources/templates/layouts/chsBaseLayout.html | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/resources/templates/layouts/chsBaseLayout.html b/src/main/resources/templates/layouts/chsBaseLayout.html index e6a56b6..e047998 100644 --- a/src/main/resources/templates/layouts/chsBaseLayout.html +++ b/src/main/resources/templates/layouts/chsBaseLayout.html @@ -68,7 +68,6 @@ -

local common-web-java

From 54aa88c80d0e2ebeb893db978fe6ec2f60b5e6b5 Mon Sep 17 00:00:00 2001 From: John Griffiths Date: Tue, 3 Sep 2024 15:14:40 +0100 Subject: [PATCH 03/11] IDVA6-1502 common template name interceptor for piwik/matomo events --- pom.xml | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index e5d7fce..87947da 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,6 @@ 4.0.0 - uk.gov.companieshouse common-web-java unversioned jar @@ -59,4 +58,13 @@
+ + + + maven-surefire-plugin + org.apache.maven.plugins + 2.22.2 + + + From e516a3bdd327f486b289dd1a4dcafc487a8fef1d Mon Sep 17 00:00:00 2001 From: John Griffiths Date: Tue, 3 Sep 2024 15:17:58 +0100 Subject: [PATCH 04/11] IDVA6-1502 common template name interceptor for piwik/matomo events --- pom.xml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pom.xml b/pom.xml index 87947da..8d3ea4e 100644 --- a/pom.xml +++ b/pom.xml @@ -22,6 +22,8 @@ 21 3.2.4 3.3.0 + ${java.version} + ${java.version} From 575a85dd958d99e2703930bfb7035fb6c30a500b Mon Sep 17 00:00:00 2001 From: John Griffiths Date: Tue, 3 Sep 2024 15:50:22 +0100 Subject: [PATCH 05/11] IDVA6-1502 common template name interceptor for piwik/matomo events --- pom.xml | 9 --------- 1 file changed, 9 deletions(-) diff --git a/pom.xml b/pom.xml index 8d3ea4e..222d927 100644 --- a/pom.xml +++ b/pom.xml @@ -60,13 +60,4 @@ - - - - maven-surefire-plugin - org.apache.maven.plugins - 2.22.2 - - - From 366aff91f6ce26ef58fe75c55d9a00568f45584d Mon Sep 17 00:00:00 2001 From: John Griffiths Date: Tue, 3 Sep 2024 16:15:27 +0100 Subject: [PATCH 06/11] IDVA6-1502 common template name interceptor for piwik/matomo events --- README.md | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index bb354d1..7abbf3f 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ headers, footers, continue buttons, etc. Example usage of the standard layout and fragments can be found in ```authentication-service```, ```oauth-web``` and ```user.web.identity.ch.gov.uk``` -Welsh language support is being added and requires the addition of ```localse/common-messages``` to the basenames of the +Welsh language support is being added and requires the addition of ```locales/common-messages``` to the basenames of the ```MessageCongig``` class in the service eg: ``` messageSource.setBasenames("locales/messages", "locales/common-messages"); @@ -90,6 +90,25 @@ Requires ```serviceName``` variable to be set to the name of the service using t The following fragments are used by this baseLayout depending on the setting of variables described in each fragment. +## Common Interceptors + +### TemplateNameInterceptor + +Sets templateName model attribute to the name of the template (determined by last part of http request). + +Added into a service by using the following in your interceptorConfig: +``` +import uk.gov.companieshouse.common.web.interceptor.TemplateNameInterceptor; + +@Override +public void addInterceptors(@NonNull InterceptorRegistry registry) { + ... + // Add interceptor to get template names for matomo events + registry.addInterceptor(new TemplateNameInterceptor()); + ... +} +``` + ## Fragments ### piwikWithCookieCheck.html @@ -132,10 +151,6 @@ Fragment that provides a button to go backwards in the journey. Requires a ```ba If the ```backLink``` model attribute is absent, the 'back' link won't appear. If set, it should contain href for back button -### piwik.html - -Fragment that listens to user interactions. Contains a customisable field ```${moduleName}``` which is set in the ```chsBaseLayout.html```, as mentioned above. This fragment requires the ```piwik.url``` and ```piwik.siteId``` properties in your project's ```application.properties``` file. - ### footer.html Fragment that provides useful links to the user below the main page content. Links give information about our policies, Cookies, contacting Companies House and information specific to Developers. @@ -159,4 +174,9 @@ Fragment that contains several links and information for the user. Links to Your Generic error page that gives the user an option to email Companies House. Requires ```enquiries``` property to be set in the service's ```application.properties``` or ```application.yaml``` files for the "email us" email address. -e.g. ```enquiries=mailto:enquiries@companieshouse.gov.uk``` \ No newline at end of file +e.g. ```enquiries=mailto:enquiries@companieshouse.gov.uk``` + +### piwik.html + +Fragment that listens to user interactions. remains for legacy reasons - not used by chsBaseLayout.html +Contains a customisable field ```${moduleName}``` which is set in the ```chsBaseLayout.html```, as mentioned above. This fragment requires the ```piwik.url``` and ```piwik.siteId``` properties in your project's ```application.properties``` file. From be9ec83abfc50adc81dab4000df38cb538dd05a2 Mon Sep 17 00:00:00 2001 From: John Griffiths Date: Wed, 4 Sep 2024 09:39:36 +0100 Subject: [PATCH 07/11] IDVA6-1502 Add unit tests --- pom.xml | 43 +++++++++++++++- .../interceptor/TemplateNameInterceptor.java | 10 ++-- .../TemplateNameInterceptorUnitTest.java | 50 +++++++++++++++++++ 3 files changed, 97 insertions(+), 6 deletions(-) create mode 100644 src/test/java/uk/gov/companieshouse/common/web/unit/interceptor/TemplateNameInterceptorUnitTest.java diff --git a/pom.xml b/pom.xml index 222d927..e56487c 100644 --- a/pom.xml +++ b/pom.xml @@ -20,10 +20,11 @@ 21 - 3.2.4 - 3.3.0 + 3.3.3 + 3.3.3 ${java.version} ${java.version} + 1.3.2 @@ -58,6 +59,44 @@ snakeyaml 2.2 + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.sonarsource.scanner.maven + sonar-maven-plugin + ${sonar-maven-plugin.version} + test + + + + + maven-surefire-plugin + + + + + + + junit-platform-surefire-provider + org.junit.platform + ${junit-platform-surefire-provider.version} + + + org.apache.maven.plugins + 2.22.2 + + + jacoco-maven-plugin + org.jacoco + + + + diff --git a/src/main/java/uk/gov/companieshouse/common/web/interceptor/TemplateNameInterceptor.java b/src/main/java/uk/gov/companieshouse/common/web/interceptor/TemplateNameInterceptor.java index 7362e0a..34d51af 100644 --- a/src/main/java/uk/gov/companieshouse/common/web/interceptor/TemplateNameInterceptor.java +++ b/src/main/java/uk/gov/companieshouse/common/web/interceptor/TemplateNameInterceptor.java @@ -2,6 +2,7 @@ import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; +import org.springframework.lang.NonNull; import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; @@ -15,10 +16,11 @@ public class TemplateNameInterceptor implements HandlerInterceptor { @Override - public void postHandle(HttpServletRequest request, HttpServletResponse response, - Object handler, ModelAndView modelAndView) { - // Ensure that this is a GET request and a model/view exists - if (request.getMethod().equalsIgnoreCase("GET") && modelAndView != null) { + public void postHandle(HttpServletRequest request, @NonNull HttpServletResponse response, + @NonNull Object handler, ModelAndView modelAndView) { + + // Ensure that this is a GET request + if (request.getMethod().equalsIgnoreCase("GET")) { // Extract the request URI and remove leading '/' String requestURI = request.getRequestURI().substring(1); diff --git a/src/test/java/uk/gov/companieshouse/common/web/unit/interceptor/TemplateNameInterceptorUnitTest.java b/src/test/java/uk/gov/companieshouse/common/web/unit/interceptor/TemplateNameInterceptorUnitTest.java new file mode 100644 index 0000000..4daeb36 --- /dev/null +++ b/src/test/java/uk/gov/companieshouse/common/web/unit/interceptor/TemplateNameInterceptorUnitTest.java @@ -0,0 +1,50 @@ +package uk.gov.companieshouse.common.web.unit.interceptor; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.web.servlet.ModelAndView; +import uk.gov.companieshouse.common.web.interceptor.TemplateNameInterceptor; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +@ExtendWith(MockitoExtension.class) +class TemplateNameInterceptorUnitTest { + @InjectMocks + private TemplateNameInterceptor interceptor; + + + @Test + void postHandle_GetSuccess() { + + var request = new MockHttpServletRequest(); + var response = new MockHttpServletResponse(); + var modelAndView = new ModelAndView(); + + request.setMethod("GET"); + request.setRequestURI("/route-name/page-name"); + + interceptor.postHandle(request, response, new Object(), modelAndView); + + assertEquals("page-name", modelAndView.getModel().get("templateName")); + } + + @Test + void postHandle_NotGet() { + + var request = new MockHttpServletRequest(); + var response = new MockHttpServletResponse(); + var modelAndView = new ModelAndView(); + + request.setMethod("POST"); + request.setRequestURI("/route-name/page-name"); + + interceptor.postHandle(request, response, new Object(), modelAndView); + + assertNull(modelAndView.getModel().get("templateName")); + } +} From f12dde57026dc4aa313c2846dad978c67ab6c54b Mon Sep 17 00:00:00 2001 From: John Griffiths Date: Wed, 4 Sep 2024 09:50:09 +0100 Subject: [PATCH 08/11] IDVA6-1502 Add sonar-pr-analysis profile --- pom.xml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pom.xml b/pom.xml index e56487c..404f327 100644 --- a/pom.xml +++ b/pom.xml @@ -26,6 +26,14 @@ ${java.version} 1.3.2 + + + sonar-pr-analysis + + main + + + From e3abcf4c119e92d50087e220d827e18d55d3b09c Mon Sep 17 00:00:00 2001 From: John Griffiths Date: Wed, 4 Sep 2024 09:59:18 +0100 Subject: [PATCH 09/11] IDVA6-1502 use latest parent pom --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 404f327..4234cc7 100644 --- a/pom.xml +++ b/pom.xml @@ -15,7 +15,7 @@ uk.gov.companieshouse companies-house-parent - 2.1.5 + 2.1.6 From 58ab4bbb4f77243dd6785132b84a1f4856589def Mon Sep 17 00:00:00 2001 From: John Griffiths Date: Wed, 4 Sep 2024 10:16:18 +0100 Subject: [PATCH 10/11] IDVA6-1502 code smell --- .../common/web/interceptor/TemplateNameInterceptor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/uk/gov/companieshouse/common/web/interceptor/TemplateNameInterceptor.java b/src/main/java/uk/gov/companieshouse/common/web/interceptor/TemplateNameInterceptor.java index 34d51af..26113e3 100644 --- a/src/main/java/uk/gov/companieshouse/common/web/interceptor/TemplateNameInterceptor.java +++ b/src/main/java/uk/gov/companieshouse/common/web/interceptor/TemplateNameInterceptor.java @@ -22,7 +22,7 @@ public void postHandle(HttpServletRequest request, @NonNull HttpServletResponse // Ensure that this is a GET request if (request.getMethod().equalsIgnoreCase("GET")) { // Extract the request URI and remove leading '/' - String requestURI = request.getRequestURI().substring(1); + var requestURI = request.getRequestURI().substring(1); // Get the last part of the URI (assuming it matches the HTML file name) String[] uriParts = requestURI.split("/"); From 8506b0f84ec686309fe297c6e520082b16a8f888 Mon Sep 17 00:00:00 2001 From: John Griffiths Date: Wed, 4 Sep 2024 10:44:27 +0100 Subject: [PATCH 11/11] IDVA6-1502 missing suppression file --- suppress.xml | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 suppress.xml diff --git a/suppress.xml b/suppress.xml new file mode 100644 index 0000000..fbf9371 --- /dev/null +++ b/suppress.xml @@ -0,0 +1,3 @@ + + +