@Test public void testWrite() throws Exception { final Path test = new Path(DriveHomeFinderService.MYDRIVE_FOLDER, UUID.randomUUID().toString(), EnumSet.of(Path.Type.file)); final DriveFileidProvider fileid = new DriveFileidProvider(session).withCache(cache); { final TransferStatus status = new TransferStatus(); status.setMime("x-application/cyberduck"); final byte[] content = RandomUtils.nextBytes(2048); status.setLength(content.length); final HttpResponseOutputStream<VersionId> out = new DriveWriteFeature(session, fileid).write(test, status, new DisabledConnectionCallback()); new StreamCopier(new TransferStatus(), new TransferStatus()).transfer(new ByteArrayInputStream(content), out); out.close(); assertNotNull(out.getStatus().id); test.attributes().withVersionId(out.getStatus().id); assertTrue(new DefaultFindFeature(session).find(test)); final PathAttributes attributes = new DriveListService(session, fileid).list(test.getParent(), new DisabledListProgressListener()).get(test).attributes(); assertEquals(content.length, attributes.getSize()); final Write.Append append = new DriveWriteFeature(session, fileid).append(test, status.getLength(), PathCache.empty()); assertTrue(append.override); assertEquals(content.length, append.size, 0L); final byte[] buffer = new byte[content.length]; final InputStream in = new DriveReadFeature(session, fileid).read(test, new TransferStatus(), new DisabledConnectionCallback()); IOUtils.readFully(in, buffer); in.close(); assertArrayEquals(content, buffer); assertEquals("x-application/cyberduck", session.getClient().files().get(test.attributes().getVersionId()).execute().getMimeType()); } { final TransferStatus status = new TransferStatus(); status.setMime("x-application/cyberduck"); status.setExists(true); final byte[] content = RandomUtils.nextBytes(1024); status.setLength(content.length); final HttpResponseOutputStream<VersionId> out = new DriveWriteFeature(session, fileid).write(test, status, new DisabledConnectionCallback()); new StreamCopier(new TransferStatus(), new TransferStatus()).transfer(new ByteArrayInputStream(content), out); out.close(); assertNotNull(out.getStatus().id); final PathAttributes attributes = new DriveListService(session, fileid).list(test.getParent(), new DisabledListProgressListener()).get(test).attributes(); assertEquals(content.length, attributes.getSize()); assertEquals("x-application/cyberduck", session.getClient().files().get(test.attributes().getVersionId()).execute().getMimeType()); } new DriveDeleteFeature(session, fileid).delete(Collections.singletonList(test), new DisabledLoginCallback(), new Delete.DisabledCallback()); }
@Override public HttpResponseOutputStream<VersionId> write(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException { final DelayedHttpEntityCallable<VersionId> command = new DelayedHttpEntityCallable<VersionId>() { @Override public VersionId call(final AbstractHttpEntity entity) throws BackgroundException { try { final HttpEntityEnclosingRequestBase request; if(status.isExists()) { final String fileid = DriveWriteFeature.this.fileid.getFileid(file, new DisabledListProgressListener()); request = new HttpPatch(String.format("%supload/drive/v3/files/%s?supportsTeamDrives=true", session.getClient().getRootUrl(), fileid)); if(StringUtils.isNotBlank(status.getMime())) { request.setHeader(HttpHeaders.CONTENT_TYPE, status.getMime()); } request.setEntity(entity); } else { request = new HttpPost(String.format("%supload/drive/v3/files?uploadType=resumable&supportsTeamDrives=%s", session.getClient().getRootUrl(), PreferencesFactory.get().getBoolean("googledrive.teamdrive.enable"))); request.setEntity(new StringEntity("{\"name\": \"" + file.getName() + "\", \"parents\": [\"" + fileid.getFileid(file.getParent(), new DisabledListProgressListener()) + "\"]}", ContentType.create("application/json", StandardCharsets.UTF_8.name()))); if(StringUtils.isNotBlank(status.getMime())) { request.addHeader("X-Upload-Content-Type", status.getMime()); } } request.addHeader(HTTP.CONTENT_TYPE, MEDIA_TYPE); final HttpClient client = session.getHttpClient(); final HttpResponse response = client.execute(request); try { switch(response.getStatusLine().getStatusCode()) { case HttpStatus.SC_OK: break; default: throw new DefaultHttpResponseExceptionMappingService().map( new HttpResponseException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase())); } } finally { EntityUtils.consume(response.getEntity()); } if(!status.isExists()) { if(response.containsHeader(HttpHeaders.LOCATION)) { final String putTarget = response.getFirstHeader(HttpHeaders.LOCATION).getValue(); final HttpPut put = new HttpPut(putTarget); put.setEntity(entity); final HttpResponse putResponse = client.execute(put); try { switch(putResponse.getStatusLine().getStatusCode()) { case HttpStatus.SC_OK: case HttpStatus.SC_CREATED: try (JsonReader reader = new JsonReader(new InputStreamReader(putResponse.getEntity().getContent(), StandardCharsets.UTF_8))) { reader.beginObject(); while(reader.hasNext()) { final String name = reader.nextName(); final String value = reader.nextString(); switch(name) { case "id": final VersionId version = new VersionId(value); status.setVersion(version); return version; } } reader.endObject(); } break; default: throw new DefaultHttpResponseExceptionMappingService().map( new HttpResponseException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase())); } } finally { EntityUtils.consume(putResponse.getEntity()); } } else { throw new DefaultHttpResponseExceptionMappingService().map( new HttpResponseException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase())); } } return new VersionId(DriveWriteFeature.this.fileid.getFileid(file, new DisabledListProgressListener())); } catch(IOException e) { throw new DriveExceptionMappingService().map("Upload failed", e, file); } } @Override public long getContentLength() { return status.getLength(); } }; return this.write(file, status, command); }
DriveWriteFeature extends AbstractHttpWriteFeature<VersionId> implements Write<VersionId> { @Override public HttpResponseOutputStream<VersionId> write(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException { final DelayedHttpEntityCallable<VersionId> command = new DelayedHttpEntityCallable<VersionId>() { @Override public VersionId call(final AbstractHttpEntity entity) throws BackgroundException { try { final HttpEntityEnclosingRequestBase request; if(status.isExists()) { final String fileid = DriveWriteFeature.this.fileid.getFileid(file, new DisabledListProgressListener()); request = new HttpPatch(String.format("%supload/drive/v3/files/%s?supportsTeamDrives=true", session.getClient().getRootUrl(), fileid)); if(StringUtils.isNotBlank(status.getMime())) { request.setHeader(HttpHeaders.CONTENT_TYPE, status.getMime()); } request.setEntity(entity); } else { request = new HttpPost(String.format("%supload/drive/v3/files?uploadType=resumable&supportsTeamDrives=%s", session.getClient().getRootUrl(), PreferencesFactory.get().getBoolean("googledrive.teamdrive.enable"))); request.setEntity(new StringEntity("{\"name\": \"" + file.getName() + "\", \"parents\": [\"" + fileid.getFileid(file.getParent(), new DisabledListProgressListener()) + "\"]}", ContentType.create("application/json", StandardCharsets.UTF_8.name()))); if(StringUtils.isNotBlank(status.getMime())) { request.addHeader("X-Upload-Content-Type", status.getMime()); } } request.addHeader(HTTP.CONTENT_TYPE, MEDIA_TYPE); final HttpClient client = session.getHttpClient(); final HttpResponse response = client.execute(request); try { switch(response.getStatusLine().getStatusCode()) { case HttpStatus.SC_OK: break; default: throw new DefaultHttpResponseExceptionMappingService().map( new HttpResponseException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase())); } } finally { EntityUtils.consume(response.getEntity()); } if(!status.isExists()) { if(response.containsHeader(HttpHeaders.LOCATION)) { final String putTarget = response.getFirstHeader(HttpHeaders.LOCATION).getValue(); final HttpPut put = new HttpPut(putTarget); put.setEntity(entity); final HttpResponse putResponse = client.execute(put); try { switch(putResponse.getStatusLine().getStatusCode()) { case HttpStatus.SC_OK: case HttpStatus.SC_CREATED: try (JsonReader reader = new JsonReader(new InputStreamReader(putResponse.getEntity().getContent(), StandardCharsets.UTF_8))) { reader.beginObject(); while(reader.hasNext()) { final String name = reader.nextName(); final String value = reader.nextString(); switch(name) { case "id": final VersionId version = new VersionId(value); status.setVersion(version); return version; } } reader.endObject(); } break; default: throw new DefaultHttpResponseExceptionMappingService().map( new HttpResponseException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase())); } } finally { EntityUtils.consume(putResponse.getEntity()); } } else { throw new DefaultHttpResponseExceptionMappingService().map( new HttpResponseException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase())); } } return new VersionId(DriveWriteFeature.this.fileid.getFileid(file, new DisabledListProgressListener())); } catch(IOException e) { throw new DriveExceptionMappingService().map("Upload failed", e, file); } } @Override public long getContentLength() { return status.getLength(); } }; return this.write(file, status, command); } }
DriveWriteFeature extends AbstractHttpWriteFeature<VersionId> implements Write<VersionId> { @Override public HttpResponseOutputStream<VersionId> write(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException { final DelayedHttpEntityCallable<VersionId> command = new DelayedHttpEntityCallable<VersionId>() { @Override public VersionId call(final AbstractHttpEntity entity) throws BackgroundException { try { final HttpEntityEnclosingRequestBase request; if(status.isExists()) { final String fileid = DriveWriteFeature.this.fileid.getFileid(file, new DisabledListProgressListener()); request = new HttpPatch(String.format("%supload/drive/v3/files/%s?supportsTeamDrives=true", session.getClient().getRootUrl(), fileid)); if(StringUtils.isNotBlank(status.getMime())) { request.setHeader(HttpHeaders.CONTENT_TYPE, status.getMime()); } request.setEntity(entity); } else { request = new HttpPost(String.format("%supload/drive/v3/files?uploadType=resumable&supportsTeamDrives=%s", session.getClient().getRootUrl(), PreferencesFactory.get().getBoolean("googledrive.teamdrive.enable"))); request.setEntity(new StringEntity("{\"name\": \"" + file.getName() + "\", \"parents\": [\"" + fileid.getFileid(file.getParent(), new DisabledListProgressListener()) + "\"]}", ContentType.create("application/json", StandardCharsets.UTF_8.name()))); if(StringUtils.isNotBlank(status.getMime())) { request.addHeader("X-Upload-Content-Type", status.getMime()); } } request.addHeader(HTTP.CONTENT_TYPE, MEDIA_TYPE); final HttpClient client = session.getHttpClient(); final HttpResponse response = client.execute(request); try { switch(response.getStatusLine().getStatusCode()) { case HttpStatus.SC_OK: break; default: throw new DefaultHttpResponseExceptionMappingService().map( new HttpResponseException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase())); } } finally { EntityUtils.consume(response.getEntity()); } if(!status.isExists()) { if(response.containsHeader(HttpHeaders.LOCATION)) { final String putTarget = response.getFirstHeader(HttpHeaders.LOCATION).getValue(); final HttpPut put = new HttpPut(putTarget); put.setEntity(entity); final HttpResponse putResponse = client.execute(put); try { switch(putResponse.getStatusLine().getStatusCode()) { case HttpStatus.SC_OK: case HttpStatus.SC_CREATED: try (JsonReader reader = new JsonReader(new InputStreamReader(putResponse.getEntity().getContent(), StandardCharsets.UTF_8))) { reader.beginObject(); while(reader.hasNext()) { final String name = reader.nextName(); final String value = reader.nextString(); switch(name) { case "id": final VersionId version = new VersionId(value); status.setVersion(version); return version; } } reader.endObject(); } break; default: throw new DefaultHttpResponseExceptionMappingService().map( new HttpResponseException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase())); } } finally { EntityUtils.consume(putResponse.getEntity()); } } else { throw new DefaultHttpResponseExceptionMappingService().map( new HttpResponseException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase())); } } return new VersionId(DriveWriteFeature.this.fileid.getFileid(file, new DisabledListProgressListener())); } catch(IOException e) { throw new DriveExceptionMappingService().map("Upload failed", e, file); } } @Override public long getContentLength() { return status.getLength(); } }; return this.write(file, status, command); } DriveWriteFeature(final DriveSession session, final DriveFileidProvider fileid);  DriveWriteFeature(final DriveSession session, final DriveFileidProvider fileid, final Find finder, final AttributesFinder attributes); }
DriveWriteFeature extends AbstractHttpWriteFeature<VersionId> implements Write<VersionId> { @Override public HttpResponseOutputStream<VersionId> write(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException { final DelayedHttpEntityCallable<VersionId> command = new DelayedHttpEntityCallable<VersionId>() { @Override public VersionId call(final AbstractHttpEntity entity) throws BackgroundException { try { final HttpEntityEnclosingRequestBase request; if(status.isExists()) { final String fileid = DriveWriteFeature.this.fileid.getFileid(file, new DisabledListProgressListener()); request = new HttpPatch(String.format("%supload/drive/v3/files/%s?supportsTeamDrives=true", session.getClient().getRootUrl(), fileid)); if(StringUtils.isNotBlank(status.getMime())) { request.setHeader(HttpHeaders.CONTENT_TYPE, status.getMime()); } request.setEntity(entity); } else { request = new HttpPost(String.format("%supload/drive/v3/files?uploadType=resumable&supportsTeamDrives=%s", session.getClient().getRootUrl(), PreferencesFactory.get().getBoolean("googledrive.teamdrive.enable"))); request.setEntity(new StringEntity("{\"name\": \"" + file.getName() + "\", \"parents\": [\"" + fileid.getFileid(file.getParent(), new DisabledListProgressListener()) + "\"]}", ContentType.create("application/json", StandardCharsets.UTF_8.name()))); if(StringUtils.isNotBlank(status.getMime())) { request.addHeader("X-Upload-Content-Type", status.getMime()); } } request.addHeader(HTTP.CONTENT_TYPE, MEDIA_TYPE); final HttpClient client = session.getHttpClient(); final HttpResponse response = client.execute(request); try { switch(response.getStatusLine().getStatusCode()) { case HttpStatus.SC_OK: break; default: throw new DefaultHttpResponseExceptionMappingService().map( new HttpResponseException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase())); } } finally { EntityUtils.consume(response.getEntity()); } if(!status.isExists()) { if(response.containsHeader(HttpHeaders.LOCATION)) { final String putTarget = response.getFirstHeader(HttpHeaders.LOCATION).getValue(); final HttpPut put = new HttpPut(putTarget); put.setEntity(entity); final HttpResponse putResponse = client.execute(put); try { switch(putResponse.getStatusLine().getStatusCode()) { case HttpStatus.SC_OK: case HttpStatus.SC_CREATED: try (JsonReader reader = new JsonReader(new InputStreamReader(putResponse.getEntity().getContent(), StandardCharsets.UTF_8))) { reader.beginObject(); while(reader.hasNext()) { final String name = reader.nextName(); final String value = reader.nextString(); switch(name) { case "id": final VersionId version = new VersionId(value); status.setVersion(version); return version; } } reader.endObject(); } break; default: throw new DefaultHttpResponseExceptionMappingService().map( new HttpResponseException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase())); } } finally { EntityUtils.consume(putResponse.getEntity()); } } else { throw new DefaultHttpResponseExceptionMappingService().map( new HttpResponseException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase())); } } return new VersionId(DriveWriteFeature.this.fileid.getFileid(file, new DisabledListProgressListener())); } catch(IOException e) { throw new DriveExceptionMappingService().map("Upload failed", e, file); } } @Override public long getContentLength() { return status.getLength(); } }; return this.write(file, status, command); } DriveWriteFeature(final DriveSession session, final DriveFileidProvider fileid);  DriveWriteFeature(final DriveSession session, final DriveFileidProvider fileid, final Find finder, final AttributesFinder attributes); @Override Append append(final Path file, final Long length, final Cache<Path> cache); @Override boolean temporary(); @Override boolean random(); @Override HttpResponseOutputStream<VersionId> write(final Path file, final TransferStatus status, final ConnectionCallback callback); }
DriveWriteFeature extends AbstractHttpWriteFeature<VersionId> implements Write<VersionId> { @Override public HttpResponseOutputStream<VersionId> write(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException { final DelayedHttpEntityCallable<VersionId> command = new DelayedHttpEntityCallable<VersionId>() { @Override public VersionId call(final AbstractHttpEntity entity) throws BackgroundException { try { final HttpEntityEnclosingRequestBase request; if(status.isExists()) { final String fileid = DriveWriteFeature.this.fileid.getFileid(file, new DisabledListProgressListener()); request = new HttpPatch(String.format("%supload/drive/v3/files/%s?supportsTeamDrives=true", session.getClient().getRootUrl(), fileid)); if(StringUtils.isNotBlank(status.getMime())) { request.setHeader(HttpHeaders.CONTENT_TYPE, status.getMime()); } request.setEntity(entity); } else { request = new HttpPost(String.format("%supload/drive/v3/files?uploadType=resumable&supportsTeamDrives=%s", session.getClient().getRootUrl(), PreferencesFactory.get().getBoolean("googledrive.teamdrive.enable"))); request.setEntity(new StringEntity("{\"name\": \"" + file.getName() + "\", \"parents\": [\"" + fileid.getFileid(file.getParent(), new DisabledListProgressListener()) + "\"]}", ContentType.create("application/json", StandardCharsets.UTF_8.name()))); if(StringUtils.isNotBlank(status.getMime())) { request.addHeader("X-Upload-Content-Type", status.getMime()); } } request.addHeader(HTTP.CONTENT_TYPE, MEDIA_TYPE); final HttpClient client = session.getHttpClient(); final HttpResponse response = client.execute(request); try { switch(response.getStatusLine().getStatusCode()) { case HttpStatus.SC_OK: break; default: throw new DefaultHttpResponseExceptionMappingService().map( new HttpResponseException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase())); } } finally { EntityUtils.consume(response.getEntity()); } if(!status.isExists()) { if(response.containsHeader(HttpHeaders.LOCATION)) { final String putTarget = response.getFirstHeader(HttpHeaders.LOCATION).getValue(); final HttpPut put = new HttpPut(putTarget); put.setEntity(entity); final HttpResponse putResponse = client.execute(put); try { switch(putResponse.getStatusLine().getStatusCode()) { case HttpStatus.SC_OK: case HttpStatus.SC_CREATED: try (JsonReader reader = new JsonReader(new InputStreamReader(putResponse.getEntity().getContent(), StandardCharsets.UTF_8))) { reader.beginObject(); while(reader.hasNext()) { final String name = reader.nextName(); final String value = reader.nextString(); switch(name) { case "id": final VersionId version = new VersionId(value); status.setVersion(version); return version; } } reader.endObject(); } break; default: throw new DefaultHttpResponseExceptionMappingService().map( new HttpResponseException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase())); } } finally { EntityUtils.consume(putResponse.getEntity()); } } else { throw new DefaultHttpResponseExceptionMappingService().map( new HttpResponseException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase())); } } return new VersionId(DriveWriteFeature.this.fileid.getFileid(file, new DisabledListProgressListener())); } catch(IOException e) { throw new DriveExceptionMappingService().map("Upload failed", e, file); } } @Override public long getContentLength() { return status.getLength(); } }; return this.write(file, status, command); } DriveWriteFeature(final DriveSession session, final DriveFileidProvider fileid);  DriveWriteFeature(final DriveSession session, final DriveFileidProvider fileid, final Find finder, final AttributesFinder attributes); @Override Append append(final Path file, final Long length, final Cache<Path> cache); @Override boolean temporary(); @Override boolean random(); @Override HttpResponseOutputStream<VersionId> write(final Path file, final TransferStatus status, final ConnectionCallback callback);  }