Skip to content

Commit 23c291b

Browse files
fix: register Map resources on the Map element (#9134) (CP: 25.1) (#9138)
This PR cherry-picks changes from the original PR #9134 to branch 25.1. --- > ## Description > > Map uses a custom JSON serializer to automatically register stream resources in the UI when it encounters those during serialization. Currently, those are registered on the UI itself and not on the Map element. This results in access to those resources being denied from the server while a server-side modal is opened. > > This fixes the serializer to register the stream resources on the map. Thus clients are allowed to access these resources when the map is within a strict modal dialog for example. > > Fixes #9129 > > ## Type of change > > - Bugfix > Co-authored-by: Sascha Ißbrücker <sissbruecker@vaadin.com>
1 parent 6b964d2 commit 23c291b

2 files changed

Lines changed: 17 additions & 10 deletions

File tree

vaadin-map-flow-parent/vaadin-map-flow/src/main/java/com/vaadin/flow/component/map/serialization/MapSerializer.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.util.Map;
1414

1515
import com.vaadin.flow.component.UI;
16+
import com.vaadin.flow.component.map.MapBase;
1617
import com.vaadin.flow.server.StreamRegistration;
1718
import com.vaadin.flow.server.StreamResource;
1819
import com.vaadin.flow.server.StreamResourceRegistry;
@@ -35,14 +36,14 @@ public class MapSerializer {
3536
private final ObjectMapper mapper;
3637
private final Map<Object, StreamRegistration> streamRegistrationCache = new HashMap<>();
3738

38-
public MapSerializer(com.vaadin.flow.component.map.MapBase map) {
39+
public MapSerializer(MapBase map) {
3940
// Create mapper that automatically registers stream resources and
4041
// download handlers in the current UI's stream resource registry
4142
SimpleModule mapModule = new SimpleModule()
4243
.addSerializer(StreamResource.class,
4344
new StreamResourceSerializer())
4445
.addSerializer(DownloadHandler.class,
45-
new DownloadHandlerSerializer());
46+
new DownloadHandlerSerializer(map));
4647
this.mapper = JsonMapper.builder().addModule(mapModule).build();
4748

4849
// Unregister stream registrations when the map is detached
@@ -118,8 +119,11 @@ private URI getURI(StreamResource resource) {
118119
private class DownloadHandlerSerializer
119120
extends StdSerializer<DownloadHandler> {
120121

121-
public DownloadHandlerSerializer() {
122+
private final MapBase map;
123+
124+
public DownloadHandlerSerializer(MapBase map) {
122125
super(DownloadHandler.class);
126+
this.map = map;
123127
}
124128

125129
@Override
@@ -139,7 +143,8 @@ private URI getURI(DownloadHandler resource) {
139143
if (registration == null) {
140144
StreamResourceRegistry resourceRegistry = UI.getCurrentOrThrow()
141145
.getSession().getResourceRegistry();
142-
registration = resourceRegistry.registerResource(resource);
146+
registration = resourceRegistry.registerResource(resource,
147+
map.getElement());
143148
streamRegistrationCache.put(resource, registration);
144149
}
145150
return registration.getResourceUri();

vaadin-map-flow-parent/vaadin-map-flow/src/test/java/com/vaadin/flow/component/map/MapSerializationTest.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.vaadin.flow.component.map.configuration.source.OSMSource;
2525
import com.vaadin.flow.component.map.configuration.style.Icon;
2626
import com.vaadin.flow.component.map.configuration.style.Style;
27+
import com.vaadin.flow.dom.Element;
2728
import com.vaadin.flow.internal.JacksonUtils;
2829
import com.vaadin.flow.server.AbstractStreamResource;
2930
import com.vaadin.flow.server.StreamRegistration;
@@ -56,8 +57,9 @@ public void setup() throws URISyntaxException {
5657
.registerResource((AbstractStreamResource) Mockito.any()))
5758
.thenReturn(streamRegistrationMock);
5859

59-
Mockito.when(streamResourceRegistryMock
60-
.registerResource((ElementRequestHandler) Mockito.any()))
60+
Mockito.when(streamResourceRegistryMock.registerResource(
61+
(ElementRequestHandler) Mockito.any(),
62+
Mockito.any(Element.class)))
6163
.thenReturn(streamRegistrationMock, streamRegistrationMock);
6264

6365
map = new Map();
@@ -117,22 +119,22 @@ public void serializeIcon_registerStreamResourceExactlyOnce() {
117119
ui.fakeClientCommunication();
118120

119121
Mockito.verify(streamResourceRegistryMock, Mockito.times(1))
120-
.registerResource(Assets.PIN.getHandler());
122+
.registerResource(Assets.PIN.getHandler(), map.getElement());
121123
Mockito.clearInvocations(streamResourceRegistryMock);
122124

123125
// Force another sync of the same icon
124126
marker.getIcon().setScale(42);
125127
ui.fakeClientCommunication();
126128

127129
Mockito.verify(streamResourceRegistryMock, Mockito.never())
128-
.registerResource(Assets.PIN.getHandler());
130+
.registerResource(Assets.PIN.getHandler(), map.getElement());
129131

130132
// Sync a different icon with the same resource
131133
setupMarker();
132134
ui.fakeClientCommunication();
133135

134136
Mockito.verify(streamResourceRegistryMock, Mockito.never())
135-
.registerResource(Assets.PIN.getHandler());
137+
.registerResource(Assets.PIN.getHandler(), map.getElement());
136138
}
137139

138140
@Test
@@ -160,7 +162,7 @@ public void detachMap_reattachMap_streamResourceRegisteredAgain() {
160162
ui.fakeClientCommunication();
161163

162164
Mockito.verify(streamResourceRegistryMock, Mockito.times(1))
163-
.registerResource(Assets.PIN.getHandler());
165+
.registerResource(Assets.PIN.getHandler(), map.getElement());
164166
}
165167

166168
private MarkerFeature setupMarker() {

0 commit comments

Comments
 (0)