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