X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Fdmaap%2Fdbcapi%2Fclient%2FDrProvConnection.java;h=3cb3d672b92bfe712bfb03546d611236d0b4c041;hb=d6ac6f8b10e90411dd650d6f7a9ee51179e39bfc;hp=9807fba7e557ae2cad46528fa2e140c7151eec55;hpb=0bff051a842b164b680bc938f4a56db435dd5841;p=dmaap%2Fdbcapi.git diff --git a/src/main/java/org/onap/dmaap/dbcapi/client/DrProvConnection.java b/src/main/java/org/onap/dmaap/dbcapi/client/DrProvConnection.java index 9807fba..3cb3d67 100644 --- a/src/main/java/org/onap/dmaap/dbcapi/client/DrProvConnection.java +++ b/src/main/java/org/onap/dmaap/dbcapi/client/DrProvConnection.java @@ -3,6 +3,8 @@ * org.onap.dmaap * ================================================================================ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * + * Modifications Copyright (C) 2019 IBM. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,29 +22,21 @@ package org.onap.dmaap.dbcapi.client; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.OutputStream; -import java.net.ConnectException; -import java.net.ProtocolException; -import java.net.SocketException; -import java.net.URL; -import java.util.ArrayList; - -import javax.net.ssl.HttpsURLConnection; - -import org.apache.log4j.Logger; -import org.apache.log4j.PropertyConfigurator; import org.onap.dmaap.dbcapi.logging.BaseLoggingClass; import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum; import org.onap.dmaap.dbcapi.model.ApiError; -import org.onap.dmaap.dbcapi.model.DR_Pub; import org.onap.dmaap.dbcapi.model.DR_Sub; import org.onap.dmaap.dbcapi.model.Feed; import org.onap.dmaap.dbcapi.service.DmaapService; -import org.onap.dmaap.dbcapi.util.RandomInteger; +import org.onap.dmaap.dbcapi.util.DmaapConfig; + +import javax.net.ssl.HttpsURLConnection; +import java.io.*; +import java.net.ConnectException; +import java.net.ProtocolException; +import java.net.SocketException; +import java.net.URL; +import java.util.Arrays; @@ -50,6 +44,10 @@ public class DrProvConnection extends BaseLoggingClass { private String provURL; + private String provApi; + private String behalfHeader; + private String feedContentType; + private String subContentType; private HttpsURLConnection uc; @@ -59,6 +57,13 @@ public class DrProvConnection extends BaseLoggingClass { if ( provURL.length() < 1 ) { errorLogger.error( DmaapbcLogMessageEnum.PREREQ_DMAAP_OBJECT, "getDrProvUrl"); } + DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig(); + provApi = p.getProperty( "DR.provApi", "ONAP" ); + behalfHeader = p.getProperty( "DR.onBehalfHeader", "X-DMAAP-DR-ON-BEHALF-OF"); + feedContentType = p.getProperty( "DR.feedContentType", "application/vnd.dmaap-dr.feed"); + subContentType = p.getProperty( "DR.subContentType", "application/vnd.dmaap-dr.subscription"); + logger.info( "provURL=" + provURL + " provApi=" + provApi + " behalfHeader=" + behalfHeader + + " feedContentType=" + feedContentType + " subContentType=" + subContentType ); } @@ -143,16 +148,16 @@ public class DrProvConnection extends BaseLoggingClass { public String doPostFeed( Feed postFeed, ApiError err ) { byte[] postData = postFeed.getBytes(); - logger.info( "post fields=" + postData.toString() ); + logger.info( "post fields=" + Arrays.toString(postData) ); String responsemessage = null; String responseBody = null; try { logger.info( "uc=" + uc ); uc.setRequestMethod("POST"); - uc.setRequestProperty("Content-Type", "application/vnd.att-dr.feed"); + uc.setRequestProperty("Content-Type", feedContentType); uc.setRequestProperty( "charset", "utf-8"); - uc.setRequestProperty( "X-ATT-DR-ON-BEHALF-OF", postFeed.getOwner() ); + uc.setRequestProperty( behalfHeader, postFeed.getOwner() ); uc.setRequestProperty( "Content-Length", Integer.toString( postData.length )); uc.setUseCaches(false); uc.setDoOutput(true); @@ -238,19 +243,10 @@ public class DrProvConnection extends BaseLoggingClass { try { uc.setRequestMethod("POST"); -// uc.setRequestProperty("Content-Type", "application/vnd.att-dr.feed"); -// uc.setRequestProperty( "charset", "utf-8"); -// uc.setRequestProperty( "X-ATT-DR-ON-BEHALF-OF", postFeed.getOwner() ); -// uc.setRequestProperty( "Content-Length", Integer.toString( postData.length )); -// uc.setUseCaches(false); -// uc.setDoOutput(true); - OutputStream os = null; - - + + try { uc.connect(); - os = uc.getOutputStream(); - } catch (ProtocolException pe) { // Rcvd error instead of 100-Continue @@ -259,6 +255,7 @@ public class DrProvConnection extends BaseLoggingClass { // without this, Java will connect multiple times to the server to run the same request uc.setDoOutput(false); } catch (Exception e) { + logger.error(e.getMessage(), e); } } rc = uc.getResponseCode(); @@ -273,12 +270,14 @@ public class DrProvConnection extends BaseLoggingClass { err.setMessage(responsemessage); } } catch (Exception e) { - System.err.println("Unable to read response " ); + logger.error("Unable to read response " ); e.printStackTrace(); } finally { try { uc.disconnect(); - } catch ( Exception e ) {} + } catch ( Exception e ) { + logger.error(e.getMessage(), e); + } } return rc; @@ -287,7 +286,7 @@ public class DrProvConnection extends BaseLoggingClass { public String doPostDr_Sub( DR_Sub postSub, ApiError err ) { logger.info( "entry: doPostDr_Sub() " ); - byte[] postData = postSub.getBytes(); + byte[] postData = postSub.getBytes(provApi ); logger.info( "post fields=" + postData ); String responsemessage = null; String responseBody = null; @@ -296,9 +295,9 @@ public class DrProvConnection extends BaseLoggingClass { uc.setRequestMethod("POST"); - uc.setRequestProperty("Content-Type", "application/vnd.att-dr.subscription"); + uc.setRequestProperty("Content-Type", subContentType ); uc.setRequestProperty( "charset", "utf-8"); - uc.setRequestProperty( "X-ATT-DR-ON-BEHALF-OF", "DGL" ); + uc.setRequestProperty( behalfHeader, "DGL" ); uc.setRequestProperty( "Content-Length", Integer.toString( postData.length )); uc.setUseCaches(false); uc.setDoOutput(true); @@ -361,16 +360,16 @@ public class DrProvConnection extends BaseLoggingClass { public String doPutFeed(Feed putFeed, ApiError err) { byte[] postData = putFeed.getBytes(); - logger.info( "post fields=" + postData.toString() ); + logger.info( "post fields=" + Arrays.toString(postData) ); String responsemessage = null; String responseBody = null; try { logger.info( "uc=" + uc ); uc.setRequestMethod("PUT"); - uc.setRequestProperty("Content-Type", "application/vnd.att-dr.feed"); + uc.setRequestProperty("Content-Type", feedContentType ); uc.setRequestProperty( "charset", "utf-8"); - uc.setRequestProperty( "X-ATT-DR-ON-BEHALF-OF", putFeed.getOwner() ); + uc.setRequestProperty( behalfHeader, putFeed.getOwner() ); uc.setRequestProperty( "Content-Length", Integer.toString( postData.length )); uc.setUseCaches(false); uc.setDoOutput(true); @@ -412,7 +411,7 @@ public class DrProvConnection extends BaseLoggingClass { if (rc >= 200 && rc < 300 ) { responseBody = bodyToString( uc.getInputStream() ); logger.info( "responseBody=" + responseBody ); - + err.setCode( rc ); } else if ( rc == 404 ) { err.setCode( rc ); err.setFields( "feedid"); @@ -452,7 +451,7 @@ public class DrProvConnection extends BaseLoggingClass { } public String doPutDr_Sub(DR_Sub postSub, ApiError err) { logger.info( "entry: doPutDr_Sub() " ); - byte[] postData = postSub.getBytes(); + byte[] postData = postSub.getBytes(provApi); logger.info( "post fields=" + postData ); String responsemessage = null; String responseBody = null; @@ -461,9 +460,9 @@ public class DrProvConnection extends BaseLoggingClass { uc.setRequestMethod("PUT"); - uc.setRequestProperty("Content-Type", "application/vnd.att-dr.subscription"); + uc.setRequestProperty("Content-Type", subContentType ); uc.setRequestProperty( "charset", "utf-8"); - uc.setRequestProperty( "X-ATT-DR-ON-BEHALF-OF", "DGL" ); + uc.setRequestProperty( behalfHeader, "DGL" ); uc.setRequestProperty( "Content-Length", Integer.toString( postData.length )); uc.setUseCaches(false); uc.setDoOutput(true); @@ -516,8 +515,8 @@ public class DrProvConnection extends BaseLoggingClass { err.setCode( 500 ); err.setMessage("Backend connection refused"); } catch (Exception e) { - System.err.println("Unable to read response " ); - e.printStackTrace(); + logger.error("Unable to read response " ); + logger.error(e.getMessage(), e); } finally { uc.disconnect(); } @@ -537,22 +536,13 @@ public class DrProvConnection extends BaseLoggingClass { logger.info( "templog:doGetNodes at 12.10.14.11" ); uc.setRequestMethod("GET"); - - //uc.setRequestProperty("Content-Type", "application/vnd.att-dr.subscription"); - //uc.setRequestProperty( "charset", "utf-8"); - //uc.setRequestProperty( "X-ATT-DR-ON-BEHALF-OF", "DGL" ); - //uc.setRequestProperty( "Content-Length", Integer.toString( postData.length )); - //uc.setUseCaches(false); - //uc.setDoOutput(true); - OutputStream os = null; int rc = -1; logger.info( "templog:doGetNodes at 12.10.14.12" ); try { - uc.connect(); - logger.info( "templog:doGetNodes at 12.10.14.13" ); - //os = uc.getOutputStream(); - //os.write( postData ); + uc.connect(); + logger.info( "templog:doGetNodes at 12.10.14.13" ); + } catch (ProtocolException pe) { logger.info( "templog:doGetNodes at 12.10.14.14" ); @@ -623,9 +613,9 @@ public class DrProvConnection extends BaseLoggingClass { uc.setRequestMethod("PUT"); - //uc.setRequestProperty("Content-Type", "application/vnd.att-dr.subscription"); + //uc.setRequestProperty("Content-Type", subContentType ); //uc.setRequestProperty( "charset", "utf-8"); - //uc.setRequestProperty( "X-ATT-DR-ON-BEHALF-OF", "DGL" ); + //uc.setRequestProperty( behalfHeader, "DGL" ); //uc.setRequestProperty( "Content-Length", Integer.toString( postData.length )); uc.setUseCaches(false); //uc.setDoOutput(true); @@ -695,9 +685,9 @@ public class DrProvConnection extends BaseLoggingClass { try { logger.info( "uc=" + uc ); uc.setRequestMethod("DELETE"); - uc.setRequestProperty("Content-Type", "application/vnd.att-dr.feed"); + uc.setRequestProperty("Content-Type", feedContentType ); uc.setRequestProperty( "charset", "utf-8"); - uc.setRequestProperty( "X-ATT-DR-ON-BEHALF-OF", putFeed.getOwner() ); + uc.setRequestProperty( behalfHeader, putFeed.getOwner() ); //uc.setRequestProperty( "Content-Length", Integer.toString( postData.length )); uc.setUseCaches(false); uc.setDoOutput(true); @@ -778,6 +768,79 @@ public class DrProvConnection extends BaseLoggingClass { return responseBody; } + public String doDeleteDr_Sub(DR_Sub delSub, ApiError err) { + logger.info( "entry: doDeleteDr_Sub() " ); + byte[] postData = delSub.getBytes(provApi); + logger.info( "post fields=" + postData ); + String responsemessage = null; + String responseBody = null; + + try { + + uc.setRequestMethod("DELETE"); + + uc.setRequestProperty("Content-Type", subContentType); + uc.setRequestProperty( "charset", "utf-8"); + uc.setRequestProperty( behalfHeader, "DGL" ); + //uc.setRequestProperty( "Content-Length", Integer.toString( postData.length )); + uc.setUseCaches(false); + uc.setDoOutput(true); + OutputStream os = null; + int rc = -1; + + try { + uc.connect(); + os = uc.getOutputStream(); + //os.write( postData ); + + } catch (ProtocolException pe) { + // Rcvd error instead of 100-Continue + try { + // work around glitch in Java 1.7.0.21 and likely others + // without this, Java will connect multiple times to the server to run the same request + uc.setDoOutput(false); + } catch (Exception e) { + } + } + rc = uc.getResponseCode(); + logger.info( "http response code:" + rc ); + responsemessage = uc.getResponseMessage(); + logger.info( "responsemessage=" + responsemessage ); + + + if (responsemessage == null) { + // work around for glitch in Java 1.7.0.21 and likely others + // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is + String h0 = uc.getHeaderField(0); + if (h0 != null) { + int i = h0.indexOf(' '); + int j = h0.indexOf(' ', i + 1); + if (i != -1 && j != -1) { + responsemessage = h0.substring(j + 1); + } + } + } + err.setCode(rc); + if (rc == 204 ) { + responseBody = bodyToString( uc.getInputStream() ); + logger.info( "responseBody=" + responseBody ); + } else { + err.setMessage(responsemessage); + } + + } catch (ConnectException ce) { + errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() ); + err.setCode( 500 ); + err.setMessage("Backend connection refused"); + } catch (Exception e) { + System.err.println("Unable to read response " ); + e.printStackTrace(); + } finally { + uc.disconnect(); + } + return responseBody; + + } /* public static void main( String[] args ) throws Exception { PropertyConfigurator.configure("log4j.properties");