org.eclipse.jetty.websocket.api.UpgradeRequest Java Examples

The following examples show how to use org.eclipse.jetty.websocket.api.UpgradeRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JettyWebSocketSessionTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
@SuppressWarnings("resource")
public void getPrincipalFromNativeSession() {
	TestPrincipal user = new TestPrincipal("joe");

	UpgradeRequest request = Mockito.mock(UpgradeRequest.class);
	given(request.getUserPrincipal()).willReturn(user);

	UpgradeResponse response = Mockito.mock(UpgradeResponse.class);
	given(response.getAcceptedSubProtocol()).willReturn(null);

	Session nativeSession = Mockito.mock(Session.class);
	given(nativeSession.getUpgradeRequest()).willReturn(request);
	given(nativeSession.getUpgradeResponse()).willReturn(response);

	JettyWebSocketSession session = new JettyWebSocketSession(attributes);
	session.initializeNativeSession(nativeSession);

	reset(nativeSession);

	assertSame(user, session.getPrincipal());
	verifyNoMoreInteractions(nativeSession);
}
 
Example #2
Source File: RestrictedGuacamoleWebSocketCreator.java    From guacamole-client with Apache License 2.0 6 votes vote down vote up
@Override
public Object createWebSocket(UpgradeRequest request, UpgradeResponse response) {

    // Validate and use "guacamole" subprotocol
    for (String subprotocol : request.getSubProtocols()) {

        if ("guacamole".equals(subprotocol)) {
            response.setAcceptedSubProtocol(subprotocol);
            return new RestrictedGuacamoleWebSocketTunnelListener(tunnelRequestService);
        }

    }

    // Invalid protocol
    return null;

}
 
Example #3
Source File: RestrictedGuacamoleWebSocketCreator.java    From guacamole-client with Apache License 2.0 6 votes vote down vote up
@Override
public Object createWebSocket(UpgradeRequest request, UpgradeResponse response) {

    // Validate and use "guacamole" subprotocol
    for (String subprotocol : request.getSubProtocols()) {

        if ("guacamole".equals(subprotocol)) {
            response.setAcceptedSubProtocol(subprotocol);
            return new RestrictedGuacamoleWebSocketTunnelListener(tunnelRequestService);
        }

    }

    // Invalid protocol
    return null;

}
 
Example #4
Source File: JettyWebSocketSessionTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void getAcceptedProtocol() {

	String protocol = "foo";

	UpgradeRequest request = Mockito.mock(UpgradeRequest.class);
	given(request.getUserPrincipal()).willReturn(null);

	UpgradeResponse response = Mockito.mock(UpgradeResponse.class);
	given(response.getAcceptedSubProtocol()).willReturn(protocol);

	Session nativeSession = Mockito.mock(Session.class);
	given(nativeSession.getUpgradeRequest()).willReturn(request);
	given(nativeSession.getUpgradeResponse()).willReturn(response);

	JettyWebSocketSession session = new JettyWebSocketSession(attributes);
	session.initializeNativeSession(nativeSession);

	reset(nativeSession);

	assertSame(protocol, session.getAcceptedProtocol());
	verifyNoMoreInteractions(nativeSession);
}
 
Example #5
Source File: JettyWebSocketSessionTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void getPrincipalNotAvailable() {

	UpgradeRequest request = Mockito.mock(UpgradeRequest.class);
	given(request.getUserPrincipal()).willReturn(null);

	UpgradeResponse response = Mockito.mock(UpgradeResponse.class);
	given(response.getAcceptedSubProtocol()).willReturn(null);

	Session nativeSession = Mockito.mock(Session.class);
	given(nativeSession.getUpgradeRequest()).willReturn(request);
	given(nativeSession.getUpgradeResponse()).willReturn(response);

	JettyWebSocketSession session = new JettyWebSocketSession(attributes);
	session.initializeNativeSession(nativeSession);

	reset(nativeSession);

	assertNull(session.getPrincipal());
	verifyNoMoreInteractions(nativeSession);
}
 
Example #6
Source File: JettyWebSocketSessionTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void getPrincipalFromNativeSession() {

	TestPrincipal user = new TestPrincipal("joe");

	UpgradeRequest request = Mockito.mock(UpgradeRequest.class);
	given(request.getUserPrincipal()).willReturn(user);

	UpgradeResponse response = Mockito.mock(UpgradeResponse.class);
	given(response.getAcceptedSubProtocol()).willReturn(null);

	Session nativeSession = Mockito.mock(Session.class);
	given(nativeSession.getUpgradeRequest()).willReturn(request);
	given(nativeSession.getUpgradeResponse()).willReturn(response);

	JettyWebSocketSession session = new JettyWebSocketSession(attributes);
	session.initializeNativeSession(nativeSession);

	reset(nativeSession);

	assertSame(user, session.getPrincipal());
	verifyNoMoreInteractions(nativeSession);
}
 
