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