2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.dmaap.dbcapi.client;
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;
32 import java.util.ArrayList;
33 import java.util.Arrays;
35 import javax.net.ssl.HttpsURLConnection;
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;
50 public class DrProvConnection extends BaseLoggingClass {
53 private String provURL;
55 private HttpsURLConnection uc;
58 public DrProvConnection() {
59 provURL = new DmaapService().getDmaap().getDrProvUrl();
60 if ( provURL.length() < 1 ) {
61 errorLogger.error( DmaapbcLogMessageEnum.PREREQ_DMAAP_OBJECT, "getDrProvUrl");
66 public boolean makeFeedConnection() {
67 return makeConnection( provURL );
69 public boolean makeFeedConnection(String feedId) {
70 return makeConnection( provURL + "/feed/" + feedId );
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 );
78 public boolean makeSubPutConnection( String subId ) {
79 String revisedURL = provURL + "/subs/" + subId;
80 logger.info( "mapping " + subId + " to " + revisedURL );
81 return makeConnection( revisedURL );
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 );
89 public boolean makeEgressConnection( String sub, String nodep ) {
90 String uri = String.format("/internal/route/egress/?sub=%s&node=%s",
92 return makeConnection( provURL + uri );
94 public boolean makeNodesConnection( String varName ) {
96 String uri = String.format("/internal/api/%s", varName);
97 return makeConnection( provURL + uri );
100 public boolean makeNodesConnection( String varName, String val ) {
105 String cv = val.replaceAll("\\|", "%7C");
106 String uri = String.format( "/internal/api/%s?val=%s", varName, cv );
108 return makeConnection( provURL + uri );
111 private boolean makeConnection( String pURL ) {
114 URL u = new URL( pURL );
115 uc = (HttpsURLConnection) u.openConnection();
116 uc.setInstanceFollowRedirects(false);
117 logger.info( "successful connect to " + pURL );
119 } catch (Exception e) {
120 errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_ERROR, pURL, e.getMessage() );
127 public String bodyToString( InputStream is ) {
128 logger.info( "is=" + is );
129 StringBuilder sb = new StringBuilder();
130 BufferedReader br = new BufferedReader( new InputStreamReader(is));
133 while ((line = br.readLine()) != null ) {
136 } catch (IOException ex ) {
137 errorLogger.error( DmaapbcLogMessageEnum.IO_EXCEPTION, ex.getMessage());
140 return sb.toString();
144 public String doPostFeed( Feed postFeed, ApiError err ) {
146 byte[] postData = postFeed.getBytes();
147 logger.info( "post fields=" + Arrays.toString(postData) );
148 String responsemessage = null;
149 String responseBody = null;
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;
165 os = uc.getOutputStream();
166 os.write( postData );
168 } catch (ProtocolException pe) {
169 // Rcvd error instead of 100-Continue
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) {
177 rc = uc.getResponseCode();
178 logger.info( "http response code:" + rc );
179 responsemessage = uc.getResponseMessage();
180 logger.info( "responsemessage=" + responsemessage );
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);
188 int i = h0.indexOf(' ');
189 int j = h0.indexOf(' ', i + 1);
190 if (i != -1 && j != -1) {
191 responsemessage = h0.substring(j + 1);
196 responseBody = bodyToString( uc.getInputStream() );
197 logger.info( "responseBody=" + responseBody );
201 err.setMessage(responsemessage);
204 } catch (ConnectException ce) {
205 errorLogger.error(DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
207 err.setMessage("Backend connection refused");
208 } catch (SocketException se) {
209 errorLogger.error( DmaapbcLogMessageEnum.SOCKET_EXCEPTION, se.getMessage(), "response from prov server" );
211 err.setMessage( "Unable to read response from DR");
212 } catch (Exception e) {
213 logger.warn("Unable to read response " );
216 err.setCode( uc.getResponseCode());
217 err.setMessage(uc.getResponseMessage());
218 } catch (Exception e2) {
220 err.setMessage("Unable to determine response message");
226 } catch ( Exception e ) {}
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 ) {
237 String responsemessage = null;
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;
253 os = uc.getOutputStream();
256 } catch (ProtocolException pe) {
257 // Rcvd error instead of 100-Continue
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) {
265 rc = uc.getResponseCode();
266 logger.info( "http response code:" + rc );
267 responsemessage = uc.getResponseMessage();
268 logger.info( "responsemessage=" + responsemessage );
272 if (rc < 200 || rc >= 300 ) {
274 err.setMessage(responsemessage);
276 } catch (Exception e) {
277 System.err.println("Unable to read response " );
282 } catch ( Exception e ) {}
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;
298 uc.setRequestMethod("POST");
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;
311 os = uc.getOutputStream();
312 os.write( postData );
314 } catch (ProtocolException pe) {
315 // Rcvd error instead of 100-Continue
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) {
323 rc = uc.getResponseCode();
324 logger.info( "http response code:" + rc );
325 responsemessage = uc.getResponseMessage();
326 logger.info( "responsemessage=" + responsemessage );
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);
334 int i = h0.indexOf(' ');
335 int j = h0.indexOf(' ', i + 1);
336 if (i != -1 && j != -1) {
337 responsemessage = h0.substring(j + 1);
342 responseBody = bodyToString( uc.getInputStream() );
343 logger.info( "responseBody=" + responseBody );
347 err.setMessage(responsemessage);
350 } catch (Exception e) {
351 System.err.println("Unable to read response " );
356 } catch ( Exception e ) {}
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;
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;
383 os = uc.getOutputStream();
384 os.write( postData );
386 } catch (ProtocolException pe) {
387 // Rcvd error instead of 100-Continue
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) {
395 rc = uc.getResponseCode();
396 logger.info( "http response code:" + rc );
397 responsemessage = uc.getResponseMessage();
398 logger.info( "responsemessage=" + responsemessage );
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);
406 int i = h0.indexOf(' ');
407 int j = h0.indexOf(' ', i + 1);
408 if (i != -1 && j != -1) {
409 responsemessage = h0.substring(j + 1);
413 if (rc >= 200 && rc < 300 ) {
414 responseBody = bodyToString( uc.getInputStream() );
415 logger.info( "responseBody=" + responseBody );
417 } else if ( rc == 404 ) {
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() );
426 err.setMessage(responsemessage);
429 } catch (ConnectException ce) {
430 errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
432 err.setMessage("Backend connection refused");
433 } catch (SocketException se) {
434 errorLogger.error( DmaapbcLogMessageEnum.SOCKET_EXCEPTION, se.getMessage(), "response from Prov server" );
436 err.setMessage( "Unable to read response from DR");
437 } catch (Exception e) {
438 logger.warn("Unable to read response " );
441 err.setCode( uc.getResponseCode());
442 err.setMessage(uc.getResponseMessage());
443 } catch (Exception e2) {
445 err.setMessage("Unable to determine response message");
450 } catch ( Exception e ) {}
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;
463 uc.setRequestMethod("PUT");
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;
476 os = uc.getOutputStream();
477 os.write( postData );
479 } catch (ProtocolException pe) {
480 // Rcvd error instead of 100-Continue
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) {
488 rc = uc.getResponseCode();
489 logger.info( "http response code:" + rc );
490 responsemessage = uc.getResponseMessage();
491 logger.info( "responsemessage=" + responsemessage );
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);
499 int i = h0.indexOf(' ');
500 int j = h0.indexOf(' ', i + 1);
501 if (i != -1 && j != -1) {
502 responsemessage = h0.substring(j + 1);
507 responseBody = bodyToString( uc.getInputStream() );
508 logger.info( "responseBody=" + responseBody );
512 err.setMessage(responsemessage);
515 } catch (ConnectException ce) {
516 errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
518 err.setMessage("Backend connection refused");
519 } catch (Exception e) {
520 System.err.println("Unable to read response " );
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" );
538 logger.info( "templog:doGetNodes at 12.10.14.11" );
540 uc.setRequestMethod("GET");
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;
551 logger.info( "templog:doGetNodes at 12.10.14.12" );
554 logger.info( "templog:doGetNodes at 12.10.14.13" );
555 //os = uc.getOutputStream();
556 //os.write( postData );
558 } catch (ProtocolException pe) {
559 logger.info( "templog:doGetNodes at 12.10.14.14" );
560 // Rcvd error instead of 100-Continue
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) {
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" );
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);
582 int i = h0.indexOf(' ');
583 int j = h0.indexOf(' ', i + 1);
584 if (i != -1 && j != -1) {
585 responsemessage = h0.substring(j + 1);
589 logger.info( "templog:doGetNodes at 12.10.14.18" );
590 err.setCode(rc); // may not really be an error, but we save rc
592 responseBody = bodyToString( uc.getInputStream() );
593 logger.info( "responseBody=" + responseBody );
595 err.setMessage(responsemessage);
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() );
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 " );
609 logger.info( "templog:doGetNodes at 12.10.14.22" );
610 if ( uc != null ) uc.disconnect();
612 logger.info( "templog:doGetNodes at 12.10.14.23" );
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;
625 uc.setRequestMethod("PUT");
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;
638 //os = uc.getOutputStream();
639 //os.write( postData );
641 } catch (ProtocolException pe) {
642 // Rcvd error instead of 100-Continue
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) {
650 rc = uc.getResponseCode();
651 logger.info( "http response code:" + rc );
652 responsemessage = uc.getResponseMessage();
653 logger.info( "responsemessage=" + responsemessage );
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);
661 int i = h0.indexOf(' ');
662 int j = h0.indexOf(' ', i + 1);
663 if (i != -1 && j != -1) {
664 responsemessage = h0.substring(j + 1);
670 responseBody = bodyToString( uc.getInputStream() );
671 logger.info( "responseBody=" + responseBody );
675 err.setMessage(responsemessage);
678 } catch (Exception e) {
679 System.err.println("Unable to read response " + e.getMessage() );
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;
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;
710 os = uc.getOutputStream();
711 //os.write( postData );
713 } catch (ProtocolException pe) {
714 // Rcvd error instead of 100-Continue
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) {
722 rc = uc.getResponseCode();
723 logger.info( "http response code:" + rc );
724 responsemessage = uc.getResponseMessage();
725 logger.info( "responsemessage=" + responsemessage );
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);
733 int i = h0.indexOf(' ');
734 int j = h0.indexOf(' ', i + 1);
735 if (i != -1 && j != -1) {
736 responsemessage = h0.substring(j + 1);
740 if (rc >= 200 && rc < 300 ) {
741 responseBody = bodyToString( uc.getInputStream() );
742 logger.info( "responseBody=" + responseBody );
744 } else if ( rc == 404 ) {
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() );
753 err.setMessage(responsemessage);
756 } catch (ConnectException ce) {
757 errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
759 err.setMessage("Backend connection refused");
760 } catch (SocketException se) {
761 errorLogger.error( DmaapbcLogMessageEnum.SOCKET_EXCEPTION, se.getMessage(), "response from Prov server" );
763 err.setMessage( "Unable to read response from DR");
764 } catch (Exception e) {
765 logger.warn("Unable to read response " );
768 err.setCode( uc.getResponseCode());
769 err.setMessage(uc.getResponseMessage());
770 } catch (Exception e2) {
772 err.setMessage("Unable to determine response message");
777 } catch ( Exception e ) {}
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;
791 uc.setRequestMethod("DELETE");
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;
804 os = uc.getOutputStream();
805 //os.write( postData );
807 } catch (ProtocolException pe) {
808 // Rcvd error instead of 100-Continue
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) {
816 rc = uc.getResponseCode();
817 logger.info( "http response code:" + rc );
818 responsemessage = uc.getResponseMessage();
819 logger.info( "responsemessage=" + responsemessage );
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);
827 int i = h0.indexOf(' ');
828 int j = h0.indexOf(' ', i + 1);
829 if (i != -1 && j != -1) {
830 responsemessage = h0.substring(j + 1);
836 responseBody = bodyToString( uc.getInputStream() );
837 logger.info( "responseBody=" + responseBody );
839 err.setMessage(responsemessage);
842 } catch (ConnectException ce) {
843 errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
845 err.setMessage("Backend connection refused");
846 } catch (Exception e) {
847 System.err.println("Unable to read response " );
856 public static void main( String[] args ) throws Exception {
857 PropertyConfigurator.configure("log4j.properties");
858 logger.info("Started.");
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()) ;
863 Feed tst = new Feed( "dgl feed " + i,
865 "dgl feed " + i + "for testing",
869 ArrayList<DR_Pub> pubs = new ArrayList<DR_Pub>();
870 pubs.add( new DR_Pub( "centralLocation" ) );
874 DrProvConnection context = new DrProvConnection();
875 rc = context.makeFeedConnection();
876 logger.info( "makeFeedConnection returns " + rc);
877 ApiError err = new ApiError();
879 String tmp = context.doPostFeed( tst, err );
880 logger.info( "doPostFeed returns " + tmp);