001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.workplace;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsResource;
032import org.opencms.main.CmsException;
033import org.opencms.main.CmsResourceInitException;
034import org.opencms.main.I_CmsResourceInit;
035import org.opencms.security.CmsOrganizationalUnit;
036import org.opencms.ui.login.CmsLoginHelper;
037
038import javax.servlet.http.HttpServletRequest;
039import javax.servlet.http.HttpServletResponse;
040
041/**
042 * Resource init handler that loads the login form with the right parameters.<p>
043 *
044 * The login uri must have following format:<br>
045 * <code>/${CONTEXT}/${SERVLET}/system/login/${OU_PATH}</code><p>
046 *
047 * for example:<br>
048 * <code>/opencms/opencms/system/login/intranet/marketing</code><p>
049 *
050 * @since 6.5.6
051 */
052public class CmsWorkplaceLoginHandler implements I_CmsResourceInit {
053
054    /** The login handler path. */
055    public static final String LOGIN_HANDLER = "/system/login";
056
057    /** The login form path. */
058    public static final String LOGIN_FORM = "/system/login/index.html";
059
060    /**
061     * @see org.opencms.main.I_CmsResourceInit#initResource(org.opencms.file.CmsResource, org.opencms.file.CmsObject, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
062     */
063    public CmsResource initResource(
064        CmsResource resource,
065        CmsObject cms,
066        HttpServletRequest req,
067        HttpServletResponse res) throws CmsResourceInitException {
068
069        if (resource != null) {
070            return resource;
071        }
072
073        String uri = cms.getRequestContext().getUri();
074        // check if the resource starts with the LOGIN_HANDLER
075        if (!uri.startsWith(LOGIN_HANDLER)) {
076            return resource;
077        }
078        String storedSiteRoot = cms.getRequestContext().getSiteRoot();
079        try {
080            // we now must switch to the root site to read the resource
081            cms.getRequestContext().setSiteRoot("/");
082            // read the resource
083            resource = cms.readResource(LOGIN_FORM);
084        } catch (CmsException e) {
085            throw new CmsResourceInitException(e.getMessageContainer(), e);
086        } finally {
087            // restore the siteroot
088            cms.getRequestContext().setSiteRoot(storedSiteRoot);
089            // resource may be null in case of an error
090            if (resource != null) {
091                // modify the uri to the one of the real resource
092                cms.getRequestContext().setUri(cms.getSitePath(resource));
093            }
094        }
095        // get the ou path
096        String ou = uri.substring(LOGIN_HANDLER.length());
097        if (!ou.startsWith(CmsOrganizationalUnit.SEPARATOR)) {
098            ou = CmsOrganizationalUnit.SEPARATOR + ou;
099        }
100        if (!ou.endsWith(CmsOrganizationalUnit.SEPARATOR)) {
101            ou += CmsOrganizationalUnit.SEPARATOR;
102        }
103        req.setAttribute(CmsLoginHelper.PARAM_PREDEF_OUFQN, ou);
104        return resource;
105    }
106}