2021-10-11 10:38:37 +00:00
|
|
|
package net.sourceforge.plantuml.servlet.server;
|
2011-11-02 22:11:52 +00:00
|
|
|
|
2021-10-15 13:11:55 +00:00
|
|
|
import java.util.EnumSet;
|
2011-11-02 22:11:52 +00:00
|
|
|
|
2021-10-15 13:11:55 +00:00
|
|
|
import org.eclipse.jetty.http.UriCompliance;
|
|
|
|
import org.eclipse.jetty.http.UriCompliance.Violation;
|
|
|
|
import org.eclipse.jetty.server.HttpConnectionFactory;
|
2011-11-02 22:11:52 +00:00
|
|
|
import org.eclipse.jetty.server.Server;
|
2021-10-15 13:11:55 +00:00
|
|
|
import org.eclipse.jetty.server.ServerConnector;
|
|
|
|
import org.eclipse.jetty.server.handler.DefaultHandler;
|
|
|
|
import org.eclipse.jetty.server.handler.HandlerList;
|
2011-11-02 22:11:52 +00:00
|
|
|
import org.eclipse.jetty.webapp.WebAppContext;
|
|
|
|
|
2021-10-11 10:38:37 +00:00
|
|
|
public class EmbeddedJettyServer implements ServerUtils {
|
2011-11-02 22:11:52 +00:00
|
|
|
|
2021-10-15 13:11:55 +00:00
|
|
|
private static final String contextPath = "/plantuml";
|
2014-02-15 18:04:30 +00:00
|
|
|
private Server server;
|
2011-11-02 22:11:52 +00:00
|
|
|
|
2021-10-11 10:38:37 +00:00
|
|
|
public EmbeddedJettyServer() {
|
2023-05-03 22:52:00 +00:00
|
|
|
String[] virtualHosts = new String[]{"localhost", "test.localhost"};
|
2021-10-15 13:11:55 +00:00
|
|
|
server = new Server();
|
|
|
|
|
|
|
|
ServerConnector connector = new ServerConnector(server);
|
|
|
|
// Proxy and OldProxy need empty path segments support in URIs
|
|
|
|
// Hence: allow AMBIGUOUS_EMPTY_SEGMENT
|
|
|
|
UriCompliance uriCompliance = UriCompliance.from(EnumSet.of(Violation.AMBIGUOUS_EMPTY_SEGMENT));
|
|
|
|
connector.getConnectionFactory(HttpConnectionFactory.class)
|
|
|
|
.getHttpConfiguration()
|
|
|
|
.setUriCompliance(uriCompliance);
|
|
|
|
server.addConnector(connector);
|
|
|
|
|
|
|
|
// PlantUML server web application
|
|
|
|
WebAppContext context = new WebAppContext(server, "src/main/webapp", EmbeddedJettyServer.contextPath);
|
2023-05-03 22:52:00 +00:00
|
|
|
context.addVirtualHosts(virtualHosts);
|
2021-10-15 13:11:55 +00:00
|
|
|
|
|
|
|
// Add static webjars resource files
|
|
|
|
// The maven-dependency-plugin in the pom.xml provides these files.
|
|
|
|
WebAppContext res = new WebAppContext(
|
|
|
|
server,
|
|
|
|
"target/classes/META-INF/resources/webjars",
|
|
|
|
EmbeddedJettyServer.contextPath + "/webjars"
|
|
|
|
);
|
2023-05-03 22:52:00 +00:00
|
|
|
res.addVirtualHosts(virtualHosts);
|
2021-10-15 13:11:55 +00:00
|
|
|
|
|
|
|
// Create server handler
|
|
|
|
HandlerList handlers = new HandlerList();
|
|
|
|
handlers.addHandler(res); // provides: /plantuml/webjars
|
|
|
|
handlers.addHandler(context); // provides: /plantuml
|
|
|
|
handlers.addHandler(new DefaultHandler()); // provides: /
|
|
|
|
|
|
|
|
server.setHandler(handlers);
|
2011-11-02 22:11:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void startServer() throws Exception {
|
|
|
|
server.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void stopServer() throws Exception {
|
|
|
|
server.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getServerUrl() {
|
2021-10-15 13:11:55 +00:00
|
|
|
return String.format(
|
|
|
|
"%s://%s%s",
|
|
|
|
server.getURI().getScheme(),
|
|
|
|
server.getURI().getAuthority(),
|
|
|
|
EmbeddedJettyServer.contextPath
|
|
|
|
);
|
2011-11-02 22:11:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|