1e267e6267821b6da4b091dbec35f5d2f53192e5
[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 import org.glassfish.jersey.client.ClientConfig;
17
18 /**
19  * An utility that represents an HTTP client that allows to make
20  * HTTP requests.
21  */
22 //Suppressed so UT's can mock it using Mockito.
23 @SuppressWarnings("checkstyle:FinalClass")
24 public class SimpleHttpClient {
25
26     private final Client client;
27
28     private SimpleHttpClient(final Client client) {
29         this.client = client;
30     }
31
32     /**
33      * Obtain a builder for {@code SimpleHttpClient}.
34      *
35      * @return the client builder.
36      */
37     public static Builder clientBuilder() {
38         return new Builder();
39     }
40
41     /**
42      * Obtain a builder for {@link SimpleHttpRequest}.
43      *
44      * @param outputType the return type of the request.
45      * @param <T> the return type of the request.
46      * @return the request builder.
47      */
48     public <T> SimpleHttpRequest.Builder<T> requestBuilder(Class<T> outputType) {
49         return new SimpleHttpRequest.Builder<>(client, outputType);
50     }
51
52     public static class Builder {
53
54         private SSLContext sslContext;
55         private HostnameVerifier hostnameVerifier;
56         private final Set<Class<?>> providers = new HashSet<>();
57
58         private Builder() {
59
60         }
61
62         /**
63          * Sets the SSLContext to be used for SSL requests.
64          *
65          * @param context the SSLContext.
66          * @return self, the client builder.
67          */
68         public Builder sslContext(final SSLContext context) {
69             this.sslContext = context;
70             return this;
71         }
72
73         /**
74          * Sets the hostname verifier the request is made with.
75          *
76          * @param verifier the hostname verifier.
77          * @return self, the client builder.
78          */
79         public Builder hostnameVerifier(final HostnameVerifier verifier) {
80             this.hostnameVerifier = verifier;
81             return this;
82         }
83
84         /**
85          * Sets a JAX-RS provider to use for this request. Can be called
86          * multiple times to add multiple providers.
87          *
88          * @param provider the provider.
89          * @return self, the client builder.
90          */
91         public Builder provider(final Class<?> provider) {
92             providers.add(provider);
93             return this;
94         }
95
96         /**
97          * Build the client.
98          *
99          * @return the client.
100          */
101         public SimpleHttpClient build() {
102             final ClientConfig clientConfig = new ClientConfig();
103             providers.forEach(clientConfig::register);
104             Client client = ClientBuilder.newBuilder().sslContext(sslContext).hostnameVerifier(hostnameVerifier)
105                     .withConfig(clientConfig).build();
106             return new SimpleHttpClient(client);
107         }
108
109     }
110 }