Skip to content

Commit

Permalink
[UNDERTOW-2504] Review anonymous classes in Undertow io.undertow.webs…
Browse files Browse the repository at this point in the history
…ockets.jsr.test.security
  • Loading branch information
lvydra committed Oct 18, 2024
1 parent c5281bf commit eedca37
Showing 1 changed file with 22 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import jakarta.websocket.ContainerProvider;
import jakarta.websocket.Endpoint;
import jakarta.websocket.EndpointConfig;
import jakarta.websocket.MessageHandler;
import jakarta.websocket.OnOpen;
import jakarta.websocket.Session;
import jakarta.websocket.server.ServerEndpoint;
Expand Down Expand Up @@ -147,12 +146,7 @@ public static void cleanup() throws ServletException {
@Test
public void testAuthenticatedWebsocket() throws Exception {
ProgramaticClientEndpoint endpoint = new ProgramaticClientEndpoint();
ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create().configurator(new ClientConfigurator(){
@Override
public void beforeRequest(Map<String, List<String>> headers) {
headers.put(AUTHORIZATION.toString(), Collections.singletonList(BASIC + " " + FlexBase64.encodeString("user1:password1".getBytes(), false)));
}
}).build();
ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create().configurator(new CustomClientConfigurator()).build();
ContainerProvider.getWebSocketContainer().connectToServer(endpoint, clientEndpointConfig, new URI("ws://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default") + "/servletContext/secured"));
assertEquals("user1", endpoint.getResponses().poll(15, TimeUnit.SECONDS));
endpoint.session.close();
Expand All @@ -179,13 +173,7 @@ public static class ProgramaticClientEndpoint extends Endpoint {
@Override
public void onOpen(Session session, EndpointConfig config) {
this.session = session;
session.addMessageHandler(new MessageHandler.Whole<String>() {

@Override
public void onMessage(String message) {
responses.add(message);
}
});
session.addMessageHandler(String.class, (message) -> responses.add(message));
}

@Override
Expand Down Expand Up @@ -217,12 +205,7 @@ public void init(FilterConfig filterConfig) {

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
filterChain.doFilter(new HttpServletRequestWrapper((HttpServletRequest) servletRequest) {
@Override
public Principal getUserPrincipal() {
return () -> "wrapped";
}
}, servletResponse);
filterChain.doFilter(new ServletRequestWrapper((HttpServletRequest) servletRequest), servletResponse);
}

@Override
Expand All @@ -231,4 +214,23 @@ public void destroy() {
}
}

private static class ServletRequestWrapper extends HttpServletRequestWrapper {

ServletRequestWrapper(HttpServletRequest request) {
super(request);
}

@Override
public Principal getUserPrincipal() {
return () -> "wrapped";
}
}

private static class CustomClientConfigurator extends ClientConfigurator {

@Override
public void beforeRequest(Map<String, List<String>> headers) {
headers.put(AUTHORIZATION.toString(), Collections.singletonList(BASIC + " " + FlexBase64.encodeString("user1:password1".getBytes(), false)));
}
}
}

0 comments on commit eedca37

Please sign in to comment.