2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T 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=========================================================
22 package org.onap.ccsdk.sli.northbound.uebclient;
24 import java.io.BufferedReader;
25 import java.io.DataOutputStream;
26 import java.io.IOException;
27 import java.io.InputStreamReader;
28 import java.net.Authenticator;
29 import java.net.HttpURLConnection;
30 import java.net.PasswordAuthentication;
33 import javax.net.ssl.HostnameVerifier;
34 import javax.net.ssl.HttpsURLConnection;
35 import javax.net.ssl.SSLSession;
37 import org.apache.commons.codec.binary.Base64;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 public class SdncOdlConnection {
44 private static final Logger LOG = LoggerFactory
45 .getLogger(SdncOdlConnection.class);
47 private HttpURLConnection httpConn = null;
49 private String url = null;
50 private String user = null;
51 private String password = null;
53 private class SdncAuthenticator extends Authenticator {
56 private String passwd;
58 SdncAuthenticator(String user, String passwd) {
63 protected PasswordAuthentication getPasswordAuthentication() {
64 return new PasswordAuthentication(user, passwd.toCharArray());
69 private SdncOdlConnection() {
73 private SdncOdlConnection(String url, String user, String password) {
76 this.password = password;
79 URL sdncUrl = new URL(url);
80 Authenticator.setDefault(new SdncAuthenticator(user, password));
82 this.httpConn = (HttpURLConnection) sdncUrl.openConnection();
83 } catch (Exception e) {
84 LOG.error("Unable to create http connection", e);
88 public static SdncOdlConnection newInstance(String url, String user, String password) throws IOException
90 return new SdncOdlConnection(url, user, password);
95 public String send(String method, String contentType, String msg) throws IOException {
97 LOG.info("Sending REST " + method + " to " + url);
98 LOG.info("Message body:\n" + msg);
99 String authStr = user + ":" + password;
100 String encodedAuthStr = new String(Base64.encodeBase64(authStr.getBytes()));
102 httpConn.addRequestProperty("Authentication", "Basic " + encodedAuthStr);
104 httpConn.setRequestMethod(method);
105 httpConn.setRequestProperty("Content-Type", contentType);
106 httpConn.setRequestProperty("Accept", contentType);
108 httpConn.setDoInput(true);
109 httpConn.setDoOutput(true);
110 httpConn.setUseCaches(false);
112 if (httpConn instanceof HttpsURLConnection) {
113 HostnameVerifier hostnameVerifier = new HostnameVerifier() {
115 public boolean verify(String hostname, SSLSession session) {
119 ((HttpsURLConnection) httpConn).setHostnameVerifier(hostnameVerifier);
123 httpConn.setRequestProperty("Content-Length", "" + msg.length());
124 DataOutputStream outStr = new DataOutputStream(httpConn.getOutputStream());
125 outStr.write(msg.getBytes());
129 BufferedReader respRdr;
131 LOG.info("Response: " + httpConn.getResponseCode() + " " + httpConn.getResponseMessage());
133 if (httpConn.getResponseCode() < 300) {
135 respRdr = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
137 respRdr = new BufferedReader(new InputStreamReader(httpConn.getErrorStream()));
140 StringBuilder respBuff = new StringBuilder();
144 while ((respLn = respRdr.readLine()) != null) {
145 respBuff.append(respLn + "\n");
149 String respString = respBuff.toString();
151 LOG.info("Response body :\n" + respString);