Skip to content

Commit

Permalink
Migrating github.com:jetty-project/embedded-jetty-with-web-resources
Browse files Browse the repository at this point in the history
  • Loading branch information
joakime committed Feb 2, 2024
1 parent 327b518 commit 02ee330
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 22 deletions.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
<parent>
<groupId>org.eclipse.jetty.examples.embedded</groupId>
<artifactId>jetty-embedded-examples</artifactId>
<version>9.4.x</version>
<version>12.0.x</version>
</parent>
<artifactId>metainf-resources</artifactId>
<version>9.4.x</version>
<artifactId>ee10-metainf-resources</artifactId>
<version>12.0.x</version>
<packaging>jar</packaging>
<name>Jetty Examples :: Jetty 9.4.x :: Embedded :: Integrating jars with META-INF/resources without WebAppContext</name>
<name>Jetty Examples :: Jetty 12.0.x :: Embedded :: EE10 Integrating jars with META-INF/resources without WebAppContext</name>

<dependencies>
<dependency>
Expand All @@ -19,7 +19,12 @@
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<artifactId>jetty-slf4j-impl</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.ee10</groupId>
<artifactId>jetty-ee10-servlet</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
import java.util.Collections;
import java.util.List;

import org.eclipse.jetty.ee10.servlet.DefaultServlet;
import org.eclipse.jetty.ee10.servlet.ServletContextHandler;
import org.eclipse.jetty.ee10.servlet.ServletHolder;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.util.resource.ResourceCollection;
import org.eclipse.jetty.util.resource.ResourceFactory;

public class MetaInfResourceDemo
{
Expand All @@ -40,12 +40,14 @@ public static Server newServer(int port) throws Exception
{
Server server = new Server(port);

HandlerList handlers = new HandlerList();
Handler.Sequence handlers = new Handler.Sequence();

ServletContextHandler context = new ServletContextHandler();
context.setContextPath("/");

Resource manifestResources = findManifestResources(MetaInfResourceDemo.class.getClassLoader());
ResourceFactory resourceFactory = ResourceFactory.of(context);

Resource manifestResources = findManifestResources(resourceFactory, MetaInfResourceDemo.class.getClassLoader());
context.setBaseResource(manifestResources);

// Add something to serve the static files
Expand All @@ -60,15 +62,15 @@ public static Server newServer(int port) throws Exception
return server;
}

private static Resource findManifestResources(ClassLoader classLoader) throws IOException
private static Resource findManifestResources(ResourceFactory resourceFactory, ClassLoader classLoader) throws IOException
{
List<URL> hits = Collections.list(classLoader.getResources("META-INF/resources"));
int size = hits.size();
Resource[] resources = new Resource[hits.size()];
for (int i = 0; i < size; i++)
{
resources[i] = Resource.newResource(hits.get(i));
resources[i] = resourceFactory.newResource(hits.get(i));
}
return new ResourceCollection(resources);
return ResourceFactory.combine(resources);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.component.LifeCycle;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.util.resource.ResourceFactory;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -81,11 +82,12 @@ public void testGetBootStrapResource() throws Exception
*/
private String findMetaInfResourceFile(ClassLoader classLoader, String prefix, String regex) throws IOException
{
List<URL> hits = Collections.list(classLoader.getResources("META-INF/resources" + prefix));
for (URL hit : hits)
try (ResourceFactory.Closeable resourceFactory = ResourceFactory.closeable())
{
try (Resource res = Resource.newResource(hit))
List<URL> hits = Collections.list(classLoader.getResources("META-INF/resources" + prefix));
for (URL hit : hits)
{
Resource res = resourceFactory.newResource(hit);
Resource match = findNestedResource(res, regex);
if (match != null)
{
Expand All @@ -110,10 +112,9 @@ private String findMetaInfResourceFile(ClassLoader classLoader, String prefix, S

private Resource findNestedResource(Resource res, String regex) throws IOException
{
for (String content : res.list())
for (Resource subresource : res.list())
{
Resource subresource = res.addPath(content);
if (content.matches(regex))
if (subresource.getFileName().matches(regex))
return subresource;
if (subresource.isDirectory())
{
Expand Down
2 changes: 1 addition & 1 deletion embedded/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<module>deploying</module>
<module>ee8-webapp-context</module>
<module>ee10-file-server</module>
<module>ee10-metainf-resources</module>
<module>ee10-servlet-config</module>
<module>ee10-servlet-security</module>
<module>ee10-webapp-context</module>
Expand All @@ -31,7 +32,6 @@
<module>form-post</module>
<module>http-config</module>
<module>jndi</module>
<module>metainf-resources</module>
<module>redirect</module>
<module>requestlog</module>
<module>rewrite</module>
Expand Down

0 comments on commit 02ee330

Please sign in to comment.