public void addServletMapping()

in modules/host-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/TomcatServer.java [219:430]


    public void addServletMapping(String suri, Servlet servlet, final SecurityContext securityContext) {
        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";
            }            
        }
        
        int tmpPortNumber = uri.getPort();
        if (tmpPortNumber == -1) {
            if ("http".equals(scheme)) {
                tmpPortNumber = defaultPortNumber;
            } else {
                tmpPortNumber = defaultPortNumber;
            }
        }
        
        final int portNumber = tmpPortNumber;

        // Get the port object associated with the given port number
        Port port = ports.get(portNumber);
        if (port == null) {

            // Create an engine
            // Allow privileged access to read properties. Requires PropertiesPermission read in
            // security policy.
            final StandardEngine engine = AccessController.doPrivileged(new PrivilegedAction<StandardEngine>() {
                public StandardEngine run() {
                    return new StandardEngine();
                }
            });

            engine.setBaseDir("");
            engine.setDefaultHost("localhost");
            engine.setName("engine/" + portNumber);

            // Create a host
            final StandardHost host = new StandardHost();
            host.setAppBase("");
            host.setName("localhost");
            // Allow privileged access to read properties. Requires PropertiesPermission read in
            // security policy.
            AccessController.doPrivileged(new PrivilegedAction<Object>() {
                public Object run() {
                    engine.addChild(host);
                    return null;
                }
            });

            // Create the root context
            StandardContext context = new StandardContext();
            final ClassLoader tccl = Thread.currentThread().getContextClassLoader();
            context.setLoader(new TuscanyLoader(tccl));
            // context.setParentClassLoader(tccl.getParent());
            context.setDocBase("");
            context.setPath("");
            ContextConfig config = new ContextConfig();
            ((Lifecycle)context).addLifecycleListener(config);
            host.addChild(context);

            // Install an HTTP connector
            // Allow privileged access to read properties. Requires PropertiesPermission read in
            // security policy.
            try {
                AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
                    public Object run() throws LifecycleException {
                        engine.start();
                        return null;
                    }
                });
            } catch (PrivilegedActionException e) {
                // throw (LifecycleException)e.getException();
                throw new ServletMappingException(e);
            }
            Connector connector;
            // Allow privileged access to read properties. Requires PropertiesPermission read in
            // security policy.
            try {
                final String protocol = scheme;
                connector = AccessController.doPrivileged(new PrivilegedExceptionAction<CustomConnector>() {
                    public CustomConnector run() throws Exception {
                        CustomConnector customConnector = new CustomConnector();
                        customConnector.setPort(portNumber);
                        customConnector.setContainer(engine);

                        if ("https".equalsIgnoreCase(protocol)) {
                            configureSSL(customConnector, securityContext);
                            ((Http11Protocol) customConnector.getProtocolHandler()).setSSLEnabled(true);
                        }
                        customConnector.initialize();
                        customConnector.start();
                        return customConnector;
                    }

                    private void configureSSL(CustomConnector customConnector, SecurityContext securityContext) {
                        String keyStoreType;
                        String keyStore;
                        String keyStorePass;

                        String trustStoreType;
                        String trustStore;
                        String trustStorePass;

                        if(securityContext == null) {
                            keyStoreType = System.getProperty("javax.net.ssl.keyStoreType", KeyStore.getDefaultType());
                            keyStore = System.getProperty("javax.net.ssl.keyStore");
                            keyStorePass = System.getProperty("javax.net.ssl.keyStorePassword");

                            trustStoreType = System.getProperty("javax.net.ssl.trustStoreType", KeyStore.getDefaultType());
                            trustStore = System.getProperty("javax.net.ssl.trustStore");
                            trustStorePass = System.getProperty("javax.net.ssl.trustStorePassword");
                        } else {
                            keyStoreType = securityContext.getSSLProperties().getProperty("javax.net.ssl.keyStoreType", KeyStore.getDefaultType());
                            keyStore = securityContext.getSSLProperties().getProperty("javax.net.ssl.keyStore");
                            keyStorePass = securityContext.getSSLProperties().getProperty("javax.net.ssl.keyStorePassword");

                            trustStoreType = securityContext.getSSLProperties().getProperty("javax.net.ssl.trustStoreType", KeyStore.getDefaultType());
                            trustStore = securityContext.getSSLProperties().getProperty("javax.net.ssl.trustStore");
                            trustStorePass = securityContext.getSSLProperties().getProperty("javax.net.ssl.trustStorePassword");
                        }
                        
                        customConnector.setProperty("protocol", "TLS");

                        customConnector.setProperty("keytype", keyStoreType);
                        customConnector.setProperty("keystore", keyStore);
                        customConnector.setProperty("keypass", keyStorePass);

                        customConnector.setProperty("trusttype", trustStoreType);
                        customConnector.setProperty("truststore", trustStore);
                        customConnector.setProperty("trustpass", trustStorePass);

                        customConnector.setProperty("clientauth", "false");
                        customConnector.setProtocol("HTTP/1.1");
                        customConnector.setScheme(protocol);
                        customConnector.setProperty("backlog", "10");
                        customConnector.setSecure(true);
                    }
                });
            } catch (Exception e) {
                throw new ServletMappingException(e);
            }
            // Keep track of the running server
            port = new Port(engine, host, connector);
            ports.put(portNumber, port);
        }

        // Register the Servlet mapping
        String path = uri.getPath();

        if (!path.startsWith("/")) {
            path = '/' + path;
        }

        if (!path.startsWith(contextPath)) {
            path = contextPath + path;
        }

        ServletWrapper wrapper;
        if (servlet instanceof DefaultResourceServlet) {
            String defaultServletPath = path;

            // Optimize the handling of resource requests, use the Tomcat default Servlet
            // instead of our default resource Servlet
            if (defaultServletPath.endsWith("*")) {
                defaultServletPath = defaultServletPath.substring(0, defaultServletPath.length() - 1);
            }
            if (defaultServletPath.endsWith("/")) {
                defaultServletPath = defaultServletPath.substring(0, defaultServletPath.length() - 1);
            }
            DefaultResourceServlet resourceServlet = (DefaultResourceServlet)servlet;
            TomcatDefaultServlet defaultServlet =
                new TomcatDefaultServlet(defaultServletPath, resourceServlet.getDocumentRoot());
            wrapper = new ServletWrapper(defaultServlet);

        } else {
            wrapper = new ServletWrapper(servlet);
        }
        Context context = port.getHost().map(path);
        wrapper.setName(path);
        wrapper.addMapping(path);
        context.addChild(wrapper);
        context.addServletMapping(path, path);
        port.getConnector().getMapper().addWrapper("localhost", "", path, wrapper);

        // Initialize the Servlet
        try {
            wrapper.initServlet();
        } catch (ServletException e) {
            throw new ServletMappingException(e);
        }

        // Compute the complete URL
        String host;
        try {
            host = InetAddress.getLocalHost().getHostName();
        } 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);
    }