001/* 002 * (C) Copyright 2019 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 * bdelbosc 018 */ 019package org.nuxeo.ecm.core.scroll; 020 021import static org.nuxeo.ecm.core.api.security.SecurityConstants.SYSTEM_USERNAME; 022 023import java.util.List; 024import java.util.Map; 025import java.util.NoSuchElementException; 026 027import javax.security.auth.login.LoginException; 028 029import org.nuxeo.ecm.core.api.CoreInstance; 030import org.nuxeo.ecm.core.api.CoreSession; 031import org.nuxeo.ecm.core.api.ScrollResult; 032import org.nuxeo.ecm.core.api.scroll.Scroll; 033import org.nuxeo.ecm.core.api.scroll.ScrollRequest; 034import org.nuxeo.runtime.api.Framework; 035import org.nuxeo.runtime.api.login.NuxeoLoginContext; 036 037/** 038 * Scrolls document identifiers using the repository search. 039 * 040 * @since 11.1 041 */ 042public class RepositoryScroll implements Scroll { 043 044 protected DocumentScrollRequest request; 045 046 protected NuxeoLoginContext loginContext; 047 048 protected CoreSession session; 049 050 protected ScrollResult<String> repoScroller; 051 052 protected Boolean hasNextResult; 053 054 @Override 055 public void init(ScrollRequest request, Map<String, String> options) { 056 if (!(request instanceof DocumentScrollRequest)) { 057 throw new IllegalArgumentException("Requires a DocumentScrollRequest"); 058 } 059 this.request = (DocumentScrollRequest) request; 060 login(); 061 openSession(); 062 hasNextResult = null; 063 } 064 065 protected void login() { 066 String username = request.getUsername(); 067 try { 068 loginContext = SYSTEM_USERNAME.equals(username) ? Framework.loginSystem() : Framework.loginUser(username); 069 } catch (LoginException e) { 070 throw new IllegalArgumentException("Cannot login as user: " + username, e); 071 } 072 } 073 074 protected void openSession() { 075 session = CoreInstance.getCoreSession(request.getRepository()); 076 } 077 078 @Override 079 public boolean hasNext() { 080 if (hasNextResult == null) { 081 hasNextResult = fetch(); 082 } 083 return hasNextResult; 084 } 085 086 protected boolean fetch() { 087 if (repoScroller == null) { 088 repoScroller = session.scroll(request.getQuery(), request.getSize(), 089 (int) request.getTimeout().toSeconds()); 090 } else { 091 repoScroller = session.scroll(repoScroller.getScrollId()); 092 } 093 return repoScroller.hasResults(); 094 } 095 096 @Override 097 public List<String> next() { 098 if (hasNextResult == null) { 099 hasNextResult = fetch(); 100 } 101 if (!hasNextResult) { 102 throw new NoSuchElementException(); 103 } 104 hasNextResult = null; 105 return repoScroller.getResults(); 106 } 107 108 @Override 109 public void close() { 110 if (loginContext != null) { 111 loginContext.close(); 112 loginContext = null; 113 } 114 } 115 116 @Override 117 public String toString() { 118 return "RepositoryScroll{" + "request=" + request + '}'; 119 } 120 121}