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: JOOoConvertPluginImpl.java 18651 2007-05-13 20:28:53Z sfermigier $ 020 */ 021 022package org.nuxeo.ecm.platform.ui.web.restAPI; 023 024import java.io.Serializable; 025 026import org.dom4j.dom.DOMDocument; 027import org.dom4j.dom.DOMDocumentFactory; 028import org.nuxeo.ecm.core.api.DocumentModel; 029import org.nuxeo.ecm.core.api.DocumentModelList; 030import org.nuxeo.ecm.core.api.IdRef; 031import org.nuxeo.ecm.core.api.NuxeoException; 032import org.nuxeo.ecm.core.api.repository.RepositoryManager; 033import org.nuxeo.runtime.api.Framework; 034import org.restlet.Request; 035import org.restlet.Response; 036import org.restlet.data.CharacterSet; 037import org.restlet.data.MediaType; 038import org.w3c.dom.DOMException; 039import org.w3c.dom.Element; 040 041public class BrowseRestlet extends BaseStatelessNuxeoRestlet implements Serializable { 042 043 private static final long serialVersionUID = -4518256101431979971L; 044 045 @Override 046 protected void doHandleStatelessRequest(Request req, Response res) { 047 logDeprecation(); 048 String repo = (String) req.getAttributes().get("repo"); 049 String docid = (String) req.getAttributes().get("docid"); 050 051 DOMDocumentFactory domFactory = new DOMDocumentFactory(); 052 053 DOMDocument result = (DOMDocument) domFactory.createDocument(); 054 055 if (repo == null || repo.equals("*")) { 056 try { 057 Element serversNode = result.createElement("avalaibleServers"); 058 result.setRootElement((org.dom4j.Element) serversNode); 059 060 RepositoryManager repositoryManager = Framework.getService(RepositoryManager.class); 061 for (String repositoryName : repositoryManager.getRepositoryNames()) { 062 Element server = result.createElement("server"); 063 server.setAttribute("title", repositoryName); 064 server.setAttribute("url", getRelURL(repositoryName, "*")); 065 serversNode.appendChild(server); 066 } 067 res.setEntity(result.asXML(), MediaType.APPLICATION_XML); 068 res.getEntity().setCharacterSet(CharacterSet.UTF_8); 069 return; 070 } catch (DOMException e) { 071 handleError(result, res, e); 072 return; 073 } 074 } else { 075 DocumentModel dm; 076 077 boolean init = initRepository(res, repo); 078 boolean isRoot = false; 079 try { 080 if (init) { 081 if (docid == null || docid.equals("*")) { 082 dm = session.getRootDocument(); 083 isRoot = true; 084 } else { 085 dm = session.getDocument(new IdRef(docid)); 086 } 087 } else { 088 handleError(res, "Unable to init repository"); 089 return; 090 } 091 } catch (NuxeoException e) { 092 handleError(res, e); 093 return; 094 } 095 096 Element current = result.createElement("document"); 097 try { 098 current.setAttribute("title", dm.getTitle()); 099 } catch (DOMException | NuxeoException e) { 100 handleError(res, e); 101 } 102 current.setAttribute("type", dm.getType()); 103 current.setAttribute("id", dm.getId()); 104 current.setAttribute("name", dm.getName()); 105 if (isRoot) { 106 current.setAttribute("url", getRelURL(repo, "")); 107 } else { 108 current.setAttribute("url", getRelURL(repo, dm.getRef().toString())); 109 } 110 result.setRootElement((org.dom4j.Element) current); 111 112 if (dm.isFolder()) { 113 // Element childrenElem = result.createElement("children"); 114 // root.appendChild(childrenElem); 115 116 DocumentModelList children; 117 try { 118 children = session.getChildren(dm.getRef()); 119 } catch (NuxeoException e) { 120 handleError(result, res, e); 121 return; 122 } 123 124 for (DocumentModel child : children) { 125 Element el = result.createElement("document"); 126 try { 127 el.setAttribute("title", child.getTitle()); 128 } catch (DOMException e) { 129 handleError(res, e); 130 } catch (NuxeoException e) { 131 handleError(res, e); 132 } 133 el.setAttribute("type", child.getType()); 134 el.setAttribute("id", child.getId()); 135 el.setAttribute("name", child.getName()); 136 el.setAttribute("url", getRelURL(repo, child.getRef().toString())); 137 current.appendChild(el); 138 } 139 } 140 141 res.setEntity(result.asXML(), MediaType.APPLICATION_XML); 142 res.getEntity().setCharacterSet(CharacterSet.UTF_8); 143 } 144 } 145 146 private static String getRelURL(String repo, String uuid) { 147 return '/' + repo + '/' + uuid; 148 } 149 150}