ttomcat-1778514358873.zip-extract/apache-tomcat-11.0.18-src/test/org/apache/tomcat/websocket/TesterWsClientAutobahn.java

Path
ttomcat-1778514358873.zip-extract/apache-tomcat-11.0.18-src/test/org/apache/tomcat/websocket/TesterWsClientAutobahn.java
Status
scanned
Type
file
Name
TesterWsClientAutobahn.java
Extension
.java
Programming language
Java
Mime type
text/x-c
File type
C source, ASCII text, with CRLF line terminators
Tag

      
    
Rootfs path

      
    
Size
6837 (6.7 KB)
MD5
0371dfa37a1d7e2c9f56eb4a12cc8e67
SHA1
f25b151394d1f6d32c02713bb4cc3541dac5627b
SHA256
80af393c6a50a1a9b19256a1bbf8671c7c4d2e594109b1f968f8325ea94f4669
SHA512

      
    
SHA1_git
e8d7ffd93b1d89c06e4c050810cc91a9af75e4b0
Is binary

      
    
Is text
True
Is archive

      
    
Is media

      
    
Is legal

      
    
Is manifest

      
    
Is readme

      
    
Is top level

      
    
Is key file

      
    
TesterWsClientAutobahn.java | 6.7 KB |

/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.tomcat.websocket; import java.io.IOException; import java.net.URI; import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.List; import java.util.concurrent.CountDownLatch; import jakarta.websocket.ClientEndpoint; import jakarta.websocket.ClientEndpointConfig; import jakarta.websocket.ContainerProvider; import jakarta.websocket.Endpoint; import jakarta.websocket.Extension; import jakarta.websocket.OnClose; import jakarta.websocket.OnError; import jakarta.websocket.OnMessage; import jakarta.websocket.Session; import jakarta.websocket.WebSocketContainer; import org.apache.tomcat.util.ExceptionUtils; import org.apache.tomcat.websocket.pojo.PojoEndpointClient; /** * Runs the Autobahn test suite in client mode for testing the WebSocket client implementation. */ public class TesterWsClientAutobahn { private static final String HOST = "localhost"; private static final int PORT = 9001; private static final String USER_AGENT = "ApacheTomcat8WebSocketClient"; public static void main(String[] args) throws Exception { WebSocketContainer wsc = ContainerProvider.getWebSocketContainer(); int testCaseCount = getTestCaseCount(wsc); System.out.println("There are " + testCaseCount + " test cases"); for (int testCase = 1; testCase <= testCaseCount; testCase++) { if (testCase % 50 == 0) { System.out.println(testCase); } else { System.out.print('.'); } try { executeTestCase(wsc, testCase); } catch (Throwable t) { ExceptionUtils.handleThrowable(t); t.printStackTrace(); } } System.out.println("Testing complete"); updateReports(wsc); } private static int getTestCaseCount(WebSocketContainer wsc) throws Exception { URI uri = new URI("ws://" + HOST + ":" + PORT + "/getCaseCount"); CaseCountClient caseCountClient = new CaseCountClient(); wsc.connectToServer(caseCountClient, uri); return caseCountClient.getCaseCount(); } private static void executeTestCase(WebSocketContainer wsc, int testCase) throws Exception { URI uri = new URI("ws://" + HOST + ":" + PORT + "/runCase?case=" + testCase + "&agent=" + USER_AGENT); TestCaseClient testCaseClient = new TestCaseClient(); Extension permessageDeflate = new WsExtension("permessage-deflate"); // Advertise support for client_max_window_bits // Client only supports some values so there will be some failures here // Note Autobahn returns a 400 response if you provide a value for // client_max_window_bits permessageDeflate.getParameters().add(new WsExtensionParameter("client_max_window_bits", null)); List<Extension> extensions = new ArrayList<>(1); extensions.add(permessageDeflate); Endpoint ep = new PojoEndpointClient(testCaseClient, null, null); ClientEndpointConfig.Builder builder = ClientEndpointConfig.Builder.create(); ClientEndpointConfig config = builder.extensions(extensions).build(); wsc.connectToServer(ep, config, uri); testCaseClient.waitForClose(); } private static void updateReports(WebSocketContainer wsc) throws Exception { URI uri = new URI("ws://" + HOST + ":" + PORT + "/updateReports?agent=" + USER_AGENT); UpdateReportsClient updateReportsClient = new UpdateReportsClient(); wsc.connectToServer(updateReportsClient, uri); } @ClientEndpoint public static class CaseCountClient { private final CountDownLatch latch = new CountDownLatch(1); private volatile int caseCount = 0; // Need to wait for message public int getCaseCount() throws InterruptedException { latch.await(); return caseCount; } @OnMessage public void onMessage(String msg) { latch.countDown(); caseCount = Integer.parseInt(msg); } @OnError public void onError(Throwable t) { latch.countDown(); t.printStackTrace(); } } @ClientEndpoint public static class TestCaseClient { private final CountDownLatch latch = new CountDownLatch(1); public void waitForClose() throws InterruptedException { latch.await(); } @OnMessage public void echoTextMessage(Session session, String msg, boolean last) { try { if (session.isOpen()) { session.getBasicRemote().sendText(msg, last); } } catch (IOException ioe) { try { session.close(); } catch (IOException ignore) { // Ignore } } } @OnMessage public void echoBinaryMessage(Session session, ByteBuffer bb, boolean last) { try { if (session.isOpen()) { session.getBasicRemote().sendBinary(bb, last); } } catch (IOException ioe) { try { session.close(); } catch (IOException ignore) { // Ignore } } } @OnClose public void releaseLatch() { latch.countDown(); } } @ClientEndpoint public static class UpdateReportsClient { private final CountDownLatch latch = new CountDownLatch(1); public void waitForClose() throws InterruptedException { latch.await(); } @OnClose public void onClose() { latch.countDown(); } } }
Detected license expression
apache-2.0
Detected license expression (SPDX)
Apache-2.0
Percentage of license text
19.13
Copyrights

      
    
Holders

      
    
Authors

      
    
License detections License expression License expression SPDX
apache_2_0-4bde3f57-78aa-4201-96bf-531cba09e7de apache-2.0 Apache-2.0
URL Start line End line
http://www.apache.org/licenses/LICENSE-2.0 9 9