Search in sources :

Example 1 with JettyKeystoreException

use of org.teknux.jettybootstrap.keystore.JettyKeystoreException in project jetty-bootstrap by teknux-org.

the class JettyBootstrap method getInitializedConfiguration.

/**
 * Parse the {@link IJettyConfiguration}, validate the configuration and initialize it if necessary. Clean temp directory if necessary and generates SSL keystore when
 * necessary.
 *
 * @return IJettyConfiguration Jetty Configuration
 * @throws JettyBootstrapException
 *             on failure
 */
protected IJettyConfiguration getInitializedConfiguration() throws JettyBootstrapException {
    if (!isInitializedConfiguration) {
        LOG.debug("Init Configuration...");
        LOG.trace("Check Temp Directory...");
        if (iJettyConfiguration.getTempDirectory() == null) {
            iJettyConfiguration.setTempDirectory(TEMP_DIRECTORY_DEFAULT);
        }
        if (iJettyConfiguration.getTempDirectory().exists() && iJettyConfiguration.isCleanTempDir()) {
            LOG.trace("Clean Temp Directory...");
            try {
                FileUtils.deleteDirectory(iJettyConfiguration.getTempDirectory());
            } catch (IOException e) {
                throw new JettyBootstrapException("Can't clean temporary directory");
            }
        }
        if (!iJettyConfiguration.getTempDirectory().exists() && !iJettyConfiguration.getTempDirectory().mkdirs()) {
            throw new JettyBootstrapException("Can't create temporary directory");
        }
        LOG.trace("Check required properties...");
        if (iJettyConfiguration.getHost() == null || iJettyConfiguration.getHost().isEmpty()) {
            throw new JettyBootstrapException("Host not specified");
        }
        LOG.trace("Check connectors...");
        if (iJettyConfiguration.hasJettyConnector(JettyConnector.HTTPS)) {
            // Checks keystore path only if keyStore object and SSL private key or SSL certificate are not specified
            if (iJettyConfiguration.getSslKeyStore() == null && (iJettyConfiguration.getSslPrivateKeyPath() == null || iJettyConfiguration.getSslPrivateKeyPath().isEmpty() || iJettyConfiguration.getSslCertificatePath() == null || iJettyConfiguration.getSslCertificatePath().isEmpty())) {
                // If keystore path is not specified, use default keystore path
                if (iJettyConfiguration.getSslKeyStorePath() == null || iJettyConfiguration.getSslKeyStorePath().isEmpty()) {
                    iJettyConfiguration.setSslKeyStorePath(iJettyConfiguration.getTempDirectory().getPath() + File.separator + DEFAULT_KEYSTORE_FILENAME);
                }
                // Create keystore file if not exits
                File keystoreFile = new File(iJettyConfiguration.getSslKeyStorePath());
                if (!keystoreFile.exists()) {
                    try {
                        JettyKeystoreGeneratorBuilder jettyKeystoreGeneratorBuilder = new JettyKeystoreGeneratorBuilder();
                        jettyKeystoreGeneratorBuilder.setAlgorithm(iJettyConfiguration.getSslKeyStoreAlgorithm());
                        jettyKeystoreGeneratorBuilder.setSignatureAlgorithm(iJettyConfiguration.getSslKeyStoreSignatureAlgorithm());
                        jettyKeystoreGeneratorBuilder.setRdnOuValue(iJettyConfiguration.getSslKeyStoreRdnOuValue());
                        jettyKeystoreGeneratorBuilder.setRdnOValue(iJettyConfiguration.getSslKeyStoreRdnOValue());
                        jettyKeystoreGeneratorBuilder.setDateNotBeforeNumberOfDays(iJettyConfiguration.getSslKeyStoreDateNotBeforeNumberOfDays());
                        jettyKeystoreGeneratorBuilder.setDateNotAfterNumberOfDays(iJettyConfiguration.getSslKeyStoreDateNotAfterNumberOfDays());
                        KeyStore keyStore = jettyKeystoreGeneratorBuilder.build(iJettyConfiguration.getSslKeyStoreDomainName(), iJettyConfiguration.getSslKeyStoreAlias(), iJettyConfiguration.getSslKeyStorePassword());
                        JettyKeystoreGeneratorBuilder.saveKeyStore(keyStore, keystoreFile, iJettyConfiguration.getSslKeyStorePassword());
                    } catch (JettyKeystoreException e) {
                        throw new JettyBootstrapException("Can't generate keyStore", e);
                    }
                }
            }
        }
        if (iJettyConfiguration.isRedirectWebAppsOnHttpsConnector() && (!iJettyConfiguration.hasJettyConnector(JettyConnector.HTTP) || !iJettyConfiguration.hasJettyConnector(JettyConnector.HTTPS))) {
            throw new JettyBootstrapException("You can't redirect all from HTTP to HTTPS Connector if both connectors are not setted");
        }
        isInitializedConfiguration = true;
        LOG.trace("Configuration : {}", iJettyConfiguration);
    }
    return iJettyConfiguration;
}
Also used : JettyKeystoreGeneratorBuilder(org.teknux.jettybootstrap.keystore.JettyKeystoreGeneratorBuilder) JettyKeystoreException(org.teknux.jettybootstrap.keystore.JettyKeystoreException) IOException(java.io.IOException) File(java.io.File) KeyStore(java.security.KeyStore)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 KeyStore (java.security.KeyStore)1 JettyKeystoreException (org.teknux.jettybootstrap.keystore.JettyKeystoreException)1 JettyKeystoreGeneratorBuilder (org.teknux.jettybootstrap.keystore.JettyKeystoreGeneratorBuilder)1