Open wise-coders opened 9 months ago
@wise-coders you don't show all your code, but I assume the code you show is a subclass of org.eclipse.jetty.server.Server
.
If that's the case, then:
remove this line, which is wrong:
contextHandler.setHandler( handlers );
since 2 lines below you call handlers.addHandler( contextHandler );
which is correct.
Then, you need to start the whole Server
instance, not just the ContextHandler
, at the last line.
Then, the try-finally
block is wrong, since it will close the ServerConnector
after the last line.
Finally, you don't need the ContextHandlerCollection
if you only have one ContextHandler
.
In summary, your code can be better be written in this way:
Server server = new Server();
ServerConnector sc = new ServerConnector(server);
sc.setPort(port);
sc.setHost("localhost");
server.addConnector(sc);
ServletContextHandler contextHandler = new ServletContextHandler("/", ServletContextHandler.SESSIONS);
server.setHandler(contextHandler);
contextHandler.setBaseResource(ResourceFactory.of( server ).newResource(new File(Sys.installForm).toPath()));
contextHandler.addServlet( new ServletHolder( new InternalImageServlet()), "/" + FormUtil.LOCAL_IMAGES + "*");
contextHandler.addServlet( new ServletHolder( new DbImageServlet()), "/" + FormUtil.DATABASE_IMAGES + "*");
ServletHolder holderDef = new ServletHolder( "default", DefaultServlet.class);
holderDef.setInitParameter("dirAllowed", "false");
contextHandler.addServlet(holderDef, "/");
server.start();
Thank you very much for your help. This works. I still have a problem with the local files, they seem not to be served. The variable Sys.installForm is an URL pointing to the local folder where this files are stored. Something similar worked in the old version of Jetty. What could be wrong?
contextHandler.setBaseResource(ResourceFactory.of(this).newResource(Sys.installForm));
ServletHolder holderDef = new ServletHolder( "default", DefaultServlet.class);
holderDef.setInitParameter("dirAllowed", "false");
contextHandler.addServlet(holderDef, "/");
You don't specify the Jetty version you are using.
Would be great if you can detail exactly what the problem is. Do you get an error? An exception on server side? A 404? What is the URI you are asking for? Are you sure the file is there? Etc.
Too little information to help you.
I use:
implementation 'org.eclipse.jetty:jetty-server:12.0.5'
implementation 'org.eclipse.jetty.ee10:jetty-ee10-servlet:12.0.5'
The request is
http://localhost:8087/css/forms.css
returns
HTTP ERROR 404 Not Found
URI: http://localhost:8087/css/forms.css
STATUS: 404
MESSAGE: Not Found
The path c:\work\dbschema\build\resources\main\forms\css\forms.css exists, Sys.installForms=file:/C:/work/dbschema/build/resources/main/forms/
The problem could be the file:/
initial part of the URI of Sys.installForms
.
I don't have Windows available, can you try setting the URI to file:///C:work/dbschema/build/resources/main/forms/
?
Note the triple slash after the scheme.
Unfortunately it does not work. I reverted to:
implementation 'org.eclipse.jetty:jetty-server:11.0.19'
implementation 'org.eclipse.jetty:jetty-servlet:11.0.19'
and it works fine with this code:
super( new InetSocketAddress("127.0.0.1", port));
// ACCEPT ONLY CONNECTIONS FROM LOCALHOST
try ( ServerConnector sc = new ServerConnector(this)) {
sc.setPort(port);
sc.setHost("localhost");
setConnectors(new Connector[]{sc});
this.contextHandler = new ServletContextHandler( null,"/", ServletContextHandler.SESSIONS );
this.setHandler( contextHandler );
contextHandler.setBaseResource( Resource.newResource(new File(Sys.installForm)));
contextHandler.addServlet( new ServletHolder( new InternalImageServlet()), "/" + FormUtil.LOCAL_IMAGES + "*");
contextHandler.addServlet( new ServletHolder( new DbImageServlet()), "/" + FormUtil.DATABASE_IMAGES + "*");
// FILES ARE SERVED HERE
ServletHolder holderDef = new ServletHolder( "default", DefaultServlet.class);
holderDef.setInitParameter("dirAllowed", "false");
contextHandler.addServlet(holderDef, "/");
}
The path c:\work\dbschema\build\resources\main\forms\css\forms.css exists, Sys.installForms=file:/C:/work/dbschema/build/resources/main/forms/
Is there a link (hardlink / symlink / etc) present in that path? If so, then it's considered an alias and then you need to enable the appropriate AliasChecker to allow it (which also reduces your security level).
There is no link in the path.
Run this code with Jetty 12 and tell us the output.
import java.nio.file.Path;
import java.nio.file.Files;
import org.eclipse.jetty.util.resource.PathResource;
Path pathA = Path.of(Sys.installForm);
PathResource resourceA = new PathResource(pathA);
System.out.println("pathA exists() = " + Files.exists(pathA));
System.out.println("pathA isDirectory() = " + Files.isDirectory(pathA));
System.out.println("resourceA = " + resourceA);
System.out.println("resourceA getURI() = " + resourceA.getURI());
System.out.println("resourceA realPath() = " + resourceA.getRealPath());
System.out.println("resourceA realURI() = " + resourceA.getRealURI());
System.out.println("resourceA isAlias() = " + resourceA.isAlias());
Here is the result:
pathA exists() = true
pathA isDirectory() = true
resourceA = file:///C:/work/dbschema/build/resources/main/forms/
resourceA getURI() = file:///C:/work/dbschema/build/resources/main/forms/
resourceA realPath() = C:\work\dbschema\build\resources\main\forms
resourceA realURI() = file:///C:/work/dbschema/build/resources/main/forms/
resourceA isAlias() = false
The code is slightly different, as the PathResource has no public constructor. So I used:
super( new InetSocketAddress("127.0.0.1", port));
// ACCEPT ONLY CONNECTIONS FROM LOCALHOST
try ( ServerConnector sc = new ServerConnector(this)) {
sc.setPort(port);
sc.setHost("localhost");
setConnectors(new Connector[]{sc});
this.contextHandler = new ServletContextHandler( "/", ServletContextHandler.SESSIONS );
this.setHandler( contextHandler );
Path pathA = new File(Sys.installForm).toPath();
PathResource resourceA = (PathResource)ResourceFactory.of( this ).newResource( pathA );
System.out.println("pathA exists() = " + Files.exists(pathA));
System.out.println("pathA isDirectory() = " + Files.isDirectory(pathA));
System.out.println("resourceA = " + resourceA);
System.out.println("resourceA getURI() = " + resourceA.getURI());
System.out.println("resourceA realPath() = " + resourceA.getRealPath());
System.out.println("resourceA realURI() = " + resourceA.getRealURI());
System.out.println("resourceA isAlias() = " + resourceA.isAlias());
contextHandler.setBaseResource( resourceA );
contextHandler.addServlet( new ServletHolder( new InternalImageServlet()), "/" + FormUtil.LOCAL_IMAGES + "*");
contextHandler.addServlet( new ServletHolder( new DbImageServlet()), "/" + FormUtil.DATABASE_IMAGES + "*");
// FILES ARE SERVED HERE
ServletHolder holderDef = new ServletHolder( "default", DefaultServlet.class);
holderDef.setInitParameter("dirAllowed", "false");
contextHandler.addServlet(holderDef, "/");
}
Thanks, I have enough to attempt to replicate, please be patient (new years and all)
I am executing
and getting
Maybe I do something wrong due migration from previous versions?