Skip to content

Commit b107721

Browse files
authored
fix: Using a custom RouteRegistry (#11033)
Make it possible to create a custom RouteRegistry and add test module for it. Fixes #11022
1 parent 22d5fed commit b107721

17 files changed

Lines changed: 596 additions & 72 deletions

File tree

‎flow-server/src/main/java/com/vaadin/flow/router/Router.java‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,21 @@
1616
package com.vaadin.flow.router;
1717

1818
import java.io.Serializable;
19+
import java.util.HashMap;
1920
import java.util.Map;
2021
import java.util.Optional;
2122

2223
import javax.servlet.http.HttpServletResponse;
2324

2425
import com.vaadin.flow.component.UI;
26+
import com.vaadin.flow.router.internal.AbstractRouteRegistry;
2527
import com.vaadin.flow.router.internal.DefaultRouteResolver;
2628
import com.vaadin.flow.router.internal.ErrorStateRenderer;
2729
import com.vaadin.flow.router.internal.ErrorTargetEntry;
2830
import com.vaadin.flow.router.internal.InternalRedirectHandler;
2931
import com.vaadin.flow.router.internal.NavigationStateRenderer;
3032
import com.vaadin.flow.router.internal.ResolveRequest;
33+
import com.vaadin.flow.server.ErrorRouteRegistry;
3134
import com.vaadin.flow.server.RouteRegistry;
3235
import com.vaadin.flow.server.SessionRouteRegistry;
3336
import com.vaadin.flow.server.VaadinRequest;
@@ -313,8 +316,8 @@ public RouteRegistry getRegistry() {
313316
*/
314317
public Optional<ErrorTargetEntry> getErrorNavigationTarget(
315318
Exception exception) {
316-
if (registry instanceof ApplicationRouteRegistry) {
317-
return ((ApplicationRouteRegistry) registry)
319+
if (registry instanceof ErrorRouteRegistry) {
320+
return ((ErrorRouteRegistry) registry)
318321
.getErrorNavigationTarget(exception);
319322
}
320323
return Optional.empty();

‎flow-server/src/main/java/com/vaadin/flow/router/internal/AbstractRouteRegistry.java‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,30 @@ private void configureWithFullTemplate(String path,
357357
});
358358
}
359359

360+
@Override
361+
public NavigationRouteTarget getNavigationRouteTarget(String url) {
362+
return getConfiguration().getNavigationRouteTarget(url);
363+
}
364+
365+
@Override
366+
public RouteTarget getRouteTarget(Class<? extends Component> target,
367+
RouteParameters parameters) {
368+
return getConfiguration().getRouteTarget(target, parameters);
369+
}
370+
371+
@Override
372+
public Optional<Class<? extends Component>> getNavigationTarget(
373+
String url) {
374+
Objects.requireNonNull(url, "url must not be null.");
375+
return getConfiguration().getTarget(url);
376+
}
377+
378+
@Override
379+
public Optional<Class<? extends Component>> getNavigationTarget(String url,
380+
List<String> segments) {
381+
return getNavigationTarget(PathUtil.getPath(url, segments));
382+
}
383+
360384
/**
361385
* Add the given error target to the exceptionTargetMap. This will handle
362386
* existing overlapping exception types by assigning the correct error

‎flow-server/src/main/java/com/vaadin/flow/router/internal/NavigationRouteTarget.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@
3333
public class NavigationRouteTarget implements Serializable {
3434

3535
// Processed path.
36-
private String path;
36+
private final String path;
3737

3838
// Target found for the specified path.
39-
private RouteTarget routeTarget;
39+
private final RouteTarget routeTarget;
4040

4141
// Parameters found in the path.
42-
private RouteParameters parameters;
42+
private final RouteParameters parameters;
4343

44-
NavigationRouteTarget(String path, RouteTarget routeTarget,
44+
public NavigationRouteTarget(String path, RouteTarget routeTarget,
4545
Map<String, String> parameters) {
4646
this.path = path;
4747
this.routeTarget = routeTarget;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2000-2021 Vaadin Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.vaadin.flow.server;
18+
19+
import java.io.Serializable;
20+
import java.util.Optional;
21+
22+
import com.vaadin.flow.router.internal.ErrorTargetEntry;
23+
24+
/**
25+
* Interface class for RouteRegistries that can be used to request for error
26+
* navigation views for Exceptions.
27+
*
28+
* @since
29+
*/
30+
public interface ErrorRouteRegistry extends Serializable {
31+
/**
32+
* Get a registered navigation target for given exception. First we will
33+
* search for a matching cause for in the exception chain and if no match
34+
* found search by extended type.
35+
*
36+
* @param exception
37+
* exception to search error view for
38+
* @return optional error target entry corresponding to the given exception
39+
*/
40+
Optional<ErrorTargetEntry> getErrorNavigationTarget(Exception exception);
41+
}

‎flow-server/src/main/java/com/vaadin/flow/server/startup/ApplicationRouteRegistry.java‎

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import com.vaadin.flow.router.internal.NavigationRouteTarget;
4343
import com.vaadin.flow.router.internal.PathUtil;
4444
import com.vaadin.flow.router.internal.RouteTarget;
45+
import com.vaadin.flow.server.ErrorRouteRegistry;
4546
import com.vaadin.flow.server.PWA;
4647
import com.vaadin.flow.server.VaadinContext;
4748

@@ -51,7 +52,8 @@
5152
*
5253
* @since 1.3
5354
*/
54-
public class ApplicationRouteRegistry extends AbstractRouteRegistry {
55+
public class ApplicationRouteRegistry extends AbstractRouteRegistry
56+
implements ErrorRouteRegistry {
5557

5658
private AtomicReference<Class<?>> pwaConfigurationClass = new AtomicReference<>();
5759
private static final Set<Class<? extends Component>> defaultErrorHandlers = Stream
@@ -168,15 +170,7 @@ private boolean allErrorFiltersMatch(Class<? extends Component> target) {
168170
.allMatch(filter -> filter.testErrorNavigationTarget(target));
169171
}
170172

171-
/**
172-
* Get a registered navigation target for given exception. First we will
173-
* search for a matching cause for in the exception chain and if no match
174-
* found search by extended type.
175-
*
176-
* @param exception
177-
* exception to search error view for
178-
* @return optional error target entry corresponding to the given exception
179-
*/
173+
@Override
180174
public Optional<ErrorTargetEntry> getErrorNavigationTarget(
181175
Exception exception) {
182176
if (getConfiguration().getExceptionHandlers().isEmpty()) {
@@ -189,30 +183,6 @@ public Optional<ErrorTargetEntry> getErrorNavigationTarget(
189183
return result;
190184
}
191185

192-
@Override
193-
public NavigationRouteTarget getNavigationRouteTarget(String url) {
194-
return getConfiguration().getNavigationRouteTarget(url);
195-
}
196-
197-
@Override
198-
public RouteTarget getRouteTarget(Class<? extends Component> target,
199-
RouteParameters parameters) {
200-
return getConfiguration().getRouteTarget(target, parameters);
201-
}
202-
203-
@Override
204-
public Optional<Class<? extends Component>> getNavigationTarget(
205-
String url) {
206-
Objects.requireNonNull(url, "url must not be null.");
207-
return getConfiguration().getTarget(url);
208-
}
209-
210-
@Override
211-
public Optional<Class<? extends Component>> getNavigationTarget(String url,
212-
List<String> segments) {
213-
return getNavigationTarget(PathUtil.getPath(url, segments));
214-
}
215-
216186
/**
217187
* Check if there are registered navigation targets in the registry.
218188
*

‎flow-server/src/test/java/com/vaadin/flow/router/internal/TestAbstractRouteRegistry.java‎

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,38 +17,6 @@
1717

1818
package com.vaadin.flow.router.internal;
1919

20-
import java.util.List;
21-
import java.util.Objects;
22-
import java.util.Optional;
23-
24-
import com.vaadin.flow.component.Component;
25-
import com.vaadin.flow.router.RouteParameters;
26-
2720
public class TestAbstractRouteRegistry extends AbstractRouteRegistry {
2821

29-
@Override
30-
public NavigationRouteTarget getNavigationRouteTarget(String url) {
31-
return getConfiguration().getNavigationRouteTarget(url);
32-
}
33-
34-
@Override
35-
public RouteTarget getRouteTarget(Class<? extends Component> target,
36-
RouteParameters parameters) {
37-
return getConfiguration().getRouteTarget(target, parameters);
38-
}
39-
40-
@Override
41-
public Optional<Class<? extends Component>> getNavigationTarget(
42-
String pathString) {
43-
Objects.requireNonNull(pathString, "pathString must not be null.");
44-
return getConfiguration().getTarget(pathString);
45-
}
46-
47-
@Override
48-
public Optional<Class<? extends Component>> getNavigationTarget(
49-
String pathString, List<String> segments) {
50-
51-
return getNavigationTarget(PathUtil.getPath(pathString, segments));
52-
}
53-
5422
}

‎flow-tests/pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@
353353
<module>test-v14-bootstrap</module>
354354
<module>test-v14-bootstrap/pom-production.xml</module>
355355

356+
<module>test-custom-route-registry</module>
356357
<!-- tests are disabled in this module, this reference is for maven version plugin -->
357358
<module>test-no-theme</module>
358359
<!-- move theme tests to the end, because theme switch live reload-->
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<artifactId>flow-tests</artifactId>
8+
<groupId>com.vaadin</groupId>
9+
<version>8.0-SNAPSHOT</version>
10+
</parent>
11+
<artifactId>test-custom-route-registry</artifactId>
12+
<name>Test using Flow with a custom RouteRegistry implementation</name>
13+
14+
<packaging>war</packaging>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>com.vaadin</groupId>
19+
<artifactId>flow-test-resources</artifactId>
20+
<version>${project.version}</version>
21+
</dependency>
22+
<dependency>
23+
<groupId>com.vaadin</groupId>
24+
<artifactId>flow-html-components-testbench</artifactId>
25+
<version>${project.version}</version>
26+
<scope>test</scope>
27+
</dependency>
28+
</dependencies>
29+
30+
<build>
31+
<plugins>
32+
<!-- Run flow plugin to build frontend -->
33+
<plugin>
34+
<groupId>com.vaadin</groupId>
35+
<artifactId>flow-maven-plugin</artifactId>
36+
<executions>
37+
<execution>
38+
<goals>
39+
<goal>prepare-frontend</goal>
40+
<goal>build-frontend</goal>
41+
</goals>
42+
</execution>
43+
</executions>
44+
<configuration>
45+
<productionMode>true</productionMode>
46+
</configuration>
47+
</plugin>
48+
<!-- Run jetty before integration tests, and stop after -->
49+
<plugin>
50+
<groupId>org.eclipse.jetty</groupId>
51+
<artifactId>jetty-maven-plugin</artifactId>
52+
</plugin>
53+
</plugins>
54+
</build>
55+
56+
<profiles>
57+
<profile>
58+
<id>local-run</id>
59+
<activation>
60+
<property>
61+
<name>!test.use.hub</name>
62+
</property>
63+
</activation>
64+
<build>
65+
<plugins>
66+
<plugin>
67+
<groupId>com.lazerycode.selenium</groupId>
68+
<artifactId>driver-binary-downloader-maven-plugin</artifactId>
69+
<version>${driver.binary.downloader.maven.plugin.version}</version>
70+
<configuration>
71+
<onlyGetDriversForHostOperatingSystem>true</onlyGetDriversForHostOperatingSystem>
72+
<rootStandaloneServerDirectory>${project.rootdir}/driver</rootStandaloneServerDirectory>
73+
<downloadedZipFileDirectory>${project.rootdir}/driver_zips</downloadedZipFileDirectory>
74+
<customRepositoryMap>${project.rootdir}/drivers.xml</customRepositoryMap>
75+
</configuration>
76+
<executions>
77+
<execution>
78+
<phase>pre-integration-test</phase>
79+
<goals>
80+
<goal>selenium</goal>
81+
</goals>
82+
</execution>
83+
</executions>
84+
</plugin>
85+
</plugins>
86+
</build>
87+
</profile>
88+
</profiles>
89+
</project>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2000-2021 Vaadin Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.vaadin.flow.custom;
18+
19+
import com.vaadin.flow.component.Component;
20+
import com.vaadin.flow.component.Tag;
21+
import com.vaadin.flow.component.html.Span;
22+
import com.vaadin.flow.router.BeforeEnterEvent;
23+
import com.vaadin.flow.router.ErrorParameter;
24+
import com.vaadin.flow.router.HasErrorParameter;
25+
26+
/**
27+
* Customized error handler for IllegalAccessException.
28+
* <p>
29+
* This should not be available through the CustomRouteRegistry.
30+
*/
31+
@Tag(Tag.DIV)
32+
public class CustomErrorHandler extends Component
33+
implements HasErrorParameter<IllegalAccessException> {
34+
35+
@Override
36+
public int setErrorParameter(BeforeEnterEvent event,
37+
ErrorParameter<IllegalAccessException> parameter) {
38+
getElement().appendChild(
39+
new Span("This shouldn't be available").getElement());
40+
return 0;
41+
}
42+
}

0 commit comments

Comments
 (0)