flexible/java-11/websocket-jetty/src/main/java/com/example/flexible/websocket/jettynative/SendServlet.java [36:141]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@WebServlet("/send")
/** Servlet that sends the message sent over POST to over a websocket connection. */
public class SendServlet extends HttpServlet {

  private Logger logger = Logger.getLogger(SendServlet.class.getName());

  private static final String ENDPOINT = "/echo";
  private static final String WEBSOCKET_PROTOCOL_PREFIX = "ws://";
  private static final String WEBSOCKET_HTTPS_PROTOCOL_PREFIX = "wss://";
  private static final String APPENGINE_HOST_SUFFIX = ".appspot.com";

  // GAE_INSTANCE environment is used to detect App Engine Flexible Environment
  private static final String GAE_INSTANCE_VAR = "GAE_INSTANCE";
  // GOOGLE_CLOUD_PROJECT environment variable is set to the GCP project ID on App Engine Flexible.
  private static final String GOOGLE_CLOUD_PROJECT_ENV_VAR = "GOOGLE_CLOUD_PROJECT";
  // GAE_SERVICE environment variable is set to the GCP service name.
  private static final String GAE_SERVICE_ENV_VAR = "GAE_SERVICE";

  private final HttpClient httpClient;
  private final WebSocketClient webSocketClient;
  private final ClientSocket clientSocket;

