X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Fdmaap%2Fdbcapi%2Fclient%2FDrProvConnection.java;h=a8f075c91a274da92b420a9f15ccac9c085bee9a;hb=022f4cde96a57fdc6f002e09ae5d9b314e4150e3;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..a8f075c 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,9 +243,9 @@ public class DrProvConnection extends BaseLoggingClass { try { uc.setRequestMethod("POST"); -// uc.setRequestProperty("Content-Type", "application/vnd.att-dr.feed"); +// uc.setRequestProperty("Content-Type", feedContenType ); // 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); @@ -287,7 +292,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 +301,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 +366,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); @@ -452,7 +457,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 +466,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); @@ -538,9 +543,9 @@ public class DrProvConnection extends BaseLoggingClass { uc.setRequestMethod("GET"); - //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); @@ -623,9 +628,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 +700,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 +783,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");