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.repository;
029
030import org.opencms.configuration.CmsConfigurationException;
031import org.opencms.configuration.CmsParameterConfiguration;
032import org.opencms.db.CmsUserSettings;
033import org.opencms.file.CmsObject;
034import org.opencms.file.wrapper.CmsObjectWrapper;
035import org.opencms.file.wrapper.I_CmsResourceWrapper;
036import org.opencms.main.CmsException;
037import org.opencms.main.CmsLog;
038import org.opencms.main.OpenCms;
039import org.opencms.workplace.CmsWorkplace;
040
041import java.util.ArrayList;
042import java.util.Collections;
043import java.util.List;
044
045import org.apache.commons.logging.Log;
046
047/**
048 * Creates a repository session to access OpenCms.<p>
049 *
050 * The configuration of the used {@link I_CmsResourceWrapper} is done here.
051 * This is the main class to get access to the resources in the VFS of
052 * OpenCms. The method {@link #login(String, String)} logs in to OpenCms
053 * and returns a {@link CmsRepositorySession} to use for basic file and
054 * folder operations.<p>
055 *
056 * The project and the site to use for the access to OpenCms is read out
057 * of the user settings.<p>
058 *
059 * @see CmsObjectWrapper
060 *
061 * @since 6.5.6
062 */
063public class CmsRepository extends A_CmsRepository {
064
065    /** The log object for this class. */
066    private static final Log LOG = CmsLog.getLog(CmsRepository.class);
067
068    /** The name of the parameter of the configuration. */
069    private static final String PARAM_WRAPPER = "wrapper";
070
071    /** The list of configured wrappers of the repository. */
072    private List<I_CmsResourceWrapper> m_wrappers;
073
074    /**
075     * Empty default constructor.<p>
076     */
077    public CmsRepository() {
078
079        super();
080        m_wrappers = new ArrayList<I_CmsResourceWrapper>();
081    }
082
083    /**
084     * @see org.opencms.repository.A_CmsRepository#initConfiguration()
085     */
086    @Override
087    public void initConfiguration() throws CmsConfigurationException {
088
089        CmsParameterConfiguration config = getConfiguration();
090        List<I_CmsResourceWrapper> wrapperObjects = CmsRepositoryManager.createResourceWrappersFromConfiguration(
091            config,
092            PARAM_WRAPPER,
093            LOG);
094        m_wrappers = Collections.unmodifiableList(wrapperObjects);
095        super.initConfiguration();
096    }
097
098    /**
099     * @see org.opencms.repository.I_CmsRepository#initializeCms(org.opencms.file.CmsObject)
100     */
101    public void initializeCms(CmsObject cms) {
102
103        // do nothing
104    }
105
106    /**
107     * @see org.opencms.repository.A_CmsRepository#login(java.lang.String, java.lang.String)
108     */
109    @Override
110    public I_CmsRepositorySession login(String userName, String password) throws CmsException {
111
112        CmsObject cms;
113        cms = OpenCms.initCmsObject(OpenCms.getDefaultUsers().getUserGuest());
114        cms.loginUser(userName, password);
115
116        CmsUserSettings settings = new CmsUserSettings(cms);
117
118        cms.getRequestContext().setSiteRoot(CmsWorkplace.getStartSiteRoot(cms, settings));
119        cms.getRequestContext().setCurrentProject(cms.readProject(settings.getStartProject()));
120
121        // set the object wrapper as an attribute in the request context, so that it can be
122        // used everywhere a CmsObject is accessible.
123        CmsObjectWrapper objWrapper = new CmsObjectWrapper(cms, m_wrappers);
124        cms.getRequestContext().setAttribute(CmsObjectWrapper.ATTRIBUTE_NAME, objWrapper);
125
126        return new CmsRepositorySession(objWrapper, getFilter());
127    }
128
129}