Example #7
Source File: JettyWebSocketHandlerTest.java    From engine.io-server-java with MIT License 6 votes vote down vote up
@Test
public void testSocket_write_binary() throws IOException {
    final String queryString = ParseQS.encode(new HashMap<String, String>() {{
        put("transport", WebSocket.NAME);
    }});
    final UpgradeRequest upgradeRequest = Mockito.mock(UpgradeRequest.class);
    final RemoteEndpoint remoteEndpoint = Mockito.mock(RemoteEndpoint.class);
    final Session session = Mockito.mock(Session.class);
    final EngineIoServer server = Mockito.spy(new EngineIoServer());

    Mockito.doAnswer(invocationOnMock -> queryString).when(upgradeRequest).getQueryString();
    Mockito.doAnswer(invocationOnMock -> upgradeRequest).when(session).getUpgradeRequest();
    Mockito.doAnswer(invocationOnMock -> remoteEndpoint).when(session).getRemote();
    Mockito.doAnswer(invocationOnMock -> null).when(server).handleWebSocket(Mockito.any(EngineIoWebSocket.class));

    final JettyWebSocketHandler handler = new JettyWebSocketHandler(server);
    handler.onWebSocketConnect(session);

    final byte[] message = new byte[16];
    (new Random()).nextBytes(message);
    handler.write(message);
    Mockito.verify(remoteEndpoint, Mockito.times(1))
            .sendBytes(Mockito.any(ByteBuffer.class));
}
 
Example #8
Source File: JettyWebSocketHandlerTest.java    From engine.io-server-java with MIT License 6 votes vote down vote up
@Test
public void testSocket_write_string() throws IOException {
    final String queryString = ParseQS.encode(new HashMap<String, String>() {{
        put("transport", WebSocket.NAME);
    }});
    final UpgradeRequest upgradeRequest = Mockito.mock(UpgradeRequest.class);
    final RemoteEndpoint remoteEndpoint = Mockito.mock(RemoteEndpoint.class);
    final Session session = Mockito.mock(Session.class);
    final EngineIoServer server = Mockito.spy(new EngineIoServer());

    Mockito.doAnswer(invocationOnMock -> queryString).when(upgradeRequest).getQueryString();
    Mockito.doAnswer(invocationOnMock -> upgradeRequest).when(session).getUpgradeRequest();
    Mockito.doAnswer(invocationOnMock -> remoteEndpoint).when(session).getRemote();
    Mockito.doAnswer(invocationOnMock -> null).when(server).handleWebSocket(Mockito.any(EngineIoWebSocket.class));

    final JettyWebSocketHandler handler = new JettyWebSocketHandler(server);
    handler.onWebSocketConnect(session);

    final String message = "FooBar";
    handler.write(message);
    Mockito.verify(remoteEndpoint, Mockito.times(1))
            .sendString(Mockito.eq(message));
}
 
Example #9
Source File: JettyWebSocketHandlerTest.java    From engine.io-server-java with MIT License 6 votes vote down vote up
@Test
public void testSocket_close() {
    final String queryString = ParseQS.encode(new HashMap<String, String>() {{
        put("transport", WebSocket.NAME);
    }});
    final UpgradeRequest upgradeRequest = Mockito.mock(UpgradeRequest.class);
    final Session session = Mockito.mock(Session.class);
    final EngineIoServer server = Mockito.spy(new EngineIoServer());

    Mockito.doAnswer(invocationOnMock -> queryString).when(upgradeRequest).getQueryString();
    Mockito.doAnswer(invocationOnMock -> upgradeRequest).when(session).getUpgradeRequest();
    Mockito.doAnswer(invocationOnMock -> null).when(server).handleWebSocket(Mockito.any(EngineIoWebSocket.class));

    final JettyWebSocketHandler handler = new JettyWebSocketHandler(server);
    handler.onWebSocketConnect(session);

    handler.close();
    Mockito.verify(session, Mockito.times(1))
            .close();
}
 
