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