Skip to content

Commit

Permalink
fixed base resource when accessing from jar
Browse files Browse the repository at this point in the history
  • Loading branch information
optyfr committed Jun 17, 2024
1 parent 6c22a7b commit 69d2e1a
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 24 deletions.
6 changes: 4 additions & 2 deletions jrmserver/src/main/java/jrm/fullserver/FullServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.eclipse.jetty.server.SslConnectionFactory;
import org.eclipse.jetty.server.handler.gzip.GzipHandler;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.util.resource.ResourceFactory;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.util.thread.QueuedThreadPool;

Expand Down Expand Up @@ -159,7 +160,7 @@ public static void parseArgs(String... args) throws IOException, URISyntaxExcept

cmd.parse(args);
debug = jArgs.debug;
clientPath = getClientPath(jArgs.clientPath);
clientPath = jArgs.clientPath;
bind = jArgs.bind;
httpPort = jArgs.httpPort;
httpsPort = jArgs.httpsPort;
Expand Down Expand Up @@ -368,7 +369,8 @@ public static void initialize() throws Exception
final var gh = gzipHandler();

final var context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setBaseResource(clientPath);
final var resourceFactory = ResourceFactory.of(context);
context.setBaseResource(getClientPath(resourceFactory, clientPath));
context.setContextPath("/");

context.addServlet(new ServletHolder("datasources", FullDataSourceServlet.class), "/datasources/*");
Expand Down
72 changes: 52 additions & 20 deletions jrmserver/src/main/java/jrm/server/AbstractServer.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package jrm.server;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.FileSystemNotFoundException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
Expand All @@ -16,10 +18,10 @@
import org.eclipse.jetty.ee9.servlet.DefaultServlet;
import org.eclipse.jetty.ee9.servlet.ServletHolder;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.resource.PathResourceFactory;
import org.eclipse.jetty.util.URIUtil;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.util.resource.ResourceFactory;
import org.eclipse.jetty.util.resource.URLResourceFactory;
import org.eclipse.jetty.util.resource.Resources;

import jrm.fullserver.FullServer;
import jrm.misc.Log;
Expand All @@ -35,7 +37,7 @@ public abstract class AbstractServer implements Daemon
private static final String TRUE = "true";
private static final String FALSE = "false";

protected static Resource clientPath;
protected static String clientPath;
protected static Server jettyserver = null;
protected static boolean debug;

Expand Down Expand Up @@ -200,27 +202,57 @@ public JettyException(String message, Throwable cause)
}
}

private static ResourceFactory prf = new PathResourceFactory();
private static ResourceFactory urf = new URLResourceFactory();

protected static Resource getClientPath(String path) throws URISyntaxException
protected static Resource getClientPath(ResourceFactory resourceFactory, String path) throws IOException, URISyntaxException
{
if (path != null)
return prf.newResource(getPath(path));
final var p = getPath("jrt:/jrm.merged.module/webclient/");
if (Files.exists(p))
return prf.newResource(p);
return urf.newResource(FullServer.class.getResource("/webclient/"));
Resource resource;

if (path != null)
{
resource = resourceFactory.newResource(path);
if (Resources.exists(resource))
return resource;
resource = resourceFactory.newClassLoaderResource(path, true);
if (Resources.exists(resource))
return resource;
}

resource = resourceFactory.newResource("jrt:/jrm.merged.module/webclient/");
if (Resources.exists(resource))
return resource;
URL url = FullServer.class.getResource("/webclient/");
if (url != null)
{
resource = resourceFactory.newResource(URIUtil.correctURI(url.toURI()));
if (Resources.exists(resource))
return resource;
}
throw new FileNotFoundException("Unable to find webclient path");
}

protected static Resource getCertsPath(String path) throws URISyntaxException
protected static Resource getCertsPath(String path) throws URISyntaxException, IOException
{
if (path != null)
return prf.newResource(getPath(path));
final var p = getPath("jrt:/jrm.merged.module/certs/localhost.pfx");
if (Files.exists(p))
return prf.newResource(p);
return urf.newResource(FullServer.class.getResource("/certs/localhost.pfx"));
Resource resource;
final var resourceFactory = ResourceFactory.root();
if (path != null)
{
resource = resourceFactory.newResource(path);
if (Resources.exists(resource))
return resource;
resource = resourceFactory.newClassLoaderResource(path, true);
if (Resources.exists(resource))
return resource;
}
resource = resourceFactory.newResource("jrt:/jrm.merged.module/certs/localhost.pfx");
if (Resources.exists(resource))
return resource;
URL url = FullServer.class.getResource("/certs/localhost.pfx");
if (url != null)
{
resource = resourceFactory.newResource(URIUtil.correctURI(url.toURI()));
if (Resources.exists(resource))
return resource;
}
throw new FileNotFoundException("Unable to find webclient path");
}

protected static Path getPath(String path)
Expand Down
6 changes: 4 additions & 2 deletions jrmserver/src/main/java/jrm/server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.gzip.GzipHandler;
import org.eclipse.jetty.util.resource.ResourceFactory;

import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
Expand Down Expand Up @@ -86,7 +87,7 @@ public static void parseArgs(String... args) throws NumberFormatException, IOExc

cmd.parse(args);
debug = jArgs.debug;
clientPath = getClientPath(jArgs.clientPath);
clientPath = jArgs.clientPath;
bind = jArgs.bind;
httpPort = jArgs.httpPort;
Optional.ofNullable(jArgs.workPath).map(s -> s.replace("%HOMEPATH%", System.getProperty("user.home"))).ifPresent(s -> System.setProperty("jrommanager.dir", s));
Expand Down Expand Up @@ -138,7 +139,8 @@ public static void initialize() throws Exception
jettyserver = new org.eclipse.jetty.server.Server();

final var context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setBaseResource(clientPath);
final var resourceFactory = ResourceFactory.of(context);
context.setBaseResource(getClientPath(resourceFactory, clientPath));
context.setContextPath("/");

final var gzipHandler = new GzipHandler();
Expand Down

0 comments on commit 69d2e1a

Please sign in to comment.