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