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