BindSwiftObjectMetadataToRequest implements Binder { @SuppressWarnings("unchecked") @Override public <R extends HttpRequest> R bindToRequest(R request, Object input) { checkArgument(checkNotNull(input, "input") instanceof SwiftObject, "this binder is only valid for SwiftObject!"); checkNotNull(request, "request"); SwiftObject object = (SwiftObject) input; if (object.getPayload().getContentMetadata().getContentType() == null) object.getPayload().getContentMetadata().setContentType(MediaType.APPLICATION_OCTET_STREAM); if (object.getPayload().getContentMetadata().getContentLength() != null && object.getPayload().getContentMetadata().getContentLength() >= 0) { checkArgument(object.getPayload().getContentMetadata().getContentLength() <= 5l * 1024 * 1024 * 1024, "maximum size for put object is 5GB"); } else { request = (R) request.toBuilder().replaceHeader("Transfer-Encoding", "chunked").build(); } request = mdBinder.bindToRequest(request, object2Blob.apply(object)); return request; } @Inject  BindSwiftObjectMetadataToRequest(ObjectToBlob object2Blob, BindUserMetadataToHeadersWithPrefix mdBinder); @SuppressWarnings("unchecked") @Override R bindToRequest(R request, Object input);  }
@Test public void testPassWithMinimumDetailsAndPayload5GB() { SwiftObject object = injector.getInstance(SwiftObject.Factory.class).create(null); Payload payload = Payloads.newStringPayload(""); payload.getContentMetadata().setContentLength(5 * 1024 * 1024 * 1024l); object.setPayload(payload); object.getInfo().setName("foo"); HttpRequest request = HttpRequest.builder().method("PUT").endpoint("http: BindSwiftObjectMetadataToRequest binder = injector.getInstance(BindSwiftObjectMetadataToRequest.class); assertEquals(binder.bindToRequest(request, object), HttpRequest.builder().method("PUT").endpoint( URI.create("http: }
@Test public void testExtendedPropertiesBind() { SwiftObject object = injector.getInstance(SwiftObject.Factory.class).create(null); Payload payload = Payloads.newStringPayload(""); payload.getContentMetadata().setContentLength(5 * 1024 * 1024 * 1024l); object.setPayload(payload); object.getInfo().setName("foo"); object.getInfo().getMetadata().putAll(ImmutableMap.of("foo", "bar")); HttpRequest request = HttpRequest.builder().method("PUT").endpoint("http: BindSwiftObjectMetadataToRequest binder = injector.getInstance(BindSwiftObjectMetadataToRequest.class); assertEquals(binder.bindToRequest(request, object), HttpRequest.builder().method("PUT") .endpoint("http: }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNoNameIsBad() { SwiftObject object = injector.getInstance(SwiftObject.Factory.class).create(null); Payload payload = Payloads.newStringPayload(""); payload.getContentMetadata().setContentLength(5368709120000l); object.setPayload(payload); HttpRequest request = HttpRequest.builder().method("PUT").endpoint("http: BindSwiftObjectMetadataToRequest binder = injector.getInstance(BindSwiftObjectMetadataToRequest.class); binder.bindToRequest(request, object); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testOver5GBIsBad() { SwiftObject object = injector.getInstance(SwiftObject.Factory.class).create(null); Payload payload = Payloads.newStringPayload(""); payload.getContentMetadata().setContentLength(5 * 1024 * 1024 * 1024l + 1); object.setPayload(payload); object.getInfo().setName("foo"); HttpRequest request = HttpRequest.builder().method("PUT").endpoint("http: BindSwiftObjectMetadataToRequest binder = injector.getInstance(BindSwiftObjectMetadataToRequest.class); binder.bindToRequest(request, object); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testMustBeSwiftObject() { HttpRequest request = HttpRequest.builder().method("POST").endpoint("http: injector.getInstance(BindSwiftObjectMetadataToRequest.class).bindToRequest(request, new File("foo")); }
@Test(expectedExceptions = { NullPointerException.class, IllegalStateException.class }) public void testNullIsBad() { BindSwiftObjectMetadataToRequest binder = injector.getInstance(BindSwiftObjectMetadataToRequest.class); HttpRequest request = HttpRequest.builder().method("GET").endpoint("http: binder.bindToRequest(request, null); }