2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
7 * Modifications Copyright (C) 2019 IBM.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.dmaap.dbcapi.aaf;
29 import java.io.BufferedReader;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.InputStreamReader;
33 import java.io.OutputStream;
34 import java.net.ProtocolException;
36 import java.net.UnknownHostException;
37 import java.net.ConnectException;
39 import javax.net.ssl.HttpsURLConnection;
40 import javax.net.ssl.SSLContext;
41 import javax.net.ssl.SSLHandshakeException;
43 import javax.net.ssl.TrustManager;
44 import javax.net.ssl.X509TrustManager;
45 import org.apache.commons.codec.binary.Base64;
46 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
47 import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;
48 import org.onap.dmaap.dbcapi.util.DmaapConfig;
51 public class AafConnection extends BaseLoggingClass {
57 private String aafCred;
58 private String unit_test;
61 private HttpsURLConnection uc;
64 public AafConnection( String cred ) {
66 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
67 unit_test = p.getProperty( "UnitTest", "No" );
72 private boolean makeConnection( String pURL ) {
75 URL u = new URL( pURL );
76 uc = (HttpsURLConnection) u.openConnection();
77 uc.setInstanceFollowRedirects(false);
78 logger.info( "successful connect to " + pURL );
80 } catch ( UnknownHostException uhe ) {
81 errorLogger.error(DmaapbcLogMessageEnum.UNKNOWN_HOST_EXCEPTION, pURL, uhe.getMessage() );
82 logger.error("Error", uhe);
84 } catch (Exception e) {
85 logger.error("Error", e);
86 errorLogger.error(DmaapbcLogMessageEnum.HTTP_CONNECTION_ERROR, pURL, e.getMessage());
92 static String bodyToString( InputStream is ) {
93 StringBuilder sb = new StringBuilder();
94 BufferedReader br = new BufferedReader( new InputStreamReader(is));
97 while ((line = br.readLine()) != null ) {
100 } catch (IOException ex ) {
101 errorLogger.error( DmaapbcLogMessageEnum.IO_EXCEPTION + ex.getMessage(),ex);
104 return sb.toString();
109 public int postAaf( AafObject obj, String pURL ) {
110 logger.info( "entry: postAaf() to " + pURL );
111 String auth = "Basic " + Base64.encodeBase64String(aafCred.getBytes());
115 if ( ! makeConnection( pURL ) ) {
120 byte[] postData = obj.getBytes();
121 //logger.info( "post fields=" + postData ); //byte isn't very readable
122 String responsemessage = null;
123 String responseBody = null;
127 uc.setRequestProperty("Authorization", auth);
129 uc.setRequestMethod("POST");
130 uc.setRequestProperty("Content-Type", "application/json");
131 uc.setRequestProperty( "charset", "utf-8");
132 uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
133 uc.setUseCaches(false);
134 uc.setDoOutput(true);
136 SSLContext sc = SSLContext.getInstance("SSL");
137 sc.init(null, trustAllCerts, new java.security.SecureRandom());
138 uc.setSSLSocketFactory(sc.getSocketFactory());
139 OutputStream os = null;
144 os = uc.getOutputStream();
145 os.write( postData );
147 } catch (ProtocolException pe) {
148 logger.error("Error", pe);
149 // Rcvd error instead of 100-Continue
151 // work around glitch in Java 1.7.0.21 and likely others
152 // without this, Java will connect multiple times to the server to run the same request
153 uc.setDoOutput(false);
154 } catch (Exception e) {
155 logger.error("Error", e);
157 } catch ( SSLHandshakeException she ) {
158 logger.error("Error", she);
159 errorLogger.error( DmaapbcLogMessageEnum.SSL_HANDSHAKE_ERROR, pURL);
160 } catch ( UnknownHostException uhe ) {
161 logger.error("Error", uhe);
162 errorLogger.error(DmaapbcLogMessageEnum.UNKNOWN_HOST_EXCEPTION, pURL, uhe.getMessage() );
165 } catch ( ConnectException ce ) {
166 logger.error("Error", ce);
167 if ( "Yes".equals(unit_test) ) {
171 errorLogger.error(DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, pURL, ce.getMessage() );
176 rc = uc.getResponseCode();
177 } catch ( SSLHandshakeException she ) {
178 logger.error("Error", she);
179 errorLogger.error( DmaapbcLogMessageEnum.SSL_HANDSHAKE_ERROR, pURL);
183 logger.info( "http response code:" + rc );
184 responsemessage = uc.getResponseMessage();
185 logger.info( "responsemessage=" + responsemessage );
187 if (responsemessage == null) {
188 // work around for glitch in Java 1.7.0.21 and likely others
189 // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
190 String h0 = uc.getHeaderField(0);
192 int i = h0.indexOf(' ');
193 int j = h0.indexOf(' ', i + 1);
194 if (i != -1 && j != -1) {
195 responsemessage = h0.substring(j + 1);
199 if ( rc >= 200 && rc < 300 ) {
200 responseBody = bodyToString( uc.getInputStream() );
201 logger.info( "responseBody=" + responseBody );
203 logger.warn( "Unsuccessful response: " + responsemessage );
206 } catch (Exception e) {
207 logger.error("Unable to read response ");
208 logger.error("Error", e);
213 } catch ( Exception e ) {
214 logger.error("Error", e);
221 public int delAaf(AafObject obj, String pURL) {
222 logger.info( "entry: delAaf() to " + pURL );
223 String auth = "Basic " + Base64.encodeBase64String(aafCred.getBytes());
227 if ( ! makeConnection( pURL ) ) {
232 byte[] postData = obj.getBytes();
233 //logger.info( "post fields=" + postData ); //byte isn't very readable
234 String responsemessage = null;
235 String responseBody = null;
239 uc.setRequestProperty("Authorization", auth);
241 uc.setRequestMethod("DELETE");
242 uc.setRequestProperty("Content-Type", "application/json");
243 uc.setRequestProperty( "charset", "utf-8");
244 uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
245 uc.setUseCaches(false);
246 uc.setDoOutput(true);
247 OutputStream os = null;
252 os = uc.getOutputStream();
253 os.write( postData );
255 } catch (ProtocolException pe) {
256 logger.error("Error", pe);
257 // Rcvd error instead of 100-Continue
259 // work around glitch in Java 1.7.0.21 and likely others
260 // without this, Java will connect multiple times to the server to run the same request
261 uc.setDoOutput(false);
262 } catch (Exception e) {
263 logger.error("Error", e);
265 } catch ( SSLHandshakeException she ) {
266 errorLogger.error( DmaapbcLogMessageEnum.SSL_HANDSHAKE_ERROR +"For:- "+pURL,she);
269 rc = uc.getResponseCode();
270 } catch ( SSLHandshakeException she ) {
271 logger.error("Error", she);
272 errorLogger.error( DmaapbcLogMessageEnum.SSL_HANDSHAKE_ERROR, pURL);
276 logger.info( "http response code:" + rc );
277 responsemessage = uc.getResponseMessage();
278 logger.info( "responsemessage=" + responsemessage );
280 if (responsemessage == null) {
281 // work around for glitch in Java 1.7.0.21 and likely others
282 // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
283 String h0 = uc.getHeaderField(0);
285 int i = h0.indexOf(' ');
286 int j = h0.indexOf(' ', i + 1);
287 if (i != -1 && j != -1) {
288 responsemessage = h0.substring(j + 1);
292 if ( rc >= 200 && rc < 300 ) {
293 responseBody = bodyToString( uc.getInputStream() );
294 logger.info( "responseBody=" + responseBody );
296 logger.warn( "Unsuccessful response: " + responsemessage );
299 } catch (Exception e) {
300 logger.error("Unable to read response ");
301 logger.error("Error", e);
307 private TrustManager[] trustAllCerts = new TrustManager[]{
308 new X509TrustManager() {
311 public java.security.cert.X509Certificate[] getAcceptedIssuers()
316 public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType)
318 //No need to implement.
321 public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType)
323 //No need to implement.