Example #10
Source File: JettyWebSocketHandlerTest.java    From engine.io-server-java with MIT License 6 votes vote down vote up
@Test
public void testOnConnect() {
    final String queryString = ParseQS.encode(new HashMap<String, String>() {{
        put("transport", WebSocket.NAME);
    }});
    final UpgradeRequest upgradeRequest = Mockito.mock(UpgradeRequest.class);
    final Session session = Mockito.mock(Session.class);
    final EngineIoServer server = Mockito.spy(new EngineIoServer());

    Mockito.doAnswer(invocationOnMock -> queryString).when(upgradeRequest).getQueryString();
    Mockito.doAnswer(invocationOnMock -> upgradeRequest).when(session).getUpgradeRequest();
    Mockito.doAnswer(invocationOnMock -> null).when(server).handleWebSocket(Mockito.any(EngineIoWebSocket.class));

    final JettyWebSocketHandler handler = new JettyWebSocketHandler(server);
    handler.onWebSocketConnect(session);

    Mockito.verify(server, Mockito.times(1))
            .handleWebSocket(Mockito.eq(handler));
    Assert.assertNotNull(handler.getQuery());
    Assert.assertTrue(handler.getQuery().containsKey("transport"));
    Assert.assertEquals(handler.getQuery().get("transport"), WebSocket.NAME);
}
 
Example #11
Source File: JettyWebSocketSessionTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
@SuppressWarnings("resource")
public void getAcceptedProtocol() {
	String protocol = "foo";

	UpgradeRequest request = Mockito.mock(UpgradeRequest.class);
	given(request.getUserPrincipal()).willReturn(null);

	UpgradeResponse response = Mockito.mock(UpgradeResponse.class);
	given(response.getAcceptedSubProtocol()).willReturn(protocol);

	Session nativeSession = Mockito.mock(Session.class);
	given(nativeSession.getUpgradeRequest()).willReturn(request);
	given(nativeSession.getUpgradeResponse()).willReturn(response);

	JettyWebSocketSession session = new JettyWebSocketSession(attributes);
	session.initializeNativeSession(nativeSession);

	reset(nativeSession);

	assertSame(protocol, session.getAcceptedProtocol());
	verifyNoMoreInteractions(nativeSession);
}
 
Example #12
Source File: JettyWebSocketSessionTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
@SuppressWarnings("resource")
public void getPrincipalNotAvailable() {
	UpgradeRequest request = Mockito.mock(UpgradeRequest.class);
	given(request.getUserPrincipal()).willReturn(null);

	UpgradeResponse response = Mockito.mock(UpgradeResponse.class);
	given(response.getAcceptedSubProtocol()).willReturn(null);

	Session nativeSession = Mockito.mock(Session.class);
	given(nativeSession.getUpgradeRequest()).willReturn(request);
	given(nativeSession.getUpgradeResponse()).willReturn(response);

	JettyWebSocketSession session = new JettyWebSocketSession(attributes);
	session.initializeNativeSession(nativeSession);

	reset(nativeSession);

	assertNull(session.getPrincipal());
	verifyNoMoreInteractions(nativeSession);
}
 
Example #13
Source File: JettyWebSocketSessionTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
@SuppressWarnings("resource")
public void getPrincipalFromNativeSession() {
	TestPrincipal user = new TestPrincipal("joe");

	UpgradeRequest request = Mockito.mock(UpgradeRequest.class);
	given(request.getUserPrincipal()).willReturn(user);

	UpgradeResponse response = Mockito.mock(UpgradeResponse.class);
	given(response.getAcceptedSubProtocol()).willReturn(null);

	Session nativeSession = Mockito.mock(Session.class);
	given(nativeSession.getUpgradeRequest()).willReturn(request);
	given(nativeSession.getUpgradeResponse()).willReturn(response);

	JettyWebSocketSession session = new JettyWebSocketSession(attributes);
	session.initializeNativeSession(nativeSession);

	reset(nativeSession);

	assertSame(user, session.getPrincipal());
	verifyNoMoreInteractions(nativeSession);
}
 
Example #14
Source File: JettyWebSocketSessionTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
@SuppressWarnings("resource")
public void getAcceptedProtocol() {
	String protocol = "foo";

	UpgradeRequest request = Mockito.mock(UpgradeRequest.class);
	given(request.getUserPrincipal()).willReturn(null);

	UpgradeResponse response = Mockito.mock(UpgradeResponse.class);
	given(response.getAcceptedSubProtocol()).willReturn(protocol);

	Session nativeSession = Mockito.mock(Session.class);
	given(nativeSession.getUpgradeRequest()).willReturn(request);
	given(nativeSession.getUpgradeResponse()).willReturn(response);

	JettyWebSocketSession session = new JettyWebSocketSession(attributes);
	session.initializeNativeSession(nativeSession);

	reset(nativeSession);

	assertSame(protocol, session.getAcceptedProtocol());
	verifyNoMoreInteractions(nativeSession);
}
 
