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