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    /**
074     * @deprecated since 10.3, use {@link BulkActionFrameworkObject BAF} instead
075     */
076    @Path("bulk")
077    @Deprecated
078    public Object getBulkDocuments(@MatrixParam("id") List<String> ids) {
079        return getBulkDocuments(this, ids);
080    }
081
082    /**
083     * @deprecated since 10.3, use {@link BulkActionFrameworkObject BAF} instead
084     */
085    @Deprecated
086    protected static Object getBulkDocuments(DefaultObject obj, List<String> ids) {
087        CoreSession session = obj.getContext().getCoreSession();
088        List<DocumentModel> docs = new ArrayList<>(ids.size());
089        for (String loopid : ids) {
090            docs.add(session.getDocument(new IdRef(loopid)));
091        }
092
093        return obj.newObject("bulk", new DocumentModelListImpl(docs));
094    }
095
096    @Path("@" + EmptyDocumentAdapter.NAME)
097    public Object getEmptyDocumentModel() {
098        return newObject("emptyDocumentAdapter");
099    }
100
101    /**
102     * @since 7.2
103     */
104    @Path("{otherPath}")
105    public Object route(@PathParam("otherPath") String otherPath) {
106        return newObject(otherPath);
107    }
108}