Do not use ClientConfig
[aaa.git] / aaa-shiro / impl / src / main / java / org / opendaylight / aaa / shiro / realm / util / http / SimpleHttpClient.java
1 /*
2  * Copyright (c) 2017 Ericsson Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.aaa.shiro.realm.util.http;
9
10 import java.util.HashSet;
11 import java.util.Set;
12 import javax.net.ssl.HostnameVerifier;
13 import javax.net.ssl.SSLContext;
14 import javax.ws.rs.client.Client;
15 import javax.ws.rs.client.ClientBuilder;
16
17 /**
18  * An utility that represents an HTTP client that allows to make
19  * HTTP requests.
20  */
21 //Suppressed so UT's can mock it using Mockito.
22 @SuppressWarnings("checkstyle:FinalClass")
23 public class SimpleHttpClient {
24
25     private final Client client;
26
27     private SimpleHttpClient(final Client client) {
28         this.client = client;
29     }
30
31     /**
32      * Obtain a builder for {@code SimpleHttpClient}.
33      *
34      * @return the client builder.
35      */
36     public static Builder clientBuilder() {
37         return new Builder();
38     }
39
40     /**
41      * Obtain a builder for {@link SimpleHttpRequest}.
42      *
43      * @param outputType the return type of the request.
44      * @param <T> the return type of the request.
45      * @return the request builder.
46      */
47     public <T> SimpleHttpRequest.Builder<T> requestBuilder(Class<T> outputType) {
48         return new SimpleHttpRequest.Builder<>(client, outputType);
49     }
50
51     public static class Builder {
52
53         private SSLContext sslContext;
54         private HostnameVerifier hostnameVerifier;
55         private final Set<Class<?>> providers = new HashSet<>();
56
57         private Builder() {
58
59         }
60
61         /**
62          * Sets the SSLContext to be used for SSL requests.
63          *
64          * @param context the SSLContext.
65          * @return self, the client builder.
66          */
67         public Builder sslContext(final SSLContext context) {
68             this.sslContext = context;
69             return this;
70         }
71
72         /**
73          * Sets the hostname verifier the request is made with.
74          *
75          * @param verifier the hostname verifier.
76          * @return self, the client builder.
77          */
78         public Builder hostnameVerifier(final HostnameVerifier verifier) {
79             this.hostnameVerifier = verifier;
80             return this;
81         }
82
83         /**
84          * Sets a JAX-RS provider to use for this request. Can be called
85          * multiple times to add multiple providers.
86          *
87          * @param provider the provider.
88          * @return self, the client builder.
89          */
90         public Builder provider(final Class<?> provider) {
91             providers.add(provider);
92             return this;
93         }
94
95         /**
96          * Build the client.
97          *
98          * @return the client.
99          */
100         public SimpleHttpClient build() {
101             final ClientBuilder clientBuilder = ClientBuilder.newBuilder()
102                 .sslContext(sslContext)
103                 .hostnameVerifier(hostnameVerifier);
104
105             providers.forEach(clientBuilder::register);
106
107             return new SimpleHttpClient(clientBuilder.build());
108         }
109
110     }
111 }