001/*
002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bjalon
018 */
019package org.nuxeo.ecm.mobile.handler;
020
021import java.util.Arrays;
022import java.util.List;
023import java.util.Map;
024
025import javax.servlet.http.HttpServletRequest;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029
030/**
031 * @author <a href="mailto:bjalon@nuxeo.com">Benjamin JALON</a>
032 * @since 5.5
033 */
034public class MobileRequestHandler implements RequestHandler {
035
036    public static final String URL_SKIPPED_PATTERNS_PROP = "urlSkippedPatterns";
037
038    private static final Log log = LogFactory.getLog(MobileRequestHandler.class);
039
040    private static List<String> MOBILE_USER_AGENT_REGEXP = Arrays.asList("(.*)Mobile(.*)Safari(.*)",
041            "(.*)AppleWebKit(.*)Mobile(.*)", "(.*)Android(.*)");
042
043    private String[] urlPatterns;
044
045    @Override
046    public boolean isRequestRedirectedToApplication(HttpServletRequest request) {
047
048        // Some resources are skipped (nxfile pattern for instance, ...) but we
049        // want login page displayed for mobile
050        String queryString = request.getQueryString() != null ? "?" + request.getQueryString() : "";
051
052        String urlRequest = request.getRequestURL() + queryString;
053
054        if (urlPatterns != null) {
055            for (String urlPattern : urlPatterns) {
056                if (urlRequest.matches(urlPattern)) {
057                    if (log.isDebugEnabled()) {
058                        log.debug(String.format("Mobile Handler: URL redirection (%s) is skipped by configuration: %s",
059                                urlRequest, urlPattern));
060                    }
061                    return false;
062                }
063            }
064        }
065
066        return isRequestRedirectedToApplicationLoginForm(request);
067
068    }
069
070    @Override
071    public boolean isRequestRedirectedToApplicationLoginForm(HttpServletRequest request) {
072        String userAgent = request.getHeader("User-Agent");
073
074        if (userAgent == null) {
075            return false;
076        }
077
078        for (String pattern : MOBILE_USER_AGENT_REGEXP) {
079            if (userAgent.matches(pattern)) {
080                return true;
081            }
082        }
083
084        return false;
085    }
086
087    @Override
088    public RequestHandler init(Map<String, String> properties) {
089        String urlSkippedPatterns = properties.get(URL_SKIPPED_PATTERNS_PROP);
090
091        if (urlSkippedPatterns != null && !"".equals(urlSkippedPatterns)) {
092            urlPatterns = urlSkippedPatterns.split("[|]");
093        }
094
095        return this;
096    }
097
098}