Example #15
Source File: JettyWebSocketSessionTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
@SuppressWarnings("resource")
public void getPrincipalNotAvailable() {
	UpgradeRequest request = Mockito.mock(UpgradeRequest.class);
	given(request.getUserPrincipal()).willReturn(null);

	UpgradeResponse response = Mockito.mock(UpgradeResponse.class);
	given(response.getAcceptedSubProtocol()).willReturn(null);

	Session nativeSession = Mockito.mock(Session.class);
	given(nativeSession.getUpgradeRequest()).willReturn(request);
	given(nativeSession.getUpgradeResponse()).willReturn(response);

	JettyWebSocketSession session = new JettyWebSocketSession(attributes);
	session.initializeNativeSession(nativeSession);

	reset(nativeSession);

	assertNull(session.getPrincipal());
	verifyNoMoreInteractions(nativeSession);
}
 
Example #16
Source File: JettyWebSocketHandlerAdapterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Before
public void setup() {
	this.session = mock(Session.class);
	given(this.session.getUpgradeRequest()).willReturn(Mockito.mock(UpgradeRequest.class));
	given(this.session.getUpgradeResponse()).willReturn(Mockito.mock(UpgradeResponse.class));

	this.webSocketHandler = mock(WebSocketHandler.class);
	this.webSocketSession = new JettyWebSocketSession(null, null);
	this.adapter = new JettyWebSocketHandlerAdapter(this.webSocketHandler, this.webSocketSession);
}
 
Example #17
Source File: JettyWebSocketHandlerAdapterTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Before
public void setup() {
	this.session = mock(Session.class);
	given(this.session.getUpgradeRequest()).willReturn(Mockito.mock(UpgradeRequest.class));
	given(this.session.getUpgradeResponse()).willReturn(Mockito.mock(UpgradeResponse.class));

	this.webSocketHandler = mock(WebSocketHandler.class);
	this.webSocketSession = new JettyWebSocketSession(null, null);
	this.adapter = new JettyWebSocketHandlerAdapter(this.webSocketHandler, this.webSocketSession);
}
 
Example #18
Source File: JettyWebSocketHandlerAdapterTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
	this.session = mock(Session.class);
	given(this.session.getUpgradeRequest()).willReturn(Mockito.mock(UpgradeRequest.class));
	given(this.session.getUpgradeResponse()).willReturn(Mockito.mock(UpgradeResponse.class));

	this.webSocketHandler = mock(WebSocketHandler.class);
	this.webSocketSession = new JettyWebSocketSession(null, null);
	this.adapter = new JettyWebSocketHandlerAdapter(this.webSocketHandler, this.webSocketSession);
}
 
Example #19
Source File: ConsoleLogSocket.java    From gocd with Apache License 2.0 5 votes vote down vote up
private long parseStartLine(UpgradeRequest request) {
    Optional<NameValuePair> startLine = URLEncodedUtils.parse(request.getRequestURI(), "UTF-8").
            stream().
            filter(pair -> "startLine".equals(pair.getName())).findFirst();

    return startLine.isPresent() ? Long.valueOf(startLine.get().getValue()) : 0L;
}
 
Example #20
Source File: JettyWebSocketClient.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public void onHandshakeRequest(UpgradeRequest request) {
	this.headers.forEach(request::setHeader);
}
 
Example #21
Source File: JettyWebSocketClient.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public void onHandshakeRequest(UpgradeRequest request) {
	this.headers.forEach(request::setHeader);
}
 
Example #22
Source File: MockSession.java    From buck with Apache License 2.0 4 votes vote down vote up
@Override
public UpgradeRequest getUpgradeRequest() {
  return null;
}
 
Example #23
Source File: WebSocketTunnelRequest.java    From guacamole-client with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a TunnelRequest implementation which delegates parameter and
 * session retrieval to the given UpgradeRequest.
 *
 * @param request The UpgradeRequest to wrap.
 */
public WebSocketTunnelRequest(UpgradeRequest request) {
    this.handshakeParameters = request.getParameterMap();
}
 
Example #24
Source File: WebSocketTunnelRequest.java    From guacamole-client with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a TunnelRequest implementation which delegates parameter and
 * session retrieval to the given UpgradeRequest.
 *
 * @param request The UpgradeRequest to wrap.
 */
public WebSocketTunnelRequest(UpgradeRequest request) {
    this.handshakeParameters = request.getParameterMap();
}