package com.claritas.sms.core.client; import java.util.Map; import java.util.Map.Entry; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.Entity; import javax.ws.rs.client.Invocation; import javax.ws.rs.client.Invocation.Builder; import javax.ws.rs.core.Response; public class SampleTestService { public static T doPostRequest(String serviceUrl, Object jsonInput, Class outputObject) { Response response = getInvocationBuilder(serviceUrl).post(Entity.json(jsonInput)); return response.readEntity(outputObject); } public static Response doPostRequest(String serviceUrl, Object jsonInput) { return getInvocationBuilder(serviceUrl).post(Entity.json(jsonInput)); } public static T doGetRequest(String serviceUrl, Class outputObject) { Response response = getInvocationBuilder(serviceUrl).get(); return response.readEntity(outputObject); } public static Response doGetRequest(String serviceUrl) { return getInvocationBuilder(serviceUrl).get(); } public static Response doGetRequest(String serviceUrl, final Map headers) { return getInvocationBuilder(serviceUrl, headers).get(); } public static void doAsyncPostRequest(final String serviceUrl, final Object jsonInput) { getInvocationBuilder(serviceUrl).async().post(Entity.json(jsonInput)); } public static void doAsyncGetRequest(final String serviceUrl) { getInvocationBuilder(serviceUrl).async().get(); } private static Builder getInvocationBuilder(final String serviceUrl, final Map headers) { Client client = ClientBuilder.newClient(); Invocation.Builder builder = client.target(serviceUrl).request(); builder.header("access_token", getAccessToken()); if (headers != null) { for (Entry headerEntry : headers.entrySet()) { builder.header(headerEntry.getKey(), headerEntry.getValue()); } } return builder; } private static Builder getInvocationBuilder(final String serviceUrl) { return getInvocationBuilder(serviceUrl, null); } public static String getAccessToken() { // Needs to be retried dynamically by calling C360 services. return "ACCESS_TOKEN"; } public static void main(String aregs[]) { String url; // Test Single address lookup url = "https://claritas360dev.claritas.com/smsfeapi/fileenhancementservices/SMS/webservice/FE/singleAddress/123 Main St, Buffalo, NY 14217"; Response response = ClientService.doGetRequest(url); if (response.getStatus() == 200) System.out.println(response.readEntity(String.class)); else System.out.println("Service errored!"); } }