001/*
002 * (C) Copyright 2006-2011 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 *     Gagnavarslan ehf
017 */
018package org.nuxeo.ecm.webdav.backend;
019
020import javax.servlet.http.HttpServletRequest;
021
022import org.nuxeo.ecm.core.api.CoreSession;
023import org.nuxeo.ecm.webdav.service.WIRequestFilter;
024import org.nuxeo.ecm.webengine.WebEngine;
025import org.nuxeo.ecm.webengine.model.WebContext;
026
027public abstract class AbstractBackendFactory implements BackendFactory {
028
029    @Override
030    public Backend getBackend(String path, HttpServletRequest request) {
031        if (request == null) {
032            throw new NullPointerException("null request");
033        }
034        Backend backend = (Backend) request.getAttribute(WIRequestFilter.BACKEND_KEY);
035        if (backend == null) {
036            // create backend from WebEngine session
037            WebContext webContext = WebEngine.getActiveContext();
038            if (webContext == null) {
039                throw new NullPointerException("null WebContext");
040            }
041            CoreSession session = webContext.getCoreSession();
042            if (session == null) {
043                throw new NullPointerException("null CoreSession");
044            }
045            backend = createRootBackend(session);
046            request.setAttribute(WIRequestFilter.BACKEND_KEY, backend);
047        }
048        return backend.getBackend(path);
049    }
050
051    public abstract Backend createRootBackend(CoreSession session);
052
053}