001/*
002 * (C) Copyright 2006-2010 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 *     Thierry Delprat
016 */
017package org.nuxeo.apidoc.filter;
018
019import java.io.IOException;
020
021import javax.servlet.FilterChain;
022import javax.servlet.ServletException;
023import javax.servlet.ServletRequest;
024import javax.servlet.ServletResponse;
025import javax.servlet.http.HttpServletRequest;
026import javax.servlet.http.HttpServletResponse;
027
028import org.nuxeo.ecm.core.api.NuxeoPrincipal;
029import org.nuxeo.ecm.platform.web.common.vh.VirtualHostHelper;
030
031public class RedirectFilter extends BaseApiDocFilter {
032
033    protected boolean isUriValidForAnonymous(HttpServletRequest request) {
034        String uri = request.getRequestURI();
035        if (uri.contains("/nxpath/")) {
036            return false;
037        }
038        if (uri.contains("/nxdoc/")) {
039            return false;
040        }
041        if (uri.contains(".faces")) {
042            return false;
043        }
044        if (uri.contains(".xhtml")) {
045            return false;
046        }
047        return true;
048    }
049
050    protected void redirectToWebEngineView(HttpServletRequest httpRequest, HttpServletResponse httpResponse)
051            throws IOException {
052        String base = VirtualHostHelper.getBaseURL(httpRequest);
053        String location = base + "site/distribution/";
054        httpResponse.sendRedirect(location);
055    }
056
057    @Override
058    protected void internalDoFilter(ServletRequest request, ServletResponse response, FilterChain chain)
059            throws IOException, ServletException {
060
061        HttpServletRequest httpRequest = (HttpServletRequest) request;
062        HttpServletResponse httpResponse = (HttpServletResponse) response;
063
064        NuxeoPrincipal nxUser = (NuxeoPrincipal) httpRequest.getUserPrincipal();
065
066        if (nxUser != null && nxUser.isAnonymous()) {
067            if (!isUriValidForAnonymous(httpRequest)) {
068                redirectToWebEngineView(httpRequest, httpResponse);
069            }
070        }
071
072        chain.doFilter(httpRequest, httpResponse);
073    }
074
075}