%@ page
import="org.opencms.jsp.*"
import="org.opencms.util.*"
import="org.opencms.flex.*"
import="java.util.*"
import="com.maxmind.geoip.*"
import="com.gridnine.ticketbrokerage.*"
pageEncoding="UTF-8"
%>
<%
//Handle dibs parameters
if ("POST".equalsIgnoreCase(request.getMethod())) {;
%>
<%
String reply = (String)request.getParameter("dibs_reply");
String uid = (String)request.getParameter("input_id");
String url = "";
if (reply != null && reply.equalsIgnoreCase("A")) {
url = DIBSHelper.buildURL(uid);
} else {
url = "/sv/declined/index.html";
}
if (url.contains("NO-UID-RETURN")) {
Enumeration
params = request.getParameterNames();
while(params.hasMoreElements()){
String paramName = params.nextElement();
String line = paramName +"::"+request.getParameter(paramName);
%>
<%=line%>
<%
}
} else {
response.sendRedirect(url);
}
%> <%
} else {
// initialize logging
// org.apache.commons.logging.Log log = org.opencms.main.OpenCms.getLog("check_country_code");
// check if request originator is a web crawler
String ua = request.getHeader("User-Agent");
Boolean isCrawler = false;
if (ua.contains("Googlebot")) { isCrawler = true; }
// if needed, add more User-Agent checks here
CmsJspActionElement cms = new CmsJspActionElement(pageContext,
request,
response);
// check if we are on main site (.com)
Boolean isMainSite = false;
String serverName = request.getServerName();
String serverTLD = serverName.substring(serverName.lastIndexOf(".") + 1);
if ("com".equals(serverTLD) || serverName.contains("188.212.109.147")) { // Hard coded IP is current virtual server (2017-10-08)
isMainSite = true;
}
// Skip checking user country code if crawler or not on main site.
// The latter means that we are on a regional site, and these
// automatically redirect to the correct language subfolder.
// FIXME : integrate with existing language check?
Boolean skipCheck = (isCrawler || (! isMainSite));
if (isCrawler) {
// redirect crawlers to swedish landing page
response.sendRedirect("/sv/");
}
if (skipCheck) {
// send bots to english site
response.sendRedirect("/en/index.html");
} else {
// Normal user - the steps:
// 1. check country code cookie, if found, redirect and stop processing
// 2. check url parameter ?set_lang=, if found, redirect and stop processing
// 3. check MaxMind GeoLite Country database, if found, redirect and stop processing
// 4. fall back to english, redirect and stop processing
String userCountryCode = null;
// check if user has userCountryCode cookie set
// In OpenCms 6.0.2 there is no request.getCookieValue,
// this is lifted from a later version - http://tinyurl.com/cmkryrr (github.com).
Cookie[] cookies = request.getCookies();
for (int i = 0; (cookies != null) && (i < cookies.length); i++) {
if ("userCountryCode".equalsIgnoreCase(cookies[i].getName())) {
userCountryCode = cookies[i].getValue();
}
}
Boolean storeToCookie = false;
Boolean sendRedirect = false;
// check url for variable ?set_lang=
// if found, store to cookie and send redirect
// FIXME : fetch languages from com.gridnine.ticketbrokerage.Manager or database?
// Usage of Set as per http://tinyurl.com/33xsogs (stackoverflow.com)
Set ValidLanguages = new HashSet(Arrays.asList(new String[] {"sv", "fi", "no", "dk", "en"}));
try {
String languageFromUrl = request.getParameter("set_lang");
if (languageFromUrl != null) {
if (ValidLanguages.contains(languageFromUrl)) {
// language was valid, store to cookie and send redirect
userCountryCode = languageFromUrl;
storeToCookie = true;
sendRedirect = true;
}
}
} catch (Exception e) {
// didn't get country code from url
}
if (userCountryCode == null) {
// resolve user country code using Maxmind GeoLite Country database
String userIp = null;
try {
userIp = cms.getRequestContext().getRemoteAddress();
} catch (Exception e) {
// didn't get user ip
}
String countryCodeFromGeoIp = null;
if (userIp != null) {
try {
// FIXME : change to use path relative to OpenCms installation
// (WEB_INF/data/ or somesuch)
String geoipDatabasePath = "/usr/local/share/GeoIP/GeoIP.dat";
LookupService lookupService = new LookupService(geoipDatabasePath,
LookupService.GEOIP_MEMORY_CACHE);
countryCodeFromGeoIp = lookupService.getCountry(userIp).getCode();
} catch (Exception e) {
// didn't get user country code
}
}
// GeoLite Country and ticket2.com don't agree on language codes, so we need
// to convert to the latter. The country codes can differ in characters so
// simple toLower() doesn't work.
Map countryTransformTable = new HashMap();
countryTransformTable.put("SE", "sv"); // Sweden
countryTransformTable.put("FI", "fi"); // Finland
countryTransformTable.put("NO", "no"); // Norway
countryTransformTable.put("DK", "dk"); // Denmark
countryTransformTable.put("NL", "nl"); // Netherlands
// if key not found, HashMap defaults to null
userCountryCode = countryTransformTable.get(countryCodeFromGeoIp);
// if transformation not found, default to english
if (userCountryCode == null) { userCountryCode = "en"; }
// send redirect
storeToCookie = false;
sendRedirect = true;
}
if (storeToCookie) {
// set user country code to cookie expiring after 30 days
Cookie cookie = new Cookie("userCountryCode", userCountryCode);
cookie.setMaxAge(2592000); // seconds, 30 days
cookie.setPath("/");
response.addCookie(cookie);
}
if (sendRedirect) {
// redirect user to language front page
response.sendRedirect("/" + userCountryCode + "/");
}
}
}
%>