2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.dmaap.dbcapi.aaf;
27 import java.io.BufferedReader;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.InputStreamReader;
31 import java.io.OutputStream;
32 import java.net.ProtocolException;
34 import java.net.UnknownHostException;
35 import java.net.ConnectException;
37 import javax.net.ssl.HttpsURLConnection;
38 import javax.net.ssl.SSLHandshakeException;
40 import org.apache.commons.codec.binary.Base64;
41 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
42 import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;
43 import org.onap.dmaap.dbcapi.service.DmaapService;
44 import org.onap.dmaap.dbcapi.util.DmaapConfig;
47 public class AafConnection extends BaseLoggingClass {
53 private String aafCred;
54 private String unit_test;
57 private HttpsURLConnection uc;
60 public AafConnection( String cred ) {
62 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
63 unit_test = p.getProperty( "UnitTest", "No" );
68 private boolean makeConnection( String pURL ) {
71 URL u = new URL( pURL );
72 uc = (HttpsURLConnection) u.openConnection();
73 uc.setInstanceFollowRedirects(false);
74 logger.info( "successful connect to " + pURL );
76 } catch ( UnknownHostException uhe ) {
77 errorLogger.error(DmaapbcLogMessageEnum.UNKNOWN_HOST_EXCEPTION, pURL, uhe.getMessage() );
78 uhe.printStackTrace();
80 } catch (Exception e) {
81 errorLogger.error(DmaapbcLogMessageEnum.HTTP_CONNECTION_ERROR, pURL, e.getMessage() );
88 static String bodyToString( InputStream is ) {
89 StringBuilder sb = new StringBuilder();
90 BufferedReader br = new BufferedReader( new InputStreamReader(is));
93 while ((line = br.readLine()) != null ) {
96 } catch (IOException ex ) {
97 errorLogger.error( DmaapbcLogMessageEnum.IO_EXCEPTION, ex.getMessage());
100 return sb.toString();
105 public int postAaf( AafObject obj, String pURL ) {
106 logger.info( "entry: postAaf() to " + pURL );
107 String auth = "Basic " + Base64.encodeBase64String(aafCred.getBytes());
111 if ( ! makeConnection( pURL ) ) {
116 byte[] postData = obj.getBytes();
117 //logger.info( "post fields=" + postData ); //byte isn't very readable
118 String responsemessage = null;
119 String responseBody = null;
123 uc.setRequestProperty("Authorization", auth);
125 uc.setRequestMethod("POST");
126 uc.setRequestProperty("Content-Type", "application/json");
127 uc.setRequestProperty( "charset", "utf-8");
128 uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
129 uc.setUseCaches(false);
130 uc.setDoOutput(true);
131 OutputStream os = null;
136 os = uc.getOutputStream();
137 os.write( postData );
139 } catch (ProtocolException pe) {
140 // Rcvd error instead of 100-Continue
142 // work around glitch in Java 1.7.0.21 and likely others
143 // without this, Java will connect multiple times to the server to run the same request
144 uc.setDoOutput(false);
145 } catch (Exception e) {
147 } catch ( SSLHandshakeException she ) {
148 errorLogger.error( DmaapbcLogMessageEnum.SSL_HANDSHAKE_ERROR, pURL);
149 } catch ( UnknownHostException uhe ) {
150 errorLogger.error(DmaapbcLogMessageEnum.UNKNOWN_HOST_EXCEPTION, pURL, uhe.getMessage() );
153 } catch ( ConnectException ce ) {
154 if ( unit_test.equals( "Yes" ) ) {
158 errorLogger.error(DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, pURL, ce.getMessage() );
163 rc = uc.getResponseCode();
164 } catch ( SSLHandshakeException she ) {
165 errorLogger.error( DmaapbcLogMessageEnum.SSL_HANDSHAKE_ERROR, pURL);
169 logger.info( "http response code:" + rc );
170 responsemessage = uc.getResponseMessage();
171 logger.info( "responsemessage=" + responsemessage );
173 if (responsemessage == null) {
174 // work around for glitch in Java 1.7.0.21 and likely others
175 // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
176 String h0 = uc.getHeaderField(0);
178 int i = h0.indexOf(' ');
179 int j = h0.indexOf(' ', i + 1);
180 if (i != -1 && j != -1) {
181 responsemessage = h0.substring(j + 1);
185 if ( rc >= 200 && rc < 300 ) {
186 responseBody = bodyToString( uc.getInputStream() );
187 logger.info( "responseBody=" + responseBody );
189 logger.warn( "Unsuccessful response: " + responsemessage );
192 } catch (Exception e) {
193 System.err.println("Unable to read response " );
199 } catch ( Exception e ) {}
201 //return responseBody;
207 public int delAaf(AafObject obj, String pURL) {
208 logger.info( "entry: delAaf() to " + pURL );
209 String auth = "Basic " + Base64.encodeBase64String(aafCred.getBytes());
213 if ( ! makeConnection( pURL ) ) {
218 byte[] postData = obj.getBytes();
219 //logger.info( "post fields=" + postData ); //byte isn't very readable
220 String responsemessage = null;
221 String responseBody = null;
225 uc.setRequestProperty("Authorization", auth);
227 uc.setRequestMethod("DELETE");
228 uc.setRequestProperty("Content-Type", "application/json");
229 uc.setRequestProperty( "charset", "utf-8");
230 uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
231 uc.setUseCaches(false);
232 uc.setDoOutput(true);
233 OutputStream os = null;
238 os = uc.getOutputStream();
239 os.write( postData );
241 } catch (ProtocolException pe) {
242 // Rcvd error instead of 100-Continue
244 // work around glitch in Java 1.7.0.21 and likely others
245 // without this, Java will connect multiple times to the server to run the same request
246 uc.setDoOutput(false);
247 } catch (Exception e) {
249 } catch ( SSLHandshakeException she ) {
250 errorLogger.error( DmaapbcLogMessageEnum.SSL_HANDSHAKE_ERROR, pURL);
253 rc = uc.getResponseCode();
254 } catch ( SSLHandshakeException she ) {
255 errorLogger.error( DmaapbcLogMessageEnum.SSL_HANDSHAKE_ERROR, pURL);
259 logger.info( "http response code:" + rc );
260 responsemessage = uc.getResponseMessage();
261 logger.info( "responsemessage=" + responsemessage );
263 if (responsemessage == null) {
264 // work around for glitch in Java 1.7.0.21 and likely others
265 // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
266 String h0 = uc.getHeaderField(0);
268 int i = h0.indexOf(' ');
269 int j = h0.indexOf(' ', i + 1);
270 if (i != -1 && j != -1) {
271 responsemessage = h0.substring(j + 1);
275 if ( rc >= 200 && rc < 300 ) {
276 responseBody = bodyToString( uc.getInputStream() );
277 logger.info( "responseBody=" + responseBody );
279 logger.warn( "Unsuccessful response: " + responsemessage );
282 } catch (Exception e) {
283 System.err.println("Unable to read response " );
286 //return responseBody;