001/*
002 * (C) Copyright 2012-2018 Nuxeo (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 *     Olivier Grisel <ogrisel@nuxeo.com>
018 *     Antoine Taillefer <ataillefer@nuxeo.com>
019 */
020package org.nuxeo.drive.operations;
021
022import java.util.Map;
023import java.util.Set;
024
025import javax.servlet.http.HttpServletRequest;
026
027import org.apache.commons.lang3.StringUtils;
028import org.nuxeo.drive.service.NuxeoDriveManager;
029import org.nuxeo.drive.service.SynchronizationRoots;
030import org.nuxeo.ecm.automation.OperationContext;
031import org.nuxeo.ecm.automation.core.Constants;
032import org.nuxeo.ecm.automation.core.annotations.Context;
033import org.nuxeo.ecm.automation.core.annotations.Operation;
034import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
035import org.nuxeo.ecm.core.api.CoreSession;
036import org.nuxeo.ecm.core.api.DocumentModelList;
037import org.nuxeo.ecm.core.api.DocumentRef;
038import org.nuxeo.ecm.core.api.IdRef;
039import org.nuxeo.ecm.core.api.NuxeoException;
040import org.nuxeo.ecm.core.api.impl.DocumentModelListImpl;
041import org.nuxeo.ecm.core.io.registry.context.RenderingContext;
042import org.nuxeo.runtime.api.Framework;
043
044/**
045 * Gets the list of synchronization roots for the currently authenticated user.
046 */
047@Operation(id = NuxeoDriveGetRootsOperation.ID, category = Constants.CAT_SERVICES, label = "Nuxeo Drive: Get Roots", description = "Get the list of synchronization roots for the currently authenticated user.")
048public class NuxeoDriveGetRootsOperation {
049
050    public static final String ID = "NuxeoDrive.GetRoots";
051
052    @Context
053    protected OperationContext ctx;
054
055    @Context
056    protected CoreSession session;
057
058    @OperationMethod
059    public DocumentModelList run() {
060        // By default get synchronization roots from all repositories, except if
061        // a specific repository name is passed as a request header
062        boolean allRepositories = true;
063        HttpServletRequest request = (HttpServletRequest) ctx.get("request");
064        if (request != null) {
065            String respositoryName = request.getHeader(RenderingContext.REPOSITORY_NAME_REQUEST_HEADER);
066            if (!StringUtils.isEmpty(respositoryName)) {
067                allRepositories = false;
068            }
069        }
070        NuxeoDriveManager driveManager = Framework.getService(NuxeoDriveManager.class);
071        Map<String, SynchronizationRoots> roots = driveManager.getSynchronizationRoots(ctx.getPrincipal());
072        DocumentModelList rootDocumentModels = new DocumentModelListImpl();
073        for (Map.Entry<String, SynchronizationRoots> rootsEntry : roots.entrySet()) {
074            if (session.getRepositoryName().equals(rootsEntry.getKey())) {
075                Set<IdRef> references = rootsEntry.getValue().getRefs();
076                rootDocumentModels.addAll(session.getDocuments(references.toArray(new DocumentRef[references.size()])));
077            } else {
078                if (allRepositories) {
079                    // XXX: do we really need to implement this now?
080                    throw new NuxeoException("Multi repo roots not yet implemented");
081                }
082            }
083        }
084        return rootDocumentModels;
085    }
086
087}