Skip to content

Commit

Permalink
Merge pull request #53 from companieshouse/feature/IDVA6-1502-common-…
Browse files Browse the repository at this point in the history
…template-name-interceptor-for-piwik-events

IDVA6-1502 common template name interceptor for piwik/matomo events
  • Loading branch information
griffithsjd authored Sep 4, 2024
2 parents 3b64da8 + 8506b0f commit a9d6ad9
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 9 deletions.
32 changes: 26 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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.
70 changes: 67 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

<modelVersion>4.0.0</modelVersion>

<groupId>uk.gov.companieshouse</groupId>
<artifactId>common-web-java</artifactId>
<version>unversioned</version>
<packaging>jar</packaging>
Expand All @@ -16,13 +15,36 @@
<parent>
<groupId>uk.gov.companieshouse</groupId>
<artifactId>companies-house-parent</artifactId>
<version>2.1.5</version>
<version>2.1.6</version>
</parent>

<properties>
<java.version>21</java.version>
<spring-boot-starter-thymeleaf.version>3.3.0</spring-boot-starter-thymeleaf.version>
<spring-boot-dependencies.version>3.3.3</spring-boot-dependencies.version>
<spring-boot-starter-thymeleaf.version>3.3.3</spring-boot-starter-thymeleaf.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<junit-platform-surefire-provider.version>1.3.2</junit-platform-surefire-provider.version>
</properties>
<profiles>
<profile>
<id>sonar-pr-analysis</id>
<properties>
<sonar.pullrequest.base>main</sonar.pullrequest.base>
</properties>
</profile>
</profiles>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot-dependencies.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
Expand All @@ -36,11 +58,53 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>2.2</version>
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>${sonar-maven-plugin.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<environmentVariables>
</environmentVariables>
</configuration>
<dependencies>
<dependency>
<artifactId>junit-platform-surefire-provider</artifactId>
<groupId>org.junit.platform</groupId>
<version>${junit-platform-surefire-provider.version}</version>
</dependency>
</dependencies>
<groupId>org.apache.maven.plugins</groupId>
<version>2.22.2</version>
</plugin>
<plugin>
<artifactId>jacoco-maven-plugin</artifactId>
<groupId>org.jacoco</groupId>
</plugin>
</plugins>
</build>

</project>
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);
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/templates/layouts/chsBaseLayout.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
</head>
<body class="govuk-template__body app-body-class">

<!-- templateName set by TemplateNameInterceptor and used in piwik-enable.js below for piwik/matomo event ids -->
<div id="templateName" th:attr="data-id=${templateName},data-metadata=${metadata}" hidden></div>

<script th:src="@{{cdnUrl}/javascripts/app/body-js-enable.js(cdnUrl=${@environment.getProperty('cdn.url')})}" type="text/javascript"></script>
Expand Down
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"));
}
}
3 changes: 3 additions & 0 deletions suppress.xml
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>

0 comments on commit a9d6ad9

Please sign in to comment.