Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/11.0.x' into 12.0.x
Browse files Browse the repository at this point in the history
  • Loading branch information
joakime committed Apr 5, 2024
2 parents 80c80a6 + 2779761 commit 87a0baa
Show file tree
Hide file tree
Showing 20 changed files with 10,144 additions and 0 deletions.
15 changes: 15 additions & 0 deletions embedded/ee10-servlet-with-cdi/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Example of using Weld with Embedded Jetty

See [ServerMain.java](src/main/java/examples/ServerMain.java) for the entry point.

## To compile this project

``` shell
mvn clean install
```

## To run this project

``` shell
java -jar target/servlet-with-cdi.jar
```
108 changes: 108 additions & 0 deletions embedded/ee10-servlet-with-cdi/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.eclipse.jetty.examples.embedded</groupId>
<artifactId>jetty-embedded-examples</artifactId>
<version>12.0.x</version>
</parent>
<artifactId>ee10-servlet-with-cdi</artifactId>
<version>12.0.x</version>
<packaging>jar</packaging>
<name>Jetty Examples :: Jetty 12.0.x :: Embedded :: Servlet with CDI</name>

<properties>
<servlet.jsp.version>3.0.0</servlet.jsp.version>
<slf4j.version>2.0.12</slf4j.version>
<weld.version>5.1.2.Final</weld.version>
</properties>

<dependencies>
<dependency>
<groupId>org.eclipse.jetty.ee10</groupId>
<artifactId>jetty-ee10-cdi</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.ee10</groupId>
<artifactId>jetty-ee10-servlet</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.ee10.websocket</groupId>
<artifactId>jetty-ee10-websocket-jakarta-server</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.weld.servlet</groupId>
<artifactId>weld-servlet-core</artifactId>
<version>${weld.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>${slf4j.version}</version>
</dependency>
</dependencies>

<build>
<finalName>${project.artifactId}</finalName>

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>examples.ServerMain</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<id>uber-jar</id>
<goals>
<goal>shade</goal>
</goals>
<phase>package</phase>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<shadedClassifierName>uber</shadedClassifierName>
<artifactSet>
<excludes>
<exclude>classworlds:classworlds</exclude>
<exclude>org.junit.jupiter:*</exclude>
</excludes>
</artifactSet>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
<exclude>META-INF/MANIFEST.MF</exclude>
<exclude>**/module-info.class</exclude>
</excludes>
</filter>
</filters>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//
// ========================================================================
// 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;

import java.net.URI;
import java.net.URL;

import examples.logging.Logging;
import org.eclipse.jetty.ee10.cdi.CdiDecoratingListener;
import org.eclipse.jetty.ee10.cdi.CdiServletContainerInitializer;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.ee10.servlet.DefaultServlet;
import org.eclipse.jetty.ee10.servlet.ServletContextHandler;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.ee10.websocket.jakarta.server.config.JakartaWebSocketServletContainerInitializer;
import org.eclipse.jetty.util.resource.ResourceFactory;
import org.slf4j.LoggerFactory;

public class ServerMain
{
public static void main(String[] args)
{
Logging.config();
try
{
new ServerMain().run(8080);
}
catch (Throwable t)
{
LoggerFactory.getLogger(ServerMain.class).warn("Failed to start Server", t);
}
}

public void run(int port) throws Exception
{
Server server = new Server(port);

URL webRootLocation = this.getClass().getResource("/static-root/index.html");
if (webRootLocation == null)
{
throw new IllegalStateException("Unable to determine webroot URL location");
}

URI webRootUri = URI.create(webRootLocation.toURI().toASCIIString().replaceFirst("/index.html$", "/"));
System.err.printf("Web Root URI: %s%n", webRootUri);

ServletContextHandler context = new ServletContextHandler();
context.setContextPath("/");
Resource webResource = ResourceFactory.of(context).newResource(webRootUri);
context.setBaseResource(webResource);
context.setWelcomeFiles(new String[]{"index.html"});

context.getMimeTypes().addMimeMapping("txt", "text/plain;charset=utf-8");

// Enable Weld + CDI
context.setInitParameter(CdiServletContainerInitializer.CDI_INTEGRATION_ATTRIBUTE, CdiDecoratingListener.MODE);
context.addServletContainerInitializer(new CdiServletContainerInitializer());
context.addServletContainerInitializer(new org.jboss.weld.environment.servlet.EnhancedListener());

// Add WebSocket endpoints
JakartaWebSocketServletContainerInitializer.configure(context, (servletContext, wsContainer) ->
wsContainer.addEndpoint(TimeSocket.class));

// Add Servlet endpoints
context.addServlet(TimeServlet.class, "/time/");
context.addServlet(DefaultServlet.class, "/");

// Add to Server
server.setHandler(context);

// Start Server
server.start();
server.join();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// ========================================================================
// 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;

import java.io.IOException;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;
import java.util.logging.Logger;

import jakarta.inject.Inject;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

public class TimeServlet extends HttpServlet
{
@Inject
public Logger logger;

private static final TimeZone TZ = TimeZone.getDefault();

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException
{
logger.info(String.format("%s:%d Requested Time", req.getRemoteAddr(), req.getRemotePort()));
Locale locale = req.getLocale();
Calendar cal = Calendar.getInstance(TZ, locale);
String dateStr = DateFormat.getDateInstance(DateFormat.DEFAULT, locale).format(cal.getTime());
String timeStr = DateFormat.getTimeInstance(DateFormat.DEFAULT, locale).format(cal.getTime());
String tzStr = TZ.getDisplayName(false, TimeZone.SHORT, locale);
resp.getWriter().println(String.format("%s %s %s", dateStr, timeStr, tzStr));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//
// ========================================================================
// 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;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

import jakarta.inject.Inject;
import jakarta.websocket.CloseReason;
import jakarta.websocket.OnClose;
import jakarta.websocket.OnOpen;
import jakarta.websocket.Session;
import jakarta.websocket.server.ServerEndpoint;

@ServerEndpoint("/time/")
public class TimeSocket implements Runnable
{
@Inject
public Logger logger;

private TimeZone timezone;
private Session session;

@OnOpen
public void onOpen(Session session)
{
logger.info("onOpen() session:" + session);
this.session = session;
this.timezone = TimeZone.getTimeZone("UTC");
new Thread(this).start();
}

@OnClose
public void onClose(CloseReason close)
{
logger.info("onClose() close:" + close);
this.session = null;
}

@Override
public void run()
{
while (this.session != null)
{
try
{
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
dateFormat.setTimeZone(timezone);

String timestamp = dateFormat.format(new Date());
this.session.getBasicRemote().sendText(timestamp);
TimeUnit.SECONDS.sleep(1);
}
catch (InterruptedException | IOException e)
{
e.printStackTrace();
}
}
}
}
Loading

0 comments on commit 87a0baa

Please sign in to comment.