001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS (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 *     Nuxeo - initial API and implementation
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.platform.ui.web.restAPI;
021
022import org.dom4j.dom.DOMDocument;
023import org.dom4j.dom.DOMDocumentFactory;
024import org.nuxeo.ecm.core.api.CoreInstance;
025import org.nuxeo.ecm.core.api.CoreSession;
026import org.nuxeo.ecm.core.api.DocumentModel;
027import org.nuxeo.ecm.core.api.DocumentRef;
028import org.nuxeo.ecm.core.api.IdRef;
029import org.nuxeo.ecm.core.api.NuxeoException;
030import org.restlet.data.Request;
031import org.restlet.data.Response;
032
033/**
034 * Base class for stateless restlet, i.e. Restlets that don't use Seam.
035 *
036 * @author tiry
037 */
038public class BaseStatelessNuxeoRestlet extends BaseNuxeoRestlet {
039
040    protected CoreSession session;
041
042    protected DocumentRef targetDocRef;
043
044    protected DocumentModel targetDocument;
045
046    protected boolean initRepository(Response res, String repoId) {
047        DOMDocumentFactory domFactory = new DOMDocumentFactory();
048        DOMDocument result = (DOMDocument) domFactory.createDocument();
049        if (repoId == null || repoId.equals("*")) {
050            handleError(result, res, "you must specify a repository");
051            return false;
052        }
053        try {
054            session = CoreInstance.openCoreSession(repoId);
055        } catch (NuxeoException e) {
056            handleError(result, res, e);
057            return false;
058        }
059        return true;
060    }
061
062    protected boolean initRepositoryAndTargetDocument(Response res, String repoId, String docId) {
063
064        DOMDocumentFactory domFactory = new DOMDocumentFactory();
065        DOMDocument result = (DOMDocument) domFactory.createDocument();
066
067        if (repoId == null || repoId.equals("*")) {
068            handleError(result, res, "you must specify a repository");
069            return false;
070        }
071
072        if (docId == null || docId.equals("")) {
073            handleError(result, res, "you must specify a document");
074            return false;
075        }
076
077        try {
078            session = CoreInstance.openCoreSession(repoId);
079        } catch (NuxeoException e) {
080            handleError(result, res, e);
081            return false;
082        }
083
084        targetDocRef = new IdRef(docId);
085
086        try {
087            targetDocument = session.getDocument(targetDocRef);
088        } catch (NuxeoException e) {
089            handleError(result, res, "Unable to open " + repoId + " repository");
090            return false;
091        }
092
093        return true;
094    }
095
096    protected void cleanUp() {
097        if (session != null) {
098            session.close();
099            session = null;
100            targetDocRef = null;
101            targetDocument = null;
102        }
103    }
104
105    @Override
106    public void handle(Request request, Response response) {
107        try {
108            doHandleStatelessRequest(request, response);
109        } finally {
110            cleanUp();
111        }
112    }
113
114    protected void doHandleStatelessRequest(Request req, Response res) {
115    }
116
117}