Tuesday, 30 August 2016

Jersey webservice client to access WS via proxy

I have already written a jersey client program to access the Restful WS for one of the projects. Recently, the company has upgraded the securities and so the internet can be accessed only via proxy. After this change, the program stopped working. It was not able to make a connection because of the change in internet connection.

I have modified the program to access the WS via proxy.

ConnectionFactory:

This class defines the proxy and the port to be used and then the same will be used in the client program.


package com.ppc.rest.client;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;

import com.sun.jersey.client.urlconnection.HttpURLConnectionFactory;

public class ConnectionFactory implements HttpURLConnectionFactory {
 Proxy proxy;

 private void initializeProxy() {
  proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("haproxy.cfa.vic.gov.au", 3128));
 }

 @Override
 public HttpURLConnection getHttpURLConnection(URL url) throws IOException {
  initializeProxy();
  return (HttpURLConnection) url.openConnection(proxy);
 }

}

ClientLogicWebservice:


package com.ppc.rest.client;

import com.cfa.ppcese.model.LogicTransctions;
import com.cfa.ppcese.util.CFAProperties;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.client.filter.HTTPDigestAuthFilter;
import com.sun.jersey.api.json.JSONConfiguration;
import com.sun.jersey.client.urlconnection.URLConnectionClientHandler;

public class ClientLogicWebservice {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  // LogicWebServiceJob.getLogicData();
  getLogicData();
 }

 public static void getLogicData() {
  // LOG.debug("Calling the Webservice data load method");
  URLConnectionClientHandler ch = new URLConnectionClientHandler(new ConnectionFactory());

  ClientConfig clientConfig = new DefaultClientConfig();
  String url = CFAProperties.getProp("logic.rest.url");

  clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
  // clientConfig.
  Client client = new Client(ch, clientConfig);
  // Client client = Client.create(clientConfig);
  client.addFilter(new HTTPDigestAuthFilter(CFAProperties.getProp("logic.rest.username"), CFAProperties.getProp("logic.rest.password")));

  try {
   url = url + "1025";

   ClientResponse resp = client.resource(url).accept("application/json").type("application/json").get(ClientResponse.class);
   System.out.println(resp.toString());

   LogicTransctions logic = resp.getEntity(LogicTransctions.class);
   if (resp.getStatus() == 200) {
    if (logic.getCount() == 0 && logic.getError().getMessage() == null) {
     System.out.println("There is no data available for this call and the URL is - " + url);
    } else {
     System.out.println("Insert Logic data");
    }
   }

  } catch (Exception e) {

  }
 }
}

Required Libraries:

  • jersey-client-1.19.1.jar
  • jersey-json-1.19.1.jar
  • jersey-server-1.19.1.jar
  • jackson-core-asl-1.19.13.jar
  • jsr311-api-1.1.1.jar