001/*
002 * (C) Copyright 2013 Nuxeo SA (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-2.1.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 *     dmetzler
016 */
017package org.nuxeo.ecm.restapi.server.jaxrs;
018
019import java.util.ArrayList;
020import java.util.List;
021
022import javax.ws.rs.MatrixParam;
023import javax.ws.rs.Path;
024import javax.ws.rs.PathParam;
025
026import org.nuxeo.ecm.core.api.CoreSession;
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.ecm.core.api.IdRef;
029import org.nuxeo.ecm.core.api.PathRef;
030import org.nuxeo.ecm.core.api.impl.DocumentModelListImpl;
031import org.nuxeo.ecm.webengine.model.WebObject;
032import org.nuxeo.ecm.webengine.model.impl.DefaultObject;
033
034/**
035 * Holds all methods bound to a repository
036 *
037 * @since 5.7.3
038 */
039@WebObject(type = "repo")
040public class RepositoryObject extends DefaultObject {
041
042    /**
043     * The regex of getDocsByPath doesn't catch the case of the root document.
044     *
045     * @param adapterName
046     * @return
047     * @since 5.8
048     */
049    @Path("path/@{adapterName}")
050    public Object getRootPathAdapter(@PathParam("adapterName") String adapterName) {
051        DocumentModel rootDocument = getContext().getCoreSession().getRootDocument();
052
053        return ctx.newAdapter(newObject("Document", rootDocument), adapterName);
054    }
055
056    @Path("path{docPath:(/(?:(?!/@).)*)}")
057    public Object getDocsByPath(@PathParam("docPath") String docPath) {
058        CoreSession session = getContext().getCoreSession();
059        DocumentModel doc = session.getDocument(new PathRef(docPath));
060        return newObject("Document", doc);
061    }
062
063    @Path("id/{id}")
064    public Object getDocsById(@PathParam("id") String id) {
065        CoreSession session = getContext().getCoreSession();
066        DocumentModel doc = session.getDocument(new IdRef(id));
067        return newObject("Document", doc);
068    }
069
070    @Path("bulk")
071    public Object getBulkDocuments(@MatrixParam("id") List<String> ids) {
072        CoreSession session = getContext().getCoreSession();
073        List<DocumentModel> docs = new ArrayList<>(ids.size());
074        for (String loopid : ids) {
075            docs.add(session.getDocument(new IdRef(loopid)));
076        }
077
078        return newObject("bulk", new DocumentModelListImpl(docs));
079
080    }
081
082    /**
083     * @since 7.2
084     */
085    @Path("{otherPath}")
086    public Object route(@PathParam("otherPath") String otherPath) {
087        return newObject(otherPath);
088    }
089}