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