All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.zmops.zeus.server.ssl.HttpDynamicSslContext Maven / Gradle / Ivy

There is a newer version: 1.0.3-RELEASE
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 com.zmops.zeus.server.ssl;

import io.netty.handler.ssl.SslContextBuilder;

import javax.net.ssl.SSLException;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Paths;
import java.security.GeneralSecurityException;

public class HttpDynamicSslContext extends AbstractSslContext {

    public static HttpDynamicSslContext forServer(String privateKeyFile, String certChainFile) {
        return new HttpDynamicSslContext(privateKeyFile, certChainFile);
    }

    public static HttpDynamicSslContext forClient(String caFile) {
        return new HttpDynamicSslContext(caFile);
    }

    protected HttpDynamicSslContext(String privateKeyFile, String certChainFile) {
        super(privateKeyFile, certChainFile);
    }

    protected HttpDynamicSslContext(String caFile) {
        super(caFile);
    }

    @Override
    protected void updateContext(String caFile) {
        try {
            setCtx(SslContextBuilder.forClient().trustManager(Paths.get(caFile).toFile()).build());
        } catch (SSLException e) {
            throw new IllegalArgumentException(e);
        }
    }

    @Override
    protected void updateContext(final String privateKeyFile, final String certChainFile) {
        try {
            setCtx(SslContextBuilder
                    .forServer(
                            new FileInputStream(Paths.get(certChainFile).toFile()),
                            PrivateKeyUtil.loadDecryptionKey(privateKeyFile)).build());
        } catch (GeneralSecurityException | IOException e) {
            throw new IllegalArgumentException(e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy