Fix sonar issues in dmaap-mr-filter 88/84488/1
authorParshad Patel <pars.patel@samsung.com>
Mon, 8 Apr 2019 08:05:24 +0000 (17:05 +0900)
committerParshad Patel <pars.patel@samsung.com>
Mon, 8 Apr 2019 08:08:07 +0000 (17:08 +0900)
Fix A "NullPointerException" could be thrown; "jsonObj" is nullable here.
Either log or rethrow this exception.
Rename field "defaultLength"

Issue-ID: DMAAP-894
Change-Id: I663eda653af6b2e092ba10e3fca44f86870d9ef1
Signed-off-by: Parshad Patel <pars.patel@samsung.com>
src/main/java/org/onap/dmaap/mr/filter/ContentLengthFilter.java
src/main/java/org/onap/dmaap/mr/filter/DefaultLength.java

index a175b16..dbf4246 100644 (file)
@@ -74,8 +74,6 @@ public class ContentLengthFilter implements Filter {
         */
        public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException,
                        ServletException {
-               // TODO Auto-generated method stub
-               // place your code here
                log.info("inside servlet do filter content length checking before pub/sub");
                HttpServletRequest request = (HttpServletRequest) req;
                JSONObject jsonObj = null;
@@ -105,12 +103,14 @@ public class ContentLengthFilter implements Filter {
                                chain.doFilter(req, res);
                        }
                } catch (CambriaApiException | NumberFormatException e) {
-                       log.error("message size is greater then default");
-                       ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_EXPECTATION_FAILED,
-                                       DMaaPResponseCode.MSG_SIZE_EXCEEDS_MSG_LIMIT.getResponseCode(), errorMessages.getMsgSizeExceeds()
-                                                       + jsonObj.toString());
-                       log.info(errRes.toString());
-                       
+                       log.error("message size is greater then default", e);
+            if (jsonObj != null) {
+                ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_EXPECTATION_FAILED,
+                        DMaaPResponseCode.MSG_SIZE_EXCEEDS_MSG_LIMIT.getResponseCode(),
+                        errorMessages.getMsgSizeExceeds()
+                                + jsonObj.toString());
+                log.info(errRes.toString());
+            }
                }
 
        }
@@ -119,14 +119,13 @@ public class ContentLengthFilter implements Filter {
         * @see Filter#init(FilterConfig)
         */
        public void init(FilterConfig fConfig) throws ServletException {
-               // TODO Auto-generated method stub
                this.filterConfig = fConfig;
                log.info("Filter Content Length Initialize");
                ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(fConfig
                                .getServletContext());
                DefaultLength defLength = (DefaultLength) ctx.getBean("defLength");
-               DMaaPErrorMessages errorMessages = (DMaaPErrorMessages) ctx.getBean("DMaaPErrorMessages");
-               this.errorMessages = errorMessages;
+               DMaaPErrorMessages errMessages = (DMaaPErrorMessages) ctx.getBean("DMaaPErrorMessages");
+               this.errorMessages = errMessages;
                this.defaultLength = defLength;
 
        }
index 598ef1b..3425823 100644 (file)
@@ -24,14 +24,14 @@ package org.onap.dmaap.mr.filter;
 
 public class DefaultLength {
        
-       String defaultLength;
+       String defLength;
 
        public String getDefaultLength() {
-               return defaultLength;
+               return defLength;
        }
 
        public void setDefaultLength(String defaultLength) {
-               this.defaultLength = defaultLength;
+               this.defLength = defaultLength;
        }
 
 }