Pentaho REST API
Make data easy with Helical Insight.
Helical Insight is the world’s best open source business intelligence tool.
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed.
Prerequisites: Eclipse Neon, JDK 1.8
When we tried to call the REST API to verify users we may get this exception
Reasons for this exception is that it occurs because HTTPS is a secure protocol. If we want to call that API, the system requires certificates related to security. First, you need to obtain the public certificate from the server you’re trying to connect to. That can be done in a variety of ways, such as contacting the server admin and asking for it. I followed the below-mentioned way to resolve the problem.
User_URL : “https://” + host + “.apiname.com/api/v1/api_users”
Source code :
public static boolean validateUser(String host, String username, String api_key) throws ParseException { String userUrl = "https://" + host + ".apiname.com/api/v1/api_users"; System.out.println("Final URL : " + userUrl); try { URL url = new URL(userUrl); HttpsURLConnection conn = (HttpsURLConnection)url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/json"); conn.setRequestProperty("Accept", "json"); JSONObject jsObje = new JSONObject(); jsObje.put("username", username); jsObje.put("api_key", api_key); try (OutputStream output = conn.getOutputStream()) { output.write(jsObje.toString().getBytes(StandardCharsets.UTF_8.name())); } conn.connect(); System.out.println(conn.toString()); System.out.println(conn.getResponseCode()); } catch (IOException e) { e.printStackTrace(); } return true; } }
If we run the code we get the following Exception
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed.
To resolve this add the below code beginning of the method.
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; } public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) { } public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) { } } }; try { SSLContext sc = SSLContext.getInstance("SSL"); sc.init(null, trustAllCerts, new java.security.SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); } catch (Exception e) { }
The code adds required security certificates into a system. It handles that exception.
Make data easy with Helical Insight.
Helical Insight is the world’s best open source business intelligence tool.
Best Open Source Business Intelligence Software Helical Insight is Here
A Business Intelligence Framework
In Case if you have any queries please get us at support@helicaltech.com
Thank You
Anjaneyulu Patturi
Helical IT Solutions Pvt Ltd