2121import javax .servlet .ServletRegistration ;
2222
2323import java .io .IOException ;
24+ import java .util .Optional ;
2425
2526import org .atmosphere .cache .UUIDBroadcasterCache ;
2627import 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