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