Skip to content

Commit

Permalink
Merge branch '10.0.x' into 11.0.x
Browse files Browse the repository at this point in the history
  • Loading branch information
joakime committed Feb 16, 2024
2 parents 525e6de + ae82f20 commit 685e81e
Show file tree
Hide file tree
Showing 45 changed files with 2,253 additions and 318 deletions.
13 changes: 13 additions & 0 deletions embedded/websocket-jakarta-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,24 @@
<version>${jetty.version}</version>
</dependency>

<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-slf4j-impl</artifactId>
<version>${jetty.version}</version>
</dependency>

<dependency>
<groupId>org.eclipse.jetty.websocket</groupId>
<artifactId>websocket-jakarta-server</artifactId>
<version>${jetty.version}</version>
</dependency>

<dependency>
<groupId>org.eclipse.jetty.toolchain</groupId>
<artifactId>jetty-test-helper</artifactId>
<version>${jetty-test-helper.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

package examples.annotated;

import jakarta.websocket.CloseReason;
import jakarta.websocket.OnClose;
import jakarta.websocket.OnError;
import jakarta.websocket.OnMessage;
import jakarta.websocket.OnOpen;
import jakarta.websocket.RemoteEndpoint;
import jakarta.websocket.Session;
import jakarta.websocket.server.ServerEndpoint;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@ServerEndpoint("/echo")
public class EchoSocket
{
private static final Logger LOG = LoggerFactory.getLogger(EchoSocket.class);
private Session session;
private RemoteEndpoint.Async remote;

@OnClose
public void onWebSocketClose(CloseReason close)
{
this.session = null;
this.remote = null;
LOG.info("WebSocket Close: {} - {}", close.getCloseCode(), close.getReasonPhrase());
}

@OnOpen
public void onWebSocketOpen(Session session)
{
this.session = session;
this.remote = this.session.getAsyncRemote();
LOG.info("WebSocket Connect: {}", session);
this.remote.sendText("You are now connected to " + this.getClass().getName());
}

@OnError
public void onWebSocketError(Throwable cause)
{
LOG.warn("WebSocket Error", cause);
}

@OnMessage
public String onWebSocketText(String message)
{
LOG.info("Echoing back text message [{}]", message);
// Using shortcut approach to sending messages.
// You could use a void method and use remote.sendText()
return message;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

package examples.annotated;

import java.net.URL;
import java.util.Objects;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.websocket.jakarta.server.config.JakartaWebSocketServletContainerInitializer;

public class Main
{
public static void main(String[] args) throws Exception
{
Server server = new Server(8080);
server.start();
server.join();
}

public static Server newServer(int port)
{
Server server = new Server(port);

ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
servletContextHandler.setContextPath("/");
server.setHandler(servletContextHandler);

// Add jakarta.websocket support
JakartaWebSocketServletContainerInitializer.configure(servletContextHandler, (context, container) ->
{
// Add echo endpoint to server container
container.addEndpoint(EchoSocket.class);
});

// Add default servlet (to serve the html/css/js)
// Figure out where the static files are stored.
URL urlStatics = Thread.currentThread().getContextClassLoader().getResource("echo-root/index.html");
Objects.requireNonNull(urlStatics, "Unable to find index.html in classpath");
String urlBase = urlStatics.toExternalForm().replaceFirst("/[^/]*$", "/");
ServletHolder defHolder = new ServletHolder("default", new DefaultServlet());
defHolder.setInitParameter("resourceBase", urlBase);
defHolder.setInitParameter("dirAllowed", "true");
servletContextHandler.addServlet(defHolder, "/");

return server;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// ========================================================================
//

package examples;
package examples.browser;

import java.util.Collections;
import java.util.List;
Expand All @@ -33,6 +33,8 @@ public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request,
private String getHeaderValue(HandshakeRequest request, String key)
{
List<String> value = request.getHeaders().get(key);
if (value == null)
return null;
return String.join(", ", value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// ========================================================================
//

package examples;
package examples.browser;

import java.net.MalformedURLException;
import java.net.URI;
Expand Down Expand Up @@ -40,7 +40,7 @@ public class JakartaBrowserMain
{
private static final Logger LOG = LoggerFactory.getLogger(JakartaBrowserMain.class);

public static void main(String[] args)
public static void main(String[] args) throws Exception
{
int port = 8080;
int sslPort = 8443;
Expand All @@ -58,30 +58,15 @@ public static void main(String[] args)
}
}

try
{
JakartaBrowserMain tool = new JakartaBrowserMain();
tool.setupServer(port, sslPort);
tool.runForever();
}
catch (Throwable t)
{
LOG.warn("Failed to start " + JakartaBrowserMain.class.getName(), t);
}
}

private Server server;

private void runForever() throws Exception
{
Server server = newServer(port, sslPort);
server.start();
server.dumpStdErr();
server.join();
}

private void setupServer(int port, int sslPort) throws MalformedURLException, URISyntaxException
public static Server newServer(int port, int sslPort) throws MalformedURLException, URISyntaxException
{
server = new Server();
Server server = new Server();

ServerConnector connector = new ServerConnector(server);
connector.setPort(port);
server.addConnector(connector);
Expand All @@ -91,12 +76,17 @@ private void setupServer(int port, int sslPort) throws MalformedURLException, UR
sslContextFactory.setKeyStoreResource(findKeyStore());
sslContextFactory.setKeyStorePassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4");
sslContextFactory.setKeyManagerPassword("OBF:1u2u1wml1z7s1z7a1wnl1u2g");
sslContextFactory.setSniRequired(false);

// Setup HTTPS Configuration
HttpConfiguration httpsConf = new HttpConfiguration();
httpsConf.setSecurePort(sslPort);
httpsConf.setSecureScheme("https");
httpsConf.addCustomizer(new SecureRequestCustomizer()); // adds ssl info to request object
SecureRequestCustomizer secureRequestCustomizer = new SecureRequestCustomizer();
// Disable SNI requirements, to allow for testing against IP or localhost
secureRequestCustomizer.setSniRequired(false);
secureRequestCustomizer.setSniHostCheck(false);
httpsConf.addCustomizer(secureRequestCustomizer); // adds ssl info to request object

// Establish the ServerConnector
ServerConnector httpsConnector = new ServerConnector(server,
Expand All @@ -110,7 +100,7 @@ private void setupServer(int port, int sslPort) throws MalformedURLException, UR
ClassLoader cl = JakartaBrowserMain.class.getClassLoader();
// We look for a file, as ClassLoader.getResource() is not
// designed to look for directories (we resolve the directory later)
URL f = cl.getResource("websocket-statics/index.html");
URL f = cl.getResource("browser-root/index.html");
if (f == null)
{
throw new RuntimeException("Unable to find resource directory");
Expand All @@ -129,7 +119,9 @@ private void setupServer(int port, int sslPort) throws MalformedURLException, UR
JakartaWebSocketServletContainerInitializer.configure(context,
(servletContext, wsContainer) -> wsContainer.addEndpoint(JakartaBrowserSocket.class));

LOG.info("{} setup on (http) port {} and (https) port {}", this.getClass().getName(), port, sslPort);
LOG.info("{} setup on (http) port {} and (https) port {}", JakartaBrowserMain.class.getName(), port, sslPort);

return server;
}

private static Resource findKeyStore() throws URISyntaxException, MalformedURLException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// ========================================================================
//

package examples;
package examples.browser;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -105,7 +105,7 @@ public void onMessage(String message)
{
case "info":
{
writeMessage("Using javax.websocket");
writeMessage("Using jakarta.websocket");
if (StringUtil.isBlank(userAgent))
{
writeMessage("Client has no User-Agent");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

package examples.endpoint;

import jakarta.websocket.CloseReason;
import jakarta.websocket.Endpoint;
import jakarta.websocket.EndpointConfig;
import jakarta.websocket.MessageHandler;
import jakarta.websocket.RemoteEndpoint;
import jakarta.websocket.Session;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class EchoSocket extends Endpoint implements MessageHandler.Whole<String>
{
private static final Logger LOG = LoggerFactory.getLogger(EchoSocket.class);
private Session session;
private RemoteEndpoint.Async remote;

@Override
public void onClose(Session session, CloseReason close)
{
super.onClose(session, close);
this.session = null;
this.remote = null;
LOG.info("WebSocket Close: {} - {}", close.getCloseCode(), close.getReasonPhrase());
}

@Override
public void onOpen(Session session, EndpointConfig config)
{
this.session = session;
this.remote = this.session.getAsyncRemote();
LOG.info("WebSocket Open: {}", session);
// attach echo message handler
session.addMessageHandler(this);
this.remote.sendText("You are now connected to " + this.getClass().getName());
}

@Override
public void onError(Session session, Throwable cause)
{
super.onError(session, cause);
LOG.warn("WebSocket Error", cause);
}

@Override
public void onMessage(String message)
{
LOG.info("Echoing back text message [{}]", message);
this.remote.sendText(message);
}
}
Loading

0 comments on commit 685e81e

Please sign in to comment.