@Test public void testReadRange() throws Exception { final String name = "ä-" + UUID.randomUUID().toString(); final Path test = new Path(DriveHomeFinderService.MYDRIVE_FOLDER, name, EnumSet.of(Path.Type.file)); final Local local = new Local(System.getProperty("java.io.tmpdir"), name); final byte[] content = new RandomStringGenerator.Builder().build().generate(1000).getBytes(); final OutputStream out = local.getOutputStream(false); assertNotNull(out); IOUtils.write(content, out); out.close(); final DriveFileidProvider fileid = new DriveFileidProvider(session).withCache(cache); new DriveUploadFeature(new DriveWriteFeature(session, fileid)).upload( test, local, new BandwidthThrottle(BandwidthThrottle.UNLIMITED), new DisabledStreamListener(), new TransferStatus().length(content.length), new DisabledConnectionCallback()); final TransferStatus status = new TransferStatus(); status.setLength(content.length); status.setAppend(true); status.setOffset(100L); final InputStream in = new DriveReadFeature(session, fileid).read(test, status.length(content.length - 100), new DisabledConnectionCallback()); assertNotNull(in); final ByteArrayOutputStream buffer = new ByteArrayOutputStream(content.length - 100); new StreamCopier(status, status).transfer(in, buffer); final byte[] reference = new byte[content.length - 100]; System.arraycopy(content, 100, reference, 0, content.length - 100); assertArrayEquals(reference, buffer.toByteArray()); in.close(); new DriveDeleteFeature(session, fileid).delete(Collections.<Path>singletonList(test), new DisabledLoginCallback(), new Delete.DisabledCallback()); }
@Override public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException { if(file.getType().contains(Path.Type.placeholder)) { final DescriptiveUrl link = new DriveUrlProvider().toUrl(file).find(DescriptiveUrl.Type.http); if(DescriptiveUrl.EMPTY.equals(link)) { log.warn(String.format("Missing web link for file %s", file)); return new NullInputStream(file.attributes().getSize()); } return IOUtils.toInputStream(UrlFileWriterFactory.get().write(link), Charset.defaultCharset()); } else { try { final HttpUriRequest request = new HttpGet(String.format("%sdrive/v3/files/%s?alt=media&supportsTeamDrives=%s", session.getClient().getRootUrl(), fileid.getFileid(file, new DisabledListProgressListener()), PreferencesFactory.get().getBoolean("googledrive.teamdrive.enable"))); return this.read(request, file, status); } catch(RetriableAccessDeniedException e) { throw e; } catch(AccessDeniedException e) { if(!PreferencesFactory.get().getBoolean(String.format("connection.unsecure.download.%s", session.getHost().getHostname()))) { callback.warn(session.getHost(), MessageFormat.format(LocaleFactory.localizedString("Download {0} failed", "Error"), file.getName()), "Acknowledge the risk of downloading known malware or other abusive file.", LocaleFactory.localizedString("Continue", "Credentials"), LocaleFactory.localizedString("Cancel", "Localizable"), String.format("connection.unsecure.download.%s", session.getHost().getHostname())); } final HttpUriRequest request = new HttpGet(String.format("%sdrive/v3/files/%s?alt=media&supportsTeamDrives=%s&acknowledgeAbuse=true", session.getClient().getRootUrl(), fileid.getFileid(file, new DisabledListProgressListener()), PreferencesFactory.get().getBoolean("googledrive.teamdrive.enable"))); return this.read(request, file, status); } } }
DriveReadFeature implements Read { @Override public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException { if(file.getType().contains(Path.Type.placeholder)) { final DescriptiveUrl link = new DriveUrlProvider().toUrl(file).find(DescriptiveUrl.Type.http); if(DescriptiveUrl.EMPTY.equals(link)) { log.warn(String.format("Missing web link for file %s", file)); return new NullInputStream(file.attributes().getSize()); } return IOUtils.toInputStream(UrlFileWriterFactory.get().write(link), Charset.defaultCharset()); } else { try { final HttpUriRequest request = new HttpGet(String.format("%sdrive/v3/files/%s?alt=media&supportsTeamDrives=%s", session.getClient().getRootUrl(), fileid.getFileid(file, new DisabledListProgressListener()), PreferencesFactory.get().getBoolean("googledrive.teamdrive.enable"))); return this.read(request, file, status); } catch(RetriableAccessDeniedException e) { throw e; } catch(AccessDeniedException e) { if(!PreferencesFactory.get().getBoolean(String.format("connection.unsecure.download.%s", session.getHost().getHostname()))) { callback.warn(session.getHost(), MessageFormat.format(LocaleFactory.localizedString("Download {0} failed", "Error"), file.getName()), "Acknowledge the risk of downloading known malware or other abusive file.", LocaleFactory.localizedString("Continue", "Credentials"), LocaleFactory.localizedString("Cancel", "Localizable"), String.format("connection.unsecure.download.%s", session.getHost().getHostname())); } final HttpUriRequest request = new HttpGet(String.format("%sdrive/v3/files/%s?alt=media&supportsTeamDrives=%s&acknowledgeAbuse=true", session.getClient().getRootUrl(), fileid.getFileid(file, new DisabledListProgressListener()), PreferencesFactory.get().getBoolean("googledrive.teamdrive.enable"))); return this.read(request, file, status); } } } }
DriveReadFeature implements Read { @Override public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException { if(file.getType().contains(Path.Type.placeholder)) { final DescriptiveUrl link = new DriveUrlProvider().toUrl(file).find(DescriptiveUrl.Type.http); if(DescriptiveUrl.EMPTY.equals(link)) { log.warn(String.format("Missing web link for file %s", file)); return new NullInputStream(file.attributes().getSize()); } return IOUtils.toInputStream(UrlFileWriterFactory.get().write(link), Charset.defaultCharset()); } else { try { final HttpUriRequest request = new HttpGet(String.format("%sdrive/v3/files/%s?alt=media&supportsTeamDrives=%s", session.getClient().getRootUrl(), fileid.getFileid(file, new DisabledListProgressListener()), PreferencesFactory.get().getBoolean("googledrive.teamdrive.enable"))); return this.read(request, file, status); } catch(RetriableAccessDeniedException e) { throw e; } catch(AccessDeniedException e) { if(!PreferencesFactory.get().getBoolean(String.format("connection.unsecure.download.%s", session.getHost().getHostname()))) { callback.warn(session.getHost(), MessageFormat.format(LocaleFactory.localizedString("Download {0} failed", "Error"), file.getName()), "Acknowledge the risk of downloading known malware or other abusive file.", LocaleFactory.localizedString("Continue", "Credentials"), LocaleFactory.localizedString("Cancel", "Localizable"), String.format("connection.unsecure.download.%s", session.getHost().getHostname())); } final HttpUriRequest request = new HttpGet(String.format("%sdrive/v3/files/%s?alt=media&supportsTeamDrives=%s&acknowledgeAbuse=true", session.getClient().getRootUrl(), fileid.getFileid(file, new DisabledListProgressListener()), PreferencesFactory.get().getBoolean("googledrive.teamdrive.enable"))); return this.read(request, file, status); } } } DriveReadFeature(final DriveSession session, final DriveFileidProvider fileid); }
DriveReadFeature implements Read { @Override public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException { if(file.getType().contains(Path.Type.placeholder)) { final DescriptiveUrl link = new DriveUrlProvider().toUrl(file).find(DescriptiveUrl.Type.http); if(DescriptiveUrl.EMPTY.equals(link)) { log.warn(String.format("Missing web link for file %s", file)); return new NullInputStream(file.attributes().getSize()); } return IOUtils.toInputStream(UrlFileWriterFactory.get().write(link), Charset.defaultCharset()); } else { try { final HttpUriRequest request = new HttpGet(String.format("%sdrive/v3/files/%s?alt=media&supportsTeamDrives=%s", session.getClient().getRootUrl(), fileid.getFileid(file, new DisabledListProgressListener()), PreferencesFactory.get().getBoolean("googledrive.teamdrive.enable"))); return this.read(request, file, status); } catch(RetriableAccessDeniedException e) { throw e; } catch(AccessDeniedException e) { if(!PreferencesFactory.get().getBoolean(String.format("connection.unsecure.download.%s", session.getHost().getHostname()))) { callback.warn(session.getHost(), MessageFormat.format(LocaleFactory.localizedString("Download {0} failed", "Error"), file.getName()), "Acknowledge the risk of downloading known malware or other abusive file.", LocaleFactory.localizedString("Continue", "Credentials"), LocaleFactory.localizedString("Cancel", "Localizable"), String.format("connection.unsecure.download.%s", session.getHost().getHostname())); } final HttpUriRequest request = new HttpGet(String.format("%sdrive/v3/files/%s?alt=media&supportsTeamDrives=%s&acknowledgeAbuse=true", session.getClient().getRootUrl(), fileid.getFileid(file, new DisabledListProgressListener()), PreferencesFactory.get().getBoolean("googledrive.teamdrive.enable"))); return this.read(request, file, status); } } } DriveReadFeature(final DriveSession session, final DriveFileidProvider fileid); @Override boolean offset(Path file); @Override InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback); }
DriveReadFeature implements Read { @Override public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException { if(file.getType().contains(Path.Type.placeholder)) { final DescriptiveUrl link = new DriveUrlProvider().toUrl(file).find(DescriptiveUrl.Type.http); if(DescriptiveUrl.EMPTY.equals(link)) { log.warn(String.format("Missing web link for file %s", file)); return new NullInputStream(file.attributes().getSize()); } return IOUtils.toInputStream(UrlFileWriterFactory.get().write(link), Charset.defaultCharset()); } else { try { final HttpUriRequest request = new HttpGet(String.format("%sdrive/v3/files/%s?alt=media&supportsTeamDrives=%s", session.getClient().getRootUrl(), fileid.getFileid(file, new DisabledListProgressListener()), PreferencesFactory.get().getBoolean("googledrive.teamdrive.enable"))); return this.read(request, file, status); } catch(RetriableAccessDeniedException e) { throw e; } catch(AccessDeniedException e) { if(!PreferencesFactory.get().getBoolean(String.format("connection.unsecure.download.%s", session.getHost().getHostname()))) { callback.warn(session.getHost(), MessageFormat.format(LocaleFactory.localizedString("Download {0} failed", "Error"), file.getName()), "Acknowledge the risk of downloading known malware or other abusive file.", LocaleFactory.localizedString("Continue", "Credentials"), LocaleFactory.localizedString("Cancel", "Localizable"), String.format("connection.unsecure.download.%s", session.getHost().getHostname())); } final HttpUriRequest request = new HttpGet(String.format("%sdrive/v3/files/%s?alt=media&supportsTeamDrives=%s&acknowledgeAbuse=true", session.getClient().getRootUrl(), fileid.getFileid(file, new DisabledListProgressListener()), PreferencesFactory.get().getBoolean("googledrive.teamdrive.enable"))); return this.read(request, file, status); } } } DriveReadFeature(final DriveSession session, final DriveFileidProvider fileid); @Override boolean offset(Path file); @Override InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback);  }