Search in sources :

Example 11 with UpgradeException

use of org.eclipse.jetty.websocket.api.UpgradeException in project jetty.project by eclipse.

the class ClientConnectTest method testBadHandshake_SwitchingProtocols_NoConnectionHeader.

@Test
public void testBadHandshake_SwitchingProtocols_NoConnectionHeader() throws Exception {
    JettyTrackingSocket wsocket = new JettyTrackingSocket();
    URI wsUri = server.getWsUri();
    Future<Session> future = client.connect(wsocket, wsUri);
    IBlockheadServerConnection connection = server.accept();
    List<String> requestLines = connection.readRequestLines();
    String key = connection.parseWebSocketKey(requestLines);
    // Send Switching Protocols 101, but no 'Connection' header
    StringBuilder resp = new StringBuilder();
    resp.append("HTTP/1.1 101 Switching Protocols\r\n");
    resp.append("Sec-WebSocket-Accept: ").append(AcceptHash.hashKey(key)).append("\r\n");
    // Intentionally leave out Connection header
    resp.append("\r\n");
    connection.respond(resp.toString());
    // The attempt to get upgrade response future should throw error
    try {
        future.get(30, TimeUnit.SECONDS);
        Assert.fail("Expected ExecutionException -> UpgradeException");
    } catch (ExecutionException e) {
        // Expected Path
        UpgradeException ue = assertExpectedError(e, wsocket, UpgradeException.class);
        Assert.assertThat("UpgradeException.requestURI", ue.getRequestURI(), notNullValue());
        Assert.assertThat("UpgradeException.requestURI", ue.getRequestURI().toASCIIString(), is(wsUri.toASCIIString()));
        Assert.assertThat("UpgradeException.responseStatusCode", ue.getResponseStatusCode(), is(101));
    }
}
Also used : UpgradeException(org.eclipse.jetty.websocket.api.UpgradeException) IBlockheadServerConnection(org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection) ExecutionException(java.util.concurrent.ExecutionException) URI(java.net.URI) Session(org.eclipse.jetty.websocket.api.Session) Test(org.junit.Test)

Example 12 with UpgradeException

use of org.eclipse.jetty.websocket.api.UpgradeException in project elastic-core-maven by OrdinaryDude.

the class PeerWebSocket method startClient.

/**
 * Start a client session
 *
 * @param   uri                 Server URI
 * @return                      TRUE if the WebSocket connection was completed
 * @throws  IOException         I/O error occurred
 */
public boolean startClient(URI uri) throws IOException {
    if (peerClient == null) {
        return false;
    }
    String address = String.format("%s:%d", uri.getHost(), uri.getPort());
    boolean useWebSocket = false;
    // 
    // Create a WebSocket connection.  We need to serialize the connection requests
    // since the NRS server will issue multiple concurrent requests to the same peer.
    // After a successful connection, the subsequent connection requests will return
    // immediately.  After an unsuccessful connection, a new connect attempt will not
    // be done until 60 seconds have passed.
    // 
    lock.lock();
    try {
        if (session != null) {
            useWebSocket = true;
        } else if (System.currentTimeMillis() > connectTime + 10 * 1000) {
            connectTime = System.currentTimeMillis();
            ClientUpgradeRequest req = new ClientUpgradeRequest();
            Future<Session> conn = peerClient.connect(this, uri, req);
            conn.get(Peers.connectTimeout + 100, TimeUnit.MILLISECONDS);
            useWebSocket = true;
        }
    } catch (ExecutionException exc) {
        if (exc.getCause() instanceof UpgradeException) {
        // We will use HTTP
        } else if (exc.getCause() instanceof IOException) {
            // Report I/O exception
            throw (IOException) exc.getCause();
        } else {
            // We will use HTTP
            Logger.logDebugMessage(String.format("WebSocket connection to %s failed", address), exc);
        }
    } catch (TimeoutException exc) {
        throw new SocketTimeoutException(String.format("WebSocket connection to %s timed out", address));
    } catch (IllegalStateException exc) {
        if (!peerClient.isStarted()) {
            Logger.logDebugMessage("WebSocket client not started or shutting down");
            throw exc;
        }
        Logger.logDebugMessage(String.format("WebSocket connection to %s failed", address), exc);
    } catch (Exception exc) {
        Logger.logDebugMessage(String.format("WebSocket connection to %s failed", address), exc);
    } finally {
        if (!useWebSocket) {
            close();
        }
        lock.unlock();
    }
    return useWebSocket;
}
Also used : UpgradeException(org.eclipse.jetty.websocket.api.UpgradeException) SocketTimeoutException(java.net.SocketTimeoutException) Future(java.util.concurrent.Future) ClientUpgradeRequest(org.eclipse.jetty.websocket.client.ClientUpgradeRequest) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) WebSocketException(org.eclipse.jetty.websocket.api.WebSocketException) TimeoutException(java.util.concurrent.TimeoutException) SocketException(java.net.SocketException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) EOFException(java.io.EOFException) UpgradeException(org.eclipse.jetty.websocket.api.UpgradeException) ExecutionException(java.util.concurrent.ExecutionException) ProtocolException(java.net.ProtocolException) TimeoutException(java.util.concurrent.TimeoutException) SocketTimeoutException(java.net.SocketTimeoutException)

Aggregations

UpgradeException (org.eclipse.jetty.websocket.api.UpgradeException)12 URI (java.net.URI)11 Session (org.eclipse.jetty.websocket.api.Session)10 ExecutionException (java.util.concurrent.ExecutionException)7 IBlockheadServerConnection (org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection)6 Test (org.junit.Test)6 ClientUpgradeRequest (org.eclipse.jetty.websocket.client.ClientUpgradeRequest)5 WebSocketClient (org.eclipse.jetty.websocket.client.WebSocketClient)4 Test (org.testng.annotations.Test)4 EOFException (java.io.EOFException)1 IOException (java.io.IOException)1 ProtocolException (java.net.ProtocolException)1 SocketException (java.net.SocketException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 Future (java.util.concurrent.Future)1 TimeoutException (java.util.concurrent.TimeoutException)1 BacklogQuota (org.apache.pulsar.common.policies.data.BacklogQuota)1 HttpResponse (org.eclipse.jetty.client.HttpResponse)1 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)1 Response (org.eclipse.jetty.client.api.Response)1