001/*
002 * (C) Copyright 2006-2007 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.platform.ui.web.restAPI;
023
024import org.dom4j.dom.DOMDocument;
025import org.dom4j.dom.DOMDocumentFactory;
026import org.nuxeo.ecm.core.api.CoreInstance;
027import org.nuxeo.ecm.core.api.CoreSession;
028import org.nuxeo.ecm.core.api.DocumentModel;
029import org.nuxeo.ecm.core.api.DocumentRef;
030import org.nuxeo.ecm.core.api.IdRef;
031import org.nuxeo.ecm.core.api.NuxeoException;
032import org.restlet.data.Request;
033import org.restlet.data.Response;
034
035/**
036 * Base class for stateless restlet, i.e. Restlets that don't use Seam.
037 *
038 * @author tiry
039 */
040public class BaseStatelessNuxeoRestlet extends BaseNuxeoRestlet {
041
042    protected CoreSession session;
043
044    protected DocumentRef targetDocRef;
045
046    protected DocumentModel targetDocument;
047
048    protected boolean initRepository(Response res, String repoId) {
049        DOMDocumentFactory domFactory = new DOMDocumentFactory();
050        DOMDocument result = (DOMDocument) domFactory.createDocument();
051        if (repoId == null || repoId.equals("*")) {
052            handleError(result, res, "you must specify a repository");
053            return false;
054        }
055        try {
056            session = CoreInstance.openCoreSession(repoId);
057        } catch (NuxeoException e) {
058            handleError(result, res, e);
059            return false;
060        }
061        return true;
062    }
063
064    protected boolean initRepositoryAndTargetDocument(Response res, String repoId, String docId) {
065
066        DOMDocumentFactory domFactory = new DOMDocumentFactory();
067        DOMDocument result = (DOMDocument) domFactory.createDocument();
068
069        if (repoId == null || repoId.equals("*")) {
070            handleError(result, res, "you must specify a repository");
071            return false;
072        }
073
074        if (docId == null || docId.equals("")) {
075            handleError(result, res, "you must specify a document");
076            return false;
077        }
078
079        try {
080            session = CoreInstance.openCoreSession(repoId);
081        } catch (NuxeoException e) {
082            handleError(result, res, e);
083            return false;
084        }
085
086        targetDocRef = new IdRef(docId);
087
088        try {
089            targetDocument = session.getDocument(targetDocRef);
090        } catch (NuxeoException e) {
091            handleError(result, res, "Unable to open " + repoId + " repository");
092            return false;
093        }
094
095        return true;
096    }
097
098    protected void cleanUp() {
099        if (session != null) {
100            session.close();
101            session = null;
102            targetDocRef = null;
103            targetDocument = null;
104        }
105    }
106
107    @Override
108    public void handle(Request request, Response response) {
109        try {
110            doHandleStatelessRequest(request, response);
111        } finally {
112            cleanUp();
113        }
114    }
115
116    protected void doHandleStatelessRequest(Request req, Response res) {
117    }
118
119}