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