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.client;
23 import java.io.BufferedReader;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
27 import java.io.OutputStream;
28 import java.net.ProtocolException;
31 import javax.net.ssl.HttpsURLConnection;
32 import javax.net.ssl.SSLException;
34 import org.apache.commons.codec.binary.Base64;
35 import org.apache.log4j.Logger;
36 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
37 import org.onap.dmaap.dbcapi.model.ApiError;
38 import org.onap.dmaap.dbcapi.model.MR_Cluster;
40 public class MrTopicConnection extends BaseLoggingClass {
41 private String topicURL;
43 private HttpsURLConnection uc;
46 private String mmProvCred;
50 public MrTopicConnection(String user, String pwd ) {
51 mmProvCred = new String( user + ":" + pwd );
55 public boolean makeTopicConnection( MR_Cluster cluster, String topic, String overrideFqdn ) {
56 String fqdn = overrideFqdn != null ? overrideFqdn : cluster.getFqdn();
57 logger.info( "connect to cluster: " + fqdn + " for topic: " + topic );
60 topicURL = cluster.getTopicProtocol() + "://" + fqdn + ":" + cluster.getTopicPort() + "/events/" + topic ;
62 return makeConnection( topicURL );
65 private boolean makeConnection( String pURL ) {
66 logger.info( "makeConnection to " + pURL );
69 URL u = new URL( pURL );
70 uc = (HttpsURLConnection) u.openConnection();
71 uc.setInstanceFollowRedirects(false);
72 logger.info( "open connection to " + pURL );
74 } catch (Exception e) {
75 logger.error("Unexpected error during openConnection of " + pURL );
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( "IOexception:" + ex);
97 public ApiError doPostMessage( String postMessage ) {
98 ApiError response = new ApiError();
99 String auth = "Basic " + Base64.encodeBase64String(mmProvCred.getBytes());
104 byte[] postData = postMessage.getBytes();
105 logger.info( "post fields=" + postMessage );
106 uc.setRequestProperty("Authorization", auth);
107 logger.info( "Authenticating with " + auth );
108 uc.setRequestMethod("POST");
109 uc.setRequestProperty("Content-Type", "application/json");
110 uc.setRequestProperty( "charset", "utf-8");
111 uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
112 uc.setUseCaches(false);
113 uc.setDoOutput(true);
114 OutputStream os = null;
119 os = uc.getOutputStream();
120 os.write( postData );
122 } catch (ProtocolException pe) {
123 // Rcvd error instead of 100-Continue
125 // work around glitch in Java 1.7.0.21 and likely others
126 // without this, Java will connect multiple times to the server to run the same request
127 uc.setDoOutput(false);
128 } catch (Exception e) {
130 } catch ( SSLException se ) {
131 response.setCode(500);
132 response.setMessage( se.getMessage());
136 response.setCode( uc.getResponseCode());
137 logger.info( "http response code:" + response.getCode());
138 response.setMessage( uc.getResponseMessage() );
139 logger.info( "response message=" + response.getMessage() );
142 if ( response.getMessage() == null) {
143 // work around for glitch in Java 1.7.0.21 and likely others
144 // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
145 String h0 = uc.getHeaderField(0);
147 int i = h0.indexOf(' ');
148 int j = h0.indexOf(' ', i + 1);
149 if (i != -1 && j != -1) {
150 response.setMessage( h0.substring(j + 1) );
154 if ( response.is2xx() ) {
155 response.setFields( bodyToString( uc.getInputStream() ) );
156 logger.info( "responseBody=" + response.getFields() );
161 } catch (Exception e) {
162 response.setCode(500);
163 response.setMessage( "Unable to read response");
164 logger.warn( response.getMessage() );
170 } catch ( Exception e ) {}