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