From e696f4e0828d2f26921e233aeede05c4359770a7 Mon Sep 17 00:00:00 2001 From: Parshad Patel Date: Thu, 6 Sep 2018 17:29:03 +0900 Subject: Fix NPE issue in msgrt Fix NullPointerException related sonar issue Issue-ID: DMAAP-687 Change-Id: I6a2f32398fdef79d0ea5c1abbbe8b3b803873594 Signed-off-by: Parshad Patel --- .../com/att/dmf/mr/utils/DMaaPResponseBuilder.java | 33 +++++++++------------- 1 file changed, 13 insertions(+), 20 deletions(-) (limited to 'src/main/java/com/att/dmf/mr/utils') diff --git a/src/main/java/com/att/dmf/mr/utils/DMaaPResponseBuilder.java b/src/main/java/com/att/dmf/mr/utils/DMaaPResponseBuilder.java index 4c38d57..ddd834c 100644 --- a/src/main/java/com/att/dmf/mr/utils/DMaaPResponseBuilder.java +++ b/src/main/java/com/att/dmf/mr/utils/DMaaPResponseBuilder.java @@ -218,7 +218,7 @@ public class DMaaPResponseBuilder { /** * interface used to define write method for outputStream */ - public static abstract interface StreamWriter { + public abstract static interface StreamWriter { /** * abstract method used to write the response * @@ -252,27 +252,20 @@ public class DMaaPResponseBuilder { boolean fResponseEntityAllowed = (!(ctx.getRequest().getMethod().equalsIgnoreCase("HEAD"))); - - OutputStream os = null; - try{ + if (fResponseEntityAllowed) { - os = ctx.getResponse().getOutputStream(); - return os; + try(OutputStream os = ctx.getResponse().getOutputStream()){ + return os; + }catch (Exception e){ + log.error("Exception in getStreamForBinaryResponse",e); + throw new IOException(); + } } else { - os = new NullStream(); - return os; - } - }catch (Exception e){ - throw new IOException(); - - } - finally{ - if(null != os){ - try{ - os.close(); - }catch(Exception e) { - - } + try(OutputStream os = new NullStream()){ + return os; + }catch (Exception e){ + log.error("Exception in getStreamForBinaryResponse",e); + throw new IOException(); } } } -- cgit 1.2.3-korg