[SDNC-5] Split listeners
[sdnc/northbound.git] / dmaap-listener / src / main / java / org / openecomp / sdnc / dmaapclient / SdncOdlConnection.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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=========================================================
20  */
21
22 package org.openecomp.sdnc.dmaapclient;
23
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;
32 import java.net.URL;
33
34 import javax.net.ssl.HostnameVerifier;
35 import javax.net.ssl.HttpsURLConnection;
36 import javax.net.ssl.SSLSession;
37
38 import org.apache.commons.codec.binary.Base64;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42
43 public class SdncOdlConnection {
44         
45         private static final Logger LOG = LoggerFactory
46                         .getLogger(SdncOdlConnection.class);
47         
48         private HttpURLConnection httpConn = null;
49         
50         private String url = null;
51         private String user = null;
52         private String password = null;
53         
54         private class SdncAuthenticator extends Authenticator {
55                 
56                 private String user;
57                 private String passwd;
58
59                 SdncAuthenticator(String user, String passwd) {
60                         this.user = user;
61                         this.passwd = passwd;
62                 }
63                 @Override
64                 protected PasswordAuthentication getPasswordAuthentication() {
65                         return new PasswordAuthentication(user, passwd.toCharArray());
66                 }
67                 
68         }
69         
70         private SdncOdlConnection() {
71                 
72         }
73         
74         private SdncOdlConnection(String url, String user, String password) {
75                 this.url = url;
76                 this.user = user;
77                 this.password = password;
78                 
79                 try {
80                         URL sdncUrl = new URL(url);
81                         Authenticator.setDefault(new SdncAuthenticator(user, password));
82                 
83                         this.httpConn = (HttpURLConnection) sdncUrl.openConnection();
84                 } catch (Exception e) {
85                         LOG.error("Unable to create http connection", e);
86                 }
87         }
88         
89         public static  SdncOdlConnection newInstance(String url, String user, String password) throws IOException
90         {
91                 return (new SdncOdlConnection(url, user, password));
92         }
93                 
94
95         
96         public String send(String method, String contentType, String msg) throws IOException {
97
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()));
102
103                 httpConn.addRequestProperty("Authentication", "Basic " + encodedAuthStr);
104
105                 httpConn.setRequestMethod(method);
106                 httpConn.setRequestProperty("Content-Type", contentType);
107                 httpConn.setRequestProperty("Accept", contentType);
108
109                 httpConn.setDoInput(true);
110                 httpConn.setDoOutput(true);
111                 httpConn.setUseCaches(false);
112
113                 if (httpConn instanceof HttpsURLConnection) {
114                         HostnameVerifier hostnameVerifier = new HostnameVerifier() {
115                                 @Override
116                                 public boolean verify(String hostname, SSLSession session) {
117                                         return true;
118                                 }
119                         };
120                         ((HttpsURLConnection) httpConn).setHostnameVerifier(hostnameVerifier);
121                 }
122
123                 // Write message
124                 httpConn.setRequestProperty("Content-Length", "" + msg.length());
125                 DataOutputStream outStr = new DataOutputStream(httpConn.getOutputStream());
126                 outStr.write(msg.getBytes());
127                 outStr.close();
128
129                 // Read response
130                 BufferedReader respRdr;
131
132                 LOG.info("Response: " + httpConn.getResponseCode() + " " + httpConn.getResponseMessage());
133
134                 if (httpConn.getResponseCode() < 300) {
135
136                         respRdr = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
137                 } else {
138                         respRdr = new BufferedReader(new InputStreamReader(httpConn.getErrorStream()));
139                 }
140
141                 StringBuffer respBuff = new StringBuffer();
142
143                 String respLn;
144
145                 while ((respLn = respRdr.readLine()) != null) {
146                         respBuff.append(respLn + "\n");
147                 }
148                 respRdr.close();
149
150                 String respString = respBuff.toString();
151
152                 LOG.info("Response body :\n" + respString);
153
154                 return (respString);
155
156         }
157                 
158
159 }