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