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;
36 import javax.net.ssl.HttpsURLConnection;
37 import javax.net.ssl.SSLHandshakeException;
39 import org.apache.commons.codec.binary.Base64;
40 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
41 import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;
42 import org.onap.dmaap.dbcapi.service.DmaapService;
45 public class AafConnection extends BaseLoggingClass {
51 private String aafCred;
54 private HttpsURLConnection uc;
57 public AafConnection( String cred ) {
62 private boolean makeConnection( String pURL ) {
65 URL u = new URL( pURL );
66 uc = (HttpsURLConnection) u.openConnection();
67 uc.setInstanceFollowRedirects(false);
68 logger.info( "successful connect to " + pURL );
70 } catch ( UnknownHostException uhe ) {
71 errorLogger.error(DmaapbcLogMessageEnum.UNKNOWN_HOST_EXCEPTION, pURL, uhe.getMessage() );
72 uhe.printStackTrace();
74 } catch (Exception e) {
75 errorLogger.error(DmaapbcLogMessageEnum.HTTP_CONNECTION_ERROR, pURL, e.getMessage() );
82 static String bodyToString( InputStream is ) {
83 StringBuilder sb = new StringBuilder();
84 BufferedReader br = new BufferedReader( new InputStreamReader(is));
87 while ((line = br.readLine()) != null ) {
90 } catch (IOException ex ) {
91 errorLogger.error( DmaapbcLogMessageEnum.IO_EXCEPTION, ex.getMessage());
99 public int postAaf( AafObject obj, String pURL ) {
100 logger.info( "entry: postAaf() to " + pURL );
101 String auth = "Basic " + Base64.encodeBase64String(aafCred.getBytes());
105 if ( ! makeConnection( pURL ) ) {
110 byte[] postData = obj.getBytes();
111 //logger.info( "post fields=" + postData ); //byte isn't very readable
112 String responsemessage = null;
113 String responseBody = null;
117 uc.setRequestProperty("Authorization", auth);
119 uc.setRequestMethod("POST");
120 uc.setRequestProperty("Content-Type", "application/json");
121 uc.setRequestProperty( "charset", "utf-8");
122 uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
123 uc.setUseCaches(false);
124 uc.setDoOutput(true);
125 OutputStream os = null;
130 os = uc.getOutputStream();
131 os.write( postData );
133 } catch (ProtocolException pe) {
134 // Rcvd error instead of 100-Continue
136 // work around glitch in Java 1.7.0.21 and likely others
137 // without this, Java will connect multiple times to the server to run the same request
138 uc.setDoOutput(false);
139 } catch (Exception e) {
141 } catch ( SSLHandshakeException she ) {
142 errorLogger.error( DmaapbcLogMessageEnum.SSL_HANDSHAKE_ERROR, pURL);
143 } catch ( UnknownHostException uhe ) {
144 errorLogger.error(DmaapbcLogMessageEnum.UNKNOWN_HOST_EXCEPTION, pURL, uhe.getMessage() );
149 rc = uc.getResponseCode();
150 } catch ( SSLHandshakeException she ) {
151 errorLogger.error( DmaapbcLogMessageEnum.SSL_HANDSHAKE_ERROR, pURL);
155 logger.info( "http response code:" + rc );
156 responsemessage = uc.getResponseMessage();
157 logger.info( "responsemessage=" + responsemessage );
159 if (responsemessage == null) {
160 // work around for glitch in Java 1.7.0.21 and likely others
161 // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
162 String h0 = uc.getHeaderField(0);
164 int i = h0.indexOf(' ');
165 int j = h0.indexOf(' ', i + 1);
166 if (i != -1 && j != -1) {
167 responsemessage = h0.substring(j + 1);
171 if ( rc >= 200 && rc < 300 ) {
172 responseBody = bodyToString( uc.getInputStream() );
173 logger.info( "responseBody=" + responseBody );
175 logger.warn( "Unsuccessful response: " + responsemessage );
178 } catch (Exception e) {
179 System.err.println("Unable to read response " );
185 } catch ( Exception e ) {}
187 //return responseBody;
193 public int delAaf(AafObject obj, String pURL) {
194 logger.info( "entry: delAaf() to " + pURL );
195 String auth = "Basic " + Base64.encodeBase64String(aafCred.getBytes());
199 if ( ! makeConnection( pURL ) ) {
204 byte[] postData = obj.getBytes();
205 //logger.info( "post fields=" + postData ); //byte isn't very readable
206 String responsemessage = null;
207 String responseBody = null;
211 uc.setRequestProperty("Authorization", auth);
213 uc.setRequestMethod("DELETE");
214 uc.setRequestProperty("Content-Type", "application/json");
215 uc.setRequestProperty( "charset", "utf-8");
216 uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
217 uc.setUseCaches(false);
218 uc.setDoOutput(true);
219 OutputStream os = null;
224 os = uc.getOutputStream();
225 os.write( postData );
227 } catch (ProtocolException pe) {
228 // Rcvd error instead of 100-Continue
230 // work around glitch in Java 1.7.0.21 and likely others
231 // without this, Java will connect multiple times to the server to run the same request
232 uc.setDoOutput(false);
233 } catch (Exception e) {
235 } catch ( SSLHandshakeException she ) {
236 errorLogger.error( DmaapbcLogMessageEnum.SSL_HANDSHAKE_ERROR, pURL);
239 rc = uc.getResponseCode();
240 } catch ( SSLHandshakeException she ) {
241 errorLogger.error( DmaapbcLogMessageEnum.SSL_HANDSHAKE_ERROR, pURL);
245 logger.info( "http response code:" + rc );
246 responsemessage = uc.getResponseMessage();
247 logger.info( "responsemessage=" + responsemessage );
249 if (responsemessage == null) {
250 // work around for glitch in Java 1.7.0.21 and likely others
251 // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
252 String h0 = uc.getHeaderField(0);
254 int i = h0.indexOf(' ');
255 int j = h0.indexOf(' ', i + 1);
256 if (i != -1 && j != -1) {
257 responsemessage = h0.substring(j + 1);
261 if ( rc >= 200 && rc < 300 ) {
262 responseBody = bodyToString( uc.getInputStream() );
263 logger.info( "responseBody=" + responseBody );
265 logger.warn( "Unsuccessful response: " + responsemessage );
268 } catch (Exception e) {
269 System.err.println("Unable to read response " );
272 //return responseBody;