2 * Copyright (c) 2017 Ericsson Inc. and others. All rights reserved.
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
8 package org.opendaylight.aaa.shiro.realm.util.http;
10 import java.util.HashSet;
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;
18 * An utility that represents an HTTP client that allows to make
21 //Suppressed so UT's can mock it using Mockito.
22 @SuppressWarnings("checkstyle:FinalClass")
23 public class SimpleHttpClient {
25 private final Client client;
27 private SimpleHttpClient(final Client client) {
32 * Obtain a builder for {@code SimpleHttpClient}.
34 * @return the client builder.
36 public static Builder clientBuilder() {
41 * Obtain a builder for {@link SimpleHttpRequest}.
43 * @param outputType the return type of the request.
44 * @param <T> the return type of the request.
45 * @return the request builder.
47 public <T> SimpleHttpRequest.Builder<T> requestBuilder(Class<T> outputType) {
48 return new SimpleHttpRequest.Builder<>(client, outputType);
51 public static class Builder {
53 private SSLContext sslContext;
54 private HostnameVerifier hostnameVerifier;
55 private final Set<Class<?>> providers = new HashSet<>();
62 * Sets the SSLContext to be used for SSL requests.
64 * @param context the SSLContext.
65 * @return self, the client builder.
67 public Builder sslContext(final SSLContext context) {
68 this.sslContext = context;
73 * Sets the hostname verifier the request is made with.
75 * @param verifier the hostname verifier.
76 * @return self, the client builder.
78 public Builder hostnameVerifier(final HostnameVerifier verifier) {
79 this.hostnameVerifier = verifier;
84 * Sets a JAX-RS provider to use for this request. Can be called
85 * multiple times to add multiple providers.
87 * @param provider the provider.
88 * @return self, the client builder.
90 public Builder provider(final Class<?> provider) {
91 providers.add(provider);
100 public SimpleHttpClient build() {
101 final ClientBuilder clientBuilder = ClientBuilder.newBuilder()
102 .sslContext(sslContext)
103 .hostnameVerifier(hostnameVerifier);
105 providers.forEach(clientBuilder::register);
107 return new SimpleHttpClient(clientBuilder.build());