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.openecomp.sdnc.dmaapclient;
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;
31 import java.net.ProtocolException;
34 import javax.net.ssl.HostnameVerifier;
35 import javax.net.ssl.HttpsURLConnection;
36 import javax.net.ssl.SSLSession;
38 import org.apache.commons.codec.binary.Base64;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 public class SdncOdlConnection {
45 private static final Logger LOG = LoggerFactory
46 .getLogger(SdncOdlConnection.class);
48 private HttpURLConnection httpConn = null;
50 private String url = null;
51 private String user = null;
52 private String password = null;
54 private class SdncAuthenticator extends Authenticator {
57 private String passwd;
59 SdncAuthenticator(String user, String passwd) {
64 protected PasswordAuthentication getPasswordAuthentication() {
65 return new PasswordAuthentication(user, passwd.toCharArray());
70 private SdncOdlConnection() {
74 private SdncOdlConnection(String url, String user, String password) {
77 this.password = password;
80 URL sdncUrl = new URL(url);
81 Authenticator.setDefault(new SdncAuthenticator(user, password));
83 this.httpConn = (HttpURLConnection) sdncUrl.openConnection();
84 } catch (Exception e) {
85 LOG.error("Unable to create http connection", e);
89 public static SdncOdlConnection newInstance(String url, String user, String password) throws IOException
91 return (new SdncOdlConnection(url, user, password));
96 public String send(String method, String contentType, String msg) throws IOException {
98 LOG.info("Sending REST " + method + " to " + url);
99 LOG.info("Message body:\n" + msg);
100 String authStr = user + ":" + password;
101 String encodedAuthStr = new String(Base64.encodeBase64(authStr.getBytes()));
103 httpConn.addRequestProperty("Authentication", "Basic " + encodedAuthStr);
105 httpConn.setRequestMethod(method);
106 httpConn.setRequestProperty("Content-Type", contentType);
107 httpConn.setRequestProperty("Accept", contentType);
109 httpConn.setDoInput(true);
110 httpConn.setDoOutput(true);
111 httpConn.setUseCaches(false);
113 if (httpConn instanceof HttpsURLConnection) {
114 HostnameVerifier hostnameVerifier = new HostnameVerifier() {
116 public boolean verify(String hostname, SSLSession session) {
120 ((HttpsURLConnection) httpConn).setHostnameVerifier(hostnameVerifier);
124 httpConn.setRequestProperty("Content-Length", "" + msg.length());
125 DataOutputStream outStr = new DataOutputStream(httpConn.getOutputStream());
126 outStr.write(msg.getBytes());
130 BufferedReader respRdr;
132 LOG.info("Response: " + httpConn.getResponseCode() + " " + httpConn.getResponseMessage());
134 if (httpConn.getResponseCode() < 300) {
136 respRdr = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
138 respRdr = new BufferedReader(new InputStreamReader(httpConn.getErrorStream()));
141 StringBuffer respBuff = new StringBuffer();
145 while ((respLn = respRdr.readLine()) != null) {
146 respBuff.append(respLn + "\n");
150 String respString = respBuff.toString();
152 LOG.info("Response body :\n" + respString);