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     * @since 5.8
049     */
050    @Path("path/@{adapterName}")
051    public Object getRootPathAdapter(@PathParam("adapterName") String adapterName) {
052        DocumentModel rootDocument = getContext().getCoreSession().getRootDocument();
053
054        return ctx.newAdapter(newObject("Document", rootDocument), adapterName);
055    }
056
057    @Path("path{docPath:(/(?:(?!/@).)*)}")
058    public Object getDocsByPath(@PathParam("docPath") String docPath) {
059        CoreSession session = getContext().getCoreSession();
060        DocumentModel doc = session.getDocument(new PathRef(docPath));
061        return newObject("Document", doc);
062    }
063
064    @Path("id/{id}")
065    public Object getDocsById(@PathParam("id") String id) {
066        CoreSession session = getContext().getCoreSession();
067        DocumentModel doc = session.getDocument(new IdRef(id));
068        return newObject("Document", doc);
069    }
070
071    /**
072     * @deprecated since 10.3, use {@link BulkActionFrameworkObject BAF} instead
073     */
074    @Path("bulk")
075    @Deprecated
076    public Object getBulkDocuments(@MatrixParam("id") List<String> ids) {
077        return getBulkDocuments(this, ids);
078    }
079
080    /**
081     * @deprecated since 10.3, use {@link BulkActionFrameworkObject BAF} instead
082     */
083    @Deprecated
084    protected static Object getBulkDocuments(DefaultObject obj, List<String> ids) {
085        CoreSession session = obj.getContext().getCoreSession();
086        List<DocumentModel> docs = new ArrayList<>(ids.size());
087        for (String loopid : ids) {
088            docs.add(session.getDocument(new IdRef(loopid)));
089        }
090
091        return obj.newObject("bulk", new DocumentModelListImpl(docs));
092    }
093
094    @Path("@" + EmptyDocumentAdapter.NAME)
095    public Object getEmptyDocumentModel() {
096        return newObject("emptyDocumentAdapter");
097    }
098
099    /**
100     * @since 7.2
101     */
102    @Path("{otherPath}")
103    public Object route(@PathParam("otherPath") String otherPath) {
104        return newObject(otherPath);
105    }
106}