About This Code
Brief Description:
Redirect web request on geo location
Contributor:
Mats Ingelborn
Last Modified:
14 Apr 2009
OpenNTF Disclaimer
All of the program code and information presented in the OpenNTF.org Code Bin are provided "as-is", and should be used at your own risk. OpenNTF.org make no express or implied warranty about anything in the Code Bin, and OpenNTF.org will not be responsible or liable for any damage caused by the use or misuse of anything from this site. OpenNTF.org makes no guarantees about anything. Please thoroughly test all of the knowledge and code you find here before you attempt to use them in your production environment.
Code / Description
If you want to redirect users depending on where they come from, country where they are connected, you can use the free tools at http://iplocationtools.com
I will use a modified version of this Java-agent as the default page for every user not specifying which country-site they want.
The user's IP address is normally found in the variable REMOTE_ADDR, in our case it is in X_HTTP_FORWARDED_FOR as the Domino server is behind a proxy/load balancer.
This agent perform a 301 redirect (good for SEO) to the URL you choose.
Please also explore the other info you can get from the http://iplocationtools.com service. I'm just using country but you will also get city, state and position in lat/long.
/Mats
Usage / Example
import lotus.domino.*;
import java.io.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.URL;
import java.net.URLConnection;
//import java.net.URLEncoder;
public class JavaAgent extends AgentBase {
public void NotesMain() {
try {
Session session = getSession();
AgentContext agentContext = session.getAgentContext();
Document doc = agentContext.getDocumentContext();
String [] tmpArr = null;
PrintWriter out = getAgentOutput();
String ipAddr = doc.getItemValue("HTTP_X_FORWARDED_FOR").elementAt(0).toString(); // Change to REMOTE_ADDR ??
//Get IP info from IP Location Tools
URL ipURL = new URL("http://iplocationtools.com/ip_query.php?ip=" + ipAddr + "&output=raw");
BufferedReader in = new BufferedReader(new InputStreamReader( ipURL.openStream()));
String ipResult = in.readLine(); // Only one line of data is returned in this format
in.close();
// out.println( ipResult );
tmpArr = ipResult.split(","); // Create an array from the CSV-data
String thisCountry = tmpArr[2].toLowerCase(); //The 3rd value in the array is the country code
out.println( "Content-type: text/html");
out.println("Location: http://company.com/apps/db.nsf&c=" + thisCountry);
} catch(Exception e) {
e.printStackTrace();
}
}
}