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 org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
26 import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;
27 import org.onap.dmaap.dbcapi.model.ApiError;
28 import org.onap.dmaap.dbcapi.model.DR_Sub;
29 import org.onap.dmaap.dbcapi.model.Feed;
30 import org.onap.dmaap.dbcapi.service.DmaapService;
31 import org.onap.dmaap.dbcapi.util.DmaapConfig;
33 import javax.net.ssl.HttpsURLConnection;
35 import java.net.ConnectException;
36 import java.net.ProtocolException;
37 import java.net.SocketException;
39 import java.util.Arrays;
43 public class DrProvConnection extends BaseLoggingClass {
46 private String provURL;
47 private String provApi;
48 private String behalfHeader;
49 private String feedContentType;
50 private String subContentType;
51 private String unit_test;
52 private String provURI;
54 private HttpsURLConnection uc;
57 public DrProvConnection() {
58 provURL = new DmaapService().getDmaap().getDrProvUrl();
59 if ( provURL.length() < 1 ) {
60 errorLogger.error( DmaapbcLogMessageEnum.PREREQ_DMAAP_OBJECT, "DmaapService().getDmaap().getDrProvUrl()");
62 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
63 provApi = p.getProperty( "DR.provApi", "ONAP" );
64 behalfHeader = p.getProperty( "DR.onBehalfHeader", "X-DMAAP-DR-ON-BEHALF-OF");
65 feedContentType = p.getProperty( "DR.feedContentType", "application/vnd.dmaap-dr.feed");
66 subContentType = p.getProperty( "DR.subContentType", "application/vnd.dmaap-dr.subscription");
67 provURI = p.getProperty( "DR.ProvisioningURI", "/internal/prov");
68 logger.info( "provURL=" + provURL + " provApi=" + provApi + " behalfHeader=" + behalfHeader
69 + " feedContentType=" + feedContentType + " subContentType=" + subContentType );
70 unit_test = p.getProperty( "UnitTest", "No" );
74 public boolean makeFeedConnection() {
75 return makeConnection( provURL );
77 public boolean makeFeedConnection(String feedId) {
78 return makeConnection( provURL + "/feed/" + feedId );
80 public boolean makeSubPostConnection( String subURL ) {
81 String[] parts = subURL.split("/");
82 String revisedURL = provURL + "/" + parts[3] + "/" + parts[4];
83 logger.info( "mapping " + subURL + " to " + revisedURL );
84 return makeConnection( revisedURL );
86 public boolean makeSubPutConnection( String subId ) {
87 String revisedURL = provURL + "/subs/" + subId;
88 logger.info( "mapping " + subId + " to " + revisedURL );
89 return makeConnection( revisedURL );
92 public boolean makeIngressConnection( String feed, String user, String subnet, String nodep ) {
93 String uri = String.format("/internal/route/ingress/?feed=%s&user=%s&subnet=%s&nodepatt=%s",
94 feed, user, subnet, nodep );
95 return makeConnection( provURL + uri );
97 public boolean makeEgressConnection( String sub, String nodep ) {
98 String uri = String.format("/internal/route/egress/?sub=%s&node=%s",
100 return makeConnection( provURL + uri );
102 public boolean makeDumpConnection() {
103 String url = provURL + provURI;
104 return makeConnection( url );
106 public boolean makeNodesConnection( String varName ) {
108 String uri = String.format("/internal/api/%s", varName);
109 return makeConnection( provURL + uri );
112 public boolean makeNodesConnection( String varName, String val ) {
117 String cv = val.replaceAll("\\|", "%7C");
118 String uri = String.format( "/internal/api/%s?val=%s", varName, cv );
120 return makeConnection( provURL + uri );
123 private boolean makeConnection( String pURL ) {
126 URL u = new URL( pURL );
127 uc = (HttpsURLConnection) u.openConnection();
128 uc.setInstanceFollowRedirects(false);
129 logger.info( "successful connect to " + pURL );
131 } catch (Exception e) {
132 errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_ERROR, pURL, e.getMessage() );
138 public String bodyToString( InputStream is ) {
139 logger.info( "is=" + is );
140 StringBuilder sb = new StringBuilder();
141 BufferedReader br = new BufferedReader( new InputStreamReader(is));
144 while ((line = br.readLine()) != null ) {
147 } catch (IOException ex ) {
148 errorLogger.error( DmaapbcLogMessageEnum.IO_EXCEPTION, ex.getMessage());
151 return sb.toString();
155 public String doPostFeed( Feed postFeed, ApiError err ) {
157 byte[] postData = postFeed.getBytes();
158 logger.info( "post fields=" + Arrays.toString(postData) );
159 String responsemessage = null;
160 String responseBody = null;
163 logger.info( "uc=" + uc );
164 uc.setRequestMethod("POST");
165 uc.setRequestProperty("Content-Type", feedContentType);
166 uc.setRequestProperty( "charset", "utf-8");
167 uc.setRequestProperty( behalfHeader, postFeed.getOwner() );
168 uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
169 uc.setUseCaches(false);
170 uc.setDoOutput(true);
171 OutputStream os = null;
176 os = uc.getOutputStream();
177 os.write( postData );
179 } catch (ProtocolException pe) {
180 // Rcvd error instead of 100-Continue
182 // work around glitch in Java 1.7.0.21 and likely others
183 // without this, Java will connect multiple times to the server to run the same request
184 uc.setDoOutput(false);
185 } catch (Exception e) {
188 rc = uc.getResponseCode();
189 logger.info( "http response code:" + rc );
190 responsemessage = uc.getResponseMessage();
191 logger.info( "responsemessage=" + responsemessage );
194 if (responsemessage == null) {
195 // work around for glitch in Java 1.7.0.21 and likely others
196 // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
197 String h0 = uc.getHeaderField(0);
199 int i = h0.indexOf(' ');
200 int j = h0.indexOf(' ', i + 1);
201 if (i != -1 && j != -1) {
202 responsemessage = h0.substring(j + 1);
207 responseBody = bodyToString( uc.getInputStream() );
208 logger.info( "responseBody=" + responseBody );
212 err.setMessage(responsemessage);
215 } catch (ConnectException ce) {
216 errorLogger.error(DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
218 err.setMessage("Backend connection refused");
219 } catch (SocketException se) {
220 errorLogger.error( DmaapbcLogMessageEnum.SOCKET_EXCEPTION, se.getMessage(), "response from prov server" );
222 err.setMessage( "Unable to read response from DR");
223 } catch (Exception e) {
224 if ( unit_test.equals( "Yes" ) ) {
226 err.setMessage( "simulated response");
227 logger.info( "artificial 200 response from doPostFeed because unit_test =" + unit_test );
229 logger.warn("Unable to read response " );
230 errorLogger.error("Unable to read response ", e.getMessage());
232 err.setCode( uc.getResponseCode());
233 err.setMessage(uc.getResponseMessage());
234 } catch (Exception e2) {
236 err.setMessage("Unable to determine response message");
243 } catch ( Exception e ) {
244 logger.error(e.getMessage(), e);
252 // the POST for /internal/route/ingress doesn't return any data, so needs a different function
253 // the POST for /internal/route/egress doesn't return any data, so needs a different function
254 public int doXgressPost( ApiError err ) {
256 String responsemessage = null;
260 uc.setRequestMethod("POST");
266 } catch (ProtocolException pe) {
267 // Rcvd error instead of 100-Continue
269 // work around glitch in Java 1.7.0.21 and likely others
270 // without this, Java will connect multiple times to the server to run the same request
271 uc.setDoOutput(false);
272 } catch (Exception e) {
273 logger.error(e.getMessage(), e);
276 rc = uc.getResponseCode();
277 logger.info( "http response code:" + rc );
278 responsemessage = uc.getResponseMessage();
279 logger.info( "responsemessage=" + responsemessage );
283 if (rc < 200 || rc >= 300 ) {
285 err.setMessage(responsemessage);
287 } catch (Exception e) {
288 if ( unit_test.equals( "Yes" ) ) {
290 err.setMessage( "simulated response");
291 logger.info( "artificial 200 response from doXgressPost because unit_test =" + unit_test );
293 logger.error("Unable to read response " );
294 logger.error(e.getMessage(), e);
300 } catch ( Exception e ) {
301 logger.error(e.getMessage(), e);
309 public String doPostDr_Sub( DR_Sub postSub, ApiError err ) {
310 logger.info( "entry: doPostDr_Sub() " );
311 byte[] postData = postSub.getBytes(provApi );
312 logger.info( "post fields=" + postData );
313 String responsemessage = null;
314 String responseBody = null;
318 uc.setRequestMethod("POST");
320 uc.setRequestProperty("Content-Type", subContentType );
321 uc.setRequestProperty( "charset", "utf-8");
322 uc.setRequestProperty( behalfHeader, "DGL" );
323 uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
324 uc.setUseCaches(false);
325 uc.setDoOutput(true);
326 OutputStream os = null;
331 os = uc.getOutputStream();
332 os.write( postData );
334 } catch (ProtocolException pe) {
335 // Rcvd error instead of 100-Continue
337 // work around glitch in Java 1.7.0.21 and likely others
338 // without this, Java will connect multiple times to the server to run the same request
339 uc.setDoOutput(false);
340 } catch (Exception e) {
341 logger.error(e.getMessage(), e);
344 rc = uc.getResponseCode();
345 logger.info( "http response code:" + rc );
346 responsemessage = uc.getResponseMessage();
347 logger.info( "responsemessage=" + responsemessage );
350 if (responsemessage == null) {
351 // work around for glitch in Java 1.7.0.21 and likely others
352 // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
353 String h0 = uc.getHeaderField(0);
355 int i = h0.indexOf(' ');
356 int j = h0.indexOf(' ', i + 1);
357 if (i != -1 && j != -1) {
358 responsemessage = h0.substring(j + 1);
363 responseBody = bodyToString( uc.getInputStream() );
364 logger.info( "responseBody=" + responseBody );
368 err.setMessage(responsemessage);
371 } catch (Exception e) {
372 if ( unit_test.equals( "Yes" ) ) {
374 err.setMessage( "simulated response");
375 logger.info( "artificial 200 response from doPostDr_Sub because unit_test =" + unit_test );
377 logger.error("Unable to read response ", e.getMessage());
383 } catch ( Exception e ) {
384 logger.error(e.getMessage(), e);
392 public String doPutFeed(Feed putFeed, ApiError err) {
393 byte[] postData = putFeed.getBytes();
394 logger.info( "post fields=" + Arrays.toString(postData) );
395 String responsemessage = null;
396 String responseBody = null;
399 logger.info( "uc=" + uc );
400 uc.setRequestMethod("PUT");
401 uc.setRequestProperty("Content-Type", feedContentType );
402 uc.setRequestProperty( "charset", "utf-8");
403 uc.setRequestProperty( behalfHeader, putFeed.getOwner() );
404 uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
405 uc.setUseCaches(false);
406 uc.setDoOutput(true);
407 OutputStream os = null;
412 os = uc.getOutputStream();
413 os.write( postData );
415 } catch (ProtocolException pe) {
416 // Rcvd error instead of 100-Continue
418 // work around glitch in Java 1.7.0.21 and likely others
419 // without this, Java will connect multiple times to the server to run the same request
420 uc.setDoOutput(false);
421 } catch (Exception e) {
422 logger.error(e.getMessage(), e);
425 rc = uc.getResponseCode();
426 logger.info( "http response code:" + rc );
427 responsemessage = uc.getResponseMessage();
428 logger.info( "responsemessage=" + responsemessage );
431 if (responsemessage == null) {
432 // work around for glitch in Java 1.7.0.21 and likely others
433 // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
434 String h0 = uc.getHeaderField(0);
436 int i = h0.indexOf(' ');
437 int j = h0.indexOf(' ', i + 1);
438 if (i != -1 && j != -1) {
439 responsemessage = h0.substring(j + 1);
443 if (rc >= 200 && rc < 300 ) {
444 responseBody = bodyToString( uc.getInputStream() );
445 logger.info( "responseBody=" + responseBody );
447 } else if ( rc == 404 ) {
449 err.setFields( "feedid");
450 String message = "FeedId " + putFeed.getFeedId() + " not found on DR to update. Out-of-sync condition?";
451 err.setMessage( message );
452 errorLogger.error( DmaapbcLogMessageEnum.PROV_OUT_OF_SYNC, "Feed", putFeed.getFeedId() );
456 err.setMessage(responsemessage);
459 } catch (ConnectException ce) {
460 if ( unit_test.equals( "Yes" ) ) {
462 err.setMessage( "simulated response");
463 logger.info( "artificial 200 response from doPutFeed because unit_test =" + unit_test );
465 errorLogger.error(DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage());
467 err.setMessage("Backend connection refused");
469 } catch (SocketException se) {
470 errorLogger.error( DmaapbcLogMessageEnum.SOCKET_EXCEPTION, se.getMessage(), "response from Prov server" );
472 err.setMessage( "Unable to read response from DR");
473 } catch (Exception e) {
474 if ( unit_test.equals( "Yes" ) ) {
476 err.setMessage( "simulated response");
477 logger.info( "artificial 200 response from doPutFeed because unit_test =" + unit_test );
479 logger.warn("Unable to read response " );
480 logger.error(e.getMessage(), e);
483 err.setCode( uc.getResponseCode());
484 err.setMessage(uc.getResponseMessage());
485 } catch (Exception e2) {
487 err.setMessage("Unable to determine response message");
488 logger.error(e2.getMessage(), e2);
493 } catch ( Exception e ) {
494 logger.error(e.getMessage(), e);
499 public String doPutDr_Sub(DR_Sub postSub, ApiError err) {
500 logger.info( "entry: doPutDr_Sub() " );
501 byte[] postData = postSub.getBytes(provApi);
502 logger.info( "post fields=" + postData );
503 String responsemessage = null;
504 String responseBody = null;
508 uc.setRequestMethod("PUT");
510 uc.setRequestProperty("Content-Type", subContentType );
511 uc.setRequestProperty( "charset", "utf-8");
512 uc.setRequestProperty( behalfHeader, "DGL" );
513 uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
514 uc.setUseCaches(false);
515 uc.setDoOutput(true);
516 OutputStream os = null;
521 os = uc.getOutputStream();
522 os.write( postData );
524 } catch (ProtocolException pe) {
525 // Rcvd error instead of 100-Continue
527 // work around glitch in Java 1.7.0.21 and likely others
528 // without this, Java will connect multiple times to the server to run the same request
529 uc.setDoOutput(false);
530 } catch (Exception e) {
531 logger.error(e.getMessage(), e);
534 rc = uc.getResponseCode();
535 logger.info( "http response code:" + rc );
536 responsemessage = uc.getResponseMessage();
537 logger.info( "responsemessage=" + responsemessage );
540 if (responsemessage == null) {
541 // work around for glitch in Java 1.7.0.21 and likely others
542 // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
543 String h0 = uc.getHeaderField(0);
545 int i = h0.indexOf(' ');
546 int j = h0.indexOf(' ', i + 1);
547 if (i != -1 && j != -1) {
548 responsemessage = h0.substring(j + 1);
553 responseBody = bodyToString( uc.getInputStream() );
554 logger.info( "responseBody=" + responseBody );
558 err.setMessage(responsemessage);
561 } catch (ConnectException ce) {
562 errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
564 err.setMessage("Backend connection refused");
565 logger.error(ce.getMessage(), ce);
566 } catch (Exception e) {
567 if ( unit_test.equals( "Yes" ) ) {
569 err.setMessage( "simulated response");
570 logger.info( "artificial 200 response from doPutDr_Sub because unit_test =" + unit_test );
572 logger.error("Unable to read response " );
573 logger.error(e.getMessage(), e);
584 public String doGetNodes( ApiError err ) {
585 logger.info( "entry: doGetNodes() " );
586 //byte[] postData = postSub.getBytes();
587 //logger.info( "get fields=" + postData );
588 String responsemessage = null;
589 String responseBody = null;
593 uc.setRequestMethod("GET");
601 } catch (ProtocolException pe) {
603 // Rcvd error instead of 100-Continue
605 // work around glitch in Java 1.7.0.21 and likely others
606 // without this, Java will connect multiple times to the server to run the same request
607 uc.setDoOutput(false);
608 } catch (Exception e) {
609 logger.error(e.getMessage(), e);
613 rc = uc.getResponseCode();
614 logger.info( "http response code:" + rc );
615 responsemessage = uc.getResponseMessage();
616 logger.info( "responsemessage=" + responsemessage );
620 if (responsemessage == null) {
622 // work around for glitch in Java 1.7.0.21 and likely others
623 // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
624 String h0 = uc.getHeaderField(0);
626 int i = h0.indexOf(' ');
627 int j = h0.indexOf(' ', i + 1);
628 if (i != -1 && j != -1) {
629 responsemessage = h0.substring(j + 1);
634 err.setCode(rc); // may not really be an error, but we save rc
636 responseBody = bodyToString( uc.getInputStream() );
637 logger.info( "responseBody=" + responseBody );
639 err.setMessage(responsemessage);
643 } catch (ConnectException ce) {
644 if ( unit_test.equals( "Yes" ) ) {
646 err.setMessage( "simulated response");
647 logger.info( "artificial 200 response from doGetNodes because unit_test =" + unit_test );
649 errorLogger.error(DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage());
651 err.setMessage("Backend connection refused");
652 logger.error(ce.getMessage(), ce);
654 } catch (Exception e) {
655 if ( unit_test.equals( "Yes" ) ) {
657 err.setMessage( "simulated response");
658 logger.info( "artificial 200 response from doGetNodes because unit_test =" + unit_test );
660 logger.error("Unable to read response ", e.getMessage());
664 if ( uc != null ) uc.disconnect();
670 public String doPutNodes( ApiError err ) {
671 logger.info( "entry: doPutNodes() " );
672 //byte[] postData = nodeList.getBytes();
673 //logger.info( "get fields=" + postData );
674 String responsemessage = null;
675 String responseBody = null;
679 uc.setRequestMethod("PUT");
681 //uc.setRequestProperty("Content-Type", subContentType );
682 //uc.setRequestProperty( "charset", "utf-8");
683 //uc.setRequestProperty( behalfHeader, "DGL" );
684 //uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
685 uc.setUseCaches(false);
686 //uc.setDoOutput(true);
687 OutputStream os = null;
692 //os = uc.getOutputStream();
693 //os.write( postData );
695 } catch (ProtocolException pe) {
696 // Rcvd error instead of 100-Continue
698 // work around glitch in Java 1.7.0.21 and likely others
699 // without this, Java will connect multiple times to the server to run the same request
700 uc.setDoOutput(false);
701 } catch (Exception e) {
704 rc = uc.getResponseCode();
705 logger.info( "http response code:" + rc );
706 responsemessage = uc.getResponseMessage();
707 logger.info( "responsemessage=" + responsemessage );
710 if (responsemessage == null) {
711 // work around for glitch in Java 1.7.0.21 and likely others
712 // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
713 String h0 = uc.getHeaderField(0);
715 int i = h0.indexOf(' ');
716 int j = h0.indexOf(' ', i + 1);
717 if (i != -1 && j != -1) {
718 responsemessage = h0.substring(j + 1);
724 responseBody = bodyToString( uc.getInputStream() );
725 logger.info( "responseBody=" + responseBody );
729 err.setMessage(responsemessage);
732 } catch (Exception e) {
733 if ( unit_test.equals( "Yes" ) ) {
735 err.setMessage( "simulated response");
736 logger.info( "artificial 200 response from doPutNodes because unit_test =" + unit_test );
738 logger.error("Unable to read response ", e.getMessage());
749 public String doDeleteFeed(Feed putFeed, ApiError err) {
750 //byte[] postData = putFeed.getBytes();
751 //logger.info( "post fields=" + postData.toString() );
752 String responsemessage = null;
753 String responseBody = null;
756 logger.info( "uc=" + uc );
757 uc.setRequestMethod("DELETE");
758 uc.setRequestProperty("Content-Type", feedContentType );
759 uc.setRequestProperty( "charset", "utf-8");
760 uc.setRequestProperty( behalfHeader, putFeed.getOwner() );
761 //uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
762 uc.setUseCaches(false);
763 uc.setDoOutput(true);
764 OutputStream os = null;
769 os = uc.getOutputStream();
770 //os.write( postData );
772 } catch (ProtocolException pe) {
773 // Rcvd error instead of 100-Continue
775 // work around glitch in Java 1.7.0.21 and likely others
776 // without this, Java will connect multiple times to the server to run the same request
777 uc.setDoOutput(false);
778 } catch (Exception e) {
779 logger.error(e.getMessage(), e);
782 rc = uc.getResponseCode();
783 logger.info( "http response code:" + rc );
784 responsemessage = uc.getResponseMessage();
785 logger.info( "responsemessage=" + responsemessage );
788 if (responsemessage == null) {
789 // work around for glitch in Java 1.7.0.21 and likely others
790 // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
791 String h0 = uc.getHeaderField(0);
793 int i = h0.indexOf(' ');
794 int j = h0.indexOf(' ', i + 1);
795 if (i != -1 && j != -1) {
796 responsemessage = h0.substring(j + 1);
800 if (rc >= 200 && rc < 300 ) {
801 responseBody = bodyToString( uc.getInputStream() );
802 logger.info( "responseBody=" + responseBody );
804 } else if ( rc == 404 ) {
806 err.setFields( "feedid");
807 String message = "FeedId " + putFeed.getFeedId() + " not found on DR to update. Out-of-sync condition?";
808 err.setMessage( message );
809 errorLogger.error( DmaapbcLogMessageEnum.PROV_OUT_OF_SYNC, "Feed", putFeed.getFeedId() );
813 err.setMessage(responsemessage);
816 } catch (ConnectException ce) {
817 errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
819 err.setMessage("Backend connection refused");
820 logger.error(ce.getMessage(), ce);
821 } catch (SocketException se) {
822 errorLogger.error( DmaapbcLogMessageEnum.SOCKET_EXCEPTION, se.getMessage(), "response from Prov server" );
824 err.setMessage( "Unable to read response from DR");
825 logger.error(se.getMessage(), se);
826 } catch (Exception e) {
827 if ( unit_test.equals( "Yes" ) ) {
829 err.setMessage( "simulated response");
830 logger.info( "artificial 200 response from doDeleteFeed because unit_test =" + unit_test );
832 logger.warn("Unable to read response " );
833 logger.error(e.getMessage(), e);
835 err.setCode( uc.getResponseCode());
836 err.setMessage(uc.getResponseMessage());
837 } catch (Exception e2) {
839 err.setMessage("Unable to determine response message");
840 logger.error(e2.getMessage(), e2);
848 } catch ( Exception e ) {
849 logger.error(e.getMessage(), e);
855 public String doDeleteDr_Sub(DR_Sub delSub, ApiError err) {
856 logger.info( "entry: doDeleteDr_Sub() " );
857 byte[] postData = delSub.getBytes(provApi);
858 logger.info( "post fields=" + postData );
859 String responsemessage = null;
860 String responseBody = null;
864 uc.setRequestMethod("DELETE");
866 uc.setRequestProperty("Content-Type", subContentType);
867 uc.setRequestProperty( "charset", "utf-8");
868 uc.setRequestProperty( behalfHeader, "DGL" );
869 //uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
870 uc.setUseCaches(false);
871 uc.setDoOutput(true);
872 OutputStream os = null;
877 os = uc.getOutputStream();
878 //os.write( postData );
880 } catch (ProtocolException pe) {
881 // Rcvd error instead of 100-Continue
883 // work around glitch in Java 1.7.0.21 and likely others
884 // without this, Java will connect multiple times to the server to run the same request
885 uc.setDoOutput(false);
886 } catch (Exception e) {
887 logger.error(e.getMessage(), e);
890 rc = uc.getResponseCode();
891 logger.info( "http response code:" + rc );
892 responsemessage = uc.getResponseMessage();
893 logger.info( "responsemessage=" + responsemessage );
896 if (responsemessage == null) {
897 // work around for glitch in Java 1.7.0.21 and likely others
898 // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
899 String h0 = uc.getHeaderField(0);
901 int i = h0.indexOf(' ');
902 int j = h0.indexOf(' ', i + 1);
903 if (i != -1 && j != -1) {
904 responsemessage = h0.substring(j + 1);
910 responseBody = bodyToString( uc.getInputStream() );
911 logger.info( "responseBody=" + responseBody );
913 err.setMessage(responsemessage);
916 } catch (ConnectException ce) {
917 if ( unit_test.equals( "Yes" ) ) {
919 err.setMessage( "simulated response");
920 logger.info( "artificial 200 response from doDeleteDr_Sub because unit_test =" + unit_test );
922 errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
924 err.setMessage("Backend connection refused");
926 } catch (Exception e) {
927 if ( unit_test.equals( "Yes" ) ) {
929 err.setMessage( "simulated response");
930 logger.info( "artificial 200 response from doDeleteDr_Sub because unit_test =" + unit_test );
932 logger.error("Unable to read response ", e.getMessage());
943 // add double-quotes around a value
944 // hope his is easier to read than in-line escaping...
945 private String dq( String v ) {
946 return ( "\"" + v + "\"");
948 private String dq( String k, String v) {
949 return( dq(k) + ":" + dq(v));
951 private String dqc( String k, String v) {
952 return( dq(k) + ":" + dq(v) + ",");
955 private String dumpSimulation() {
956 logger.info( "enter dumpSimulation()");
957 String responseBody =
960 + "{" + dq( "suspend") + ":false,"
961 + dq( "groupid") + ":0,"
962 + dqc( "description", "Some description" )
963 + dqc( "version", "m1.1")
964 + dq( "authorization") + ":"
965 + "{" + dq( "endpoint_addrs" ) + ":[],"
966 + dq( "classification", "unclassified")
967 + dq( "endpoint_ids") + ":[{"
968 + dqc( "password", "dradmin" )
969 + dq( "id", "dradmin")
971 + dq( "last_mod") + ":1553738110000,"
972 + dq( "deleted") + ":false,"
973 + dq( "feedid") + ":1,"
974 + dqc( "name", "Default PM Feed")
975 + dq( "business_description") + ":\"\","
976 + dqc( "publisher", "onap")
977 + dq( "links") + ":{"
978 + dqc( "subscribe", "https://dmaap-dr-prov/subscribe/1")
979 + dqc( "log", "https://dmaap-dr-prov/feedlog/1")
980 + dqc( "publish", "https://dmaap-dr-prov/publish/1")
981 + dq( "self", "https:/dmaap-dr-prov/feed/1")
983 + dq( "created_date") + ":1553738110000 }"
985 + dq( "groups") + ":["
987 + dq( "subscriptions") + ":["
989 + dq( "ingress") + ":["
991 + dq( "egress") + ":{"
993 + dq( "routing") + ":["
999 public String doGetDump( ApiError err ) {
1000 logger.info( "entry: doGetDump() " );
1002 String responsemessage = null;
1003 String responseBody = null;
1007 uc.setRequestMethod("GET");
1015 } catch (ProtocolException pe) {
1017 // Rcvd error instead of 100-Continue
1019 // work around glitch in Java 1.7.0.21 and likely others
1020 // without this, Java will connect multiple times to the server to run the same request
1021 uc.setDoOutput(false);
1022 } catch (Exception e) {
1023 logger.error(e.getMessage(), e);
1027 rc = uc.getResponseCode();
1028 logger.info( "http response code:" + rc );
1029 responsemessage = uc.getResponseMessage();
1030 logger.info( "responsemessage=" + responsemessage );
1034 if (responsemessage == null) {
1036 // work around for glitch in Java 1.7.0.21 and likely others
1037 // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
1038 String h0 = uc.getHeaderField(0);
1040 int i = h0.indexOf(' ');
1041 int j = h0.indexOf(' ', i + 1);
1042 if (i != -1 && j != -1) {
1043 responsemessage = h0.substring(j + 1);
1048 err.setCode(rc); // may not really be an error, but we save rc
1050 responseBody = bodyToString( uc.getInputStream() );
1051 logger.info( "responseBody=" + responseBody );
1053 err.setMessage(responsemessage);
1057 } catch (ConnectException ce) {
1058 if ( unit_test.equals( "Yes" ) ) {
1060 err.setMessage( "simulated response");
1061 logger.info( "artificial 200 response from doGetNodes because unit_test =" + unit_test );
1062 responseBody = dumpSimulation();
1065 errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
1067 err.setMessage("Backend connection refused");
1068 logger.error(ce.getMessage(), ce);
1070 } catch (Exception e) {
1071 if ( unit_test.equals( "Yes" ) ) {
1073 err.setMessage( "simulated response");
1074 logger.info( "artificial 200 response from doGetNodes because unit_test =" + unit_test );
1075 responseBody = dumpSimulation();
1078 logger.error("Unable to read response ", e.getMessage());
1082 if ( uc != null ) uc.disconnect();
1085 return responseBody;