<%@ page import="java.net.*, java.io.*, java.security.*" %> <% final String MOBILE_SITE = "http://pearlautocare.mobi"; final boolean DA_USE_COOKIES = true; final boolean DA_USE_CACHE = true; final String DA_CACHE_DIR = System.getProperty("java.io.tmpdir")+ System.getProperty("file.separator")+"DeviceAtlasCache"+System.getProperty("file.separator"); final String DA_URI = "http://detect.deviceatlas.com/query"; final int COOKIE_EXPIRY_TIME = 21600; String source = "none"; String error = ""; String daJson = null; String redirectParam = request.getParameter("redirect"); //If redirect permitted (not explicitly prevented via redirect=false param) if((null!=redirectParam && !redirectParam.equals("false")) || redirectParam==null) { boolean preventRedirect = false; if (DA_USE_COOKIES && null!=getCookieValue("Mobi_Mtld_DA_Prevent_Redirect", request.getCookies())) { //Clear the redirect-prevention cookie if we see redirect=true parameter if(null!=redirectParam && redirectParam.equals("true")) { Cookie cookie = getCookie("Mobi_Mtld_DA_Prevent_Redirect", request.getCookies()); if(null!=cookie) { cookie.setMaxAge(0); //Do we need to add it back to the response like this? response.addCookie(cookie); } } else { preventRedirect = Boolean.parseBoolean(getCookieValue("Mobi_Mtld_DA_Prevent_Redirect", request.getCookies())); //If prevent cookie has value true then stop the redirect if(preventRedirect) { source = "prevent_redirect"; } } } //Grab the user-agent String userAgent = request.getHeader("user-agent"); //Look up cookies if(DA_USE_COOKIES) { daJson = getCookieValue("Mobi_Mtld_DA_Properties", request.getCookies()); if(daJson != null && !daJson.equals("null")) { source = "cookie"; } } //Look up cache if(DA_USE_CACHE && source.equals("none")) { String filename = DA_CACHE_DIR + md5(userAgent) + ".json"; File dir = new File(DA_CACHE_DIR); if(!dir.exists() && !dir.mkdirs()) { error = "Unable to create cache directory: " + DA_CACHE_DIR + "\n"; } else { //Read the cache file if it exists daJson = readFileAsString(filename); if(daJson!=null && !daJson.trim().equals("null") && !daJson.trim().equals("")) source = "cache"; } } //Look up DA webservice if we need to if(source.equals("none")) { URL DA = new URL("http://detect.deviceatlas.com/query?User-Agent=" + URLEncoder.encode(userAgent)); URLConnection conn = DA.openConnection(); BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line = null; line = br.readLine(); while (line != null) { daJson += line; line = br.readLine(); } br.close(); if(null!=daJson) { source = "webservice"; if(DA_USE_COOKIES) { Cookie cookie = new Cookie("Mobi_Mtld_DA_Properties", daJson); response.addCookie(cookie); } if(DA_USE_CACHE) { String filename = DA_CACHE_DIR + md5(userAgent) + ".json"; writeFile(filename, daJson); } } } String isMobile = parseJson(daJson, "mobileDevice"); //Do the redirect if(null!=isMobile && isMobile.equals("true")) response.sendRedirect(MOBILE_SITE); } else { //Create a cookie to prevent redirect for subsequent requests if (DA_USE_COOKIES && null==getCookieValue("Mobi_Mtld_DA_Prevent_Redirect", request.getCookies())) { Cookie cookie = new Cookie("Mobi_Mtld_DA_Prevent_Redirect", "true"); cookie.setMaxAge(COOKIE_EXPIRY_TIME); response.addCookie(cookie); } } %> <%! //Some helper functions //Parse json for property - really basic & assumes flat json file as returned by DA service static final String parseJson(String json, String property) { if(null==json) return null; String[] tokens = json.split(",\\s\"|\":|\\{\"|:\\s|,\\s|\\}"); for(int i=0;i