Search code examples
javarestservicewebrestlet

java application to read information from ARIN RESTful Web Services


I am trying to create a java application to read the information from ARIN using an IP Address. I see ARIN is using RESTful Web Services to get the IP information but I am not sure what I need to do to start. Some people are talking about RESTLET, other people about JAX-RS,etc. Can you please help me to take me in the right direction? Thanks!


Solution

  • Restlet also has a client API to interact with a remote RESTful application. See the classes Client, ClientResource for more details. For this, you need to have following jar files from Restlet distribution:

    • org.restlet: main Restlet jar
    • org.restlet.ext.xml: Restlet support of XML
    • org.restlet.ext.json: Restlet support of JSON. In this case, the JSON jar present in libraries folder is also required.

    If I use the documentation located at this address https://www.arin.net/resources/whoisrws/whois_api.html#whoisrws. Here is a simple Restlet code you can use:

    ClientResource cr = new ClientResource("http://whois.arin.net/rest/poc/KOSTE-ARIN");
    Representation repr = cr.get();
    // Display the XML content
    System.out.println(repr.getText());
    

    or

    ClientResource cr = new ClientResource("http://whois.arin.net/rest/poc/KOSTE-ARIN.txt");
    Representation repr = cr.get();
    // Display the text content
    System.out.println(repr.getText());
    

    Restlet also provides some support at XML level. So you can have access to hints contained in the XML in a simple way, as described below:

    ClientResource cr = new ClientResource("http://whois.arin.net/rest/poc/KOSTE-ARIN");
    Representation repr = cr.get();
    DomRepresentation dRepr = new DomRepresentation(repr);
    Node firstNameNode = dRepr.getNode("//firstName");
    Node lastNameNode = dRepr.getNode("//lastName");
    System.out.println(firstNameNode.getTextContent()+" "+lastNameNode.getTextContent());
    

    Note that you can finally handle content negotiation (conneg) since it seems supported by your REST service:

    ClientResource cr = new ClientResource("http://whois.arin.net/rest/poc/KOSTE-ARIN");
    Representation repr = cr.get(MediaType.APPLICATION_JSON);
    

    In this case, your representation object contains JSON formatted data. In the same way than the DomRepresentation, there is a JsonRepresentation to inspect this representation content.

    Hope it helps you. Thierry