2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2020 Fujitsu Limited Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 package org.onap.sdnc.northbound.asyncrequests;
23 import java.io.BufferedReader;
24 import java.io.DataOutputStream;
25 import java.io.IOException;
26 import java.io.InputStreamReader;
27 import java.net.Authenticator;
28 import java.net.HttpURLConnection;
29 import java.net.PasswordAuthentication;
31 import javax.net.ssl.HostnameVerifier;
32 import javax.net.ssl.HttpsURLConnection;
33 import javax.net.ssl.SSLSession;
34 import org.apache.commons.codec.binary.Base64;
35 import org.onap.ccsdk.sli.core.utils.common.AcceptIpAddressHostNameVerifier;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 public class SdncOdlConnection {
42 private static final Logger LOG = LoggerFactory
43 .getLogger(SdncOdlConnection.class);
45 private HttpURLConnection httpConn = null;
47 private String url = null;
48 private String user = null;
49 private String password = null;
51 private class SdncAuthenticator extends Authenticator {
54 private String passwd;
56 SdncAuthenticator(String user, String passwd) {
62 protected PasswordAuthentication getPasswordAuthentication() {
63 return new PasswordAuthentication(user, passwd.toCharArray());
67 private SdncOdlConnection() {
71 private SdncOdlConnection(String url, String user, String password) {
74 this.password = password;
77 URL sdncUrl = new URL(url);
78 Authenticator.setDefault(new SdncAuthenticator(user, password));
80 this.httpConn = (HttpURLConnection) sdncUrl.openConnection();
81 } catch (Exception e) {
82 LOG.error("Unable to create http connection", e);
86 public static SdncOdlConnection newInstance(String url, String user, String password) {
87 return new SdncOdlConnection(url, user, password);
91 public String send(String method, String contentType, String msg) throws IOException {
93 LOG.info(String.format("Sending REST %s to %s", method, url));
94 LOG.info(String.format("Message body:%n%s", msg));
95 String authStr = user + ":" + password;
96 String encodedAuthStr = new String(Base64.encodeBase64(authStr.getBytes()));
98 httpConn.addRequestProperty("Authentication", "Basic " + encodedAuthStr);
100 httpConn.setRequestMethod(method);
101 httpConn.setRequestProperty("Content-Type", contentType);
102 httpConn.setRequestProperty("Accept", contentType);
104 httpConn.setDoInput(true);
105 httpConn.setDoOutput(true);
106 httpConn.setUseCaches(false);
108 if (httpConn instanceof HttpsURLConnection) {
109 HostnameVerifier hostnameVerifier = new AcceptIpAddressHostNameVerifier();
110 ((HttpsURLConnection) httpConn).setHostnameVerifier(hostnameVerifier);
114 httpConn.setRequestProperty("Content-Length", Integer.toString(msg.length()));
115 DataOutputStream outStr = new DataOutputStream(httpConn.getOutputStream());
116 outStr.write(msg.getBytes());
120 BufferedReader respRdr;
122 LOG.info("Response: " + httpConn.getResponseCode() + " " + httpConn.getResponseMessage());
124 if (httpConn.getResponseCode() < 300) {
126 respRdr = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
128 respRdr = new BufferedReader(new InputStreamReader(httpConn.getErrorStream()));
131 StringBuilder respBuff = new StringBuilder();
135 while ((respLn = respRdr.readLine()) != null) {
136 respBuff.append(respLn).append("\n");
140 String respString = respBuff.toString();
142 LOG.info(String.format("Response body :%n%s", respString));