2 * Copyright (C) 2018 Bell Canada.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.onap.ccsdk.sli.adaptors.netbox.impl;
18 import static org.apache.http.HttpHeaders.ACCEPT;
19 import static org.apache.http.HttpHeaders.AUTHORIZATION;
20 import static org.apache.http.HttpHeaders.CONTENT_TYPE;
22 import java.io.IOException;
23 import java.nio.charset.Charset;
24 import java.security.KeyManagementException;
25 import java.security.KeyStoreException;
26 import java.security.NoSuchAlgorithmException;
27 import javax.net.ssl.SSLContext;
28 import org.apache.http.HttpResponse;
29 import org.apache.http.client.methods.HttpDelete;
30 import org.apache.http.client.methods.HttpPost;
31 import org.apache.http.client.methods.HttpRequestBase;
32 import org.apache.http.conn.ssl.NoopHostnameVerifier;
33 import org.apache.http.entity.StringEntity;
34 import org.apache.http.impl.client.CloseableHttpClient;
35 import org.apache.http.impl.client.HttpClientBuilder;
36 import org.apache.http.ssl.SSLContexts;
37 import org.apache.http.ssl.TrustStrategy;
38 import org.onap.ccsdk.sli.adaptors.netbox.property.NetboxProperties;
40 public class NetboxHttpClient implements AutoCloseable {
42 private static final String APPLICATION_JSON = "application/json";
44 private final CloseableHttpClient client;
45 private final String url;
46 private final String token;
48 // Used by the blueprint container
49 public NetboxHttpClient(NetboxProperties properties) {
50 this(properties.getHost(), properties.getApiKey());
53 NetboxHttpClient(final String url, final String token) {
57 final TrustStrategy acceptingTrustStrategy = (certificate, authType) -> true;
58 final SSLContext sslContext;
60 sslContext = SSLContexts.custom()
61 .loadTrustMaterial(null, acceptingTrustStrategy).build();
62 } catch (final NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) {
63 throw new IllegalStateException("Can't create http client", e);
65 client = HttpClientBuilder.create()
66 .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
67 .setSSLContext(sslContext)
72 public void close() throws IOException {
76 HttpResponse post(final String uri, final String requestBody) throws IOException {
77 final HttpPost request = new HttpPost(url + uri);
79 request.setEntity(new StringEntity(requestBody, Charset.forName("UTF-8")));
80 return client.execute(request);
83 HttpResponse delete(final String uri) throws IOException {
84 final HttpDelete request = new HttpDelete(url + uri);
86 return client.execute(request);
89 private void setHeaders(final HttpRequestBase request) {
90 request.addHeader(ACCEPT, APPLICATION_JSON);
91 request.addHeader(CONTENT_TYPE, APPLICATION_JSON);
92 request.addHeader(AUTHORIZATION, "Token " + token);