Updates for more code coverage
[dmaap/dbcapi.git] / src / main / java / org / onap / dmaap / dbcapi / client / DrProvConnection.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dmaap
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  *
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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=========================================================
21  */
22
23 package org.onap.dmaap.dbcapi.client;
24
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;
32
33 import javax.net.ssl.HttpsURLConnection;
34 import java.io.*;
35 import java.net.ConnectException;
36 import java.net.ProtocolException;
37 import java.net.SocketException;
38 import java.net.URL;
39 import java.util.Arrays;
40
41
42
43 public class DrProvConnection extends BaseLoggingClass {
44            
45    
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         
53         private HttpsURLConnection uc;
54
55
56         public DrProvConnection() {
57                 provURL = new DmaapService().getDmaap().getDrProvUrl();
58                 if ( provURL.length() < 1 ) {
59                         errorLogger.error( DmaapbcLogMessageEnum.PREREQ_DMAAP_OBJECT, "getDrProvUrl");
60                 }
61                 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
62                 provApi = p.getProperty( "DR.provApi", "ONAP" );
63                 behalfHeader = p.getProperty( "DR.onBehalfHeader", "X-DMAAP-DR-ON-BEHALF-OF");
64                 feedContentType = p.getProperty( "DR.feedContentType", "application/vnd.dmaap-dr.feed");
65                 subContentType = p.getProperty( "DR.subContentType", "application/vnd.dmaap-dr.subscription");
66                 logger.info( "provURL=" + provURL + " provApi=" + provApi + " behalfHeader=" + behalfHeader
67                                 + " feedContentType=" + feedContentType + " subContentType=" + subContentType );
68                 unit_test = p.getProperty( "UnitTest", "No" );
69                         
70         }
71         
72         public boolean makeFeedConnection() {
73                 return makeConnection( provURL );
74         }
75         public boolean makeFeedConnection(String feedId) {
76                 return makeConnection( provURL + "/feed/" + feedId );   
77         }
78         public boolean makeSubPostConnection( String subURL ) {
79                 String[] parts = subURL.split("/");
80                 String revisedURL = provURL + "/" + parts[3] + "/" + parts[4];
81                 logger.info( "mapping " + subURL + " to " + revisedURL );
82                 return makeConnection( revisedURL );
83         }
84         public boolean makeSubPutConnection( String subId ) {
85                 String revisedURL = provURL + "/subs/" + subId;
86                 logger.info( "mapping " + subId + " to " + revisedURL );
87                 return makeConnection( revisedURL );
88         }
89
90         public boolean makeIngressConnection( String feed, String user, String subnet, String nodep ) {
91                 String uri = String.format("/internal/route/ingress/?feed=%s&user=%s&subnet=%s&nodepatt=%s", 
92                                         feed, user, subnet, nodep );
93                 return makeConnection( provURL + uri );
94         }
95         public boolean makeEgressConnection( String sub, String nodep ) {
96                 String uri = String.format("/internal/route/egress/?sub=%s&node=%s", 
97                                         sub,  nodep );
98                 return makeConnection( provURL + uri );
99         }
100         public boolean makeNodesConnection( String varName ) {
101                 
102                 String uri = String.format("/internal/api/%s", varName);
103                 return makeConnection( provURL + uri );
104         }
105         
106         public boolean makeNodesConnection( String varName, String val ) {
107
108                 if ( val == null ) {
109                         return false;
110                 } 
111                 String cv = val.replaceAll("\\|", "%7C");
112                 String uri = String.format( "/internal/api/%s?val=%s", varName, cv );
113
114                 return makeConnection( provURL + uri );
115         }
116         
117         private boolean makeConnection( String pURL ) {
118         
119                 try {
120                         URL u = new URL( pURL );
121                         uc = (HttpsURLConnection) u.openConnection();
122                         uc.setInstanceFollowRedirects(false);
123                         logger.info( "successful connect to " + pURL );
124                         return(true);
125                 } catch (Exception e) {
126                         errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_ERROR,  pURL, e.getMessage() );
127             e.printStackTrace();
128             return(false);
129         }
130
131         }
132         
133         public String bodyToString( InputStream is ) {
134                 logger.info( "is=" + is );
135                 StringBuilder sb = new StringBuilder();
136                 BufferedReader br = new BufferedReader( new InputStreamReader(is));
137                 String line;
138                 try {
139                         while ((line = br.readLine()) != null ) {
140                                 sb.append( line );
141                         }
142                 } catch (IOException ex ) {
143                         errorLogger.error( DmaapbcLogMessageEnum.IO_EXCEPTION, ex.getMessage());
144                 }
145                         
146                 return sb.toString();
147         }
148         
149
150         public  String doPostFeed( Feed postFeed, ApiError err ) {
151
152                 byte[] postData = postFeed.getBytes();
153                 logger.info( "post fields=" + Arrays.toString(postData) );
154                 String responsemessage = null;
155                 String responseBody = null;
156
157                 try {
158                         logger.info( "uc=" + uc );
159                         uc.setRequestMethod("POST");
160                         uc.setRequestProperty("Content-Type", feedContentType);
161                         uc.setRequestProperty( "charset", "utf-8");
162                         uc.setRequestProperty( behalfHeader, postFeed.getOwner() );
163                         uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
164                         uc.setUseCaches(false);
165                         uc.setDoOutput(true);
166                         OutputStream os = null;
167                         int rc = -1;
168                         
169                         try {
170                  uc.connect();
171                  os = uc.getOutputStream();
172                  os.write( postData );
173
174             } catch (ProtocolException pe) {
175                  // Rcvd error instead of 100-Continue
176                  try {
177                      // work around glitch in Java 1.7.0.21 and likely others
178                      // without this, Java will connect multiple times to the server to run the same request
179                      uc.setDoOutput(false);
180                  } catch (Exception e) {
181                  }
182             }
183                         rc = uc.getResponseCode();
184                         logger.info( "http response code:" + rc );
185             responsemessage = uc.getResponseMessage();
186             logger.info( "responsemessage=" + responsemessage );
187
188
189             if (responsemessage == null) {
190                  // work around for glitch in Java 1.7.0.21 and likely others
191                  // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
192                  String h0 = uc.getHeaderField(0);
193                  if (h0 != null) {
194                      int i = h0.indexOf(' ');
195                      int j = h0.indexOf(' ', i + 1);
196                      if (i != -1 && j != -1) {
197                          responsemessage = h0.substring(j + 1);
198                      }
199                  }
200             }
201             if (rc == 201 ) {
202                         responseBody = bodyToString( uc.getInputStream() );
203                         logger.info( "responseBody=" + responseBody );
204
205             } else {
206                 err.setCode( rc );
207                 err.setMessage(responsemessage);
208             }
209             
210                 } catch (ConnectException ce) {
211                         errorLogger.error(DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
212             err.setCode( 500 );
213                 err.setMessage("Backend connection refused");
214                 } catch (SocketException se) {
215                         errorLogger.error( DmaapbcLogMessageEnum.SOCKET_EXCEPTION, se.getMessage(), "response from prov server" );
216                         err.setCode( 500 );
217                         err.setMessage( "Unable to read response from DR");
218         } catch (Exception e) {
219                 if ( unit_test.equals( "Yes" ) ) {
220                                 err.setCode(200);
221                                 err.setMessage( "simulated response");
222                                 logger.info( "artificial 200 response from doPostFeed because unit_test =" + unit_test );
223                 } else {
224                     logger.warn("Unable to read response  " );
225                     e.printStackTrace();
226                     try {
227                             err.setCode( uc.getResponseCode());
228                             err.setMessage(uc.getResponseMessage());
229                     } catch (Exception e2) {
230                         err.setCode( 500 );
231                         err.setMessage("Unable to determine response message");
232                     }
233                 }
234         } 
235                 finally {
236                         try {
237                                 uc.disconnect();
238                         } catch ( Exception e ) {}
239                 }
240                 return responseBody;
241
242         }
243
244         
245         // the POST for /internal/route/ingress doesn't return any data, so needs a different function
246         // the POST for /internal/route/egress doesn't return any data, so needs a different function   
247         public int doXgressPost( ApiError err ) {
248                 
249                 String responsemessage = null;
250                 int rc = -1;
251
252                 try {
253                         uc.setRequestMethod("POST");
254
255
256                         try {
257                  uc.connect();
258
259             } catch (ProtocolException pe) {
260                  // Rcvd error instead of 100-Continue
261                  try {
262                      // work around glitch in Java 1.7.0.21 and likely others
263                      // without this, Java will connect multiple times to the server to run the same request
264                      uc.setDoOutput(false);
265                  } catch (Exception e) {
266                         logger.error(e.getMessage(), e);
267                  }
268             }
269                         rc = uc.getResponseCode();
270                         logger.info( "http response code:" + rc );
271             responsemessage = uc.getResponseMessage();
272             logger.info( "responsemessage=" + responsemessage );
273
274
275
276             if (rc < 200 || rc >= 300 ) {
277                 err.setCode( rc );
278                 err.setMessage(responsemessage);
279             }
280                 } catch (Exception e) {
281                 if ( unit_test.equals( "Yes" ) ) {
282                                 err.setCode(200);
283                                 err.setMessage( "simulated response");
284                                 logger.info( "artificial 200 response from doXgressPost because unit_test =" + unit_test );
285                 } else {
286                     logger.error("Unable to read response  " );
287                     e.printStackTrace();
288                 }
289         }               
290         finally {
291                         try {
292                                 uc.disconnect();
293                         } catch ( Exception e ) {
294                                 logger.error(e.getMessage(), e);
295                         }
296                 }
297         
298                 return rc;
299
300         }
301         
302         public String doPostDr_Sub( DR_Sub postSub, ApiError err ) {
303                 logger.info( "entry: doPostDr_Sub() "  );
304                 byte[] postData = postSub.getBytes(provApi );
305                 logger.info( "post fields=" + postData );
306                 String responsemessage = null;
307                 String responseBody = null;
308
309                 try {
310         
311                         uc.setRequestMethod("POST");
312                 
313                         uc.setRequestProperty("Content-Type", subContentType );
314                         uc.setRequestProperty( "charset", "utf-8");
315                         uc.setRequestProperty( behalfHeader, "DGL" );
316                         uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
317                         uc.setUseCaches(false);
318                         uc.setDoOutput(true);
319                         OutputStream os = null;
320                         int rc = -1;
321                         
322                         try {
323                  uc.connect();
324                  os = uc.getOutputStream();
325                  os.write( postData );
326
327             } catch (ProtocolException pe) {
328                  // Rcvd error instead of 100-Continue
329                  try {
330                      // work around glitch in Java 1.7.0.21 and likely others
331                      // without this, Java will connect multiple times to the server to run the same request
332                      uc.setDoOutput(false);
333                  } catch (Exception e) {
334                  }
335             }
336                         rc = uc.getResponseCode();
337                         logger.info( "http response code:" + rc );
338             responsemessage = uc.getResponseMessage();
339             logger.info( "responsemessage=" + responsemessage );
340
341
342             if (responsemessage == null) {
343                  // work around for glitch in Java 1.7.0.21 and likely others
344                  // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
345                  String h0 = uc.getHeaderField(0);
346                  if (h0 != null) {
347                      int i = h0.indexOf(' ');
348                      int j = h0.indexOf(' ', i + 1);
349                      if (i != -1 && j != -1) {
350                          responsemessage = h0.substring(j + 1);
351                      }
352                  }
353             }
354             if (rc == 201 ) {
355                         responseBody = bodyToString( uc.getInputStream() );
356                         logger.info( "responseBody=" + responseBody );
357
358             } else {
359                 err.setCode(rc);
360                 err.setMessage(responsemessage);
361             }
362             
363                 } catch (Exception e) {
364                 if ( unit_test.equals( "Yes" ) ) {
365                                 err.setCode(200);
366                                 err.setMessage( "simulated response");
367                                 logger.info( "artificial 200 response from doPostDr_Sub because unit_test =" + unit_test );
368                 } else {
369                     System.err.println("Unable to read response  " );
370                     e.printStackTrace();
371                 }
372         }               
373                 finally {
374                         try {
375                                 uc.disconnect();
376                         } catch ( Exception e ) {}
377                 }
378                 return responseBody;
379
380         }
381         
382
383         public String doPutFeed(Feed putFeed, ApiError err) {
384                 byte[] postData = putFeed.getBytes();
385                 logger.info( "post fields=" + Arrays.toString(postData) );
386                 String responsemessage = null;
387                 String responseBody = null;
388
389                 try {
390                         logger.info( "uc=" + uc );
391                         uc.setRequestMethod("PUT");
392                         uc.setRequestProperty("Content-Type", feedContentType );
393                         uc.setRequestProperty( "charset", "utf-8");
394                         uc.setRequestProperty( behalfHeader, putFeed.getOwner() );
395                         uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
396                         uc.setUseCaches(false);
397                         uc.setDoOutput(true);
398                         OutputStream os = null;
399                         int rc = -1;
400                         
401                         try {
402                  uc.connect();
403                  os = uc.getOutputStream();
404                  os.write( postData );
405
406             } catch (ProtocolException pe) {
407                  // Rcvd error instead of 100-Continue
408                  try {
409                      // work around glitch in Java 1.7.0.21 and likely others
410                      // without this, Java will connect multiple times to the server to run the same request
411                      uc.setDoOutput(false);
412                  } catch (Exception e) {
413                  }
414             }
415                         rc = uc.getResponseCode();
416                         logger.info( "http response code:" + rc );
417             responsemessage = uc.getResponseMessage();
418             logger.info( "responsemessage=" + responsemessage );
419
420
421             if (responsemessage == null) {
422                  // work around for glitch in Java 1.7.0.21 and likely others
423                  // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
424                  String h0 = uc.getHeaderField(0);
425                  if (h0 != null) {
426                      int i = h0.indexOf(' ');
427                      int j = h0.indexOf(' ', i + 1);
428                      if (i != -1 && j != -1) {
429                          responsemessage = h0.substring(j + 1);
430                      }
431                  }
432             }
433             if (rc >= 200 && rc < 300 ) {
434                         responseBody = bodyToString( uc.getInputStream() );
435                         logger.info( "responseBody=" + responseBody );
436                         err.setCode( rc );
437             } else if ( rc == 404 ) {
438                 err.setCode( rc );
439                 err.setFields( "feedid");
440                 String message =  "FeedId " + putFeed.getFeedId() + " not found on DR to update.  Out-of-sync condition?";
441                 err.setMessage( message );
442                 errorLogger.error( DmaapbcLogMessageEnum.PROV_OUT_OF_SYNC, "Feed", putFeed.getFeedId() );
443                 
444             } else {
445                 err.setCode( rc );
446                 err.setMessage(responsemessage);
447             }
448             
449                 } catch (ConnectException ce) {
450                         errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
451             err.setCode( 500 );
452                 err.setMessage("Backend connection refused");
453                 } catch (SocketException se) {
454                         errorLogger.error( DmaapbcLogMessageEnum.SOCKET_EXCEPTION, se.getMessage(), "response from Prov server" );
455                         err.setCode( 500 );
456                         err.setMessage( "Unable to read response from DR");
457         } catch (Exception e) {
458                 if ( unit_test.equals( "Yes" ) ) {
459                                 err.setCode(200);
460                                 err.setMessage( "simulated response");
461                                 logger.info( "artificial 200 response from doPutFeed because unit_test =" + unit_test );
462                 } else {
463                     logger.warn("Unable to read response  " );
464                     e.printStackTrace();
465                 }
466             try {
467                     err.setCode( uc.getResponseCode());
468                     err.setMessage(uc.getResponseMessage());
469             } catch (Exception e2) {
470                 err.setCode( 500 );
471                 err.setMessage("Unable to determine response message");
472             }
473         }               finally {
474                         try {
475                                 uc.disconnect();
476                         } catch ( Exception e ) {}
477                 }
478                 return responseBody;
479         }
480         public String doPutDr_Sub(DR_Sub postSub, ApiError err) {
481                 logger.info( "entry: doPutDr_Sub() "  );
482                 byte[] postData = postSub.getBytes(provApi);
483                 logger.info( "post fields=" + postData );
484                 String responsemessage = null;
485                 String responseBody = null;
486
487                 try {
488         
489                         uc.setRequestMethod("PUT");
490                 
491                         uc.setRequestProperty("Content-Type", subContentType );
492                         uc.setRequestProperty( "charset", "utf-8");
493                         uc.setRequestProperty( behalfHeader, "DGL" );
494                         uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
495                         uc.setUseCaches(false);
496                         uc.setDoOutput(true);
497                         OutputStream os = null;
498                         int rc = -1;
499                         
500                         try {
501                  uc.connect();
502                  os = uc.getOutputStream();
503                  os.write( postData );
504
505             } catch (ProtocolException pe) {
506                  // Rcvd error instead of 100-Continue
507                  try {
508                      // work around glitch in Java 1.7.0.21 and likely others
509                      // without this, Java will connect multiple times to the server to run the same request
510                      uc.setDoOutput(false);
511                  } catch (Exception e) {
512                  }
513             }
514                         rc = uc.getResponseCode();
515                         logger.info( "http response code:" + rc );
516             responsemessage = uc.getResponseMessage();
517             logger.info( "responsemessage=" + responsemessage );
518
519
520             if (responsemessage == null) {
521                  // work around for glitch in Java 1.7.0.21 and likely others
522                  // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
523                  String h0 = uc.getHeaderField(0);
524                  if (h0 != null) {
525                      int i = h0.indexOf(' ');
526                      int j = h0.indexOf(' ', i + 1);
527                      if (i != -1 && j != -1) {
528                          responsemessage = h0.substring(j + 1);
529                      }
530                  }
531             }
532             if (rc == 200 ) {
533                         responseBody = bodyToString( uc.getInputStream() );
534                         logger.info( "responseBody=" + responseBody );
535
536             } else {
537                 err.setCode(rc);
538                 err.setMessage(responsemessage);
539             }
540             
541                 } catch (ConnectException ce) {
542             errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
543             err.setCode( 500 );
544                 err.setMessage("Backend connection refused");
545                 } catch (Exception e) {
546                 if ( unit_test.equals( "Yes" ) ) {
547                                 err.setCode(200);
548                                 err.setMessage( "simulated response");
549                                 logger.info( "artificial 200 response from doPutDr_Sub because unit_test =" + unit_test );
550                 } else {
551                     logger.error("Unable to read response  " );
552                     logger.error(e.getMessage(), e);
553                 }
554         } finally {
555                 uc.disconnect();
556         }
557                 return responseBody;
558
559         }
560         
561         public String doGetNodes( ApiError err ) {
562                 logger.info( "entry: doGetNodes() "  );
563                 //byte[] postData = postSub.getBytes();
564                 //logger.info( "get fields=" + postData );
565                 String responsemessage = null;
566                 String responseBody = null;
567
568                 try {
569         
570                         uc.setRequestMethod("GET");
571                         int rc = -1;
572                         
573
574                         try {
575                 uc.connect();
576         
577
578             } catch (ProtocolException pe) {
579
580                  // Rcvd error instead of 100-Continue
581                  try {
582                      // work around glitch in Java 1.7.0.21 and likely others
583                      // without this, Java will connect multiple times to the server to run the same request
584                      uc.setDoOutput(false);
585                  } catch (Exception e) {
586                  }
587             } 
588         
589                         rc = uc.getResponseCode();
590                         logger.info( "http response code:" + rc );
591             responsemessage = uc.getResponseMessage();
592             logger.info( "responsemessage=" + responsemessage );
593         
594
595
596             if (responsemessage == null) {
597
598                  // work around for glitch in Java 1.7.0.21 and likely others
599                  // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
600                  String h0 = uc.getHeaderField(0);
601                  if (h0 != null) {
602                      int i = h0.indexOf(' ');
603                      int j = h0.indexOf(' ', i + 1);
604                      if (i != -1 && j != -1) {
605                          responsemessage = h0.substring(j + 1);
606                      }
607                  }
608             }
609         
610                 err.setCode(rc);  // may not really be an error, but we save rc
611             if (rc == 200 ) {
612                         responseBody = bodyToString( uc.getInputStream() );
613                         logger.info( "responseBody=" + responseBody );
614             } else {
615                 err.setMessage(responsemessage);
616             }
617             
618
619                 } catch (ConnectException ce) {
620         
621             errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
622             err.setCode( 500 );
623                 err.setMessage("Backend connection refused");
624                 } catch (Exception e) {
625                 if ( unit_test.equals( "Yes" ) ) {
626                                 err.setCode(200);
627                                 err.setMessage( "simulated response");
628                                 logger.info( "artificial 200 response from doGetNodes because unit_test =" + unit_test );
629                 } else {
630                     System.err.println("Unable to read response  " );
631                     e.printStackTrace();
632                 }
633         } finally {
634
635                         if ( uc != null ) uc.disconnect();
636         }
637
638                 return responseBody;
639
640         }
641         public String doPutNodes( ApiError err ) {
642                 logger.info( "entry: doPutNodes() "  );
643                 //byte[] postData = nodeList.getBytes();
644                 //logger.info( "get fields=" + postData );
645                 String responsemessage = null;
646                 String responseBody = null;
647
648                 try {
649         
650                         uc.setRequestMethod("PUT");
651                 
652                         //uc.setRequestProperty("Content-Type", subContentType );
653                         //uc.setRequestProperty( "charset", "utf-8");
654                         //uc.setRequestProperty( behalfHeader, "DGL" );
655                         //uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
656                         uc.setUseCaches(false);
657                         //uc.setDoOutput(true);
658                         OutputStream os = null;
659                         int rc = -1;
660                         
661                         try {
662                  uc.connect();
663                  //os = uc.getOutputStream();
664                  //os.write( postData );
665
666             } catch (ProtocolException pe) {
667                  // Rcvd error instead of 100-Continue
668                  try {
669                      // work around glitch in Java 1.7.0.21 and likely others
670                      // without this, Java will connect multiple times to the server to run the same request
671                      uc.setDoOutput(false);
672                  } catch (Exception e) {
673                  }
674             }
675                         rc = uc.getResponseCode();
676                         logger.info( "http response code:" + rc );
677             responsemessage = uc.getResponseMessage();
678             logger.info( "responsemessage=" + responsemessage );
679
680
681             if (responsemessage == null) {
682                  // work around for glitch in Java 1.7.0.21 and likely others
683                  // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
684                  String h0 = uc.getHeaderField(0);
685                  if (h0 != null) {
686                      int i = h0.indexOf(' ');
687                      int j = h0.indexOf(' ', i + 1);
688                      if (i != -1 && j != -1) {
689                          responsemessage = h0.substring(j + 1);
690                      }
691                  }
692             }
693                 err.setCode(rc);
694             if (rc == 200 ) {
695                         responseBody = bodyToString( uc.getInputStream() );
696                         logger.info( "responseBody=" + responseBody );
697
698             } else {
699   
700                 err.setMessage(responsemessage);
701             }
702             
703                 } catch (Exception e) {
704                 if ( unit_test.equals( "Yes" ) ) {
705                                 err.setCode(200);
706                                 err.setMessage( "simulated response");
707                                 logger.info( "artificial 200 response from doPutNodes because unit_test =" + unit_test );
708                 } else {
709                     System.err.println("Unable to read response  " + e.getMessage() );
710                     e.printStackTrace();
711                 }
712         } finally {
713                         if ( uc != null ) {
714                         uc.disconnect();
715                         }
716         }
717                 return responseBody;
718
719         }
720         
721         public String doDeleteFeed(Feed putFeed, ApiError err) {
722                 //byte[] postData = putFeed.getBytes();
723                 //logger.info( "post fields=" + postData.toString() );
724                 String responsemessage = null;
725                 String responseBody = null;
726
727                 try {
728                         logger.info( "uc=" + uc );
729                         uc.setRequestMethod("DELETE");
730                         uc.setRequestProperty("Content-Type", feedContentType );
731                         uc.setRequestProperty( "charset", "utf-8");
732                         uc.setRequestProperty( behalfHeader, putFeed.getOwner() );
733                         //uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
734                         uc.setUseCaches(false);
735                         uc.setDoOutput(true);
736                         OutputStream os = null;
737                         int rc = -1;
738                         
739                         try {
740                  uc.connect();
741                  os = uc.getOutputStream();
742                  //os.write( postData );
743
744             } catch (ProtocolException pe) {
745                  // Rcvd error instead of 100-Continue
746                  try {
747                      // work around glitch in Java 1.7.0.21 and likely others
748                      // without this, Java will connect multiple times to the server to run the same request
749                      uc.setDoOutput(false);
750                  } catch (Exception e) {
751                  }
752             }
753                         rc = uc.getResponseCode();
754                         logger.info( "http response code:" + rc );
755             responsemessage = uc.getResponseMessage();
756             logger.info( "responsemessage=" + responsemessage );
757
758
759             if (responsemessage == null) {
760                  // work around for glitch in Java 1.7.0.21 and likely others
761                  // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
762                  String h0 = uc.getHeaderField(0);
763                  if (h0 != null) {
764                      int i = h0.indexOf(' ');
765                      int j = h0.indexOf(' ', i + 1);
766                      if (i != -1 && j != -1) {
767                          responsemessage = h0.substring(j + 1);
768                      }
769                  }
770             }
771             if (rc >= 200 && rc < 300 ) {
772                         responseBody = bodyToString( uc.getInputStream() );
773                         logger.info( "responseBody=" + responseBody );
774
775             } else if ( rc == 404 ) {
776                 err.setCode( rc );
777                 err.setFields( "feedid");
778                 String message =  "FeedId " + putFeed.getFeedId() + " not found on DR to update.  Out-of-sync condition?";
779                 err.setMessage( message );
780                 errorLogger.error( DmaapbcLogMessageEnum.PROV_OUT_OF_SYNC, "Feed", putFeed.getFeedId() );
781                 
782             } else {
783                 err.setCode( rc );
784                 err.setMessage(responsemessage);
785             }
786             
787                 } catch (ConnectException ce) {
788                         errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
789             err.setCode( 500 );
790                 err.setMessage("Backend connection refused");
791                 } catch (SocketException se) {
792                         errorLogger.error( DmaapbcLogMessageEnum.SOCKET_EXCEPTION, se.getMessage(), "response from Prov server" );
793                         err.setCode( 500 );
794                         err.setMessage( "Unable to read response from DR");
795         } catch (Exception e) {
796                 if ( unit_test.equals( "Yes" ) ) {
797                                 err.setCode(200);
798                                 err.setMessage( "simulated response");
799                                 logger.info( "artificial 200 response from doDeleteFeed because unit_test =" + unit_test );
800                 } else {
801                     logger.warn("Unable to read response  " );
802                     e.printStackTrace();
803                     try {
804                             err.setCode( uc.getResponseCode());
805                             err.setMessage(uc.getResponseMessage());
806                     } catch (Exception e2) {
807                         err.setCode( 500 );
808                         err.setMessage("Unable to determine response message");
809                     }
810                 }
811         }               finally {
812                         try {
813                                 uc.disconnect();
814                         } catch ( Exception e ) {}
815                 }
816                 return responseBody;
817         }
818         
819         public String doDeleteDr_Sub(DR_Sub delSub, ApiError err) {
820                 logger.info( "entry: doDeleteDr_Sub() "  );
821                 byte[] postData = delSub.getBytes(provApi);
822                 logger.info( "post fields=" + postData );
823                 String responsemessage = null;
824                 String responseBody = null;
825
826                 try {
827         
828                         uc.setRequestMethod("DELETE");
829                 
830                         uc.setRequestProperty("Content-Type", subContentType);
831                         uc.setRequestProperty( "charset", "utf-8");
832                         uc.setRequestProperty( behalfHeader, "DGL" );
833                         //uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
834                         uc.setUseCaches(false);
835                         uc.setDoOutput(true);
836                         OutputStream os = null;
837                         int rc = -1;
838                         
839                         try {
840                  uc.connect();
841                  os = uc.getOutputStream();
842                  //os.write( postData );
843
844             } catch (ProtocolException pe) {
845                  // Rcvd error instead of 100-Continue
846                  try {
847                      // work around glitch in Java 1.7.0.21 and likely others
848                      // without this, Java will connect multiple times to the server to run the same request
849                      uc.setDoOutput(false);
850                  } catch (Exception e) {
851                  }
852             }
853                         rc = uc.getResponseCode();
854                         logger.info( "http response code:" + rc );
855             responsemessage = uc.getResponseMessage();
856             logger.info( "responsemessage=" + responsemessage );
857
858
859             if (responsemessage == null) {
860                  // work around for glitch in Java 1.7.0.21 and likely others
861                  // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
862                  String h0 = uc.getHeaderField(0);
863                  if (h0 != null) {
864                      int i = h0.indexOf(' ');
865                      int j = h0.indexOf(' ', i + 1);
866                      if (i != -1 && j != -1) {
867                          responsemessage = h0.substring(j + 1);
868                      }
869                  }
870             }
871                 err.setCode(rc);
872             if (rc == 204 ) {
873                         responseBody = bodyToString( uc.getInputStream() );
874                         logger.info( "responseBody=" + responseBody );
875             } else {
876                 err.setMessage(responsemessage);
877             }
878             
879                 } catch (ConnectException ce) {
880                 if ( unit_test.equals( "Yes" ) ) {
881                                 err.setCode(200);
882                                 err.setMessage( "simulated response");
883                                 logger.info( "artificial 200 response from doDeleteDr_Sub because unit_test =" + unit_test );
884                 } else {
885                     errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
886                     err.setCode( 500 );
887                         err.setMessage("Backend connection refused");
888                 }
889                 } catch (Exception e) {
890                 if ( unit_test.equals( "Yes" ) ) {
891                                 err.setCode(200);
892                                 err.setMessage( "simulated response");
893                                 logger.info( "artificial 200 response from doDeleteDr_Sub because unit_test =" + unit_test );
894                 } else {
895                     System.err.println("Unable to read response  " );
896                     e.printStackTrace();
897                 }
898         } finally {
899                 uc.disconnect();
900         }
901                 return responseBody;
902
903         }
904         
905                 
906 }