Skip to content

Commit bc2bc2b

Browse files
authored
fix: Use root servlet mapping for push url in OSGi case (#15350)
Fallback to a root mapping for push in case if not servlet registration is available (OSGi runtime). Related-to #14641
1 parent 79b4ee4 commit bc2bc2b

1 file changed

Lines changed: 24 additions & 9 deletions

File tree

flow-server/src/main/java/com/vaadin/flow/server/communication/PushRequestHandler.java

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import javax.servlet.ServletRegistration;
2222

2323
import java.io.IOException;
24+
import java.util.Optional;
2425

2526
import org.atmosphere.cache.UUIDBroadcasterCache;
2627
import org.atmosphere.client.TrackMessageSizeInterceptor;
@@ -223,9 +224,18 @@ public AtmosphereFramework addInitParameter(String name,
223224
atmosphere.addInitParameter("org.atmosphere.cpr.showSupportMessage",
224225
"false");
225226

226-
atmosphere.addInitParameter(ApplicationConfig.JSR356_MAPPING_PATH,
227-
findFirstUrlMapping(vaadinServletConfig)
228-
+ Constants.PUSH_MAPPING);
227+
Optional<ServletRegistration> servletRegistration = getServletRegistration(
228+
vaadinServletConfig);
229+
if (servletRegistration.isPresent()) {
230+
atmosphere.addInitParameter(ApplicationConfig.JSR356_MAPPING_PATH,
231+
findFirstUrlMapping(servletRegistration.get())
232+
+ Constants.PUSH_MAPPING);
233+
} else {
234+
getLogger().debug(
235+
"Unable to determine servlet registration for {}. "
236+
+ "Using root mapping for push",
237+
vaadinServletConfig.getServletName());
238+
}
229239

230240
atmosphere.addInitParameter(
231241
ApplicationConfig.JSR356_PATH_MAPPING_LENGTH, "0");
@@ -245,15 +255,20 @@ public AtmosphereFramework addInitParameter(String name,
245255
return atmosphere;
246256
}
247257

248-
private static String findFirstUrlMapping(ServletConfig config) {
258+
private static Optional<ServletRegistration> getServletRegistration(
259+
ServletConfig config) {
249260
String name = config.getServletName();
250-
String firstMapping = "/";
251261
if (name != null) {
252-
ServletRegistration reg = config.getServletContext()
253-
.getServletRegistrations().get(name);
254-
firstMapping = reg.getMappings().stream().sorted().findFirst()
255-
.orElse("/");
262+
return Optional.ofNullable(config.getServletContext()
263+
.getServletRegistrations().get(name));
256264
}
265+
return Optional.empty();
266+
}
267+
268+
private static String findFirstUrlMapping(
269+
ServletRegistration registration) {
270+
String firstMapping = registration.getMappings().stream().sorted()
271+
.findFirst().orElse("/");
257272
return firstMapping.replace("/*", "/");
258273
}
259274

0 commit comments

Comments
 (0)