in modules/host-jetty/src/main/java/org/apache/tuscany/sca/http/jetty/JettyServer.java [228:366]
public String addServletMapping(String suri, Servlet servlet, final SecurityContext securityContext)
throws ServletMappingException {
URI uri = URI.create(suri);
// Get the URI scheme and port
String scheme = null;
if (securityContext != null && securityContext.isSSLEnabled()) {
scheme = "https";
} else {
scheme = uri.getScheme();
if (scheme == null) {
scheme = "http";
}
}
String host = uri.getHost();
if ("0.0.0.0".equals(host)) {
host = null;
}
int portNumber = uri.getPort();
if (portNumber == -1) {
if ("http".equals(scheme)) {
portNumber = defaultPort;
} else {
portNumber = defaultSSLPort;
}
}
// Get the port object associated with the given port number
Port port = ports.get(portNumber);
if (port == null) {
// Create and start a new server
try {
Server server = new Server();
server.setThreadPool(new WorkSchedulerThreadPool());
if ("https".equals(scheme)) {
// Connector httpConnector = new SelectChannelConnector();
// httpConnector.setPort(portNumber);
SslSocketConnector sslConnector = new SslSocketConnector();
sslConnector.setPort(portNumber);
// FIXME: [rfeng] We should set the host to be bound but binding-ws-axis2 is passing
// in an absolute URI with host set to one of the ip addresses
sslConnector.setHost(host);
configureSSL(sslConnector, securityContext);
server.setConnectors(new Connector[] {sslConnector});
} else {
SelectChannelConnector selectConnector = new SelectChannelConnector();
selectConnector.setPort(portNumber);
// FIXME: [rfeng] We should set the host to be bound but binding-ws-axis2 is passing
// in an absolute URI with host set to one of the ip addresses
selectConnector.setHost(host);
server.setConnectors(new Connector[] {selectConnector});
}
ContextHandler contextHandler = new ContextHandler();
//contextHandler.setContextPath(contextPath);
contextHandler.setContextPath("/");
server.setHandler(contextHandler);
SessionHandler sessionHandler = new SessionHandler();
ServletHandler servletHandler = new ServletHandler();
sessionHandler.addHandler(servletHandler);
contextHandler.setHandler(sessionHandler);
server.setStopAtShutdown(true);
server.setSendServerVersion(sendServerVersion);
server.start();
// Keep track of the new server and Servlet handler
port = new Port(server, servletHandler);
ports.put(portNumber, port);
} catch (Exception e) {
throw new ServletMappingException(e);
}
}
// Register the Servlet mapping
ServletHandler servletHandler = port.getServletHandler();
ServletHolder holder;
if (servlet instanceof DefaultResourceServlet) {
// Optimize the handling of resource requests, use the Jetty default Servlet
// instead of our default resource Servlet
String servletPath = uri.getPath();
if (servletPath.endsWith("*")) {
servletPath = servletPath.substring(0, servletPath.length() - 1);
}
if (servletPath.endsWith("/")) {
servletPath = servletPath.substring(0, servletPath.length() - 1);
}
if (!servletPath.startsWith("/")) {
servletPath = '/' + servletPath;
}
DefaultResourceServlet resourceServlet = (DefaultResourceServlet)servlet;
DefaultServlet defaultServlet = new JettyDefaultServlet(servletPath, resourceServlet.getDocumentRoot());
holder = new ServletHolder(defaultServlet);
} else {
holder = new ServletHolder(servlet);
}
servletHandler.addServlet(holder);
ServletMapping mapping = new ServletMapping();
mapping.setServletName(holder.getName());
String path = uri.getPath();
if (!path.startsWith("/")) {
path = '/' + path;
}
if (!path.startsWith(contextPath)) {
path = contextPath + path;
}
mapping.setPathSpec(path);
servletHandler.addServletMapping(mapping);
// Compute the complete URL
if (host == null) {
try {
host = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
host = "localhost";
}
}
URL addedURL;
try {
addedURL = new URL(scheme, host, portNumber, path);
} catch (MalformedURLException e) {
throw new ServletMappingException(e);
}
logger.info("Added Servlet mapping: " + addedURL);
return addedURL.toString();
}