Skip to content

Commit

Permalink
JCLOUDS-1641: Use POST instead of PATCH for GCS
Browse files Browse the repository at this point in the history
Modern Java versions do not allow the reflection workaround previously
used.
  • Loading branch information
gaul committed Dec 4, 2024
1 parent 6b51855 commit d3a261b
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ private void setRequestMethodBypassingJREMethodLimitation(final HttpURLConnectio
try {
// SSL connections may have the HttpURLConnection wrapped inside
delegateField = connectionClass.getDeclaredField("delegate");
// TODO: Fails on modern Java versions:
// Unable to make field private final sun.net.www.protocol.https.DelegateHttpsURLConnection sun.net.www.protocol.https.HttpsURLConnectionImpl.delegate accessible: module java.base does not "opens sun.net.www.protocol.https" to unnamed module @1ed4004b connecting to PATCH
delegateField.setAccessible(true);
HttpURLConnection delegateConnection = (HttpURLConnection) delegateField.get(httpURLConnection);
setRequestMethodBypassingJREMethodLimitation(delegateConnection, method);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public static GoogleCloudStorageOAuthScopes create() {
return fullControlScopes();
} else if (input.getMethod().equalsIgnoreCase("PUT") || input.getMethod().equalsIgnoreCase("PATCH")) {
return fullControlScopes();
} else if ("PATCH".equalsIgnoreCase(input.getFirstHeaderOrNull("X-HTTP-Method-Override"))) {
return fullControlScopes();
}
return readOrWriteScopes().forRequest(input);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import org.jclouds.oauth.v2.filters.OAuthFilter;
import org.jclouds.rest.annotations.BinderParam;
import org.jclouds.rest.annotations.Fallback;
import org.jclouds.rest.annotations.PATCH;
import org.jclouds.rest.annotations.Headers;
import org.jclouds.rest.annotations.RequestFilters;
import org.jclouds.rest.annotations.SelectJson;
import org.jclouds.rest.annotations.SkipEncoding;
Expand Down Expand Up @@ -162,7 +162,8 @@ BucketAccessControls updateBucketAccessControls(@PathParam("bucket") String buck
* @return If successful, this method returns a BucketAccessControls resource in the response body
*/
@Named("BucketAccessControls:patch")
@PATCH
@POST
@Headers(keys = "X-HTTP-Method-Override", values = "PATCH")
@Produces(APPLICATION_JSON)
@Path("/b/{bucket}/acl/{entity}")
@Fallback(NullOnNotFoundOr404.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
import org.jclouds.oauth.v2.filters.OAuthFilter;
import org.jclouds.rest.annotations.BinderParam;
import org.jclouds.rest.annotations.Fallback;
import org.jclouds.rest.annotations.PATCH;
import org.jclouds.rest.annotations.Headers;
import org.jclouds.rest.annotations.RequestFilters;
import org.jclouds.rest.annotations.SkipEncoding;
import org.jclouds.rest.binders.BindToJsonPayload;
Expand Down Expand Up @@ -250,7 +250,8 @@ Bucket updateBucket(@PathParam("bucket") String bucketName,
* @return If successful, this method returns the updated {@link Bucket} resource.
*/
@Named("Bucket:patch")
@PATCH
@POST
@Headers(keys = "X-HTTP-Method-Override", values = "PATCH")
@Produces(APPLICATION_JSON)
@Path("/b/{bucket}")
@Fallback(NullOnNotFoundOr404.class)
Expand All @@ -272,7 +273,8 @@ Bucket patchBucket(@PathParam("bucket") String bucketName,
* @return If successful, this method returns the updated {@link Bucket} resource.
*/
@Named("Bucket:patch")
@PATCH
@POST
@Headers(keys = "X-HTTP-Method-Override", values = "PATCH")
@Produces(APPLICATION_JSON)
@Path("/b/{bucket}")
@Fallback(NullOnNotFoundOr404.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
import org.jclouds.oauth.v2.filters.OAuthFilter;
import org.jclouds.rest.annotations.BinderParam;
import org.jclouds.rest.annotations.Fallback;
import org.jclouds.rest.annotations.PATCH;
import org.jclouds.rest.annotations.Headers;
import org.jclouds.rest.annotations.RequestFilters;
import org.jclouds.rest.annotations.SelectJson;
import org.jclouds.rest.annotations.SkipEncoding;
Expand Down Expand Up @@ -170,7 +170,8 @@ ObjectAccessControls updateDefaultObjectAccessControls(@PathParam("bucket") Stri
* Name of the bucket which contains the object
*/
@Named("DefaultObjectAccessControls:patch")
@PATCH
@POST
@Headers(keys = "X-HTTP-Method-Override", values = "PATCH")
@Produces(APPLICATION_JSON)
@Path("/b/{bucket}/defaultObjectAcl/{entity}")
@Fallback(NullOnNotFoundOr404.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import org.jclouds.oauth.v2.filters.OAuthFilter;
import org.jclouds.rest.annotations.BinderParam;
import org.jclouds.rest.annotations.Fallback;
import org.jclouds.rest.annotations.PATCH;
import org.jclouds.rest.annotations.Headers;
import org.jclouds.rest.annotations.RequestFilters;
import org.jclouds.rest.annotations.SelectJson;
import org.jclouds.rest.binders.BindToJsonPayload;
Expand Down Expand Up @@ -282,7 +282,8 @@ ObjectAccessControls updateObjectAccessControls(@PathParam("bucket") String buck
* @return an {@link ObjectAccessControls }
*/
@Named("ObjectAccessControls:patch")
@PATCH
@POST
@Headers(keys = "X-HTTP-Method-Override", values = "PATCH")
@Produces(APPLICATION_JSON)
@Path("/b/{bucket}/o/{object}/acl/{entity}")
ObjectAccessControls patchObjectAccessControls(@PathParam("bucket") String bucketName,
Expand All @@ -307,7 +308,8 @@ ObjectAccessControls patchObjectAccessControls(@PathParam("bucket") String bucke
* @return {@link ObjectAccessControls }
*/
@Named("ObjectAccessControls:patch")
@PATCH
@POST
@Headers(keys = "X-HTTP-Method-Override", values = "PATCH")
@Produces(APPLICATION_JSON)
@Path("/b/{bucket}/o/{object}/acl/{entity}")
ObjectAccessControls patchObjectAccessControls(@PathParam("bucket") String bucketName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@
import org.jclouds.oauth.v2.filters.OAuthFilter;
import org.jclouds.rest.annotations.BinderParam;
import org.jclouds.rest.annotations.Fallback;
import org.jclouds.rest.annotations.Headers;
import org.jclouds.rest.annotations.MapBinder;
import org.jclouds.rest.annotations.PATCH;
import org.jclouds.rest.annotations.PayloadParam;
import org.jclouds.rest.annotations.QueryParams;
import org.jclouds.rest.annotations.RequestFilters;
Expand Down Expand Up @@ -310,7 +310,8 @@ GoogleCloudStorageObject updateObject(@PathParam("bucket") String bucketName,
* @return a {@link GoogleCloudStorageObject}
*/
@Named("Object:patch")
@PATCH
@POST
@Headers(keys = "X-HTTP-Method-Override", values = "PATCH")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
@Path("storage/v1/b/{bucket}/o/{object}")
Expand All @@ -334,7 +335,8 @@ GoogleCloudStorageObject patchObject(@PathParam("bucket") String bucketName,
* @return a {@link GoogleCloudStorageObject}
*/
@Named("Object:patch")
@PUT
@POST
@Headers(keys = "X-HTTP-Method-Override", values = "PATCH")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
@Path("storage/v1/b/{bucket}/o/{object}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,11 @@ public void testUpdateBucketAclResponseIs2xx() throws Exception {
public void testPatchBucketAclResponseIs2xx() throws Exception {
HttpRequest patchRequest = HttpRequest
.builder()
.method("PATCH")
.method("POST")
.endpoint("https://www.googleapis.com/storage/v1/b/jcloudstestbucket/acl/allUsers")
.addHeader("Accept", "application/json")
.addHeader("Authorization", "Bearer " + TOKEN)
.addHeader("X-HTTP-Method-Override", "PATCH")
.payload(payloadFromResourceWithContentType("/bucket_acl_update_initial.json",
MediaType.APPLICATION_JSON)).build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,11 @@ public void testPatchBucketWithNoOptionsResponseIs2xx() throws Exception {

HttpRequest patchRequest = HttpRequest
.builder()
.method("PATCH")
.method("POST")
.endpoint("https://www.googleapis.com/storage/v1/b/" + EXPECTED_TEST_BUCKET)
.addHeader("Accept", "application/json")
.addHeader("Authorization", "Bearer " + TOKEN)
.addHeader("X-HTTP-Method-Override", "PATCH")
.payload(payloadFromResourceWithContentType("/bucket_update_request_payload.json",
MediaType.APPLICATION_JSON)).build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,11 @@ public void testUpdateDefaultObjectAclWithOptionsResponseIs2xx() throws Exceptio
public void testPatchDefaultObjectAclWithNoOptionsResponseIs2xx() throws Exception {
HttpRequest update = HttpRequest
.builder()
.method("PATCH")
.method("POST")
.endpoint("https://www.googleapis.com/storage/v1/b/jcloudstestbucket/defaultObjectAcl/allUsers")
.addHeader("Accept", "application/json")
.addHeader("Authorization", "Bearer " + TOKEN)
.addHeader("X-HTTP-Method-Override", "PATCH")
.payload(payloadFromResourceWithContentType("/default_object_acl_update_request_payload.json",
MediaType.APPLICATION_JSON)).build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,11 @@ public void testUpdateObjectaclWithOptionsResponseIs2xx() throws Exception {
public void testPatchObjectaclWithNoOptionsResponseIs2xx() throws Exception {
HttpRequest patchRequest = HttpRequest
.builder()
.method("PATCH")
.method("POST")
.endpoint("https://www.googleapis.com/storage/v1/b/jcloudstestbucket/o/foo.txt/acl/allUsers")
.addHeader("Accept", "application/json")
.addHeader("Authorization", "Bearer " + TOKEN)
.addHeader("X-HTTP-Method-Override", "PATCH")
.payload(payloadFromResourceWithContentType("/object_acl_request_payload.json",
MediaType.APPLICATION_JSON)).build();

Expand All @@ -253,11 +254,12 @@ public void testPatchObjectaclWithNoOptionsResponseIs2xx() throws Exception {
public void testPatchObjectaclWithOptionsResponseIs2xx() throws Exception {
HttpRequest patchRequest = HttpRequest
.builder()
.method("PATCH")
.method("POST")
.endpoint("https://www.googleapis.com/storage/v1/b/jcloudstestbucket/o/foo.txt/acl/allUsers")
.addQueryParam("generation", "100")
.addHeader("Accept", "application/json")
.addHeader("Authorization", "Bearer " + TOKEN)
.addHeader("X-HTTP-Method-Override", "PATCH")
.payload(payloadFromResourceWithContentType("/object_acl_request_payload.json",
MediaType.APPLICATION_JSON)).build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public void patch() throws Exception {

assertEquals(objectApi().patchObject("test", "file_name", template),
new ParseGoogleCloudStorageObject().expected());
assertSent(server, "PATCH", "/storage/v1/b/test/o/file_name", APPLICATION_JSON,
assertSent(server, "POST", "/storage/v1/b/test/o/file_name", APPLICATION_JSON,
"{" +
" \"name\": \"file_name\"," +
" \"size\": 1000," +
Expand Down

0 comments on commit d3a261b

Please sign in to comment.