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