<%@ page import= "net.digitalenvoy.netacuity.NaGeoDb"%> <%@ page import= "java.io.File"%> <%@ page import= "java.util.*"%> <%@ page import= "java.io.FileInputStream"%> <%@ page import= "java.util.Properties"%> <%@ page import= "java.math.BigDecimal"%> <%@ page import= "java.util.Random"%> <% ServletContext ctx = this.getServletContext(); %> <%! static final int NETACUITY_API_ID = 1; static final long US_COUNTRY_CODE = 840; static final long UK_COUNTRY_CODE = 826; static final long EXPERIAN_COUNTRY_CODE = 999; // This is the code returned for visitors inside the Experian network static int maxProcessTime; static int counter = 0; static String LOCK = ""; static final String defaultDestinationPage = "index.html"; static final String propertyFilePath = "/opt/NetAcuity/properties"; //static final String propertyFilePath = "D:/Data/netucity/lib"; static final String propertyFileName = "netacuity.properties"; static final int defaultMaxProcessTime = 2000; static Properties netacuityProps; static String primaryServer; static String ukDestinationPage = ""; static List usDestinationPageList = new ArrayList(2); static List usDestinationPageASubList = new ArrayList(2); static List usDestinationPageBSubList = new ArrayList(2); static String nonUsDestinationPage; // Kamal 04/06/06 - The previous implementation was reading the properties file // for each request and was never closing the properties file. Because of this // the number of file handles on the webserver was increasing over time. So moved // the properties file reading into a static block and also did some refactoring. static { try { netacuityProps = new Properties(); File netacuityPropsFile = new File(propertyFilePath, propertyFileName); netacuityProps.load(new FileInputStream(netacuityPropsFile)); netacuityPropsFile = null; primaryServer = netacuityProps.getProperty("primaryServer"); usDestinationPageList.add(0, usDestinationPageASubList); usDestinationPageList.add(1, usDestinationPageBSubList); // Load the A pages if (netacuityProps.getProperty("usDestinationPage1.1") != null) { usDestinationPageASubList.add(0, netacuityProps.getProperty("usDestinationPage1.1")); } else { usDestinationPageASubList.add(0, defaultDestinationPage); } if (netacuityProps.getProperty("usDestinationPage1.2") != null) { usDestinationPageASubList.add(1, netacuityProps.getProperty("usDestinationPage1.2")); } else { usDestinationPageASubList.add(1, defaultDestinationPage); } // Load the B pages if (netacuityProps.getProperty("usDestinationPage2.1") != null) { usDestinationPageBSubList.add(0, netacuityProps.getProperty("usDestinationPage2.1")); } else { usDestinationPageBSubList.add(0, defaultDestinationPage); } if (netacuityProps.getProperty("usDestinationPage2.2") != null) { usDestinationPageBSubList.add(1, netacuityProps.getProperty("usDestinationPage2.2")); } else { usDestinationPageBSubList.add(1, defaultDestinationPage); } /* if (netacuityProps.getProperty("usDestinationPage2") != null) { usDestinationPageList.add(1, netacuityProps.getProperty("usDestinationPage2")); } else { usDestinationPageList.add(1, defaultDestinationPage); } */ nonUsDestinationPage = netacuityProps.getProperty("nonUsDestinationPage"); if (nonUsDestinationPage == null || nonUsDestinationPage.equals("")) { nonUsDestinationPage = defaultDestinationPage; } ukDestinationPage = netacuityProps.getProperty("ukDestinationPage"); if (nonUsDestinationPage == null || nonUsDestinationPage.equals("")) { nonUsDestinationPage = defaultDestinationPage; } maxProcessTime = Integer.parseInt(netacuityProps.getProperty("maxProcessTime")); netacuityPropsFile = null; } catch (Exception e) { System.out.println("======================================"); System.out.println("Error loading the netacuity.properties file " + e); System.out.println("======================================"); } } class CheckIp extends Thread { HttpServletRequest request; ServletContext c; StringBuffer destinationPage = null; public CheckIp(HttpServletRequest request, ServletContext ctx) { this.request = request; c = ctx; } public String getDestinationPage() { if (destinationPage == null) return null; return destinationPage.toString(); } public void run() { long countryCode = 0; String ipAddress = ""; try { /* Properties netacuityProps = new Properties(); File netacuityPropsFile = new File(propertyFilePath,propertyFileName); netacuityProps.load(new FileInputStream(netacuityPropsFile)); String primaryServer = netacuityProps.getProperty("primaryServer"); String usDestinationPage = netacuityProps.getProperty("usDestinationPage"); if (usDestinationPage == null || usDestinationPage.equals("")) { usDestinationPage = defaultDestinationPage; } String nonUsDestinationPage = netacuityProps.getProperty("nonUsDestinationPage"); if (nonUsDestinationPage == null || nonUsDestinationPage.equals("")) { nonUsDestinationPage = defaultDestinationPage; } maxProcessTime = Integer.parseInt(netacuityProps.getProperty("maxProcessTime")); */ ipAddress = request.getRemoteAddr(); // For Testing Purposes ipAddress = request.getParameter("ipAddress"); if (ipAddress == null || ipAddress.equals("")) { ipAddress = request.getRemoteAddr(); } // c.log("IP Address: " + ipAddress); // begin NetAcuity calls // final int NETACUITY_API_ID = 1; NaGeoDb geo = new NaGeoDb(); // setup server address and API id geo.set_server_addr(primaryServer); geo.set_id(NETACUITY_API_ID); geo.set_timeout(maxProcessTime); int ret = geo.naQueryGeo(ipAddress); // c.log ("Netucity return value is: " + ret); if (ret == 1) { countryCode = geo.countryCode; // c.log ("Country Code is: " + countryCode); if (countryCode == US_COUNTRY_CODE) { // int d = getRandomNumber(); // if (d < 91) { destinationPage = new StringBuffer((String)((List)usDestinationPageList.get(1)).get(0)); // B1 // } // if (d > 90) { // destinationPage = new StringBuffer((String)((List)usDestinationPageList.get(0)).get(1)); // A2 // } } else if (countryCode == UK_COUNTRY_CODE) { destinationPage = new StringBuffer(ukDestinationPage); // reinstated internal code // psj 2008.06.25 } else if (countryCode == EXPERIAN_COUNTRY_CODE) { destinationPage = new StringBuffer((String)((List)usDestinationPageList.get(1)).get(1)); // B2 (redone as redir, temp) } else { destinationPage = new StringBuffer(nonUsDestinationPage); } } else { destinationPage = new StringBuffer(nonUsDestinationPage); } geo.close(); } catch(Exception ex) { System.out.println("======================================"); System.out.println("exception from NetAcuity index.jsp " + ex.toString()); ex.printStackTrace(); System.out.println("======================================"); } } } %> <%! public int getRandomNumber() { Random generator = new Random(); double d = generator.nextDouble(); d = d * 100; return (int)d; } %> <% String finalPage = null; String cookieName = "LandingPage"; String IcookieName = "LPIndicator"; Cookie cookies [] = request.getCookies (); Cookie myCookie = null; Cookie myICookie = null; String myICookieVal = null; String myIndicatorSetVal = "set20070322"; // Set this cookie each time a new split is rolled out - it makes all visitors go through the split if (cookies != null) { for (int i = 0; i < cookies.length; i++) { // ctx.log (cookies [i].getName() + " : " + cookies [i].getValue()); if (cookies [i].getName().equals (cookieName)) { myCookie = cookies[i]; break; } } for (int j = 0; j < cookies.length; j++) { if (cookies [j].getName().equals (IcookieName)) { myICookie = cookies[j]; myICookieVal = myICookie.getValue(); break; } } } if (myCookie != null && myICookieVal != null && myICookieVal.equals(myIndicatorSetVal)) { // ctx.log ("Cookie Value is: " + myCookie.getValue()); finalPage = myCookie.getValue(); } else { // ctx.log ("Cookie is null"); maxProcessTime = defaultMaxProcessTime; CheckIp checkIp = new CheckIp(request, ctx); checkIp.start(); try { checkIp.join(maxProcessTime); } catch (InterruptedException e) { System.out.println(e); // Timeout occurred } finalPage = checkIp.getDestinationPage(); if (finalPage == null) { finalPage = defaultDestinationPage; // ctx.log ("Final page is null. So not setting the cookie"); } else { /** int secs = 0; Calendar c = Calendar.getInstance(TimeZone.getTimeZone ("GMT")); c.set(2007, 5, 31, 23, 59, 59); long millisBetween = c.getTimeInMillis() - Calendar.getInstance(TimeZone.getTimeZone ("GMT")).getTimeInMillis(); secs = (int)millisBetween/1000; // ctx.log ("Expiration seconds : " + secs); if (secs > 0) { */ Cookie cookie = new Cookie (cookieName, finalPage); // Expires in 2 months cookie.setMaxAge(5184000); // ctx.log ("Cookie Expires : " + cookie.getMaxAge()); // ctx.log ("Setting the cookie now." + cookie); response.addCookie(cookie); Cookie Icookie = new Cookie (IcookieName, myIndicatorSetVal); Icookie.setMaxAge(5184000); response.addCookie(Icookie); // } } } %>