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