2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.dmaap.dbcapi.client;
25 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
26 import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;
27 import org.onap.dmaap.dbcapi.model.ApiError;
28 import org.onap.dmaap.dbcapi.model.DR_Sub;
29 import org.onap.dmaap.dbcapi.model.Feed;
30 import org.onap.dmaap.dbcapi.service.DmaapService;
31 import org.onap.dmaap.dbcapi.util.DmaapConfig;
33 import javax.net.ssl.HttpsURLConnection;
35 import java.net.ConnectException;
36 import java.net.ProtocolException;
37 import java.net.SocketException;
39 import java.util.Arrays;
43 public class DrProvConnection extends BaseLoggingClass {
46 private String provURL;
47 private String provApi;
48 private String behalfHeader;
49 private String feedContentType;
50 private String subContentType;
52 private HttpsURLConnection uc;
55 public DrProvConnection() {
56 provURL = new DmaapService().getDmaap().getDrProvUrl();
57 if ( provURL.length() < 1 ) {
58 errorLogger.error( DmaapbcLogMessageEnum.PREREQ_DMAAP_OBJECT, "getDrProvUrl");
60 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
61 provApi = p.getProperty( "DR.provApi", "ONAP" );
62 behalfHeader = p.getProperty( "DR.onBehalfHeader", "X-DR-ON-BEHALF-OF");
63 feedContentType = p.getProperty( "DR.feedContentType", "application/vnc.dr.feed");
64 subContentType = p.getProperty( "DR.subContentType", "application/vnd.dr.subscription");
65 logger.info( "provURL=" + provURL + " provApi=" + provApi + " behalfHeader=" + behalfHeader
66 + " feedContentType=" + feedContentType + " subContentType=" + subContentType );
70 public boolean makeFeedConnection() {
71 return makeConnection( provURL );
73 public boolean makeFeedConnection(String feedId) {
74 return makeConnection( provURL + "/feed/" + feedId );
76 public boolean makeSubPostConnection( String subURL ) {
77 String[] parts = subURL.split("/");
78 String revisedURL = provURL + "/" + parts[3] + "/" + parts[4];
79 logger.info( "mapping " + subURL + " to " + revisedURL );
80 return makeConnection( revisedURL );
82 public boolean makeSubPutConnection( String subId ) {
83 String revisedURL = provURL + "/subs/" + subId;
84 logger.info( "mapping " + subId + " to " + revisedURL );
85 return makeConnection( revisedURL );
88 public boolean makeIngressConnection( String feed, String user, String subnet, String nodep ) {
89 String uri = String.format("/internal/route/ingress/?feed=%s&user=%s&subnet=%s&nodepatt=%s",
90 feed, user, subnet, nodep );
91 return makeConnection( provURL + uri );
93 public boolean makeEgressConnection( String sub, String nodep ) {
94 String uri = String.format("/internal/route/egress/?sub=%s&node=%s",
96 return makeConnection( provURL + uri );
98 public boolean makeNodesConnection( String varName ) {
100 String uri = String.format("/internal/api/%s", varName);
101 return makeConnection( provURL + uri );
104 public boolean makeNodesConnection( String varName, String val ) {
109 String cv = val.replaceAll("\\|", "%7C");
110 String uri = String.format( "/internal/api/%s?val=%s", varName, cv );
112 return makeConnection( provURL + uri );
115 private boolean makeConnection( String pURL ) {
118 URL u = new URL( pURL );
119 uc = (HttpsURLConnection) u.openConnection();
120 uc.setInstanceFollowRedirects(false);
121 logger.info( "successful connect to " + pURL );
123 } catch (Exception e) {
124 errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_ERROR, pURL, e.getMessage() );
131 public String bodyToString( InputStream is ) {
132 logger.info( "is=" + is );
133 StringBuilder sb = new StringBuilder();
134 BufferedReader br = new BufferedReader( new InputStreamReader(is));
137 while ((line = br.readLine()) != null ) {
140 } catch (IOException ex ) {
141 errorLogger.error( DmaapbcLogMessageEnum.IO_EXCEPTION, ex.getMessage());
144 return sb.toString();
148 public String doPostFeed( Feed postFeed, ApiError err ) {
150 byte[] postData = postFeed.getBytes();
151 logger.info( "post fields=" + Arrays.toString(postData) );
152 String responsemessage = null;
153 String responseBody = null;
156 logger.info( "uc=" + uc );
157 uc.setRequestMethod("POST");
158 uc.setRequestProperty("Content-Type", feedContentType);
159 uc.setRequestProperty( "charset", "utf-8");
160 uc.setRequestProperty( behalfHeader, postFeed.getOwner() );
161 uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
162 uc.setUseCaches(false);
163 uc.setDoOutput(true);
164 OutputStream os = null;
169 os = uc.getOutputStream();
170 os.write( postData );
172 } catch (ProtocolException pe) {
173 // Rcvd error instead of 100-Continue
175 // work around glitch in Java 1.7.0.21 and likely others
176 // without this, Java will connect multiple times to the server to run the same request
177 uc.setDoOutput(false);
178 } catch (Exception e) {
181 rc = uc.getResponseCode();
182 logger.info( "http response code:" + rc );
183 responsemessage = uc.getResponseMessage();
184 logger.info( "responsemessage=" + responsemessage );
187 if (responsemessage == null) {
188 // work around for glitch in Java 1.7.0.21 and likely others
189 // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
190 String h0 = uc.getHeaderField(0);
192 int i = h0.indexOf(' ');
193 int j = h0.indexOf(' ', i + 1);
194 if (i != -1 && j != -1) {
195 responsemessage = h0.substring(j + 1);
200 responseBody = bodyToString( uc.getInputStream() );
201 logger.info( "responseBody=" + responseBody );
205 err.setMessage(responsemessage);
208 } catch (ConnectException ce) {
209 errorLogger.error(DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
211 err.setMessage("Backend connection refused");
212 } catch (SocketException se) {
213 errorLogger.error( DmaapbcLogMessageEnum.SOCKET_EXCEPTION, se.getMessage(), "response from prov server" );
215 err.setMessage( "Unable to read response from DR");
216 } catch (Exception e) {
217 logger.warn("Unable to read response " );
220 err.setCode( uc.getResponseCode());
221 err.setMessage(uc.getResponseMessage());
222 } catch (Exception e2) {
224 err.setMessage("Unable to determine response message");
230 } catch ( Exception e ) {}
237 // the POST for /internal/route/ingress doesn't return any data, so needs a different function
238 // the POST for /internal/route/egress doesn't return any data, so needs a different function
239 public int doXgressPost( ApiError err ) {
241 String responsemessage = null;
245 uc.setRequestMethod("POST");
246 // uc.setRequestProperty("Content-Type", feedContenType );
247 // uc.setRequestProperty( "charset", "utf-8");
248 // uc.setRequestProperty( behalfHeader, postFeed.getOwner() );
249 // uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
250 // uc.setUseCaches(false);
251 // uc.setDoOutput(true);
252 OutputStream os = null;
257 os = uc.getOutputStream();
260 } catch (ProtocolException pe) {
261 // Rcvd error instead of 100-Continue
263 // work around glitch in Java 1.7.0.21 and likely others
264 // without this, Java will connect multiple times to the server to run the same request
265 uc.setDoOutput(false);
266 } catch (Exception e) {
269 rc = uc.getResponseCode();
270 logger.info( "http response code:" + rc );
271 responsemessage = uc.getResponseMessage();
272 logger.info( "responsemessage=" + responsemessage );
276 if (rc < 200 || rc >= 300 ) {
278 err.setMessage(responsemessage);
280 } catch (Exception e) {
281 System.err.println("Unable to read response " );
286 } catch ( Exception e ) {}
293 public String doPostDr_Sub( DR_Sub postSub, ApiError err ) {
294 logger.info( "entry: doPostDr_Sub() " );
295 byte[] postData = postSub.getBytes(provApi );
296 logger.info( "post fields=" + postData );
297 String responsemessage = null;
298 String responseBody = null;
302 uc.setRequestMethod("POST");
304 uc.setRequestProperty("Content-Type", subContentType );
305 uc.setRequestProperty( "charset", "utf-8");
306 uc.setRequestProperty( behalfHeader, "DGL" );
307 uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
308 uc.setUseCaches(false);
309 uc.setDoOutput(true);
310 OutputStream os = null;
315 os = uc.getOutputStream();
316 os.write( postData );
318 } catch (ProtocolException pe) {
319 // Rcvd error instead of 100-Continue
321 // work around glitch in Java 1.7.0.21 and likely others
322 // without this, Java will connect multiple times to the server to run the same request
323 uc.setDoOutput(false);
324 } catch (Exception e) {
327 rc = uc.getResponseCode();
328 logger.info( "http response code:" + rc );
329 responsemessage = uc.getResponseMessage();
330 logger.info( "responsemessage=" + responsemessage );
333 if (responsemessage == null) {
334 // work around for glitch in Java 1.7.0.21 and likely others
335 // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
336 String h0 = uc.getHeaderField(0);
338 int i = h0.indexOf(' ');
339 int j = h0.indexOf(' ', i + 1);
340 if (i != -1 && j != -1) {
341 responsemessage = h0.substring(j + 1);
346 responseBody = bodyToString( uc.getInputStream() );
347 logger.info( "responseBody=" + responseBody );
351 err.setMessage(responsemessage);
354 } catch (Exception e) {
355 System.err.println("Unable to read response " );
360 } catch ( Exception e ) {}
367 public String doPutFeed(Feed putFeed, ApiError err) {
368 byte[] postData = putFeed.getBytes();
369 logger.info( "post fields=" + Arrays.toString(postData) );
370 String responsemessage = null;
371 String responseBody = null;
374 logger.info( "uc=" + uc );
375 uc.setRequestMethod("PUT");
376 uc.setRequestProperty("Content-Type", feedContentType );
377 uc.setRequestProperty( "charset", "utf-8");
378 uc.setRequestProperty( behalfHeader, putFeed.getOwner() );
379 uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
380 uc.setUseCaches(false);
381 uc.setDoOutput(true);
382 OutputStream os = null;
387 os = uc.getOutputStream();
388 os.write( postData );
390 } catch (ProtocolException pe) {
391 // Rcvd error instead of 100-Continue
393 // work around glitch in Java 1.7.0.21 and likely others
394 // without this, Java will connect multiple times to the server to run the same request
395 uc.setDoOutput(false);
396 } catch (Exception e) {
399 rc = uc.getResponseCode();
400 logger.info( "http response code:" + rc );
401 responsemessage = uc.getResponseMessage();
402 logger.info( "responsemessage=" + responsemessage );
405 if (responsemessage == null) {
406 // work around for glitch in Java 1.7.0.21 and likely others
407 // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
408 String h0 = uc.getHeaderField(0);
410 int i = h0.indexOf(' ');
411 int j = h0.indexOf(' ', i + 1);
412 if (i != -1 && j != -1) {
413 responsemessage = h0.substring(j + 1);
417 if (rc >= 200 && rc < 300 ) {
418 responseBody = bodyToString( uc.getInputStream() );
419 logger.info( "responseBody=" + responseBody );
421 } else if ( rc == 404 ) {
423 err.setFields( "feedid");
424 String message = "FeedId " + putFeed.getFeedId() + " not found on DR to update. Out-of-sync condition?";
425 err.setMessage( message );
426 errorLogger.error( DmaapbcLogMessageEnum.PROV_OUT_OF_SYNC, "Feed", putFeed.getFeedId() );
430 err.setMessage(responsemessage);
433 } catch (ConnectException ce) {
434 errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
436 err.setMessage("Backend connection refused");
437 } catch (SocketException se) {
438 errorLogger.error( DmaapbcLogMessageEnum.SOCKET_EXCEPTION, se.getMessage(), "response from Prov server" );
440 err.setMessage( "Unable to read response from DR");
441 } catch (Exception e) {
442 logger.warn("Unable to read response " );
445 err.setCode( uc.getResponseCode());
446 err.setMessage(uc.getResponseMessage());
447 } catch (Exception e2) {
449 err.setMessage("Unable to determine response message");
454 } catch ( Exception e ) {}
458 public String doPutDr_Sub(DR_Sub postSub, ApiError err) {
459 logger.info( "entry: doPutDr_Sub() " );
460 byte[] postData = postSub.getBytes(provApi);
461 logger.info( "post fields=" + postData );
462 String responsemessage = null;
463 String responseBody = null;
467 uc.setRequestMethod("PUT");
469 uc.setRequestProperty("Content-Type", subContentType );
470 uc.setRequestProperty( "charset", "utf-8");
471 uc.setRequestProperty( behalfHeader, "DGL" );
472 uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
473 uc.setUseCaches(false);
474 uc.setDoOutput(true);
475 OutputStream os = null;
480 os = uc.getOutputStream();
481 os.write( postData );
483 } catch (ProtocolException pe) {
484 // Rcvd error instead of 100-Continue
486 // work around glitch in Java 1.7.0.21 and likely others
487 // without this, Java will connect multiple times to the server to run the same request
488 uc.setDoOutput(false);
489 } catch (Exception e) {
492 rc = uc.getResponseCode();
493 logger.info( "http response code:" + rc );
494 responsemessage = uc.getResponseMessage();
495 logger.info( "responsemessage=" + responsemessage );
498 if (responsemessage == null) {
499 // work around for glitch in Java 1.7.0.21 and likely others
500 // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
501 String h0 = uc.getHeaderField(0);
503 int i = h0.indexOf(' ');
504 int j = h0.indexOf(' ', i + 1);
505 if (i != -1 && j != -1) {
506 responsemessage = h0.substring(j + 1);
511 responseBody = bodyToString( uc.getInputStream() );
512 logger.info( "responseBody=" + responseBody );
516 err.setMessage(responsemessage);
519 } catch (ConnectException ce) {
520 errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
522 err.setMessage("Backend connection refused");
523 } catch (Exception e) {
524 System.err.println("Unable to read response " );
533 public String doGetNodes( ApiError err ) {
534 logger.info( "entry: doGetNodes() " );
535 //byte[] postData = postSub.getBytes();
536 //logger.info( "get fields=" + postData );
537 String responsemessage = null;
538 String responseBody = null;
539 logger.info( "templog:doGetNodes at 12.10.14.10" );
542 logger.info( "templog:doGetNodes at 12.10.14.11" );
544 uc.setRequestMethod("GET");
546 //uc.setRequestProperty("Content-Type", subContentType );
547 //uc.setRequestProperty( "charset", "utf-8");
548 //uc.setRequestProperty( behalfHeader, "DGL" );
549 //uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
550 //uc.setUseCaches(false);
551 //uc.setDoOutput(true);
552 OutputStream os = null;
555 logger.info( "templog:doGetNodes at 12.10.14.12" );
558 logger.info( "templog:doGetNodes at 12.10.14.13" );
559 //os = uc.getOutputStream();
560 //os.write( postData );
562 } catch (ProtocolException pe) {
563 logger.info( "templog:doGetNodes at 12.10.14.14" );
564 // Rcvd error instead of 100-Continue
566 // work around glitch in Java 1.7.0.21 and likely others
567 // without this, Java will connect multiple times to the server to run the same request
568 uc.setDoOutput(false);
569 } catch (Exception e) {
572 logger.info( "templog:doGetNodes at 12.10.14.15" );
573 rc = uc.getResponseCode();
574 logger.info( "http response code:" + rc );
575 responsemessage = uc.getResponseMessage();
576 logger.info( "responsemessage=" + responsemessage );
577 logger.info( "templog:doGetNodes at 12.10.14.16" );
580 if (responsemessage == null) {
581 logger.info( "templog:doGetNodes at 12.10.14.17" );
582 // work around for glitch in Java 1.7.0.21 and likely others
583 // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
584 String h0 = uc.getHeaderField(0);
586 int i = h0.indexOf(' ');
587 int j = h0.indexOf(' ', i + 1);
588 if (i != -1 && j != -1) {
589 responsemessage = h0.substring(j + 1);
593 logger.info( "templog:doGetNodes at 12.10.14.18" );
594 err.setCode(rc); // may not really be an error, but we save rc
596 responseBody = bodyToString( uc.getInputStream() );
597 logger.info( "responseBody=" + responseBody );
599 err.setMessage(responsemessage);
602 logger.info( "templog:doGetNodes at 12.10.14.19" );
603 } catch (ConnectException ce) {
604 logger.info( "templog:doGetNodes at 12.10.14.20" );
605 errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
607 err.setMessage("Backend connection refused");
608 } catch (Exception e) {
609 logger.info( "templog:doGetNodes at 12.10.14.21" );
610 System.err.println("Unable to read response " );
613 logger.info( "templog:doGetNodes at 12.10.14.22" );
614 if ( uc != null ) uc.disconnect();
616 logger.info( "templog:doGetNodes at 12.10.14.23" );
620 public String doPutNodes( ApiError err ) {
621 logger.info( "entry: doPutNodes() " );
622 //byte[] postData = nodeList.getBytes();
623 //logger.info( "get fields=" + postData );
624 String responsemessage = null;
625 String responseBody = null;
629 uc.setRequestMethod("PUT");
631 //uc.setRequestProperty("Content-Type", subContentType );
632 //uc.setRequestProperty( "charset", "utf-8");
633 //uc.setRequestProperty( behalfHeader, "DGL" );
634 //uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
635 uc.setUseCaches(false);
636 //uc.setDoOutput(true);
637 OutputStream os = null;
642 //os = uc.getOutputStream();
643 //os.write( postData );
645 } catch (ProtocolException pe) {
646 // Rcvd error instead of 100-Continue
648 // work around glitch in Java 1.7.0.21 and likely others
649 // without this, Java will connect multiple times to the server to run the same request
650 uc.setDoOutput(false);
651 } catch (Exception e) {
654 rc = uc.getResponseCode();
655 logger.info( "http response code:" + rc );
656 responsemessage = uc.getResponseMessage();
657 logger.info( "responsemessage=" + responsemessage );
660 if (responsemessage == null) {
661 // work around for glitch in Java 1.7.0.21 and likely others
662 // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
663 String h0 = uc.getHeaderField(0);
665 int i = h0.indexOf(' ');
666 int j = h0.indexOf(' ', i + 1);
667 if (i != -1 && j != -1) {
668 responsemessage = h0.substring(j + 1);
674 responseBody = bodyToString( uc.getInputStream() );
675 logger.info( "responseBody=" + responseBody );
679 err.setMessage(responsemessage);
682 } catch (Exception e) {
683 System.err.println("Unable to read response " + e.getMessage() );
694 public String doDeleteFeed(Feed putFeed, ApiError err) {
695 //byte[] postData = putFeed.getBytes();
696 //logger.info( "post fields=" + postData.toString() );
697 String responsemessage = null;
698 String responseBody = null;
701 logger.info( "uc=" + uc );
702 uc.setRequestMethod("DELETE");
703 uc.setRequestProperty("Content-Type", feedContentType );
704 uc.setRequestProperty( "charset", "utf-8");
705 uc.setRequestProperty( behalfHeader, putFeed.getOwner() );
706 //uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
707 uc.setUseCaches(false);
708 uc.setDoOutput(true);
709 OutputStream os = null;
714 os = uc.getOutputStream();
715 //os.write( postData );
717 } catch (ProtocolException pe) {
718 // Rcvd error instead of 100-Continue
720 // work around glitch in Java 1.7.0.21 and likely others
721 // without this, Java will connect multiple times to the server to run the same request
722 uc.setDoOutput(false);
723 } catch (Exception e) {
726 rc = uc.getResponseCode();
727 logger.info( "http response code:" + rc );
728 responsemessage = uc.getResponseMessage();
729 logger.info( "responsemessage=" + responsemessage );
732 if (responsemessage == null) {
733 // work around for glitch in Java 1.7.0.21 and likely others
734 // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
735 String h0 = uc.getHeaderField(0);
737 int i = h0.indexOf(' ');
738 int j = h0.indexOf(' ', i + 1);
739 if (i != -1 && j != -1) {
740 responsemessage = h0.substring(j + 1);
744 if (rc >= 200 && rc < 300 ) {
745 responseBody = bodyToString( uc.getInputStream() );
746 logger.info( "responseBody=" + responseBody );
748 } else if ( rc == 404 ) {
750 err.setFields( "feedid");
751 String message = "FeedId " + putFeed.getFeedId() + " not found on DR to update. Out-of-sync condition?";
752 err.setMessage( message );
753 errorLogger.error( DmaapbcLogMessageEnum.PROV_OUT_OF_SYNC, "Feed", putFeed.getFeedId() );
757 err.setMessage(responsemessage);
760 } catch (ConnectException ce) {
761 errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
763 err.setMessage("Backend connection refused");
764 } catch (SocketException se) {
765 errorLogger.error( DmaapbcLogMessageEnum.SOCKET_EXCEPTION, se.getMessage(), "response from Prov server" );
767 err.setMessage( "Unable to read response from DR");
768 } catch (Exception e) {
769 logger.warn("Unable to read response " );
772 err.setCode( uc.getResponseCode());
773 err.setMessage(uc.getResponseMessage());
774 } catch (Exception e2) {
776 err.setMessage("Unable to determine response message");
781 } catch ( Exception e ) {}
786 public String doDeleteDr_Sub(DR_Sub delSub, ApiError err) {
787 logger.info( "entry: doDeleteDr_Sub() " );
788 byte[] postData = delSub.getBytes(provApi);
789 logger.info( "post fields=" + postData );
790 String responsemessage = null;
791 String responseBody = null;
795 uc.setRequestMethod("DELETE");
797 uc.setRequestProperty("Content-Type", subContentType);
798 uc.setRequestProperty( "charset", "utf-8");
799 uc.setRequestProperty( behalfHeader, "DGL" );
800 //uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
801 uc.setUseCaches(false);
802 uc.setDoOutput(true);
803 OutputStream os = null;
808 os = uc.getOutputStream();
809 //os.write( postData );
811 } catch (ProtocolException pe) {
812 // Rcvd error instead of 100-Continue
814 // work around glitch in Java 1.7.0.21 and likely others
815 // without this, Java will connect multiple times to the server to run the same request
816 uc.setDoOutput(false);
817 } catch (Exception e) {
820 rc = uc.getResponseCode();
821 logger.info( "http response code:" + rc );
822 responsemessage = uc.getResponseMessage();
823 logger.info( "responsemessage=" + responsemessage );
826 if (responsemessage == null) {
827 // work around for glitch in Java 1.7.0.21 and likely others
828 // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is
829 String h0 = uc.getHeaderField(0);
831 int i = h0.indexOf(' ');
832 int j = h0.indexOf(' ', i + 1);
833 if (i != -1 && j != -1) {
834 responsemessage = h0.substring(j + 1);
840 responseBody = bodyToString( uc.getInputStream() );
841 logger.info( "responseBody=" + responseBody );
843 err.setMessage(responsemessage);
846 } catch (ConnectException ce) {
847 errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() );
849 err.setMessage("Backend connection refused");
850 } catch (Exception e) {
851 System.err.println("Unable to read response " );
860 public static void main( String[] args ) throws Exception {
861 PropertyConfigurator.configure("log4j.properties");
862 logger.info("Started.");
864 RandomInteger ri = new RandomInteger(10000);
865 //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()) ;
867 Feed tst = new Feed( "dgl feed " + i,
869 "dgl feed " + i + "for testing",
873 ArrayList<DR_Pub> pubs = new ArrayList<DR_Pub>();
874 pubs.add( new DR_Pub( "centralLocation" ) );
878 DrProvConnection context = new DrProvConnection();
879 rc = context.makeFeedConnection();
880 logger.info( "makeFeedConnection returns " + rc);
881 ApiError err = new ApiError();
883 String tmp = context.doPostFeed( tst, err );
884 logger.info( "doPostFeed returns " + tmp);