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.util.DmaapConfig;
46 public class AafConnection extends BaseLoggingClass {
52 private String aafCred;
53 private String unit_test;
56 private HttpsURLConnection uc;
59 public AafConnection( String cred ) {
61 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
62 unit_test = p.getProperty( "UnitTest", "No" );
67 private boolean makeConnection( String pURL ) {
70 URL u = new URL( pURL );
71 uc = (HttpsURLConnection) u.openConnection();
72 uc.setInstanceFollowRedirects(false);
73 logger.info( "successful connect to " + pURL );
75 } catch ( UnknownHostException uhe ) {
76 errorLogger.error(DmaapbcLogMessageEnum.UNKNOWN_HOST_EXCEPTION, pURL, uhe.getMessage() );
77 logger.error("Error", uhe);
79 } catch (Exception e) {
80 logger.error("Error", 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(),ex);
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 logger.error("Error", pe);
141 // Rcvd error instead of 100-Continue
143 // work around glitch in Java 1.7.0.21 and likely others
144 // without this, Java will connect multiple times to the server to run the same request
145 uc.setDoOutput(false);
146 } catch (Exception e) {
147 logger.error("Error", e);
149 } catch ( SSLHandshakeException she ) {
150 logger.error("Error", she);
151 errorLogger.error( DmaapbcLogMessageEnum.SSL_HANDSHAKE_ERROR, pURL);
152 } catch ( UnknownHostException uhe ) {
153 logger.error("Error", uhe);
154 errorLogger.error(DmaapbcLogMessageEnum.UNKNOWN_HOST_EXCEPTION, pURL, uhe.getMessage() );
157 } catch ( ConnectException ce ) {
158 logger.error("Error", ce);
159 if ( "Yes".equals(unit_test) ) {
163 errorLogger.error(DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, pURL, ce.getMessage() );
168 rc = uc.getResponseCode();
169 } catch ( SSLHandshakeException she ) {
170 logger.error("Error", she);
171 errorLogger.error( DmaapbcLogMessageEnum.SSL_HANDSHAKE_ERROR, pURL);
175 logger.info( "http response code:" + rc );
176 responsemessage = uc.getResponseMessage();
177 logger.info( "responsemessage=" + responsemessage );
179 if (responsemessage == null) {
180 // work around for glitch in Java 1.7.0.21 and likely others
181 // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
182 String h0 = uc.getHeaderField(0);
184 int i = h0.indexOf(' ');
185 int j = h0.indexOf(' ', i + 1);
186 if (i != -1 && j != -1) {
187 responsemessage = h0.substring(j + 1);
191 if ( rc >= 200 && rc < 300 ) {
192 responseBody = bodyToString( uc.getInputStream() );
193 logger.info( "responseBody=" + responseBody );
195 logger.warn( "Unsuccessful response: " + responsemessage );
198 } catch (Exception e) {
199 logger.error("Unable to read response ");
200 logger.error("Error", e);
205 } catch ( Exception e ) {
206 logger.error("Error", e);
213 public int delAaf(AafObject obj, String pURL) {
214 logger.info( "entry: delAaf() to " + pURL );
215 String auth = "Basic " + Base64.encodeBase64String(aafCred.getBytes());
219 if ( ! makeConnection( pURL ) ) {
224 byte[] postData = obj.getBytes();
225 //logger.info( "post fields=" + postData ); //byte isn't very readable
226 String responsemessage = null;
227 String responseBody = null;
231 uc.setRequestProperty("Authorization", auth);
233 uc.setRequestMethod("DELETE");
234 uc.setRequestProperty("Content-Type", "application/json");
235 uc.setRequestProperty( "charset", "utf-8");
236 uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
237 uc.setUseCaches(false);
238 uc.setDoOutput(true);
239 OutputStream os = null;
244 os = uc.getOutputStream();
245 os.write( postData );
247 } catch (ProtocolException pe) {
248 logger.error("Error", pe);
249 // Rcvd error instead of 100-Continue
251 // work around glitch in Java 1.7.0.21 and likely others
252 // without this, Java will connect multiple times to the server to run the same request
253 uc.setDoOutput(false);
254 } catch (Exception e) {
255 logger.error("Error", e);
257 } catch ( SSLHandshakeException she ) {
258 errorLogger.error( DmaapbcLogMessageEnum.SSL_HANDSHAKE_ERROR +"For:- "+pURL,she);
261 rc = uc.getResponseCode();
262 } catch ( SSLHandshakeException she ) {
263 logger.error("Error", she);
264 errorLogger.error( DmaapbcLogMessageEnum.SSL_HANDSHAKE_ERROR, pURL);
268 logger.info( "http response code:" + rc );
269 responsemessage = uc.getResponseMessage();
270 logger.info( "responsemessage=" + responsemessage );
272 if (responsemessage == null) {
273 // work around for glitch in Java 1.7.0.21 and likely others
274 // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
275 String h0 = uc.getHeaderField(0);
277 int i = h0.indexOf(' ');
278 int j = h0.indexOf(' ', i + 1);
279 if (i != -1 && j != -1) {
280 responsemessage = h0.substring(j + 1);
284 if ( rc >= 200 && rc < 300 ) {
285 responseBody = bodyToString( uc.getInputStream() );
286 logger.info( "responseBody=" + responseBody );
288 logger.warn( "Unsuccessful response: " + responsemessage );
291 } catch (Exception e) {
292 logger.error("Unable to read response ");
293 logger.error("Error", e);