001/* 002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * bjalon 016 */ 017package org.nuxeo.ecm.mobile.handler; 018 019import java.util.Arrays; 020import java.util.List; 021import java.util.Map; 022 023import javax.servlet.http.HttpServletRequest; 024 025import org.apache.commons.logging.Log; 026import org.apache.commons.logging.LogFactory; 027 028/** 029 * @author <a href="mailto:bjalon@nuxeo.com">Benjamin JALON</a> 030 * @since 5.5 031 */ 032public class MobileRequestHandler implements RequestHandler { 033 034 public static final String URL_SKIPPED_PATTERNS_PROP = "urlSkippedPatterns"; 035 036 private static final Log log = LogFactory.getLog(MobileRequestHandler.class); 037 038 private static List<String> MOBILE_USER_AGENT_REGEXP = Arrays.asList("(.*)Mobile(.*)Safari(.*)", 039 "(.*)AppleWebKit(.*)Mobile(.*)", "(.*)Android(.*)"); 040 041 private String[] urlPatterns; 042 043 @Override 044 public boolean isRequestRedirectedToApplication(HttpServletRequest request) { 045 046 // Some resources are skipped (nxfile pattern for instance, ...) but we 047 // want login page displayed for mobile 048 String queryString = request.getQueryString() != null ? "?" + request.getQueryString() : ""; 049 050 String urlRequest = request.getRequestURL() + queryString; 051 052 if (urlPatterns != null) { 053 for (String urlPattern : urlPatterns) { 054 if (urlRequest.matches(urlPattern)) { 055 if (log.isDebugEnabled()) { 056 log.debug(String.format("Mobile Handler: URL redirection (%s) is skipped by configuration: %s", 057 urlRequest, urlPattern)); 058 } 059 return false; 060 } 061 } 062 } 063 064 return isRequestRedirectedToApplicationLoginForm(request); 065 066 } 067 068 @Override 069 public boolean isRequestRedirectedToApplicationLoginForm(HttpServletRequest request) { 070 String userAgent = request.getHeader("User-Agent"); 071 072 if (userAgent == null) { 073 return false; 074 } 075 076 for (String pattern : MOBILE_USER_AGENT_REGEXP) { 077 if (userAgent.matches(pattern)) { 078 return true; 079 } 080 } 081 082 return false; 083 } 084 085 @Override 086 public RequestHandler init(Map<String, String> properties) { 087 String urlSkippedPatterns = properties.get(URL_SKIPPED_PATTERNS_PROP); 088 089 if (urlSkippedPatterns != null && !"".equals(urlSkippedPatterns)) { 090 urlPatterns = urlSkippedPatterns.split("[|]"); 091 } 092 093 return this; 094 } 095 096}