IP2Currency: Java / Apache Sample Code

The sample code below provide you an example on how to obtain the latest currency exchange rate and the homeland currency by visitor's IP address.


//////////////////////////////////////////////////////////////////////////////////
//
//  This source code is only licensed to our premium subscribers. It is provided "as is" and without warranties.
//
//  http://www.fraudlabs.com © All Rights Reserved 2012
///
/////////////////////////////////////////////////////////////////////////////////
import java.io.*;
import java.net.*;
import javax.activation.*;
import org.apache.soap.*;
import org.apache.soap.messaging.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import org.apache.xerces.parsers.SAXParser;

  //This Class creates a Java Proxy for the .NET endpoint
public class ApacheSoapProxy 
{ 
	private URL url_ = null;
	private String soapActionUri_ = "";
	private Message message_ = new Message ();
	private Envelope envelope_ = new Envelope ();
	DataHandler soapMessage_ = null;

	//Default No argument constructor
	public ApacheSoapProxy () throws MalformedURLException
	{
		this.url_ = new URL ("http://v1.fraudlabs.com/ip2currencywebservice.asmx");
	}

	//Set the End Point URL
	public synchronized void setEndPoint (URL url) 
	{
		this.url_ = url;
	}

	//Retrieve the End Point URL
	public synchronized URL getEndPoint () 
	{
		return this.url_;
	}

	//Apache 2.2 classes encode messages differently than .NET does.
    //Therefore we have to override the piece that builds the body and
    //the pieces that interpret the response.
	public synchronized String IP2Currency(String strVISITORIP, String strFROMCURRENCYCODE, String strTOCURRENCYCODE, String strFROMAMOUNT, String strLICENSE) throws SOAPException 
	{
		String returnValue = "";
	    if (this.url_ == null) 
		{
			throw new SOAPException (Constants.FAULT_CODE_CLIENT, "A URL must be specified through ApacheSoapProxy.setEndPoint(URL)");
		}	
		// Get this from the soapAction attribute on the
		// soap:operation element that is found within the SOAP
		// binding information in the WSDL
		this.soapActionUri_ = "http://v1.fraudlabs.com/";
		ApacheMessageBody ourBody = new ApacheMessageBody ();

		// Set the argument
		ourBody.strVISITORIP = strVISITORIP;
		ourBody.strFROMCURRENCYCODE = strFROMCURRENCYCODE;
		ourBody.strTOCURRENCYCODE = strTOCURRENCYCODE;
		ourBody.strFROMAMOUNT = strFROMAMOUNT;
		ourBody.strLICENSE = strLICENSE;
		//Replace the default body with our own
		this.envelope_.setBody (ourBody);
		message_.send (this.getEndPoint(), this.soapActionUri_, this.envelope_);
		try 
		{
			//Since the Body.unmarshall() handler is static, we can't
			//replace the basic machinery easily. Instead, we must obtain and parse the message on our own.
			this.soapMessage_ = this.message_.receive();
			XMLReader reader = (XMLReader)Class.forName("org.apache.xerces.parsers.SAXParser").newInstance();
			SAXHandler handler = new SAXHandler();
			String strResult = "";

			handler.setElementToSearchFor("FROMCURRENCYCODE");
			reader.setContentHandler(handler);
			reader.parse( new InputSource (new StringReader( this.soapMessage_.getContent().toString() )));
			strResult += "FROMCURRENCYCODE: " + handler.getResult();

			handler.setElementToSearchFor("TOCURRENCYCODE");
			reader.setContentHandler(handler);
			reader.parse( new InputSource (new StringReader( this.soapMessage_.getContent().toString() )));
			strResult += "\nTOCURRENCYCODE: " + handler.getResult();

			handler.setElementToSearchFor("TOCURRENCYSYMBOL");
			reader.setContentHandler(handler);
			reader.parse( new InputSource (new StringReader( this.soapMessage_.getContent().toString() )));
			strResult += "\nTOCURRENCYSYMBOL: " + handler.getResult();

			handler.setElementToSearchFor("FROMAMOUNT");
			reader.setContentHandler(handler);
			reader.parse( new InputSource (new StringReader( this.soapMessage_.getContent().toString() )));
			strResult += "\nFROMAMOUNT: " + handler.getResult();

			handler.setElementToSearchFor("TOAMOUNT");
			reader.setContentHandler(handler);
			reader.parse( new InputSource (new StringReader( this.soapMessage_.getContent().toString() )));
			strResult += "\nTOAMOUNT: " + handler.getResult();

			handler.setElementToSearchFor("CONVERSIONRATE");
			reader.setContentHandler(handler);
			reader.parse( new InputSource (new StringReader( this.soapMessage_.getContent().toString() )));
			strResult += "\nCONVERSIONRATE: " + handler.getResult();

			handler.setElementToSearchFor("TOCOUNTRYNAME");
			reader.setContentHandler(handler);
			reader.parse( new InputSource (new StringReader( this.soapMessage_.getContent().toString() )));
			strResult += "\nTOCOUNTRYNAME: " + handler.getResult();

			handler.setElementToSearchFor("TOCOUNTRYCODE");
			reader.setContentHandler(handler);
			reader.parse( new InputSource (new StringReader( this.soapMessage_.getContent().toString() )));
			strResult += "\nTOCOUNTRYCODE: " + handler.getResult();

			handler.setElementToSearchFor("CREDITSAVAILABLE");
			reader.setContentHandler(handler);
			reader.parse( new InputSource (new StringReader( this.soapMessage_.getContent().toString() )));
			strResult += "\nCREDITSAVAILABLE: " + handler.getResult();

			handler.setElementToSearchFor("MESSAGE");
			reader.setContentHandler(handler);
			reader.parse( new InputSource (new StringReader( this.soapMessage_.getContent().toString() )));
			strResult += "\nMESSAGE: " + handler.getResult();

			returnValue = strResult;
		}
		catch (Exception exception) 
		{
			exception.printStackTrace ();
		}
		return returnValue;
	}
}

Download the source code
You need a license key for the solution. And, it is free and available at IP2Currency Free License