388486c4f914a1802403e56b58326a398a0734e8
[ccsdk/sli/adaptors.git] /
1 /*
2  * Copyright (C) 2018 Bell Canada.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.onap.ccsdk.sli.adaptors.netbox.impl;
17
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;
21
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;
39
40 /**
41  * THIS CLASS IS A COPY OF {@link NetboxHttpClient} WITH REMOVED OSGi DEPENDENCIES
42  */
43 public class NetboxHttpClientLighty implements AutoCloseable {
44
45     private static final String APPLICATION_JSON = "application/json";
46
47     private final CloseableHttpClient client;
48     private final String url;
49     private final String token;
50
51     // Used by the blueprint container
52     public NetboxHttpClientLighty(NetboxPropertiesLighty properties) {
53         this(properties.getHost(), properties.getApiKey());
54     }
55
56     NetboxHttpClientLighty(final String url, final String token) {
57         this.url = url;
58         this.token = token;
59
60         final TrustStrategy acceptingTrustStrategy = (certificate, authType) -> true;
61         final SSLContext sslContext;
62         try {
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);
67         }
68         client = HttpClientBuilder.create()
69             .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
70             .setSSLContext(sslContext)
71             .build();
72     }
73
74     @Override
75     public void close() throws IOException {
76         client.close();
77     }
78
79     HttpResponse post(final String uri, final String requestBody) throws IOException {
80         final HttpPost request = new HttpPost(url + uri);
81         setHeaders(request);
82         request.setEntity(new StringEntity(requestBody, Charset.forName("UTF-8")));
83         return client.execute(request);
84     }
85
86     HttpResponse delete(final String uri) throws IOException {
87         final HttpDelete request = new HttpDelete(url + uri);
88         setHeaders(request);
89         return client.execute(request);
90     }
91
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);
96     }
97 }