001/* 002 * (C) Copyright 2016 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 * Antoine Taillefer <ataillefer@nuxeo.com> 018 */ 019package org.nuxeo.drive.operations; 020 021import java.io.IOException; 022import java.io.StringWriter; 023 024import org.codehaus.jackson.JsonFactory; 025import org.codehaus.jackson.JsonGenerator; 026import org.codehaus.jackson.map.ObjectMapper; 027import org.nuxeo.drive.adapter.FolderItem; 028import org.nuxeo.drive.adapter.ScrollFileSystemItemList; 029import org.nuxeo.drive.service.FileSystemItemManager; 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.automation.core.annotations.Param; 036import org.nuxeo.ecm.core.api.Blob; 037import org.nuxeo.ecm.core.api.Blobs; 038import org.nuxeo.runtime.api.Framework; 039 040/** 041 * Retrieves at most {@code batchSize} descendants of the {@link FolderItem} with the given {@code id} for the currently 042 * authenticated user and the given {@code scrollId}. 043 * <p> 044 * When passing a null {@code scrollId} the initial search request is executed and the first batch of results is 045 * returned along with a {@code scrollId} which should be passed to the next call in order to retrieve the next batch of 046 * results. 047 * <p> 048 * Ideally, the search context made available by the initial search request is kept alive during {@code keepAlive} 049 * milliseconds if {@code keepAlive} is positive. 050 * <p> 051 * Results are not necessarily sorted. 052 * 053 * @since 8.3 054 */ 055@Operation(id = NuxeoDriveScrollDescendants.ID, category = Constants.CAT_SERVICES, label = "Nuxeo Drive: Scroll descendants") 056public class NuxeoDriveScrollDescendants { 057 058 public static final String ID = "NuxeoDrive.ScrollDescendants"; 059 060 @Context 061 protected OperationContext ctx; 062 063 @Param(name = "id") 064 protected String id; 065 066 @Param(name = "scrollId", required = false) 067 protected String scrollId; 068 069 @Param(name = "batchSize") 070 protected int batchSize; 071 072 @Param(name = "keepAlive", required = false) 073 protected long keepAlive = 60000; // 1 minute 074 075 @OperationMethod 076 public Blob run() throws IOException { 077 078 FileSystemItemManager fileSystemItemManager = Framework.getService(FileSystemItemManager.class); 079 ScrollFileSystemItemList descendants = fileSystemItemManager.scrollDescendants(id, ctx.getPrincipal(), 080 scrollId, batchSize, keepAlive); 081 return writeJSONBlob(descendants); 082 } 083 084 protected Blob writeJSONBlob(ScrollFileSystemItemList scrollFSIList) throws IOException { 085 StringWriter writer = new StringWriter(); 086 JsonFactory factory = new JsonFactory(); 087 JsonGenerator jg = factory.createJsonGenerator(writer); 088 jg.setCodec(new ObjectMapper()); 089 jg.writeStartObject(); 090 jg.writeStringField("scrollId", scrollFSIList.getScrollId()); 091 jg.writeObjectField("fileSystemItems", scrollFSIList); 092 jg.writeEndObject(); 093 jg.close(); 094 return Blobs.createJSONBlob(writer.toString()); 095 } 096 097}