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.NetboxPropertiesLighty;
41 * THIS CLASS IS A COPY OF {@link NetboxHttpClient} WITH REMOVED OSGi DEPENDENCIES
43 public class NetboxHttpClientLighty implements AutoCloseable {
45 private static final String APPLICATION_JSON = "application/json";
47 private final CloseableHttpClient client;
48 private final String url;
49 private final String token;
51 // Used by the blueprint container
52 public NetboxHttpClientLighty(NetboxPropertiesLighty properties) {
53 this(properties.getHost(), properties.getApiKey());
56 NetboxHttpClientLighty(final String url, final String token) {
60 final TrustStrategy acceptingTrustStrategy = (certificate, authType) -> true;
61 final SSLContext sslContext;
63 sslContext = SSLContexts.custom()
64 .loadTrustMaterial(null, acceptingTrustStrategy).build();
65 } catch (final NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) {
66 throw new IllegalStateException("Can't create http client", e);
68 client = HttpClientBuilder.create()
69 .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
70 .setSSLContext(sslContext)
75 public void close() throws IOException {
79 HttpResponse post(final String uri, final String requestBody) throws IOException {
80 final HttpPost request = new HttpPost(url + uri);
82 request.setEntity(new StringEntity(requestBody, Charset.forName("UTF-8")));
83 return client.execute(request);
86 HttpResponse delete(final String uri) throws IOException {
87 final HttpDelete request = new HttpDelete(url + uri);
89 return client.execute(request);
92 private void setHeaders(final HttpRequestBase request) {
93 request.addHeader(ACCEPT, APPLICATION_JSON);
94 request.addHeader(CONTENT_TYPE, APPLICATION_JSON);
95 request.addHeader(AUTHORIZATION, "Token " + token);