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

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

      
    
Rootfs path

      
    
Size
8769 (8.6 KB)
MD5
fa8a9589ed7c6134e59c2405e8f7ceee
SHA1
f6071c8edbaaa8aed23d3c718d90097284f6044f
SHA256
e86ed26c0a2d47ba3155ade2aa026268e80f5da7da60a27ce83b193bfbee13a8
SHA512

      
    
SHA1_git
e8d76e54c89319c753f5daa97b4444bcd9d35dc1
Is binary

      
    
Is text
True
Is archive

      
    
Is media

      
    
Is legal

      
    
Is manifest

      
    
Is readme

      
    
Is top level

      
    
Is key file

      
    
TesterEchoServer.java | 8.6 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.io.Writer; import java.nio.ByteBuffer; import java.util.concurrent.atomic.AtomicInteger; import jakarta.servlet.ServletContextEvent; import jakarta.websocket.DeploymentException; import jakarta.websocket.OnError; import jakarta.websocket.OnMessage; import jakarta.websocket.Session; import jakarta.websocket.server.ServerContainer; import jakarta.websocket.server.ServerEndpoint; import org.apache.tomcat.websocket.server.Constants; import org.apache.tomcat.websocket.server.WsContextListener; public class TesterEchoServer { public static class Config extends WsContextListener { public static final String PATH_ASYNC = "/echoAsync"; public static final String PATH_BASIC = "/echoBasic"; public static final String PATH_BASIC_LIMIT_LOW = "/echoBasicLimitLow"; public static final String PATH_BASIC_LIMIT_HIGH = "/echoBasicLimitHigh"; public static final String PATH_WRITER_ERROR = "/echoWriterError"; @Override public void contextInitialized(ServletContextEvent sce) { super.contextInitialized(sce); ServerContainer sc = (ServerContainer) sce.getServletContext() .getAttribute(Constants.SERVER_CONTAINER_SERVLET_CONTEXT_ATTRIBUTE); try { sc.addEndpoint(Async.class); sc.addEndpoint(Basic.class); sc.addEndpoint(BasicLimitLow.class); sc.addEndpoint(BasicLimitHigh.class); sc.addEndpoint(WriterError.class); sc.addEndpoint(RootEcho.class); } catch (DeploymentException e) { throw new IllegalStateException(e); } } } @ServerEndpoint("/echoAsync") public static class Async { @OnMessage public void echoTextMessage(Session session, String msg, boolean last) { try { session.getBasicRemote().sendText(msg, last); } catch (IOException ioe) { try { session.close(); } catch (IOException ignore) { // Ignore } } } @OnMessage public void echoBinaryMessage(Session session, ByteBuffer msg, boolean last) { try { session.getBasicRemote().sendBinary(msg, last); } catch (IOException ioe) { try { session.close(); } catch (IOException ignore) { // Ignore } } } } @ServerEndpoint("/echoBasic") public static class Basic { @OnMessage public void echoTextMessage(Session session, String msg) { try { session.getBasicRemote().sendText(msg); } catch (IOException ioe) { try { session.close(); } catch (IOException ignore) { // Ignore } } } @OnMessage public void echoBinaryMessage(Session session, ByteBuffer msg) { try { session.getBasicRemote().sendBinary(msg); } catch (IOException ioe) { try { session.close(); } catch (IOException ignore) { // Ignore } } } } @ServerEndpoint("/echoBasicLimitLow") public static class BasicLimitLow { public static final long MAX_SIZE = 10; @OnMessage(maxMessageSize = MAX_SIZE) public void echoTextMessage(Session session, String msg) { try { session.getBasicRemote().sendText(msg); } catch (IOException ioe) { try { session.close(); } catch (IOException ignore) { // Ignore } } } @OnMessage(maxMessageSize = MAX_SIZE) public void echoBinaryMessage(Session session, ByteBuffer msg) { try { session.getBasicRemote().sendBinary(msg); } catch (IOException ioe) { try { session.close(); } catch (IOException ignore) { // Ignore } } } } @ServerEndpoint("/echoBasicLimitHigh") public static class BasicLimitHigh { public static final long MAX_SIZE = 32 * 1024; @OnMessage(maxMessageSize = MAX_SIZE) public void echoTextMessage(Session session, String msg) { try { session.getBasicRemote().sendText(msg); } catch (IOException ioe) { try { session.close(); } catch (IOException ignore) { // Ignore } } } @OnMessage(maxMessageSize = MAX_SIZE) public void echoBinaryMessage(Session session, ByteBuffer msg) { try { session.getBasicRemote().sendBinary(msg); } catch (IOException ioe) { try { session.close(); } catch (IOException ignore) { // Ignore } } } } @ServerEndpoint("/echoWriterError") public static class WriterError { public static final String MSG_ERROR = "error"; public static final String MSG_COUNT = "count"; public static final String RESULT_PASS = "PASS"; public static final String RESULT_FAIL = "FAIL"; private AtomicInteger errorCount = new AtomicInteger(0); @OnMessage public void echoTextMessage(Session session, String msg) { try (Writer w = session.getBasicRemote().getSendWriter()) { if (MSG_ERROR.equals(msg)) { // Simulate an error throw new RuntimeException(); } else if (MSG_COUNT.equals(msg)) { int count = 0; while (count < 200 && errorCount.get() == 0) { // 200 * 50 == 10,000ms == 10s try { Thread.sleep(50); } catch (InterruptedException e) { // Ignore } count++; } if (errorCount.get() == 1) { w.write(RESULT_PASS); } else { w.write(RESULT_FAIL); } } else { // Default is echo w.write(msg); } } catch (IOException ioe) { // Should not happen try { session.close(); } catch (IOException ignore) { // Ignore } } } @OnError public void onError(@SuppressWarnings("unused") Throwable t) { errorCount.incrementAndGet(); } } @ServerEndpoint("/") public static class RootEcho { @OnMessage public void echoTextMessage(Session session, String msg) { try { session.getBasicRemote().sendText(msg); } catch (IOException ioe) { try { session.close(); } catch (IOException ignore) { // Ignore } } } } }
Detected license expression
apache-2.0
Detected license expression (SPDX)
Apache-2.0
Percentage of license text
17.53
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