From ab54dda56c7b2d765d864b7042e8cf2b37606a93 Mon Sep 17 00:00:00 2001 From: megala21 Date: Tue, 17 Oct 2017 09:36:59 +0530 Subject: [PATCH] Refactoring --- .../framework/WebappAuthenticationValve.java | 36 +++++++++---------- .../WebappAuthenticationValveTest.java | 3 -- 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/WebappAuthenticationValve.java b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/WebappAuthenticationValve.java index 3dd4331ef5..c1cf0897dc 100644 --- a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/WebappAuthenticationValve.java +++ b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/WebappAuthenticationValve.java @@ -110,7 +110,7 @@ public class WebappAuthenticationValve extends CarbonTomcatValve { if (uri == null) { uri = ""; } - if(!uri.endsWith("/")) { + if (!uri.endsWith("/")) { uri = uri + "/"; } String contextPath = request.getContextPath(); @@ -125,7 +125,7 @@ public class WebappAuthenticationValve extends CarbonTomcatValve { while (tokenizer.hasMoreTokens()) { skippedEndPoint = tokenizer.nextToken(); skippedEndPoint = skippedEndPoint.replace("\n", "").replace("\r", "").trim(); - if(!skippedEndPoint.endsWith("/")) { + if (!skippedEndPoint.endsWith("/")) { skippedEndPoint = skippedEndPoint + "/"; } nonSecuredEndpoints.put(skippedEndPoint, "true"); @@ -138,23 +138,21 @@ public class WebappAuthenticationValve extends CarbonTomcatValve { private void processRequest(Request request, Response response, CompositeValve compositeValve, AuthenticationInfo authenticationInfo) { switch (authenticationInfo.getStatus()) { - case SUCCESS: - case CONTINUE: - this.getNext().invoke(request, response, compositeValve); - break; - case FAILURE: - String msg = "Failed to authorize incoming request"; - if (authenticationInfo.getMessage() != null && !authenticationInfo.getMessage().isEmpty()) { - msg = authenticationInfo.getMessage(); - response.setHeader("WWW-Authenticate", msg); - } - if (log.isDebugEnabled()) { - log.debug(msg + " , API : " + Encode.forUriComponent(request.getRequestURI())); - } - AuthenticationFrameworkUtil. - - handleResponse(request, response, HttpServletResponse.SC_UNAUTHORIZED, msg); - break; + case SUCCESS: + case CONTINUE: + this.getNext().invoke(request, response, compositeValve); + break; + case FAILURE: + String msg = "Failed to authorize incoming request"; + if (authenticationInfo.getMessage() != null && !authenticationInfo.getMessage().isEmpty()) { + msg = authenticationInfo.getMessage(); + response.setHeader("WWW-Authenticate", msg); + } + if (log.isDebugEnabled()) { + log.debug(msg + " , API : " + Encode.forUriComponent(request.getRequestURI())); + } + AuthenticationFrameworkUtil.handleResponse(request, response, HttpServletResponse.SC_UNAUTHORIZED, msg); + break; } } } \ No newline at end of file diff --git a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/test/java/org/wso2/carbon/webapp/authenticator/framework/WebappAuthenticationValveTest.java b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/test/java/org/wso2/carbon/webapp/authenticator/framework/WebappAuthenticationValveTest.java index 1340993c9b..69cbac221a 100644 --- a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/test/java/org/wso2/carbon/webapp/authenticator/framework/WebappAuthenticationValveTest.java +++ b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/test/java/org/wso2/carbon/webapp/authenticator/framework/WebappAuthenticationValveTest.java @@ -63,7 +63,6 @@ public class WebappAuthenticationValveTest { Mockito.doNothing().when(compositeValve).continueInvocation(Mockito.any(), Mockito.any()); request.setContext(context); webappAuthenticationValve.invoke(request, null, compositeValve); - request = new TestRequest("", "test"); context = new StandardContext(); compositeValve = Mockito.mock(CompositeValve.class); @@ -92,7 +91,6 @@ public class WebappAuthenticationValveTest { String encodedString = new String(Base64.getEncoder().encode((ADMIN_USER + ":" + ADMIN_USER).getBytes())); Request request = createRequest("basic " + encodedString); webappAuthenticationValve.invoke(request, null, compositeValve); - encodedString = new String(Base64.getEncoder().encode((ADMIN_USER + ":" + ADMIN_USER + "test").getBytes())); request = createRequest("basic " + encodedString); Response response = new Response(); @@ -135,7 +133,6 @@ public class WebappAuthenticationValveTest { context.setPath("carbon1"); context.addParameter("doAuthentication", String.valueOf(true)); request.setContext(context); - MimeHeaders mimeHeaders = new MimeHeaders(); MessageBytes bytes = mimeHeaders.addValue(BaseWebAppAuthenticatorFrameworkTest.AUTHORIZATION_HEADER); bytes.setString(authorizationHeader);