001/**
002 * Copyright (C) 2006-2018 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.JdkSslContext;
030import io.netty.handler.ssl.SslContext;
031import io.netty.handler.ssl.SslContextBuilder;
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 = SslContextBuilder
069                        .forServer(certificate.certificate(), certificate.privateKey())
070                        .trustManager(InsecureTrustManagerFactory.INSTANCE)
071                        .sslProvider(SslProvider.JDK)
072                        .build();
073                sslContext = JdkSslContext.class.cast(nettyContext).context();
074            } catch (final SSLException | CertificateException e) {
075                throw new IllegalStateException(e);
076            }
077        }
078        return (T) this;
079    }
080
081    public T setHeaderFilter(final Predicate<String> headerFilter) {
082        this.headerFilter = headerFilter;
083        return (T) this;
084    }
085
086    public T setExecutor(final Executor executor) {
087        this.executor = executor;
088        return (T) this;
089    }
090
091    public T setGlobalProxyConfiguration(final boolean globalProxyConfiguration) {
092        this.globalProxyConfiguration = globalProxyConfiguration;
093        return (T) this;
094    }
095
096    public T setPort(final int port) {
097        this.port = port;
098        return (T) this;
099    }
100
101    public T setSslContext(final SSLContext sslContext) {
102        this.sslContext = sslContext;
103        return (T) this;
104    }
105
106    public T setResponseLocator(final ResponseLocator responseLocator) {
107        this.responseLocator = responseLocator;
108        return (T) this;
109    }
110
111    public T setLogLevel(final String logLevel) {
112        this.logLevel = logLevel;
113        return (T) this;
114    }
115
116    public T setSkipProxyHeaders(final boolean skipProxyHeaders) {
117        this.skipProxyHeaders = skipProxyHeaders;
118        return (T) this;
119    }
120}