2 * Client for ECOMP notification server
4 * Reference: @link http://stackoverflow.com/questions/13022717/java-and-https-url-connection-without-downloading-certificate
8 package org.opendaylight.mwtn.dcaeConnector.impl;
10 import java.io.BufferedReader;
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.io.InputStreamReader;
14 import java.io.OutputStream;
15 import java.io.Reader;
16 import java.net.HttpURLConnection;
18 import java.net.URLConnection;
19 import java.security.KeyManagementException;
20 import java.security.KeyStoreException;
21 import java.security.NoSuchAlgorithmException;
22 import java.security.UnrecoverableKeyException;
23 import java.security.cert.CertificateException;
24 import java.security.spec.InvalidKeySpecException;
25 import java.util.Base64;
27 import javax.net.ssl.SSLContext;
29 import org.opendaylight.mwtn.base.http.BaseHTTPClient;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 public class DcaeSenderImpl implements DcaeSender {
35 private static final Logger LOG = LoggerFactory.getLogger(DcaeSenderImpl.class);
36 private static String EMPTY = "";
37 private static final String charset = "UTF-8";
39 private final String urlString;
40 private final String basicAuth;
42 private SSLContext sc = null;
43 private URL url = null;
44 private HttpURLConnection connection = null;
46 public DcaeSenderImpl( String url, String userCredentials) {
48 LOG.info("DcaeSenderImpl setup start with {} {}", url, userCredentials);
51 this.basicAuth = "Basic " + new String(Base64.getEncoder().encode(userCredentials.getBytes()));
53 if (urlString != null && !urlString.equals("off")) {
55 this.url = new URL(url);
56 sc = BaseHTTPClient.setupSsl(true);
57 } catch (KeyManagementException | NoSuchAlgorithmException | UnrecoverableKeyException | CertificateException | KeyStoreException | InvalidKeySpecException | IOException e) {
58 LOG.warn("SSL setup failed: {}", e.getMessage());
61 LOG.info("DcaeSenderImpl setup ends");
65 * Send message to ECOMP Server
66 * @param body for POST message
69 public String sendDcaePost(String body) {
73 connection = DcaeMessages.openConnection(url, basicAuth, true, sc);
74 if (connection != null) {
75 return processPost(connection, body);
77 LOG.warn("No SSL context available");
79 } catch (IOException e) {
80 LOG.warn("Dcae post failed {}", e.getMessage());
87 * Connect to Server and expect answer.
88 * @return with answer body
90 public String testConnectServer() {
94 connection = DcaeMessages.openConnection(url, null, false, sc);
95 if (connection != null) {
96 return receiveInitialAnswer(connection);
98 } catch (IOException e) {
99 LOG.warn("Dcae post failed {}", e.getMessage());
106 * Show status in readable form for testing
107 * @return String with result
109 public String getStatusAsString() {
110 StringBuffer sb = new StringBuffer();
112 sb.append("URL: "+ url.getPath() +" "+ url.getPort() + " Host: "+ url.getHost());
114 if (connection != null) {
115 sb.append("Connection setup: ");
116 sb.append(connection.getClass().getName());
119 sb.append("Connection setup: No connection (server problem or switched off)");
121 return sb.toString();
126 /*------------------------------------------------------------------------------
132 * Send Post and wait for answer
136 * @throws IOException
138 private static String processPost( HttpURLConnection connection, String body ) throws IOException {
140 LOG.debug("Post message: {}", connection.getURL().toString());
141 if (LOG.isTraceEnabled()) {
142 LOG.trace("Body: {} ", body);
145 //Send the message to destination
146 try (OutputStream output = connection.getOutputStream()) {
147 output.write(body.getBytes(charset));
152 int responseCode = connection.getResponseCode();
153 LOG.debug("Response code: {}", String.valueOf(responseCode));
155 InputStream response= null;
156 if (responseCode >= 200 && responseCode < 300)
157 response = connection.getInputStream();
159 response = connection.getErrorStream();
160 if (response == null)
161 response = connection.getInputStream();
164 if (response != null) {
165 BufferedReader rd = new BufferedReader(new InputStreamReader(response));
167 StringBuilder result = new StringBuilder();
168 while ((line = rd.readLine()) != null) {
172 if (LOG.isTraceEnabled()) {
173 LOG.trace("Result: {} ", result.toString());
175 return result.toString();
177 } catch (IOException e) {
178 LOG.debug("No response received: {}", e.getMessage());
187 * Read initial answer from Server after connect
188 * @param connection that was opened
189 * @return String with answer message
190 * @throws IOException
192 private static String receiveInitialAnswer(URLConnection iConnection) throws IOException {
195 final StringBuffer response = new StringBuffer();
197 if (iConnection != null) {
199 final Reader reader = new InputStreamReader(iConnection.getInputStream());
200 final BufferedReader br = new BufferedReader(reader);
202 while ((line = br.readLine()) != null) {
203 response.append(line);
204 response.append("\n");
209 return response.toString();