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.directory;
020
021import java.util.ArrayList;
022import java.util.List;
023
024import javax.ws.rs.GET;
025import javax.ws.rs.Path;
026import javax.ws.rs.PathParam;
027import javax.ws.rs.QueryParam;
028
029import org.nuxeo.ecm.directory.Directory;
030import org.nuxeo.ecm.directory.api.DirectoryService;
031import org.nuxeo.ecm.webengine.model.WebObject;
032import org.nuxeo.ecm.webengine.model.impl.DefaultObject;
033import org.nuxeo.runtime.api.Framework;
034
035/**
036 * @since 5.7.8
037 */
038@WebObject(type = "directory")
039public class DirectoryRootObject extends DefaultObject {
040
041    @Path("{directoryName}")
042    public Object doGetDirectory(@PathParam("directoryName") String dirName) {
043        return newObject("directoryObject", dirName);
044    }
045
046    /**
047     * Get non system directory list.
048     *
049     * @param types if not empty, returns only the directories having at least one of the requested type
050     * @return the list of directories that are not system directories
051     * @since 8.4
052     */
053    @GET
054    public List<Directory> getDirectoryNames(@QueryParam("types") List<String> types) {
055        DirectoryService directoryService = Framework.getService(DirectoryService.class);
056        List<Directory> result = new ArrayList<>();
057        for (Directory dir : directoryService.getDirectories()) {
058            if (dir.getTypes().contains(DirectoryService.SYSTEM_DIRECTORY_TYPE)) {
059                continue;
060            } else if (types == null || types.isEmpty()) {
061                result.add(dir);
062            } else {
063                if (types.stream().filter(dir.getTypes()::contains).findFirst().isPresent()) {
064                    result.add(dir);
065                }
066            }
067        }
068        return result;
069    }
070
071}