I am trying to make Http calls from a Soap service deployed as a war file on Glassfish 5.1.0 in a remote windows server. At the point of making Http calls to other Microservices from the soap service, I get NullPointerException Issue. Currently I have upgraded the client (Okhttp) to version 3.12.13 and updated the code to use some TrustCertificates but I still get same issues. This is the stack trace of the error being returned :
at okhttp3.ConnectionSpec.isCompatible(ConnectionSpec.java:207) atokhttp3.internal.connection.ConnectionSpecSelector.configureSecureSocket(ConnectionSpecSelector.java:60) at okhttp3.internal.connection.RealConnection.connectTls(RealConnection.java:313)at okhttp3.internal.connection.RealConnection.establishProtocol(RealConnection.java:284)at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:169)at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:258)atokhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:135)at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:114) at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121) at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121) at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:127) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121) at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:257) at okhttp3.RealCall.execute(RealCall.java:93) at com.tools.CallApi.banklyGetAuthToken(CallApi.java:172) at com.tools.CallApi.callBanklyRestApi(CallApi.java:278) at com.nibss.ObjNameEnquiry.process(ObjNameEnquiry.java:68) at com.bankly.nibss.NIPService.NameEnquiry(NIPService.java:187) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.sun.xml.ws.util.Trampoline.invoke(MethodUtil.java:52) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) atsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.sun.xml.ws.util.MethodUtil.invoke(MethodUtil.java:77) at com.sun.xml.ws.api.server.MethodUtil.invoke(MethodUtil.java:34) at com.sun.xml.ws.api.server.InstanceResolver$1.invoke(InstanceResolver.java:220) at com.sun.xml.ws.server.InvokerTube$2.invoke(InvokerTube.java:119) at com.sun.xml.ws.server.sei.SEIInvokerTube.processRequest(SEIInvokerTube.java:58) at com.sun.xml.ws.api.pipe.Fiber.__doRun(Fiber.java:1106) at com.sun.xml.ws.api.pipe.Fiber._doRun(Fiber.java:1020) at com.sun.xml.ws.api.pipe.Fiber.doRun(Fiber.java:989) at com.sun.xml.ws.api.pipe.Fiber.run(Fiber.java:783) at com.sun.xml.ws.api.pipe.Fiber.start(Fiber.java:390) at com.sun.xml.ws.server.WSEndpointImpl.processAsync(WSEndpointImpl.java:338) at com.sun.xml.ws.server.WSEndpointImpl.process(WSEndpointImpl.java:368)atcom.sun.xml.ws.metro.api.config.management.ManagedEndpoint.process(ManagedEndpoint.java:204)at com.sun.xml.ws.transport.http.HttpAdapter.invokeAsync(HttpAdapter.java:704)atcom.sun.xml.ws.transport.http.servlet.ServletAdapter.invokeAsync(ServletAdapter.java:182)atcom.sun.xml.ws.transport.http.servlet.WSServletDelegate.doGet(WSServletDelegate.java:131)atcom.sun.xml.ws.transport.http.servlet.WSServletDelegate.doPost(WSServletDelegate.java:167)at com.sun.xml.ws.transport.http.servlet.WSServlet.doPost(WSServlet.java:51) at javax.servlet.http.HttpServlet.service(HttpServlet.java:665) at javax.servlet.http.HttpServlet.service(HttpServlet.java:750) at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1540) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:119) at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:611) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:550) at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:75) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:114) at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:332) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:199) at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:439) at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:144) at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:182) at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:156) at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:218) at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:95) at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:260) at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:177) at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:109) at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:88) at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:53) at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:515) at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:89) at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:94) at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:33) at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:114) at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:569)atorg.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:549) at java.lang.Thread.run(Thread.java:750)
This is my code:
public JSONObject GetAuthToken(String endPoint) { String rtn = ""; OkHttpClient client = null; // Could remain null if exception occurs JSONObject res = new JSONObject(); // Move this outside try block try { System.out.println("Making Authentication Call to Identity Server >>>>>>>>>>>>>>>>>>>>"); // Load the truststore KeyStore trustStore = KeyStore.getInstance("JKS"); try (FileInputStream fis = new FileInputStream(truststorePath)) { // Use try-with-resources trustStore.load(fis, truststorePassword.toCharArray()); } // Create TrustManagerFactory TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); trustManagerFactory.init(trustStore); X509TrustManager trustManager = (X509TrustManager) trustManagerFactory.getTrustManagers()[0]; // Add ConnectionSpec with explicit TLS version ConnectionSpec spec = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS) .tlsVersions(TlsVersion.TLS_1_2) .allEnabledCipherSuites() .build(); client = new OkHttpClient.Builder() .connectTimeout(600000, TimeUnit.MILLISECONDS) .readTimeout(600000, TimeUnit.MILLISECONDS) .writeTimeout(600000, TimeUnit.MILLISECONDS) .connectionSpecs(Arrays.asList(spec)) // Use single spec instead of MODERN_TLS, COMPATIBLE_TLS .sslSocketFactory(new CustomSSLSocketFactory(), trustManager) .build(); System.out.println("client_secret before going out : " + client_secret); System.out.println("client_id before going out: " + client_id); System.out.println("grant_type before going out: " + grant_type); // Move the request creation and execution inside this try block String formargs = "client_secret=" + client_secret +"&client_id=" + client_id +"&grant_type=" + grant_type; MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded"); RequestBody body = RequestBody.create(mediaType, formargs); Request request = new Request.Builder() .url(endPoint) .method("POST", body) .addHeader("Content-Type", "application/x-www-form-urlencoded") .build(); Response response = client.newCall(request).execute(); rtn = response.body().string(); System.out.println("Raw Response Body: " + rtn); if (response.code() != 200) { throw new IOException("Server returned code: " + response.code() +" with body: " + rtn); } if (rtn == null || rtn.trim().isEmpty()) { throw new IOException("Empty response received from server"); } try { res = new JSONObject(rtn); // Verify we got the token if (!res.has("access_token")) { throw new IOException("Response missing access_token: " + rtn); } } catch (JSONException je) { throw new IOException("Invalid JSON response: " + rtn, je); } } catch (Exception ex) { ex.printStackTrace(); res = new JSONObject(); res.put("error", "Auth failed: " + ex.getMessage()); } return res;}