Allow POST dr_sub using FeedName
[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-DMAAP-DR-ON-BEHALF-OF");
63                 feedContentType = p.getProperty( "DR.feedContentType", "application/vnd.dmaap-dr.feed");
64                 subContentType = p.getProperty( "DR.subContentType", "application/vnd.dmaap-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
247
248                         try {
249                  uc.connect();
250
251             } catch (ProtocolException pe) {
252                  // Rcvd error instead of 100-Continue
253                  try {
254                      // work around glitch in Java 1.7.0.21 and likely others
255                      // without this, Java will connect multiple times to the server to run the same request
256                      uc.setDoOutput(false);
257                  } catch (Exception e) {
258                         logger.error(e.getMessage(), e);
259                  }
260             }
261                         rc = uc.getResponseCode();
262                         logger.info( "http response code:" + rc );
263             responsemessage = uc.getResponseMessage();
264             logger.info( "responsemessage=" + responsemessage );
265
266
267
268             if (rc < 200 || rc >= 300 ) {
269                 err.setCode( rc );
270                 err.setMessage(responsemessage);
271             }
272                 } catch (Exception e) {
273             logger.error("Unable to read response  " );
274             e.printStackTrace();
275         }               finally {
276                         try {
277                                 uc.disconnect();
278                         } catch ( Exception e ) {
279                                 logger.error(e.getMessage(), e);
280                         }
281                 }
282         
283                 return rc;
284
285         }
286         
287         public String doPostDr_Sub( DR_Sub postSub, ApiError err ) {
288                 logger.info( "entry: doPostDr_Sub() "  );
289                 byte[] postData = postSub.getBytes(provApi );
290                 logger.info( "post fields=" + postData );
291                 String responsemessage = null;
292                 String responseBody = null;
293
294                 try {
295         
296                         uc.setRequestMethod("POST");
297                 
298                         uc.setRequestProperty("Content-Type", subContentType );
299                         uc.setRequestProperty( "charset", "utf-8");
300                         uc.setRequestProperty( behalfHeader, "DGL" );
301                         uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
302                         uc.setUseCaches(false);
303                         uc.setDoOutput(true);
304                         OutputStream os = null;
305                         int rc = -1;
306                         
307                         try {
308                  uc.connect();
309                  os = uc.getOutputStream();
310                  os.write( postData );
311
312             } catch (ProtocolException pe) {
313                  // Rcvd error instead of 100-Continue
314                  try {
315                      // work around glitch in Java 1.7.0.21 and likely others
316                      // without this, Java will connect multiple times to the server to run the same request
317                      uc.setDoOutput(false);
318                  } catch (Exception e) {
319                  }
320             }
321                         rc = uc.getResponseCode();
322                         logger.info( "http response code:" + rc );
323             responsemessage = uc.getResponseMessage();
324             logger.info( "responsemessage=" + responsemessage );
325
326
327             if (responsemessage == null) {
328                  // work around for glitch in Java 1.7.0.21 and likely others
329                  // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
330                  String h0 = uc.getHeaderField(0);
331                  if (h0 != null) {
332                      int i = h0.indexOf(' ');
333                      int j = h0.indexOf(' ', i + 1);
334                      if (i != -1 && j != -1) {
335                          responsemessage = h0.substring(j + 1);
336                      }
337                  }
338             }
339             if (rc == 201 ) {
340                         responseBody = bodyToString( uc.getInputStream() );
341                         logger.info( "responseBody=" + responseBody );
342
343             } else {
344                 err.setCode(rc);
345                 err.setMessage(responsemessage);
346             }
347             
348                 } catch (Exception e) {
349             System.err.println("Unable to read response  " );
350             e.printStackTrace();
351         }               finally {
352                         try {
353                                 uc.disconnect();
354                         } catch ( Exception e ) {}
355                 }
356                 return responseBody;
357
358         }
359         
360
361         public String doPutFeed(Feed putFeed, ApiError err) {
362                 byte[] postData = putFeed.getBytes();
363                 logger.info( "post fields=" + Arrays.toString(postData) );
364                 String responsemessage = null;
365                 String responseBody = null;
366
367                 try {
368                         logger.info( "uc=" + uc );
369                         uc.setRequestMethod("PUT");
370                         uc.setRequestProperty("Content-Type", feedContentType );
371                         uc.setRequestProperty( "charset", "utf-8");
372                         uc.setRequestProperty( behalfHeader, putFeed.getOwner() );
373                         uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
374                         uc.setUseCaches(false);
375                         uc.setDoOutput(true);
376                         OutputStream os = null;
377                         int rc = -1;
378                         
379                         try {
380                  uc.connect();
381                  os = uc.getOutputStream();
382                  os.write( postData );
383
384             } catch (ProtocolException pe) {
385                  // Rcvd error instead of 100-Continue
386                  try {
387                      // work around glitch in Java 1.7.0.21 and likely others
388                      // without this, Java will connect multiple times to the server to run the same request
389                      uc.setDoOutput(false);
390                  } catch (Exception e) {
391                  }
392             }
393                         rc = uc.getResponseCode();
394                         logger.info( "http response code:" + rc );
395             responsemessage = uc.getResponseMessage();
396             logger.info( "responsemessage=" + responsemessage );
397
398
399             if (responsemessage == null) {
400                  // work around for glitch in Java 1.7.0.21 and likely others
401                  // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
402                  String h0 = uc.getHeaderField(0);
403                  if (h0 != null) {
404                      int i = h0.indexOf(' ');
405                      int j = h0.indexOf(' ', i + 1);
406                      if (i != -1 && j != -1) {
407                          responsemessage = h0.substring(j + 1);
408                      }
409                  }
410             }
411             if (rc >= 200 && rc < 300 ) {
412                         responseBody = bodyToString( uc.getInputStream() );
413                         logger.info( "responseBody=" + responseBody );
414                         err.setCode( rc );
415             } else if ( rc == 404 ) {
416                 err.setCode( rc );
417                 err.setFields( "feedid");
418                 String message =  "FeedId " + putFeed.getFeedId() + " not found on DR to update.  Out-of-sync condition?";
419                 err.setMessage( message );
420                 errorLogger.error( DmaapbcLogMessageEnum.PROV_OUT_OF_SYNC, "Feed", putFeed.getFeedId() );
421                 
422             } else {
423                 err.setCode( rc );
424                 err.setMessage(responsemessage);
425             }
426             
427                 } catch (ConnectException ce) {
428                         errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
429             err.setCode( 500 );
430                 err.setMessage("Backend connection refused");
431                 } catch (SocketException se) {
432                         errorLogger.error( DmaapbcLogMessageEnum.SOCKET_EXCEPTION, se.getMessage(), "response from Prov server" );
433                         err.setCode( 500 );
434                         err.setMessage( "Unable to read response from DR");
435         } catch (Exception e) {
436             logger.warn("Unable to read response  " );
437             e.printStackTrace();
438             try {
439                     err.setCode( uc.getResponseCode());
440                     err.setMessage(uc.getResponseMessage());
441             } catch (Exception e2) {
442                 err.setCode( 500 );
443                 err.setMessage("Unable to determine response message");
444             }
445         }               finally {
446                         try {
447                                 uc.disconnect();
448                         } catch ( Exception e ) {}
449                 }
450                 return responseBody;
451         }
452         public String doPutDr_Sub(DR_Sub postSub, ApiError err) {
453                 logger.info( "entry: doPutDr_Sub() "  );
454                 byte[] postData = postSub.getBytes(provApi);
455                 logger.info( "post fields=" + postData );
456                 String responsemessage = null;
457                 String responseBody = null;
458
459                 try {
460         
461                         uc.setRequestMethod("PUT");
462                 
463                         uc.setRequestProperty("Content-Type", subContentType );
464                         uc.setRequestProperty( "charset", "utf-8");
465                         uc.setRequestProperty( behalfHeader, "DGL" );
466                         uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
467                         uc.setUseCaches(false);
468                         uc.setDoOutput(true);
469                         OutputStream os = null;
470                         int rc = -1;
471                         
472                         try {
473                  uc.connect();
474                  os = uc.getOutputStream();
475                  os.write( postData );
476
477             } catch (ProtocolException pe) {
478                  // Rcvd error instead of 100-Continue
479                  try {
480                      // work around glitch in Java 1.7.0.21 and likely others
481                      // without this, Java will connect multiple times to the server to run the same request
482                      uc.setDoOutput(false);
483                  } catch (Exception e) {
484                  }
485             }
486                         rc = uc.getResponseCode();
487                         logger.info( "http response code:" + rc );
488             responsemessage = uc.getResponseMessage();
489             logger.info( "responsemessage=" + responsemessage );
490
491
492             if (responsemessage == null) {
493                  // work around for glitch in Java 1.7.0.21 and likely others
494                  // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
495                  String h0 = uc.getHeaderField(0);
496                  if (h0 != null) {
497                      int i = h0.indexOf(' ');
498                      int j = h0.indexOf(' ', i + 1);
499                      if (i != -1 && j != -1) {
500                          responsemessage = h0.substring(j + 1);
501                      }
502                  }
503             }
504             if (rc == 200 ) {
505                         responseBody = bodyToString( uc.getInputStream() );
506                         logger.info( "responseBody=" + responseBody );
507
508             } else {
509                 err.setCode(rc);
510                 err.setMessage(responsemessage);
511             }
512             
513                 } catch (ConnectException ce) {
514             errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
515             err.setCode( 500 );
516                 err.setMessage("Backend connection refused");
517                 } catch (Exception e) {
518             logger.error("Unable to read response  " );
519             logger.error(e.getMessage(), e);
520         } finally {
521                 uc.disconnect();
522         }
523                 return responseBody;
524
525         }
526         
527         public String doGetNodes( ApiError err ) {
528                 logger.info( "entry: doGetNodes() "  );
529                 //byte[] postData = postSub.getBytes();
530                 //logger.info( "get fields=" + postData );
531                 String responsemessage = null;
532                 String responseBody = null;
533                 logger.info( "templog:doGetNodes at 12.10.14.10"  );
534
535                 try {
536                 logger.info( "templog:doGetNodes at 12.10.14.11"  );
537         
538                         uc.setRequestMethod("GET");
539                         int rc = -1;
540                         
541                 logger.info( "templog:doGetNodes at 12.10.14.12"  );
542                         try {
543                 uc.connect();
544                                 logger.info( "templog:doGetNodes at 12.10.14.13"  );
545
546
547             } catch (ProtocolException pe) {
548                 logger.info( "templog:doGetNodes at 12.10.14.14"  );
549                  // Rcvd error instead of 100-Continue
550                  try {
551                      // work around glitch in Java 1.7.0.21 and likely others
552                      // without this, Java will connect multiple times to the server to run the same request
553                      uc.setDoOutput(false);
554                  } catch (Exception e) {
555                  }
556             } 
557                 logger.info( "templog:doGetNodes at 12.10.14.15"  );
558                         rc = uc.getResponseCode();
559                         logger.info( "http response code:" + rc );
560             responsemessage = uc.getResponseMessage();
561             logger.info( "responsemessage=" + responsemessage );
562                 logger.info( "templog:doGetNodes at 12.10.14.16"  );
563
564
565             if (responsemessage == null) {
566                 logger.info( "templog:doGetNodes at 12.10.14.17"  );
567                  // work around for glitch in Java 1.7.0.21 and likely others
568                  // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
569                  String h0 = uc.getHeaderField(0);
570                  if (h0 != null) {
571                      int i = h0.indexOf(' ');
572                      int j = h0.indexOf(' ', i + 1);
573                      if (i != -1 && j != -1) {
574                          responsemessage = h0.substring(j + 1);
575                      }
576                  }
577             }
578                 logger.info( "templog:doGetNodes at 12.10.14.18"  );
579                 err.setCode(rc);  // may not really be an error, but we save rc
580             if (rc == 200 ) {
581                         responseBody = bodyToString( uc.getInputStream() );
582                         logger.info( "responseBody=" + responseBody );
583             } else {
584                 err.setMessage(responsemessage);
585             }
586             
587                 logger.info( "templog:doGetNodes at 12.10.14.19"  );
588                 } catch (ConnectException ce) {
589                 logger.info( "templog:doGetNodes at 12.10.14.20"  );
590             errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
591             err.setCode( 500 );
592                 err.setMessage("Backend connection refused");
593                 } catch (Exception e) {
594                 logger.info( "templog:doGetNodes at 12.10.14.21"  );
595             System.err.println("Unable to read response  " );
596             e.printStackTrace();
597         } finally {
598                 logger.info( "templog:doGetNodes at 12.10.14.22"  );
599                         if ( uc != null ) uc.disconnect();
600         }
601                 logger.info( "templog:doGetNodes at 12.10.14.23"  );
602                 return responseBody;
603
604         }
605         public String doPutNodes( ApiError err ) {
606                 logger.info( "entry: doPutNodes() "  );
607                 //byte[] postData = nodeList.getBytes();
608                 //logger.info( "get fields=" + postData );
609                 String responsemessage = null;
610                 String responseBody = null;
611
612                 try {
613         
614                         uc.setRequestMethod("PUT");
615                 
616                         //uc.setRequestProperty("Content-Type", subContentType );
617                         //uc.setRequestProperty( "charset", "utf-8");
618                         //uc.setRequestProperty( behalfHeader, "DGL" );
619                         //uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
620                         uc.setUseCaches(false);
621                         //uc.setDoOutput(true);
622                         OutputStream os = null;
623                         int rc = -1;
624                         
625                         try {
626                  uc.connect();
627                  //os = uc.getOutputStream();
628                  //os.write( postData );
629
630             } catch (ProtocolException pe) {
631                  // Rcvd error instead of 100-Continue
632                  try {
633                      // work around glitch in Java 1.7.0.21 and likely others
634                      // without this, Java will connect multiple times to the server to run the same request
635                      uc.setDoOutput(false);
636                  } catch (Exception e) {
637                  }
638             }
639                         rc = uc.getResponseCode();
640                         logger.info( "http response code:" + rc );
641             responsemessage = uc.getResponseMessage();
642             logger.info( "responsemessage=" + responsemessage );
643
644
645             if (responsemessage == null) {
646                  // work around for glitch in Java 1.7.0.21 and likely others
647                  // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
648                  String h0 = uc.getHeaderField(0);
649                  if (h0 != null) {
650                      int i = h0.indexOf(' ');
651                      int j = h0.indexOf(' ', i + 1);
652                      if (i != -1 && j != -1) {
653                          responsemessage = h0.substring(j + 1);
654                      }
655                  }
656             }
657                 err.setCode(rc);
658             if (rc == 200 ) {
659                         responseBody = bodyToString( uc.getInputStream() );
660                         logger.info( "responseBody=" + responseBody );
661
662             } else {
663   
664                 err.setMessage(responsemessage);
665             }
666             
667                 } catch (Exception e) {
668             System.err.println("Unable to read response  " + e.getMessage() );
669             e.printStackTrace();
670         } finally {
671                         if ( uc != null ) {
672                         uc.disconnect();
673                         }
674         }
675                 return responseBody;
676
677         }
678         
679         public String doDeleteFeed(Feed putFeed, ApiError err) {
680                 //byte[] postData = putFeed.getBytes();
681                 //logger.info( "post fields=" + postData.toString() );
682                 String responsemessage = null;
683                 String responseBody = null;
684
685                 try {
686                         logger.info( "uc=" + uc );
687                         uc.setRequestMethod("DELETE");
688                         uc.setRequestProperty("Content-Type", feedContentType );
689                         uc.setRequestProperty( "charset", "utf-8");
690                         uc.setRequestProperty( behalfHeader, putFeed.getOwner() );
691                         //uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
692                         uc.setUseCaches(false);
693                         uc.setDoOutput(true);
694                         OutputStream os = null;
695                         int rc = -1;
696                         
697                         try {
698                  uc.connect();
699                  os = uc.getOutputStream();
700                  //os.write( postData );
701
702             } catch (ProtocolException pe) {
703                  // Rcvd error instead of 100-Continue
704                  try {
705                      // work around glitch in Java 1.7.0.21 and likely others
706                      // without this, Java will connect multiple times to the server to run the same request
707                      uc.setDoOutput(false);
708                  } catch (Exception e) {
709                  }
710             }
711                         rc = uc.getResponseCode();
712                         logger.info( "http response code:" + rc );
713             responsemessage = uc.getResponseMessage();
714             logger.info( "responsemessage=" + responsemessage );
715
716
717             if (responsemessage == null) {
718                  // work around for glitch in Java 1.7.0.21 and likely others
719                  // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
720                  String h0 = uc.getHeaderField(0);
721                  if (h0 != null) {
722                      int i = h0.indexOf(' ');
723                      int j = h0.indexOf(' ', i + 1);
724                      if (i != -1 && j != -1) {
725                          responsemessage = h0.substring(j + 1);
726                      }
727                  }
728             }
729             if (rc >= 200 && rc < 300 ) {
730                         responseBody = bodyToString( uc.getInputStream() );
731                         logger.info( "responseBody=" + responseBody );
732
733             } else if ( rc == 404 ) {
734                 err.setCode( rc );
735                 err.setFields( "feedid");
736                 String message =  "FeedId " + putFeed.getFeedId() + " not found on DR to update.  Out-of-sync condition?";
737                 err.setMessage( message );
738                 errorLogger.error( DmaapbcLogMessageEnum.PROV_OUT_OF_SYNC, "Feed", putFeed.getFeedId() );
739                 
740             } else {
741                 err.setCode( rc );
742                 err.setMessage(responsemessage);
743             }
744             
745                 } catch (ConnectException ce) {
746                         errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
747             err.setCode( 500 );
748                 err.setMessage("Backend connection refused");
749                 } catch (SocketException se) {
750                         errorLogger.error( DmaapbcLogMessageEnum.SOCKET_EXCEPTION, se.getMessage(), "response from Prov server" );
751                         err.setCode( 500 );
752                         err.setMessage( "Unable to read response from DR");
753         } catch (Exception e) {
754             logger.warn("Unable to read response  " );
755             e.printStackTrace();
756             try {
757                     err.setCode( uc.getResponseCode());
758                     err.setMessage(uc.getResponseMessage());
759             } catch (Exception e2) {
760                 err.setCode( 500 );
761                 err.setMessage("Unable to determine response message");
762             }
763         }               finally {
764                         try {
765                                 uc.disconnect();
766                         } catch ( Exception e ) {}
767                 }
768                 return responseBody;
769         }
770         
771         public String doDeleteDr_Sub(DR_Sub delSub, ApiError err) {
772                 logger.info( "entry: doDeleteDr_Sub() "  );
773                 byte[] postData = delSub.getBytes(provApi);
774                 logger.info( "post fields=" + postData );
775                 String responsemessage = null;
776                 String responseBody = null;
777
778                 try {
779         
780                         uc.setRequestMethod("DELETE");
781                 
782                         uc.setRequestProperty("Content-Type", subContentType);
783                         uc.setRequestProperty( "charset", "utf-8");
784                         uc.setRequestProperty( behalfHeader, "DGL" );
785                         //uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
786                         uc.setUseCaches(false);
787                         uc.setDoOutput(true);
788                         OutputStream os = null;
789                         int rc = -1;
790                         
791                         try {
792                  uc.connect();
793                  os = uc.getOutputStream();
794                  //os.write( postData );
795
796             } catch (ProtocolException pe) {
797                  // Rcvd error instead of 100-Continue
798                  try {
799                      // work around glitch in Java 1.7.0.21 and likely others
800                      // without this, Java will connect multiple times to the server to run the same request
801                      uc.setDoOutput(false);
802                  } catch (Exception e) {
803                  }
804             }
805                         rc = uc.getResponseCode();
806                         logger.info( "http response code:" + rc );
807             responsemessage = uc.getResponseMessage();
808             logger.info( "responsemessage=" + responsemessage );
809
810
811             if (responsemessage == null) {
812                  // work around for glitch in Java 1.7.0.21 and likely others
813                  // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
814                  String h0 = uc.getHeaderField(0);
815                  if (h0 != null) {
816                      int i = h0.indexOf(' ');
817                      int j = h0.indexOf(' ', i + 1);
818                      if (i != -1 && j != -1) {
819                          responsemessage = h0.substring(j + 1);
820                      }
821                  }
822             }
823                 err.setCode(rc);
824             if (rc == 204 ) {
825                         responseBody = bodyToString( uc.getInputStream() );
826                         logger.info( "responseBody=" + responseBody );
827             } else {
828                 err.setMessage(responsemessage);
829             }
830             
831                 } catch (ConnectException ce) {
832             errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
833             err.setCode( 500 );
834                 err.setMessage("Backend connection refused");
835                 } catch (Exception e) {
836             System.err.println("Unable to read response  " );
837             e.printStackTrace();
838         } finally {
839                 uc.disconnect();
840         }
841                 return responseBody;
842
843         }
844         /*
845          public static void main( String[] args ) throws Exception {
846                 PropertyConfigurator.configure("log4j.properties");
847                 logger.info("Started.");
848                 
849                 RandomInteger ri = new RandomInteger(10000);
850                         //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()) ;
851                         int i = ri.next();
852                         Feed tst = new Feed( "dgl feed " + i,
853                                                                 "v1.0",
854                                                                 "dgl feed " + i + "for testing",
855                                                                 "TEST",
856                                                                 "unclassified"
857                                         );
858                         ArrayList<DR_Pub> pubs = new ArrayList<DR_Pub>();
859                         pubs.add( new DR_Pub( "centralLocation" ) ); 
860                         tst.setPubs(pubs);
861
862                 boolean rc;
863                 DrProvConnection context = new DrProvConnection();
864                 rc = context.makeFeedConnection();
865                 logger.info( "makeFeedConnection returns " + rc);
866                 ApiError err = new ApiError();
867                 if ( rc ) {
868                         String tmp  = context.doPostFeed( tst, err );
869                         logger.info( "doPostFeed returns " + tmp);
870                 }
871     
872          }
873  */
874         
875                 
876 }