ninja.standalone.StandaloneHelper.java Source code

Java tutorial

Introduction

Here is the source code for ninja.standalone.StandaloneHelper.java

Source

/**
 * Copyright (C) 2012-2015 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ninja.standalone;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.URI;
import java.net.URL;
import java.security.KeyStore;
import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Helper utilities for working with standalone applications.
 */
public class StandaloneHelper {
    static private final Logger log = LoggerFactory.getLogger(StandaloneHelper.class);

    static private final String URI_SCHEME_CLASSPATH = "classpath";

    static public int findAvailablePort(int min, int max) {
        for (int port = min; port < max; port++) {
            try {
                new ServerSocket(port).close();
                return port;
            } catch (IOException e) {
                // Must already be taken
            }
        }
        throw new IllegalStateException("Could not find available port in range " + min + " to " + max);
    }

    /**
     * Resolves which standalone class to use. Either defined as a system
     * property or falling back to the default.
     * @return The resolved standalone class to use
     */
    static public Class<? extends Standalone> resolveStandaloneClass() {
        String standaloneClassName = System.getProperty(Standalone.KEY_NINJA_STANDALONE_CLASS,
                Standalone.DEFAULT_STANDALONE_CLASS);
        try {
            return (Class<Standalone>) Class.forName(standaloneClassName);
        } catch (Exception e) {
            throw new RuntimeException(
                    "Unable to find standalone class '" + standaloneClassName + "' (class does not exist)");
        }
    }

    static public Standalone create(Class<? extends Standalone> standaloneClass) {
        try {
            return standaloneClass.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            throw new RuntimeException("Unable to create " + standaloneClass.getCanonicalName()
                    + " (either not on classpath or invalid class name)");
        }
    }

    static void checkContextPath(String contextPath) {
        // per servlet docs
        // The path starts with a / character but does not end with a / character.
        // For servlets in the default (root) context, this method returns ""
        if (StringUtils.isEmpty(contextPath)) {
            return;
        }

        if (!contextPath.startsWith("/")) {
            throw new IllegalArgumentException("A context path must start with a '/' character. "
                    + " See https://docs.oracle.com/javaee/6/api/javax/servlet/ServletContext.html#getContextPath() for more info");
        }

        if (contextPath.endsWith("/")) {
            throw new IllegalArgumentException("A context path must not end with a '/' character. "
                    + " See https://docs.oracle.com/javaee/6/api/javax/servlet/ServletContext.html#getContextPath() for more info");
        }
    }

    static public InputStream openKeyStoreInput(URI uri) throws IOException {
        if (uri.getScheme().equals(URI_SCHEME_CLASSPATH)) {
            String resourceName = uri.getPath();

            log.debug("Opening keystore on classpath with resource {}", resourceName);

            InputStream stream = StandaloneHelper.class.getResourceAsStream(resourceName);

            if (stream == null) {
                throw new IOException("Resource '" + resourceName + "' not found on classpath");
            }

            return stream;
        } else {
            URL url = uri.toURL();

            log.debug("Opening keystore with url {}", url);

            return url.openStream();
        }
    }

    static public KeyStore loadKeyStore(URI uri, char[] password) throws Exception {
        try (InputStream stream = openKeyStoreInput(uri)) {
            KeyStore ks = KeyStore.getInstance("JKS");
            ks.load(stream, password);
            return ks;
        }
    }

    static public SSLContext createSSLContext(URI keystoreUri, char[] keystorePassword, URI truststoreUri,
            char[] truststorePassword) throws Exception {

        // load keystore
        KeyStore keystore = loadKeyStore(keystoreUri, keystorePassword);
        KeyManager[] keyManagers;
        KeyManagerFactory keyManagerFactory = KeyManagerFactory
                .getInstance(KeyManagerFactory.getDefaultAlgorithm());
        keyManagerFactory.init(keystore, keystorePassword);
        keyManagers = keyManagerFactory.getKeyManagers();

        // load truststore
        KeyStore truststore = loadKeyStore(truststoreUri, truststorePassword);
        TrustManager[] trustManagers;
        TrustManagerFactory trustManagerFactory = TrustManagerFactory
                .getInstance(TrustManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init(truststore);
        trustManagers = trustManagerFactory.getTrustManagers();

        SSLContext sslContext;
        sslContext = SSLContext.getInstance("TLS");
        sslContext.init(keyManagers, trustManagers, null);

        return sslContext;
    }

}