From 2b0800794de38ddf6cc7c90ed9daea73f2555cec Mon Sep 17 00:00:00 2001 From: Janak Amarasena Date: Fri, 28 Apr 2017 16:28:45 +0530 Subject: [PATCH] Minor improvements to the code --- .../apimgt/handlers/AuthenticationHandler.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.handlers/src/main/java/org/wso2/carbon/apimgt/handlers/AuthenticationHandler.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.handlers/src/main/java/org/wso2/carbon/apimgt/handlers/AuthenticationHandler.java index bb8783dec4d..2891e061796 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.handlers/src/main/java/org/wso2/carbon/apimgt/handlers/AuthenticationHandler.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.handlers/src/main/java/org/wso2/carbon/apimgt/handlers/AuthenticationHandler.java @@ -148,14 +148,17 @@ public class AuthenticationHandler extends AbstractHandler { } } else if (headers.containsKey(AuthConstants.MUTUAL_AUTH_HEADER)) { javax.security.cert.X509Certificate[] certs = - (javax.security.cert.X509Certificate[])axisMC.getProperty(AuthConstants.CLIENT_CERTIFICATE); + (javax.security.cert.X509Certificate[]) axisMC.getProperty(AuthConstants.CLIENT_CERTIFICATE); CertificateFactory cf = CertificateFactory.getInstance("X.509"); ByteArrayInputStream bais = new ByteArrayInputStream(certs[0].getEncoded()); - X509Certificate x509 = (X509Certificate) cf.generateCertificate(bais); - if (x509 != null ) { + X509Certificate x509 = (X509Certificate) cf.generateCertificate(bais); + if (bais != null) { + bais.close(); + } + if (x509 != null) { headers.put(AuthConstants.PROXY_MUTUAL_AUTH_HEADER, CertificateGenerator.getCommonName(x509)); return true; - }else { + } else { response = null; } } else if (headers.containsKey(AuthConstants.ENCODED_PEM)) { @@ -197,13 +200,12 @@ public class AuthenticationHandler extends AbstractHandler { log.error("Error while processing certificate.", e); return false; } catch (CertificateException e) { - e.printStackTrace(); + log.error("Certificate issue occurred when generating converting PEM to x509Certificate", e); return false; } catch (CertificateEncodingException e) { - e.printStackTrace(); + log.error("Error while attempting to encode certificate.", e); return false; } - } @Override @@ -215,7 +217,7 @@ public class AuthenticationHandler extends AbstractHandler { private String getDeviceType(String url) { StringTokenizer parts = new StringTokenizer(url, "/"); while (parts.hasMoreElements()) { - if (parts.nextElement().equals("device-mgt")) { + if (parts.nextElement().equals("api")) { return (String) parts.nextElement(); } }