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 javax.servlet.http.Cookie;
020import javax.servlet.http.HttpServletRequest;
021
022/**
023 * @author <a href="mailto:bjalon@nuxeo.com">Benjamin JALON</a>
024 * @since 5.5
025 */
026public class MobileWithCookieRequestHandler extends MobileRequestHandler {
027
028    private static final String COOKIE_NAME = "skipMobileRedirection";
029
030    protected String getCookieName() {
031        return COOKIE_NAME;
032    }
033
034    @Override
035    public boolean isRequestRedirectedToApplication(HttpServletRequest request) {
036        Boolean skipMobileRedirection = getCookieValue(request);
037        if (skipMobileRedirection != null && skipMobileRedirection) {
038            return !skipMobileRedirection;
039        }
040
041        return super.isRequestRedirectedToApplication(request);
042    }
043
044    @Override
045    public boolean isRequestRedirectedToApplicationLoginForm(HttpServletRequest request) {
046        Boolean checkCookie = getCookieValue(request);
047        if (checkCookie != null && checkCookie) {
048            return checkCookie;
049        }
050
051        return super.isRequestRedirectedToApplicationLoginForm(request);
052    }
053
054    private Boolean getCookieValue(HttpServletRequest request) {
055        if (request.getCookies() != null) {
056            for (Cookie cookie : request.getCookies()) {
057                if (COOKIE_NAME.equals(cookie.getName())) {
058                    return Boolean.parseBoolean(cookie.getValue());
059                }
060            }
061        }
062        return null;
063    }
064
065}