-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from companieshouse/feature/IDVA6-1502-common-…
…template-name-interceptor-for-piwik-events IDVA6-1502 common template name interceptor for piwik/matomo events
- Loading branch information
Showing
6 changed files
with
182 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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:[email protected]``` | ||
e.g. ```enquiries=mailto:[email protected]``` | ||
|
||
### 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. |
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
35 changes: 35 additions & 0 deletions
35
src/main/java/uk/gov/companieshouse/common/web/interceptor/TemplateNameInterceptor.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,35 @@ | ||
package uk.gov.companieshouse.common.web.interceptor; | ||
|
||
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; | ||
|
||
/* | ||
* 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, @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 '/' | ||
var 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); | ||
} | ||
} | ||
} |
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
50 changes: 50 additions & 0 deletions
50
...va/uk/gov/companieshouse/common/web/unit/interceptor/TemplateNameInterceptorUnitTest.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,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")); | ||
} | ||
} |
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,3 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<suppressions xmlns="https://jeremylong.github.io/DependencyCheck/dependency-suppression.1.3.xsd"> | ||
</suppressions> |