Switching code – JSP


<%@ page import="java.net.*, java.io.*, java.security.*" %>
<% final String mobileSite = "http://live.mobi"; final String desktopSite = "http://google.com"; if(request.getParameter("redirect")!=null && request.getParameter("redirect")=="false") { response.sendRedirect(desktopSite); } final String DA_URI = "http://detect.deviceatlas.com/query"; String source = "none"; boolean useCookies = true; boolean useCache = true; String error = ""; String daJson = null; final String DA_CACHE_DIR = System.getProperty("java.io.tmpdir")+ System.getProperty("file.separator")+"DeviceAtlasCache"+System.getProperty("file.separator"); String userAgent = request.getHeader("user-agent"); //Look up cookies if(useCookies) { daJson = getCookieValue("Mobi_Mtld_DA_Properties", request.getCookies()); if(daJson != null && !daJson.equals("null")) { source = "cookie"; } } //Look up cache if(useCache && 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(useCookies) { Cookie cookie = new Cookie("Mobi_Mtld_DA_Properties", daJson); response.addCookie(cookie); } if(useCache) { 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(mobileSite); else response.sendRedirect(desktopSite); %>
<%! //Parse json for property - really basic & assumes flat json file as returned by DA service static final String parseJson(String json, String property) { String[] tokens = json.split(",\\s\"|\":|\\{\"|:\\s|,\\s|\\}"); for(int i=0;i

Other Products

Market leading device intelligence for the web, app and MNO ecosystems
DeviceAtlas - Device Intelligence

Real-time identification of fraudulent and misrepresented traffic
DeviceAssure - Device Verification

A free tool for developers, designers and marketers to test website performance
mobiReady - Evaluate your websites’ mobile readiness

© 2024 DeviceAtlas Limited. All rights reserved.

This is a website of DeviceAtlas Limited, a private company limited by shares, incorporated and registered in the Republic of Ireland with registered number 398040 and registered office at 6th Floor, 2 Grand Canal Square, Dublin 2, Ireland