2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
7 * Modifications Copyright (C) 2019 IBM.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.dmaap.dbcapi.client;
25 import java.io.BufferedReader;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.InputStreamReader;
29 import java.io.OutputStream;
30 import java.net.ConnectException;
31 import java.net.ProtocolException;
32 import java.net.SocketException;
34 import java.util.Arrays;
35 import javax.net.ssl.HttpsURLConnection;
36 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
37 import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;
38 import org.onap.dmaap.dbcapi.model.ApiError;
39 import org.onap.dmaap.dbcapi.model.DR_Sub;
40 import org.onap.dmaap.dbcapi.model.Feed;
41 import org.onap.dmaap.dbcapi.service.DmaapService;
42 import org.onap.dmaap.dbcapi.util.DmaapConfig;
46 public class DrProvConnection extends BaseLoggingClass {
49 private String provURL;
50 private String provApi;
51 private String behalfHeader;
52 private String feedContentType;
53 private String subContentType;
54 private String unit_test;
55 private String provURI;
57 private HttpsURLConnection uc;
60 public DrProvConnection() {
61 provURL = new DmaapService().getDmaap().getDrProvUrl();
62 if ( provURL.length() < 1 ) {
63 errorLogger.error( DmaapbcLogMessageEnum.PREREQ_DMAAP_OBJECT, "DmaapService().getDmaap().getDrProvUrl()");
65 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
66 provApi = p.getProperty( "DR.provApi", "ONAP" );
67 behalfHeader = p.getProperty( "DR.onBehalfHeader", "X-DMAAP-DR-ON-BEHALF-OF");
68 feedContentType = p.getProperty( "DR.feedContentType", "application/vnd.dmaap-dr.feed");
69 subContentType = p.getProperty( "DR.subContentType", "application/vnd.dmaap-dr.subscription");
70 provURI = p.getProperty( "DR.ProvisioningURI", "/internal/prov");
71 logger.info( "provURL=" + provURL + " provApi=" + provApi + " behalfHeader=" + behalfHeader
72 + " feedContentType=" + feedContentType + " subContentType=" + subContentType );
73 unit_test = p.getProperty( "UnitTest", "No" );
77 public boolean makeFeedConnection() {
78 return makeConnection( provURL );
80 public boolean makeFeedConnection(String feedId) {
81 return makeConnection( provURL + "/feed/" + feedId );
83 public boolean makeSubPostConnection( String subURL ) {
84 String[] parts = subURL.split("/");
85 String revisedURL = provURL + "/" + parts[3] + "/" + parts[4];
86 logger.info( "mapping " + subURL + " to " + revisedURL );
87 return makeConnection( revisedURL );
89 public boolean makeSubPutConnection( String subId ) {
90 String revisedURL = provURL + "/subs/" + subId;
91 logger.info( "mapping " + subId + " to " + revisedURL );
92 return makeConnection( revisedURL );
95 public boolean makeIngressConnection( String feed, String user, String subnet, String nodep ) {
96 String uri = String.format("/internal/route/ingress/?feed=%s&user=%s&subnet=%s&nodepatt=%s",
97 feed, user, subnet, nodep );
98 return makeConnection( provURL + uri );
100 public boolean makeEgressConnection( String sub, String nodep ) {
101 String uri = String.format("/internal/route/egress/?sub=%s&node=%s",
103 return makeConnection( provURL + uri );
105 public boolean makeDumpConnection() {
106 String url = provURL + provURI;
107 return makeConnection( url );
109 public boolean makeNodesConnection( String varName ) {
111 String uri = String.format("/internal/api/%s", varName);
112 return makeConnection( provURL + uri );
115 public boolean makeNodesConnection( String varName, String val ) {
120 String cv = val.replaceAll("\\|", "%7C");
121 String uri = String.format( "/internal/api/%s?val=%s", varName, cv );
123 return makeConnection( provURL + uri );
126 private boolean makeConnection( String pURL ) {
129 URL u = new URL( pURL );
130 uc = (HttpsURLConnection) u.openConnection();
131 uc.setInstanceFollowRedirects(false);
132 logger.info( "successful connect to " + pURL );
133 uc.setSSLSocketFactory(DmaapConfig.getSSLSocketFactory());
135 } catch (Exception e) {
136 errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_ERROR, pURL, e.getMessage() );
141 public String bodyToString( InputStream is ) {
142 logger.info( "is=" + is );
143 StringBuilder sb = new StringBuilder();
144 BufferedReader br = new BufferedReader( new InputStreamReader(is));
147 while ((line = br.readLine()) != null ) {
150 } catch (IOException ex ) {
151 errorLogger.error( DmaapbcLogMessageEnum.IO_EXCEPTION, ex.getMessage());
154 return sb.toString();
158 public String doPostFeed( Feed postFeed, ApiError err ) {
160 byte[] postData = postFeed.getBytes();
161 logger.info( "post fields=" + Arrays.toString(postData) );
162 String responsemessage = null;
163 String responseBody = null;
167 logger.info( "uc=" + uc );
168 uc.setRequestMethod("POST");
169 uc.setRequestProperty("Content-Type", feedContentType);
170 uc.setRequestProperty( "charset", "utf-8");
171 uc.setRequestProperty( behalfHeader, postFeed.getOwner() );
172 uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
173 uc.setUseCaches(false);
174 uc.setDoOutput(true);
175 OutputStream os = null;
179 os = uc.getOutputStream();
180 os.write( postData );
182 } catch (ProtocolException pe) {
183 // Rcvd error instead of 100-Continue
185 // work around glitch in Java 1.7.0.21 and likely others
186 // without this, Java will connect multiple times to the server to run the same request
187 uc.setDoOutput(false);
188 } catch (Exception e) {
190 } catch (Exception e) {
191 logger.info( "Exception: " + e.getMessage() );
194 rc = uc.getResponseCode();
195 logger.info( "http response code:" + rc );
196 responsemessage = uc.getResponseMessage();
197 logger.info( "responsemessage=" + responsemessage );
198 if (responsemessage == null) {
199 // work around for glitch in Java 1.7.0.21 and likely others
200 // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
201 String h0 = uc.getHeaderField(0);
203 int i = h0.indexOf(' ');
204 int j = h0.indexOf(' ', i + 1);
205 if (i != -1 && j != -1) {
206 responsemessage = h0.substring(j + 1);
211 responseBody = bodyToString( uc.getInputStream() );
212 logger.info( "responseBody=" + responseBody );
216 err.setMessage(responsemessage);
219 } catch (ConnectException ce) {
220 errorLogger.error(DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
222 err.setMessage("Backend connection refused");
223 } catch (SocketException se) {
224 errorLogger.error( DmaapbcLogMessageEnum.SOCKET_EXCEPTION, se.getMessage(), "response from prov server" );
226 err.setMessage( "Unable to read response from DR");
227 } catch (Exception e) {
228 if ( unit_test.equals( "Yes" ) ) {
230 err.setMessage( "simulated response");
231 logger.info( "artificial 200 response from doPostFeed because unit_test =" + unit_test );
233 logger.warn("Unable to read response " );
234 errorLogger.error("Unable to read response ", e.getMessage());
236 err.setCode( uc.getResponseCode());
237 err.setMessage(uc.getResponseMessage());
238 } catch (Exception e2) {
240 err.setMessage("Unable to determine response message");
247 } catch ( Exception e ) {
248 logger.error(e.getMessage(), e);
256 // the POST for /internal/route/ingress doesn't return any data, so needs a different function
257 // the POST for /internal/route/egress doesn't return any data, so needs a different function
258 public int doXgressPost( ApiError err ) {
260 String responsemessage = null;
264 uc.setRequestMethod("POST");
270 } catch (ProtocolException pe) {
271 // Rcvd error instead of 100-Continue
273 // work around glitch in Java 1.7.0.21 and likely others
274 // without this, Java will connect multiple times to the server to run the same request
275 uc.setDoOutput(false);
276 } catch (Exception e) {
277 logger.error(e.getMessage(), e);
279 } catch (Exception e) {
280 logger.info( "Exception: " + e.getMessage() );
283 rc = uc.getResponseCode();
284 logger.info( "http response code:" + rc );
285 responsemessage = uc.getResponseMessage();
286 logger.info( "responsemessage=" + responsemessage );
290 if (rc < 200 || rc >= 300 ) {
292 err.setMessage(responsemessage);
294 } catch (Exception e) {
295 if ( unit_test.equals( "Yes" ) ) {
297 err.setMessage( "simulated response");
298 logger.info( "artificial 200 response from doXgressPost because unit_test =" + unit_test );
300 logger.error("Unable to read response " );
301 logger.error(e.getMessage(), e);
307 } catch ( Exception e ) {
308 logger.error(e.getMessage(), e);
316 public String doPostDr_Sub( DR_Sub postSub, ApiError err ) {
317 logger.info( "entry: doPostDr_Sub() " );
318 byte[] postData = postSub.getBytes(provApi );
319 logger.info( "post fields=" + postData );
320 String responsemessage = null;
321 String responseBody = null;
325 uc.setRequestMethod("POST");
327 uc.setRequestProperty("Content-Type", subContentType );
328 uc.setRequestProperty( "charset", "utf-8");
329 uc.setRequestProperty( behalfHeader, "DGL" );
330 uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
331 uc.setUseCaches(false);
332 uc.setDoOutput(true);
333 OutputStream os = null;
338 os = uc.getOutputStream();
339 os.write( postData );
341 } catch (ProtocolException pe) {
342 // Rcvd error instead of 100-Continue
344 // work around glitch in Java 1.7.0.21 and likely others
345 // without this, Java will connect multiple times to the server to run the same request
346 uc.setDoOutput(false);
347 } catch (Exception e) {
348 logger.error(e.getMessage(), e);
350 } catch (Exception e) {
351 logger.info( "Exception: " + e.getMessage() );
354 rc = uc.getResponseCode();
355 logger.info( "http response code:" + rc );
356 responsemessage = uc.getResponseMessage();
357 logger.info( "responsemessage=" + responsemessage );
360 if (responsemessage == null) {
361 // work around for glitch in Java 1.7.0.21 and likely others
362 // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
363 String h0 = uc.getHeaderField(0);
365 int i = h0.indexOf(' ');
366 int j = h0.indexOf(' ', i + 1);
367 if (i != -1 && j != -1) {
368 responsemessage = h0.substring(j + 1);
373 responseBody = bodyToString( uc.getInputStream() );
374 logger.info( "responseBody=" + responseBody );
378 err.setMessage(responsemessage);
381 } catch (Exception e) {
382 if ( unit_test.equals( "Yes" ) ) {
384 err.setMessage( "simulated response");
385 logger.info( "artificial 200 response from doPostDr_Sub because unit_test =" + unit_test );
387 logger.error("Unable to read response ", e.getMessage());
393 } catch ( Exception e ) {
394 logger.error(e.getMessage(), e);
402 public String doPutFeed(Feed putFeed, ApiError err) {
403 byte[] postData = putFeed.getBytes();
404 logger.info( "post fields=" + Arrays.toString(postData) );
405 String responsemessage = null;
406 String responseBody = null;
409 logger.info( "uc=" + uc );
410 uc.setRequestMethod("PUT");
411 uc.setRequestProperty("Content-Type", feedContentType );
412 uc.setRequestProperty( "charset", "utf-8");
413 uc.setRequestProperty( behalfHeader, putFeed.getOwner() );
414 uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
415 uc.setUseCaches(false);
416 uc.setDoOutput(true);
417 OutputStream os = null;
422 os = uc.getOutputStream();
423 os.write( postData );
425 } catch (ProtocolException pe) {
426 // Rcvd error instead of 100-Continue
428 // work around glitch in Java 1.7.0.21 and likely others
429 // without this, Java will connect multiple times to the server to run the same request
430 uc.setDoOutput(false);
431 } catch (Exception e) {
432 logger.error(e.getMessage(), e);
434 } catch (Exception e) {
435 logger.info( "Exception: " + e.getMessage() );
438 rc = uc.getResponseCode();
439 logger.info( "http response code:" + rc );
440 responsemessage = uc.getResponseMessage();
441 logger.info( "responsemessage=" + responsemessage );
444 if (responsemessage == null) {
445 // work around for glitch in Java 1.7.0.21 and likely others
446 // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
447 String h0 = uc.getHeaderField(0);
449 int i = h0.indexOf(' ');
450 int j = h0.indexOf(' ', i + 1);
451 if (i != -1 && j != -1) {
452 responsemessage = h0.substring(j + 1);
456 if (rc >= 200 && rc < 300 ) {
457 responseBody = bodyToString( uc.getInputStream() );
458 logger.info( "responseBody=" + responseBody );
460 } else if ( rc == 404 ) {
462 err.setFields( "feedid");
463 String message = "FeedId " + putFeed.getFeedId() + " not found on DR to update. Out-of-sync condition?";
464 err.setMessage( message );
465 errorLogger.error( DmaapbcLogMessageEnum.PROV_OUT_OF_SYNC, "Feed", putFeed.getFeedId() );
469 err.setMessage(responsemessage);
472 } catch (ConnectException ce) {
473 if ( unit_test.equals( "Yes" ) ) {
475 err.setMessage( "simulated response");
476 logger.info( "artificial 200 response from doPutFeed because unit_test =" + unit_test );
478 errorLogger.error(DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage());
480 err.setMessage("Backend connection refused");
482 } catch (SocketException se) {
483 errorLogger.error( DmaapbcLogMessageEnum.SOCKET_EXCEPTION, se.getMessage(), "response from Prov server" );
485 err.setMessage( "Unable to read response from DR");
486 } catch (Exception e) {
487 if ( unit_test.equals( "Yes" ) ) {
489 err.setMessage( "simulated response");
490 logger.info( "artificial 200 response from doPutFeed because unit_test =" + unit_test );
492 logger.warn("Unable to read response " );
493 logger.error(e.getMessage(), e);
496 err.setCode( uc.getResponseCode());
497 err.setMessage(uc.getResponseMessage());
498 } catch (Exception e2) {
500 err.setMessage("Unable to determine response message");
501 logger.error(e2.getMessage(), e2);
506 } catch ( Exception e ) {
507 logger.error(e.getMessage(), e);
512 public String doPutDr_Sub(DR_Sub postSub, ApiError err) {
513 logger.info( "entry: doPutDr_Sub() " );
514 byte[] postData = postSub.getBytes(provApi);
515 logger.info( "post fields=" + postData );
516 String responsemessage = null;
517 String responseBody = null;
521 uc.setRequestMethod("PUT");
523 uc.setRequestProperty("Content-Type", subContentType );
524 uc.setRequestProperty( "charset", "utf-8");
525 uc.setRequestProperty( behalfHeader, "DGL" );
526 uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
527 uc.setUseCaches(false);
528 uc.setDoOutput(true);
529 OutputStream os = null;
534 os = uc.getOutputStream();
535 os.write( postData );
537 } catch (ProtocolException pe) {
538 // Rcvd error instead of 100-Continue
540 // work around glitch in Java 1.7.0.21 and likely others
541 // without this, Java will connect multiple times to the server to run the same request
542 uc.setDoOutput(false);
543 } catch (Exception e) {
544 logger.error(e.getMessage(), e);
546 } catch (Exception e) {
547 logger.info( "Exception: " + e.getMessage() );
550 rc = uc.getResponseCode();
551 logger.info( "http response code:" + rc );
552 responsemessage = uc.getResponseMessage();
553 logger.info( "responsemessage=" + responsemessage );
556 if (responsemessage == null) {
557 // work around for glitch in Java 1.7.0.21 and likely others
558 // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
559 String h0 = uc.getHeaderField(0);
561 int i = h0.indexOf(' ');
562 int j = h0.indexOf(' ', i + 1);
563 if (i != -1 && j != -1) {
564 responsemessage = h0.substring(j + 1);
569 responseBody = bodyToString( uc.getInputStream() );
570 logger.info( "responseBody=" + responseBody );
574 err.setMessage(responsemessage);
577 } catch (ConnectException ce) {
578 errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
580 err.setMessage("Backend connection refused");
581 logger.error(ce.getMessage(), ce);
582 } catch (Exception e) {
583 if ( unit_test.equals( "Yes" ) ) {
585 err.setMessage( "simulated response");
586 logger.info( "artificial 200 response from doPutDr_Sub because unit_test =" + unit_test );
588 logger.error("Unable to read response " );
589 logger.error(e.getMessage(), e);
600 public String doGetNodes( ApiError err ) {
601 logger.info( "entry: doGetNodes() " );
602 //byte[] postData = postSub.getBytes();
603 //logger.info( "get fields=" + postData );
604 String responsemessage = null;
605 String responseBody = null;
609 uc.setRequestMethod("GET");
617 } catch (ProtocolException pe) {
619 // Rcvd error instead of 100-Continue
621 // work around glitch in Java 1.7.0.21 and likely others
622 // without this, Java will connect multiple times to the server to run the same request
623 uc.setDoOutput(false);
624 } catch (Exception e) {
625 logger.error(e.getMessage(), e);
627 } catch (Exception e) {
628 logger.info( "Exception: " + e.getMessage() );
632 rc = uc.getResponseCode();
633 logger.info( "http response code:" + rc );
634 responsemessage = uc.getResponseMessage();
635 logger.info( "responsemessage=" + responsemessage );
637 if (responsemessage == null) {
639 // work around for glitch in Java 1.7.0.21 and likely others
640 // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
641 String h0 = uc.getHeaderField(0);
643 int i = h0.indexOf(' ');
644 int j = h0.indexOf(' ', i + 1);
645 if (i != -1 && j != -1) {
646 responsemessage = h0.substring(j + 1);
651 err.setCode(rc); // may not really be an error, but we save rc
653 responseBody = bodyToString( uc.getInputStream() );
654 logger.info( "responseBody=" + responseBody );
656 err.setMessage(responsemessage);
660 } catch (ConnectException ce) {
661 if ( unit_test.equals( "Yes" ) ) {
663 err.setMessage( "simulated response");
664 logger.info( "artificial 200 response from doGetNodes because unit_test =" + unit_test );
666 errorLogger.error(DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage());
668 err.setMessage("Backend connection refused");
669 logger.error(ce.getMessage(), ce);
671 } catch (Exception e) {
672 if ( unit_test.equals( "Yes" ) ) {
674 err.setMessage( "simulated response");
675 logger.info( "artificial 200 response from doGetNodes because unit_test =" + unit_test );
677 logger.error("Unable to read response ", e.getMessage());
681 if ( uc != null ) uc.disconnect();
687 public String doPutNodes( ApiError err ) {
688 logger.info( "entry: doPutNodes() " );
689 String responsemessage = null;
690 String responseBody = null;
693 uc.setRequestMethod("PUT");
694 uc.setUseCaches(false);
699 } catch (ProtocolException pe) {
700 // Rcvd error instead of 100-Continue
702 // work around glitch in Java 1.7.0.21 and likely others
703 // without this, Java will connect multiple times to the server to run the same request
704 uc.setDoOutput(false);
705 } catch (Exception e) {
707 } catch (Exception e) {
708 logger.info( "Exception: " + e.getMessage() );
711 rc = uc.getResponseCode();
712 logger.info( "http response code:" + rc );
713 responsemessage = uc.getResponseMessage();
714 logger.info( "responsemessage=" + responsemessage );
717 if (responsemessage == null) {
718 // work around for glitch in Java 1.7.0.21 and likely others
719 // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
720 String h0 = uc.getHeaderField(0);
722 int i = h0.indexOf(' ');
723 int j = h0.indexOf(' ', i + 1);
724 if (i != -1 && j != -1) {
725 responsemessage = h0.substring(j + 1);
731 responseBody = bodyToString( uc.getInputStream() );
732 logger.info( "responseBody=" + responseBody );
736 err.setMessage(responsemessage);
739 } catch (Exception e) {
740 if ( unit_test.equals( "Yes" ) ) {
742 err.setMessage( "simulated response");
743 logger.info( "artificial 200 response from doPutNodes because unit_test =" + unit_test );
745 logger.error("Unable to read response ", e.getMessage());
756 public String doDeleteFeed(Feed putFeed, ApiError err) {
757 String responsemessage = null;
758 String responseBody = null;
761 logger.info( "uc=" + uc );
762 uc.setRequestMethod("DELETE");
763 uc.setRequestProperty("Content-Type", feedContentType );
764 uc.setRequestProperty( "charset", "utf-8");
765 uc.setRequestProperty( behalfHeader, putFeed.getOwner() );
766 uc.setUseCaches(false);
767 uc.setDoOutput(true);
768 OutputStream os = null;
773 os = uc.getOutputStream();
774 //os.write( postData );
776 } catch (ProtocolException pe) {
777 // Rcvd error instead of 100-Continue
779 // work around glitch in Java 1.7.0.21 and likely others
780 // without this, Java will connect multiple times to the server to run the same request
781 uc.setDoOutput(false);
782 } catch (Exception e) {
783 logger.error(e.getMessage(), e);
785 } catch (Exception e) {
786 logger.info( "Exception: " + e.getMessage() );
789 rc = uc.getResponseCode();
790 logger.info( "http response code:" + rc );
791 responsemessage = uc.getResponseMessage();
792 logger.info( "responsemessage=" + responsemessage );
795 if (responsemessage == null) {
796 // work around for glitch in Java 1.7.0.21 and likely others
797 // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
798 String h0 = uc.getHeaderField(0);
800 int i = h0.indexOf(' ');
801 int j = h0.indexOf(' ', i + 1);
802 if (i != -1 && j != -1) {
803 responsemessage = h0.substring(j + 1);
807 if (rc >= 200 && rc < 300 ) {
808 responseBody = bodyToString( uc.getInputStream() );
809 logger.info( "responseBody=" + responseBody );
811 } else if ( rc == 404 ) {
813 err.setFields( "feedid");
814 String message = "FeedId " + putFeed.getFeedId() + " not found on DR to update. Out-of-sync condition?";
815 err.setMessage( message );
816 errorLogger.error( DmaapbcLogMessageEnum.PROV_OUT_OF_SYNC, "Feed", putFeed.getFeedId() );
820 err.setMessage(responsemessage);
823 } catch (ConnectException ce) {
824 errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
826 err.setMessage("Backend connection refused");
827 logger.error(ce.getMessage(), ce);
828 } catch (SocketException se) {
829 errorLogger.error( DmaapbcLogMessageEnum.SOCKET_EXCEPTION, se.getMessage(), "response from Prov server" );
831 err.setMessage( "Unable to read response from DR");
832 logger.error(se.getMessage(), se);
833 } catch (Exception e) {
834 if ( unit_test.equals( "Yes" ) ) {
836 err.setMessage( "simulated response");
837 logger.info( "artificial 200 response from doDeleteFeed because unit_test =" + unit_test );
839 logger.warn("Unable to read response " );
840 logger.error(e.getMessage(), e);
842 err.setCode( uc.getResponseCode());
843 err.setMessage(uc.getResponseMessage());
844 } catch (Exception e2) {
846 err.setMessage("Unable to determine response message");
847 logger.error(e2.getMessage(), e2);
855 } catch ( Exception e ) {
856 logger.error(e.getMessage(), e);
862 public String doDeleteDr_Sub(DR_Sub delSub, ApiError err) {
863 logger.info( "entry: doDeleteDr_Sub() " );
864 byte[] postData = delSub.getBytes(provApi);
865 logger.info( "post fields=" + Arrays.toString(postData));
866 String responsemessage = null;
867 String responseBody = null;
871 uc.setRequestMethod("DELETE");
873 uc.setRequestProperty("Content-Type", subContentType);
874 uc.setRequestProperty( "charset", "utf-8");
875 uc.setRequestProperty( behalfHeader, "DGL" );
876 uc.setUseCaches(false);
877 uc.setDoOutput(true);
878 OutputStream os = null;
883 os = uc.getOutputStream();
884 //os.write( postData );
886 } catch (ProtocolException pe) {
887 // Rcvd error instead of 100-Continue
889 // work around glitch in Java 1.7.0.21 and likely others
890 // without this, Java will connect multiple times to the server to run the same request
891 uc.setDoOutput(false);
892 } catch (Exception e) {
893 logger.error(e.getMessage(), e);
895 } catch (Exception e) {
896 logger.info( "Exception: " + e.getMessage() );
899 rc = uc.getResponseCode();
900 logger.info( "http response code:" + rc );
901 responsemessage = uc.getResponseMessage();
902 logger.info( "responsemessage=" + responsemessage );
905 if (responsemessage == null) {
906 // work around for glitch in Java 1.7.0.21 and likely others
907 // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
908 String h0 = uc.getHeaderField(0);
910 int i = h0.indexOf(' ');
911 int j = h0.indexOf(' ', i + 1);
912 if (i != -1 && j != -1) {
913 responsemessage = h0.substring(j + 1);
919 responseBody = bodyToString( uc.getInputStream() );
920 logger.info( "responseBody=" + responseBody );
922 err.setMessage(responsemessage);
925 } catch (ConnectException ce) {
926 if ( unit_test.equals( "Yes" ) ) {
928 err.setMessage( "simulated response");
929 logger.info( "artificial 200 response from doDeleteDr_Sub because unit_test =" + unit_test );
931 errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
933 err.setMessage("Backend connection refused");
935 } catch (Exception e) {
936 if ( unit_test.equals( "Yes" ) ) {
938 err.setMessage( "simulated response");
939 logger.info( "artificial 200 response from doDeleteDr_Sub because unit_test =" + unit_test );
941 logger.error("Unable to read response ", e.getMessage());
952 // add double-quotes around a value
953 // hope his is easier to read than in-line escaping...
954 private String dq( String v ) {
955 return ( "\"" + v + "\"");
957 private String dq( String k, String v) {
958 return( dq(k) + ":" + dq(v));
960 private String dqc( String k, String v) {
961 return( dq(k) + ":" + dq(v) + ",");
964 private String dumpSimulation() {
965 logger.info( "enter dumpSimulation()");
966 String responseBody =
969 + "{" + dq( "suspend") + ":false,"
970 + dq( "groupid") + ":0,"
971 + dqc( "description", "Some description" )
972 + dqc( "version", "m1.1")
973 + dq( "authorization") + ":"
974 + "{" + dq( "endpoint_addrs" ) + ":[],"
975 + dq( "classification", "unclassified")
976 + dq( "endpoint_ids") + ":[{"
977 + dqc( "password", "dradmin" )
978 + dq( "id", "dradmin")
980 + dq( "last_mod") + ":1553738110000,"
981 + dq( "deleted") + ":false,"
982 + dq( "feedid") + ":1,"
983 + dqc( "name", "Default PM Feed")
984 + dq( "business_description") + ":\"\","
985 + dqc( "publisher", "onap")
986 + dq( "links") + ":{"
987 + dqc( "subscribe", "https://dmaap-dr-prov/subscribe/1")
988 + dqc( "log", "https://dmaap-dr-prov/feedlog/1")
989 + dqc( "publish", "https://dmaap-dr-prov/publish/1")
990 + dq( "self", "https:/dmaap-dr-prov/feed/1")
992 + dq( "created_date") + ":1553738110000 }"
994 + dq( "groups") + ":["
996 + dq( "subscriptions") + ":["
998 + dq( "ingress") + ":["
1000 + dq( "egress") + ":{"
1002 + dq( "routing") + ":["
1005 return responseBody;
1008 public String doGetDump( ApiError err ) {
1009 logger.info( "entry: doGetDump() " );
1011 String responsemessage = null;
1012 String responseBody = null;
1016 uc.setRequestMethod("GET");
1024 } catch (ProtocolException pe) {
1026 // Rcvd error instead of 100-Continue
1028 // work around glitch in Java 1.7.0.21 and likely others
1029 // without this, Java will connect multiple times to the server to run the same request
1030 uc.setDoOutput(false);
1031 } catch (Exception e) {
1032 logger.error(e.getMessage(), e);
1034 } catch (Exception e) {
1035 logger.info( "Exception: " + e.getMessage() );
1036 e.printStackTrace();
1039 rc = uc.getResponseCode();
1040 logger.info( "http response code:" + rc );
1041 responsemessage = uc.getResponseMessage();
1042 logger.info( "responsemessage=" + responsemessage );
1046 if (responsemessage == null) {
1048 // work around for glitch in Java 1.7.0.21 and likely others
1049 // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
1050 String h0 = uc.getHeaderField(0);
1052 int i = h0.indexOf(' ');
1053 int j = h0.indexOf(' ', i + 1);
1054 if (i != -1 && j != -1) {
1055 responsemessage = h0.substring(j + 1);
1060 err.setCode(rc); // may not really be an error, but we save rc
1062 responseBody = bodyToString( uc.getInputStream() );
1063 logger.info( "responseBody=" + responseBody );
1065 err.setMessage(responsemessage);
1069 } catch (ConnectException ce) {
1070 if ( unit_test.equals( "Yes" ) ) {
1072 err.setMessage( "simulated response");
1073 logger.info( "artificial 200 response from doGetNodes because unit_test =" + unit_test );
1074 responseBody = dumpSimulation();
1077 errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
1079 err.setMessage("Backend connection refused");
1080 logger.error(ce.getMessage(), ce);
1082 } catch (Exception e) {
1083 if ( unit_test.equals( "Yes" ) ) {
1085 err.setMessage( "simulated response");
1086 logger.info( "artificial 200 response from doGetNodes because unit_test =" + unit_test );
1087 responseBody = dumpSimulation();
1090 logger.error("Unable to read response ", e.getMessage());
1094 if ( uc != null ) uc.disconnect();
1097 return responseBody;