001/**
002 * Copyright (C) 2006-2021 Talend Inc. - www.talend.com
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.talend.sdk.component.junit.http.api;
017
018import java.security.cert.CertificateException;
019import java.util.concurrent.Executor;
020import java.util.concurrent.Executors;
021import java.util.function.Predicate;
022
023import javax.net.ssl.SSLContext;
024import javax.net.ssl.SSLException;
025
026import org.talend.sdk.component.junit.http.internal.impl.DefaultHeaderFilter;
027import org.talend.sdk.component.junit.http.internal.impl.DefaultResponseLocator;
028
029import io.netty.handler.ssl.IdentityCipherSuiteFilter;
030import io.netty.handler.ssl.JdkSslContext;
031import io.netty.handler.ssl.SslContext;
032import io.netty.handler.ssl.SslProvider;
033import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
034import io.netty.handler.ssl.util.SelfSignedCertificate;
035
036import lombok.Getter;
037import lombok.extern.slf4j.Slf4j;
038
039/**
040 * Handler used to customize the behavior of the mock server during the test.
041 *
042 * @param <T> fluent API type.
043 */
044@Getter
045@Slf4j
046public class HttpApiHandler<T extends HttpApiHandler<?>> {
047
048    private Executor executor = Executors.newCachedThreadPool();
049
050    private boolean globalProxyConfiguration = true;
051
052    private int port;
053
054    private SSLContext sslContext;
055
056    private ResponseLocator responseLocator = new DefaultResponseLocator(DefaultResponseLocator.PREFIX, null);
057
058    private String logLevel = "DEBUG";
059
060    private Predicate<String> headerFilter = new DefaultHeaderFilter();
061
062    private boolean skipProxyHeaders;
063
064    public T activeSsl() {
065        if (sslContext == null) {
066            try {
067                final SelfSignedCertificate certificate = new SelfSignedCertificate();
068                final SslContext nettyContext = SslContext
069                        .newServerContext(SslProvider.JDK, null, InsecureTrustManagerFactory.INSTANCE,
070                                certificate.certificate(), certificate.privateKey(), null, null, null,
071                                IdentityCipherSuiteFilter.INSTANCE, null, 0, 0);
072                sslContext = JdkSslContext.class.cast(nettyContext).context();
073            } catch (final SSLException | CertificateException e) {
074                throw new IllegalStateException(e);
075            }
076        }
077        return (T) this;
078    }
079
080    public T setHeaderFilter(final Predicate<String> headerFilter) {
081        this.headerFilter = headerFilter;
082        return (T) this;
083    }
084
085    public T setExecutor(final Executor executor) {
086        this.executor = executor;
087        return (T) this;
088    }
089
090    public T setGlobalProxyConfiguration(final boolean globalProxyConfiguration) {
091        this.globalProxyConfiguration = globalProxyConfiguration;
092        return (T) this;
093    }
094
095    public T setPort(final int port) {
096        this.port = port;
097        return (T) this;
098    }
099
100    public T setSslContext(final SSLContext sslContext) {
101        this.sslContext = sslContext;
102        return (T) this;
103    }
104
105    public T setResponseLocator(final ResponseLocator responseLocator) {
106        this.responseLocator = responseLocator;
107        return (T) this;
108    }
109
110    public T setLogLevel(final String logLevel) {
111        this.logLevel = logLevel;
112        return (T) this;
113    }
114
115    public T setSkipProxyHeaders(final boolean skipProxyHeaders) {
116        this.skipProxyHeaders = skipProxyHeaders;
117        return (T) this;
118    }
119}