From e0ae7c2e91e4aa4598383532a3b1abea76eefb1c Mon Sep 17 00:00:00 2001 From: "Lott, Christopher (cl778h)" Date: Wed, 17 Jul 2019 13:23:55 -0400 Subject: Repair null-pointer exception in init method Change-Id: I0669adbbc67987b3e7034fa1af5e1376e156b52d Issue-ID: PORTAL-657 Signed-off-by: Lott, Christopher (cl778h) --- .../core/onboarding/crossapi/CadiAuthFilter.java | 82 ++++++++++------------ 1 file changed, 37 insertions(+), 45 deletions(-) diff --git a/ecomp-sdk/epsdk-fw/src/main/java/org/onap/portalsdk/core/onboarding/crossapi/CadiAuthFilter.java b/ecomp-sdk/epsdk-fw/src/main/java/org/onap/portalsdk/core/onboarding/crossapi/CadiAuthFilter.java index 66effc0f..2336a514 100644 --- a/ecomp-sdk/epsdk-fw/src/main/java/org/onap/portalsdk/core/onboarding/crossapi/CadiAuthFilter.java +++ b/ecomp-sdk/epsdk-fw/src/main/java/org/onap/portalsdk/core/onboarding/crossapi/CadiAuthFilter.java @@ -40,6 +40,7 @@ package org.onap.portalsdk.core.onboarding.crossapi; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; +import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -49,81 +50,73 @@ import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.onap.aaf.cadi.filter.CadiFilter; import org.onap.portalsdk.core.onboarding.util.AuthUtil; -import org.onap.portalsdk.core.onboarding.util.CipherUtil; import org.onap.portalsdk.core.onboarding.util.PortalApiConstants; import org.onap.portalsdk.core.onboarding.util.PortalApiProperties; public class CadiAuthFilter extends CadiFilter { - private static String include_url_endpoints =""; - private static String exclude_url_endpoints = ""; + private static final Log logger = LogFactory.getLog(CadiAuthFilter.class); + public static final String AUTHORIZATION = "Authorization"; - private static final Log logger = LogFactory.getLog(CipherUtil.class); - + public static final String EXCLUDE_URL_ENDPOINTS = "exclude_url_endpoints"; + public static final String INCLUDE_URL_ENDPOINTS = "include_url_endpoints"; + public static final String REMOTE = "remote"; + private List includeUrlEndPointList; + private List excludeUrlEndPointList; + public void init(FilterConfig filterConfig) throws ServletException { super.init(filterConfig); - include_url_endpoints = filterConfig.getInitParameter("include_url_endpoints"); - exclude_url_endpoints = filterConfig.getInitParameter("exclude_url_endpoints"); - logger.debug("include_url_endpoints values: "+ include_url_endpoints); - logger.debug("exclude_url_endpoints values: "+ exclude_url_endpoints); + String include_url_endpoints = filterConfig.getInitParameter(INCLUDE_URL_ENDPOINTS); + String exclude_url_endpoints = filterConfig.getInitParameter(EXCLUDE_URL_ENDPOINTS); + logger.debug(INCLUDE_URL_ENDPOINTS + ": " + include_url_endpoints); + logger.debug(EXCLUDE_URL_ENDPOINTS + ": " + exclude_url_endpoints); + if (include_url_endpoints == null || include_url_endpoints.isEmpty()) + throw new ServletException("Filter init parameter " + INCLUDE_URL_ENDPOINTS + " is null or empty"); + if (exclude_url_endpoints == null || exclude_url_endpoints.isEmpty()) + throw new ServletException("Filter init parameter " + EXCLUDE_URL_ENDPOINTS + " is null or empty"); + includeUrlEndPointList = new ArrayList(Arrays.asList(include_url_endpoints.split(","))); + excludeUrlEndPointList = new ArrayList(Arrays.asList(exclude_url_endpoints.split(","))); } - public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) // throws IOException, ServletException { - - if (include_url_endpoints.equals("") || include_url_endpoints == null || include_url_endpoints.isEmpty()) { - throw new NullPointerException("inlclude_url_endpoints is null"); + if (excludeFilter(request, excludeUrlEndPointList)) { + logger.debug("doFilter: request excluded from cadifilter"); + chain.doFilter(request, response); + } else if (includeFilter(request, includeUrlEndPointList)) { + logger.debug("doFilter: request is entering cadifilter"); + super.doFilter(request, response, chain); } else { - String includeUrlEndPointString = include_url_endpoints; - if (exclude_url_endpoints.equals("") || exclude_url_endpoints == null || exclude_url_endpoints.isEmpty()) { - throw new NullPointerException("exculde_url_endpoints is null"); - } - String excludeUrlEndPointString = exclude_url_endpoints; - ArrayList excludeUrlEndPointList = new ArrayList( - Arrays.asList(excludeUrlEndPointString.split(","))); - ArrayList includeUrlEndPointList = new ArrayList( - Arrays.asList(includeUrlEndPointString.split(","))); - if (excludeFilter(request, excludeUrlEndPointList)){ - logger.debug("Request excluded from cadifilter"); - chain.doFilter(request, response); - }else if (includeFilter(request, includeUrlEndPointList)){ - logger.debug("Request is entering cadifilter"); - super.doFilter(request, response, chain); - }else - chain.doFilter(request, response); + chain.doFilter(request, response); } } - private String getUrl(ServletRequest request) { - String path = ""; HttpServletRequest httpRequest = (HttpServletRequest) request; - path = httpRequest.getRequestURI().substring(httpRequest.getContextPath().length() + 1); - return path; + return httpRequest.getRequestURI().substring(httpRequest.getContextPath().length() + 1); } - private boolean excludeFilter(ServletRequest request, ArrayList excludeUrlEndPointList) { + private boolean excludeFilter(ServletRequest request, List excludeUrlEndPointList) { boolean isUrlExcluded = false; - String Path = getUrl(request); - + String path = getUrl(request); for (String str : excludeUrlEndPointList) { if (!isUrlExcluded) - isUrlExcluded = AuthUtil.matchPattern(Path, str.substring(1)); + isUrlExcluded = AuthUtil.matchPattern(path, str.substring(1)); } return isUrlExcluded; } - private boolean includeFilter(ServletRequest request, ArrayList includeapisList) { + private boolean includeFilter(ServletRequest request, List includeapisList) { boolean isauthenticated = false; HttpServletRequest httpRequest = (HttpServletRequest) request; - - if(httpRequest.getHeader(AUTHORIZATION) == null) + + if (httpRequest.getHeader(AUTHORIZATION) == null) return isauthenticated; // TODO: refactor to have exclusion pattern String path = httpRequest.getRequestURI().substring(httpRequest.getContextPath().length() + 1); @@ -131,8 +124,8 @@ public class CadiAuthFilter extends CadiFilter { if (!isauthenticated) isauthenticated = matchPattern(path, str); } - if (isauthenticated && PortalApiProperties.getProperty(PortalApiConstants.ROLE_ACCESS_CENTRALIZED) - .equalsIgnoreCase("remote")) + if (isauthenticated + && PortalApiProperties.getProperty(PortalApiConstants.ROLE_ACCESS_CENTRALIZED).equalsIgnoreCase(REMOTE)) isauthenticated = true; else isauthenticated = false; @@ -154,7 +147,6 @@ public class CadiAuthFilter extends CadiFilter { } else if (roleFunctionArray[i].equals("*")) { match = true; } - } } if (match) -- cgit 1.2.3-korg