OfframpHandshakeInterceptor extends HttpSessionHandshakeInterceptor { @Override public boolean beforeHandshake( ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception { log.info("Request received: {} {} - with headers: {}", request.getMethod(), request.getURI(), request.getHeaders()); Map<String, String> segments = URI_TEMPLATE.match(request.getURI().getPath()); attributes.put(VERSION, segments.get(VERSION)); attributes.put(ROAD_NAME, segments.get(ROAD_NAME)); attributes.put(STREAM_NAME, segments.get(STREAM_NAME)); MultiValueMap<String, String> parameters = UriComponentsBuilder.fromUri(request.getURI()).build().getQueryParams(); DefaultOffset defaultOffset = Mono .justOrEmpty(parameters.getFirst(DEFAULT_OFFSET)) .map(DefaultOffset::valueOf) .defaultIfEmpty(LATEST) .block(); attributes.put(DEFAULT_OFFSET, defaultOffset); Set<Sensitivity> grants = Mono .justOrEmpty(parameters.get(GRANTS)) .flatMapMany(Flux::fromIterable) .filter(s -> !s.isEmpty()) .map(Sensitivity::valueOf) .collect(toSet()) .block(); attributes.put(GRANTS, grants); return super.beforeHandshake(request, response, wsHandler, attributes); }  @Override boolean beforeHandshake(
      ServerHttpRequest request,
      ServerHttpResponse response,
      WebSocketHandler wsHandler,
      Map<String, Object> attributes);  }
@Test public void allParameters() throws Exception { when(request.getURI()).thenReturn( URI.create("ws: Map<String, Object> attributes = new HashMap<>(); underTest.beforeHandshake(request, null, null, attributes); assertThat(attributes.get(ROAD_NAME), is("road1")); assertThat(attributes.get(STREAM_NAME), is("stream1")); assertThat(attributes.get(DEFAULT_OFFSET), is(EARLIEST)); assertThat(attributes.get(GRANTS), is(singleton(PII))); }
@Test public void missingDefaultOffset() throws Exception { when(request.getURI()) .thenReturn(URI.create("ws: Map<String, Object> attributes = new HashMap<>(); underTest.beforeHandshake(request, null, null, attributes); assertThat(attributes.get(ROAD_NAME), is("road1")); assertThat(attributes.get(STREAM_NAME), is("stream1")); assertThat(attributes.get(DEFAULT_OFFSET), is(LATEST)); assertThat(attributes.get(GRANTS), is(singleton(PII))); }
@Test public void missingGrants() throws Exception { when(request.getURI()).thenReturn( URI.create("ws: Map<String, Object> attributes = new HashMap<>(); underTest.beforeHandshake(request, null, null, attributes); assertThat(attributes.get(ROAD_NAME), is("road1")); assertThat(attributes.get(STREAM_NAME), is("stream1")); assertThat(attributes.get(DEFAULT_OFFSET), is(EARLIEST)); assertThat(attributes.get(GRANTS), is(emptySet())); }
@Test public void emptyGrants() throws Exception { when(request.getURI()).thenReturn( URI.create("ws: Map<String, Object> attributes = new HashMap<>(); underTest.beforeHandshake(request, null, null, attributes); assertThat(attributes.get(ROAD_NAME), is("road1")); assertThat(attributes.get(STREAM_NAME), is("stream1")); assertThat(attributes.get(DEFAULT_OFFSET), is(EARLIEST)); assertThat(attributes.get(GRANTS), is(emptySet())); }