Search in sources :

Example 66 with File

use of com.google.api.services.drive.model.File in project incubator-gobblin by apache.

the class GoogleDriveFsHelperTest method testPagination.

public void testPagination() throws IOException, FileBasedHelperException {
    State state = new State();
    state.appendToSetProp(GoogleDriveFileSystem.PAGE_SIZE, Integer.toString(1));
    GoogleDriveFsHelper fsHelper = new GoogleDriveFsHelper(state, client, Closer.create());
    List listRequest = mock(List.class);
    when(files.list()).thenReturn(listRequest);
    when(listRequest.setPageSize(anyInt())).thenReturn(listRequest);
    when(listRequest.setFields(anyString())).thenReturn(listRequest);
    when(listRequest.setQ(anyString())).thenReturn(listRequest);
    when(listRequest.setPageToken(anyString())).thenReturn(listRequest);
    int paginatedCalls = 5;
    final MutableInt i = new MutableInt(paginatedCalls);
    final File file = new File();
    file.setId("testId");
    file.setModifiedTime(new DateTime(System.currentTimeMillis()));
    when(listRequest.execute()).thenAnswer(new Answer<FileList>() {

        @Override
        public FileList answer(InvocationOnMock invocation) throws Throwable {
            FileList fileList = new FileList();
            fileList.setFiles(ImmutableList.of(file));
            if (i.intValue() > 0) {
                fileList.setNextPageToken("token");
                i.decrement();
            }
            return fileList;
        }
    });
    fsHelper.ls("test");
    int expectedCalls = 1 + paginatedCalls;
    verify(listRequest, times(expectedCalls)).execute();
}
Also used : FileList(com.google.api.services.drive.model.FileList) State(org.apache.gobblin.configuration.State) InvocationOnMock(org.mockito.invocation.InvocationOnMock) MutableInt(org.apache.commons.lang.mutable.MutableInt) FileList(com.google.api.services.drive.model.FileList) ImmutableList(com.google.common.collect.ImmutableList) File(com.google.api.services.drive.model.File) DateTime(com.google.api.client.util.DateTime)

Example 67 with File

use of com.google.api.services.drive.model.File in project incubator-gobblin by apache.

the class GoogleDriveFileSystem method lsFileMetadata.

private List<File> lsFileMetadata(String folderId, String fileId) throws IOException {
    String pageToken = null;
    List<File> result = new ArrayList<>();
    Optional<String> query = buildQuery(folderId, fileId);
    do {
        Drive.Files.List request = client.files().list().setFields("files/id,files/mimeType,files/modifiedTime,files/size,files/permissions").setPageSize(pageSize);
        if (query.isPresent()) {
            request = request.setQ(query.get());
        }
        if (pageToken != null) {
            request = request.setPageToken(pageToken);
        }
        LOG.info("Google drive List request: " + request);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Google drive List request: " + request);
        }
        FileList fileList = null;
        try {
            fileList = request.execute();
        } catch (GoogleJsonResponseException e) {
            GoogleJsonError error = e.getDetails();
            if (404 == error.getCode()) {
                throw new FileNotFoundException("File not found. Request: " + request);
            }
            throw e;
        }
        pageToken = fileList.getNextPageToken();
        List<File> files = fileList.getFiles();
        if (files == null || files.isEmpty()) {
            return result;
        }
        result.addAll(files);
    } while (pageToken != null);
    return result;
}
Also used : GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) FileList(com.google.api.services.drive.model.FileList) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) GoogleJsonError(com.google.api.client.googleapis.json.GoogleJsonError) File(com.google.api.services.drive.model.File)

Example 68 with File

use of com.google.api.services.drive.model.File in project incubator-gobblin by apache.

the class GoogleDriveFileSystem method listStatus.

@Override
public FileStatus[] listStatus(Path path) throws FileNotFoundException, IOException {
    String folderId = toFileId(path);
    List<File> fileMetadata = lsFileMetadata(folderId, null);
    if (fileMetadata.isEmpty()) {
        throw new FileNotFoundException();
    }
    FileStatus[] statusArr = new FileStatus[fileMetadata.size()];
    int idx = 0;
    for (File metadata : fileMetadata) {
        FileStatus status = toFileStatus(metadata);
        statusArr[idx++] = status;
    }
    return statusArr;
}
Also used : FileStatus(org.apache.hadoop.fs.FileStatus) FileNotFoundException(java.io.FileNotFoundException) File(com.google.api.services.drive.model.File)

Example 69 with File

use of com.google.api.services.drive.model.File in project incubator-gobblin by apache.

the class GoogleDriveFileSystem method getFileStatus.

@Override
public FileStatus getFileStatus(Path p) throws IOException {
    Preconditions.checkNotNull(p);
    String fileId = toFileId(p);
    File metadata = client.files().get(fileId).setFields("id,mimeType,modifiedTime,size,permissions").execute();
    return toFileStatus(metadata);
}
Also used : File(com.google.api.services.drive.model.File)

Example 70 with File

use of com.google.api.services.drive.model.File in project keepass2android by PhilippC.

the class GoogleDriveFileStorage method listFiles.

@Override
public List<FileEntry> listFiles(String parentPath) throws Exception {
    GDrivePath gdrivePath = new GDrivePath(parentPath);
    String parentId = gdrivePath.getGDriveId();
    List<FileEntry> result = new ArrayList<FileEntry>();
    Drive driveService = getDriveService(gdrivePath.getAccount());
    try {
        if (driveService.files().get(parentId).execute().getLabels().getTrashed())
            throw new FileNotFoundException(parentPath + " is trashed!");
        logDebug("listing files in " + parentId);
        Files.List request = driveService.files().list().setQ("trashed=false and '" + parentId + "' in parents");
        do {
            try {
                FileList files = request.execute();
                for (File file : files.getItems()) {
                    String path = new GDrivePath(parentPath, file).getFullPath();
                    logDebug("listing file " + path);
                    FileEntry e = convertToFileEntry(file, path);
                    result.add(e);
                }
                request.setPageToken(files.getNextPageToken());
            } catch (IOException e) {
                System.out.println("An error occurred: " + e);
                request.setPageToken(null);
                throw e;
            }
        } while (request.getPageToken() != null && request.getPageToken().length() > 0);
    } catch (Exception e) {
        throw convertException(e);
    }
    return result;
}
Also used : FileList(com.google.api.services.drive.model.FileList) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) UserRecoverableAuthIOException(com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException) IOException(java.io.IOException) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) UserRecoverableAuthIOException(com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Drive(com.google.api.services.drive.Drive) Files(com.google.api.services.drive.Drive.Files) File(com.google.api.services.drive.model.File)

Aggregations

File (com.google.api.services.drive.model.File)94 FileList (com.google.api.services.drive.model.FileList)35 Test (org.junit.Test)33 IOException (java.io.IOException)27 ArrayList (java.util.ArrayList)24 Before (org.junit.Before)12 FileContent (com.google.api.client.http.FileContent)10 HashMap (java.util.HashMap)10 UserRecoverableAuthIOException (com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException)9 FileNotFoundException (java.io.FileNotFoundException)7 ParseException (java.text.ParseException)7 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)6 DateTime (com.google.api.client.util.DateTime)6 Drive (com.google.api.services.drive.Drive)6 InputStream (java.io.InputStream)6 GeneralSecurityException (java.security.GeneralSecurityException)6 Matchers.anyString (org.mockito.Matchers.anyString)6 ParentReference (com.google.api.services.drive.model.ParentReference)5 Permission (com.google.api.services.drive.model.Permission)5 User (com.google.api.services.drive.model.User)5