  public SendServlet() {
    this.httpClient = createHttpClient();
    this.webSocketClient = createWebSocketClient();
    this.clientSocket = new ClientSocket();
  }

  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String message = request.getParameter("message");
    try {
      sendMessageOverWebSocket(message);
      response.sendRedirect("/");
    } catch (Exception e) {
      logger.severe("Error sending message over socket: " + e.getMessage());
      e.printStackTrace(response.getWriter());
      response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR_500);
    }
  }

  private HttpClient createHttpClient() {
    HttpClient httpClient;
    if (System.getenv(GAE_INSTANCE_VAR) != null) {
      // If on HTTPS, create client with SSL Context
      SslContextFactory.Client sslContextFactory = new SslContextFactory.Client();
      httpClient = new HttpClient(sslContextFactory);
    } else {
      // local testing on HTTP
      httpClient = new HttpClient();
    }
    return httpClient;
  }

  private WebSocketClient createWebSocketClient() {
    return new WebSocketClient(this.httpClient);
  }

  private void sendMessageOverWebSocket(String message) throws Exception {
    if (!httpClient.isRunning()) {
      try {
        httpClient.start();
      } catch (URISyntaxException e) {
        e.printStackTrace();
      }
    }
    if (!webSocketClient.isRunning()) {
      try {
        webSocketClient.start();
      } catch (URISyntaxException e) {
        e.printStackTrace();
      }
    }
    ClientUpgradeRequest request = new ClientUpgradeRequest();
    // Attempt connection
    Future<Session> future =
        webSocketClient.connect(clientSocket, new URI(getWebSocketAddress()), request);
    // Wait for Connect
    Session session = future.get();
    // Send a message
    session.getRemote().sendString(message);
    // Close session
    session.close();
  }

  /**
   * Returns the host:port/echo address a client needs to use to communicate with the server. On App
   * engine Flex environments, result will be in the form wss://project-id.appspot.com/echo
   */
  public static String getWebSocketAddress() {
    // Use ws://127.0.0.1:8080/echo when testing locally
    String webSocketHost = "127.0.0.1:8080";
    String webSocketProtocolPrefix = WEBSOCKET_PROTOCOL_PREFIX;

    // On App Engine flexible environment, use wss://project-id.appspot.com/echo
    if (System.getenv(GAE_INSTANCE_VAR) != null) {
      String projectId = System.getenv(GOOGLE_CLOUD_PROJECT_ENV_VAR);
      if (projectId != null) {
        String serviceName = System.getenv(GAE_SERVICE_ENV_VAR);
        webSocketHost = serviceName + "-dot-" + projectId + APPENGINE_HOST_SUFFIX;
      }
      Preconditions.checkNotNull(webSocketHost);
      // Use wss:// instead of ws:// protocol when connecting over https
      webSocketProtocolPrefix = WEBSOCKET_HTTPS_PROTOCOL_PREFIX;
    }
    return webSocketProtocolPrefix + webSocketHost + ENDPOINT;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



flexible/java-17/websocket-jetty/src/main/java/com/example/flexible/websocket/jettynative/SendServlet.java [36:141]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@WebServlet("/send")
/** Servlet that sends the message sent over POST to over a websocket connection. */
public class SendServlet extends HttpServlet {

  private Logger logger = Logger.getLogger(SendServlet.class.getName());

  private static final String ENDPOINT = "/echo";
  private static final String WEBSOCKET_PROTOCOL_PREFIX = "ws://";
  private static final String WEBSOCKET_HTTPS_PROTOCOL_PREFIX = "wss://";
  private static final String APPENGINE_HOST_SUFFIX = ".appspot.com";

  // GAE_INSTANCE environment is used to detect App Engine Flexible Environment
  private static final String GAE_INSTANCE_VAR = "GAE_INSTANCE";
  // GOOGLE_CLOUD_PROJECT environment variable is set to the GCP project ID on App Engine Flexible.
  private static final String GOOGLE_CLOUD_PROJECT_ENV_VAR = "GOOGLE_CLOUD_PROJECT";
  // GAE_SERVICE environment variable is set to the GCP service name.
  private static final String GAE_SERVICE_ENV_VAR = "GAE_SERVICE";

  private final HttpClient httpClient;
  private final WebSocketClient webSocketClient;
  private final ClientSocket clientSocket;

  public SendServlet() {
    this.httpClient = createHttpClient();
    this.webSocketClient = createWebSocketClient();
    this.clientSocket = new ClientSocket();
  }

  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String message = request.getParameter("message");
    try {
      sendMessageOverWebSocket(message);
      response.sendRedirect("/");
    } catch (Exception e) {
      logger.severe("Error sending message over socket: " + e.getMessage());
      e.printStackTrace(response.getWriter());
      response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR_500);
    }
  }

  private HttpClient createHttpClient() {
    HttpClient httpClient;
    if (System.getenv(GAE_INSTANCE_VAR) != null) {
      // If on HTTPS, create client with SSL Context
      SslContextFactory.Client sslContextFactory = new SslContextFactory.Client();
      httpClient = new HttpClient(sslContextFactory);
    } else {
      // local testing on HTTP
      httpClient = new HttpClient();
    }
    return httpClient;
  }

  private WebSocketClient createWebSocketClient() {
    return new WebSocketClient(this.httpClient);
  }

  private void sendMessageOverWebSocket(String message) throws Exception {
    if (!httpClient.isRunning()) {
      try {
        httpClient.start();
      } catch (URISyntaxException e) {
        e.printStackTrace();
      }
    }
    if (!webSocketClient.isRunning()) {
      try {
        webSocketClient.start();
      } catch (URISyntaxException e) {
        e.printStackTrace();
      }
    }
    ClientUpgradeRequest request = new ClientUpgradeRequest();
    // Attempt connection
    Future<Session> future =
        webSocketClient.connect(clientSocket, new URI(getWebSocketAddress()), request);
    // Wait for Connect
    Session session = future.get();
    // Send a message
    session.getRemote().sendString(message);
    // Close session
    session.close();
  }

  /**
   * Returns the host:port/echo address a client needs to use to communicate with the server. On App
   * engine Flex environments, result will be in the form wss://project-id.appspot.com/echo
   */
  public static String getWebSocketAddress() {
    // Use ws://127.0.0.1:8080/echo when testing locally
    String webSocketHost = "127.0.0.1:8080";
    String webSocketProtocolPrefix = WEBSOCKET_PROTOCOL_PREFIX;

    // On App Engine flexible environment, use wss://project-id.appspot.com/echo
    if (System.getenv(GAE_INSTANCE_VAR) != null) {
      String projectId = System.getenv(GOOGLE_CLOUD_PROJECT_ENV_VAR);
      if (projectId != null) {
        String serviceName = System.getenv(GAE_SERVICE_ENV_VAR);
        webSocketHost = serviceName + "-dot-" + projectId + APPENGINE_HOST_SUFFIX;
      }
      Preconditions.checkNotNull(webSocketHost);
      // Use wss:// instead of ws:// protocol when connecting over https
      webSocketProtocolPrefix = WEBSOCKET_HTTPS_PROTOCOL_PREFIX;
    }
    return webSocketProtocolPrefix + webSocketHost + ENDPOINT;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



