mirror of https://github.com/apache/jclouds.git
Issue 76: retyped PostBinder so that it can be used with PUTs that also need parameters
git-svn-id: http://jclouds.googlecode.com/svn/trunk@1826 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
parent
c63e532271
commit
feb0385c0a
|
@ -32,7 +32,7 @@ import javax.ws.rs.core.HttpHeaders;
|
||||||
import javax.ws.rs.core.MediaType;
|
import javax.ws.rs.core.MediaType;
|
||||||
|
|
||||||
import org.jclouds.http.HttpRequest;
|
import org.jclouds.http.HttpRequest;
|
||||||
import org.jclouds.rest.PostEntityBinder;
|
import org.jclouds.rest.MapEntityBinder;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
|
@ -43,7 +43,7 @@ import com.google.inject.Inject;
|
||||||
* @author Adrian Cole
|
* @author Adrian Cole
|
||||||
* @since 4.0
|
* @since 4.0
|
||||||
*/
|
*/
|
||||||
public class JsonBinder implements PostEntityBinder {
|
public class JsonBinder implements MapEntityBinder {
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
protected Gson gson;
|
protected Gson gson;
|
||||||
|
|
|
@ -89,7 +89,7 @@ public class JaxrsAnnotationProcessor {
|
||||||
private final Map<Method, Map<Integer, Set<Annotation>>> methodToIndexOfParamToHeaderParamAnnotations = createMethodToIndexOfParamToAnnotation(HeaderParam.class);
|
private final Map<Method, Map<Integer, Set<Annotation>>> methodToIndexOfParamToHeaderParamAnnotations = createMethodToIndexOfParamToAnnotation(HeaderParam.class);
|
||||||
private final Map<Method, Map<Integer, Set<Annotation>>> methodToIndexOfParamToHostPrefixParamAnnotations = createMethodToIndexOfParamToAnnotation(HostPrefixParam.class);
|
private final Map<Method, Map<Integer, Set<Annotation>>> methodToIndexOfParamToHostPrefixParamAnnotations = createMethodToIndexOfParamToAnnotation(HostPrefixParam.class);
|
||||||
private final Map<Method, Map<Integer, Set<Annotation>>> methodToindexOfParamToPathParamAnnotations = createMethodToIndexOfParamToAnnotation(PathParam.class);
|
private final Map<Method, Map<Integer, Set<Annotation>>> methodToindexOfParamToPathParamAnnotations = createMethodToIndexOfParamToAnnotation(PathParam.class);
|
||||||
private final Map<Method, Map<Integer, Set<Annotation>>> methodToindexOfParamToPostParamAnnotations = createMethodToIndexOfParamToAnnotation(PostParam.class);
|
private final Map<Method, Map<Integer, Set<Annotation>>> methodToindexOfParamToPostParamAnnotations = createMethodToIndexOfParamToAnnotation(MapEntityParam.class);
|
||||||
private final Map<Method, Map<Integer, Set<Annotation>>> methodToindexOfParamToParamParserAnnotations = createMethodToIndexOfParamToAnnotation(ParamParser.class);
|
private final Map<Method, Map<Integer, Set<Annotation>>> methodToindexOfParamToParamParserAnnotations = createMethodToIndexOfParamToAnnotation(ParamParser.class);
|
||||||
|
|
||||||
static Map<Method, Map<Integer, Set<Annotation>>> createMethodToIndexOfParamToAnnotation(
|
static Map<Method, Map<Integer, Set<Annotation>>> createMethodToIndexOfParamToAnnotation(
|
||||||
|
@ -323,31 +323,31 @@ public class JaxrsAnnotationProcessor {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public PostEntityBinder getPostEntityBinderOrNull(Method method, Object[] args) {
|
public MapEntityBinder getMapEntityBinderOrNull(Method method, Object[] args) {
|
||||||
for (Object arg : args) {
|
for (Object arg : args) {
|
||||||
if (arg instanceof Object[]) {
|
if (arg instanceof Object[]) {
|
||||||
Object[] postBinders = (Object[]) arg;
|
Object[] postBinders = (Object[]) arg;
|
||||||
if (postBinders.length == 0) {
|
if (postBinders.length == 0) {
|
||||||
} else if (postBinders.length == 1) {
|
} else if (postBinders.length == 1) {
|
||||||
if (postBinders[0] instanceof PostEntityBinder) {
|
if (postBinders[0] instanceof MapEntityBinder) {
|
||||||
PostEntityBinder binder = (PostEntityBinder) postBinders[0];
|
MapEntityBinder binder = (MapEntityBinder) postBinders[0];
|
||||||
injector.injectMembers(binder);
|
injector.injectMembers(binder);
|
||||||
return binder;
|
return binder;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (postBinders[0] instanceof PostEntityBinder) {
|
if (postBinders[0] instanceof MapEntityBinder) {
|
||||||
throw new IllegalArgumentException(
|
throw new IllegalArgumentException(
|
||||||
"we currently do not support multiple varargs postBinders in: "
|
"we currently do not support multiple varargs postBinders in: "
|
||||||
+ method.getName());
|
+ method.getName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (arg instanceof PostEntityBinder) {
|
} else if (arg instanceof MapEntityBinder) {
|
||||||
PostEntityBinder binder = (PostEntityBinder) arg;
|
MapEntityBinder binder = (MapEntityBinder) arg;
|
||||||
injector.injectMembers(binder);
|
injector.injectMembers(binder);
|
||||||
return binder;
|
return binder;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
PostBinder annotation = method.getAnnotation(PostBinder.class);
|
MapBinder annotation = method.getAnnotation(MapBinder.class);
|
||||||
if (annotation != null) {
|
if (annotation != null) {
|
||||||
return injector.getInstance(annotation.value());
|
return injector.getInstance(annotation.value());
|
||||||
}
|
}
|
||||||
|
@ -392,17 +392,17 @@ public class JaxrsAnnotationProcessor {
|
||||||
HttpRequest request) {
|
HttpRequest request) {
|
||||||
switch (request.getMethod()) {
|
switch (request.getMethod()) {
|
||||||
case POST:
|
case POST:
|
||||||
PostEntityBinder postBinder = null;
|
case PUT:
|
||||||
Map<String, String> postParams = buildPostParams(method, args);
|
MapEntityBinder mapBinder = null;
|
||||||
// post binder is only useful if there are parameters. We guard here in case the
|
Map<String, String> mapParams = buildPostParams(method, args);
|
||||||
// PostEntityBinder is also an EntityBinder. If so, it can be used with or without
|
// MapEntityBinder is only useful if there are parameters. We guard here in case the
|
||||||
|
// MapEntityBinder is also an EntityBinder. If so, it can be used with or without
|
||||||
// parameters.
|
// parameters.
|
||||||
if (postParams.size() > 0
|
if (mapParams.size() > 0
|
||||||
&& (postBinder = this.getPostEntityBinderOrNull(method, args)) != null) {
|
&& (mapBinder = this.getMapEntityBinderOrNull(method, args)) != null) {
|
||||||
postBinder.addEntityToRequest(postParams, request);
|
mapBinder.addEntityToRequest(mapParams, request);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case PUT:
|
|
||||||
HttpRequestOptions options = findOptionsIn(method, args);
|
HttpRequestOptions options = findOptionsIn(method, args);
|
||||||
if (options != null) {
|
if (options != null) {
|
||||||
optionsBinder.addEntityToRequest(options, request);
|
optionsBinder.addEntityToRequest(options, request);
|
||||||
|
@ -568,7 +568,7 @@ public class JaxrsAnnotationProcessor {
|
||||||
postParams.put(((PathParam) key).value(), injector.getInstance(extractor.value())
|
postParams.put(((PathParam) key).value(), injector.getInstance(extractor.value())
|
||||||
.apply(args[entry.getKey()]));
|
.apply(args[entry.getKey()]));
|
||||||
} else {
|
} else {
|
||||||
String paramKey = ((PostParam) key).value();
|
String paramKey = ((MapEntityParam) key).value();
|
||||||
String paramValue = args[entry.getKey()].toString();
|
String paramValue = args[entry.getKey()].toString();
|
||||||
postParams.put(paramKey, paramValue);
|
postParams.put(paramKey, paramValue);
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,11 +36,11 @@ import java.lang.annotation.Target;
|
||||||
*/
|
*/
|
||||||
@Target(METHOD)
|
@Target(METHOD)
|
||||||
@Retention(RUNTIME)
|
@Retention(RUNTIME)
|
||||||
public @interface PostBinder {
|
public @interface MapBinder {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* How to bind {@link PostParam} values, if there is no {@link PostEntityBinder} in the method
|
* How to bind {@link MapEntityParam} values, if there is no {@link MapEntityBinder} in the method
|
||||||
* definition
|
* definition
|
||||||
*/
|
*/
|
||||||
Class<? extends PostEntityBinder> value();
|
Class<? extends MapEntityBinder> value();
|
||||||
}
|
}
|
|
@ -33,12 +33,12 @@ import org.jclouds.http.HttpRequest;
|
||||||
* @author Adrian Cole
|
* @author Adrian Cole
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public interface PostEntityBinder extends EntityBinder {
|
public interface MapEntityBinder extends EntityBinder {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* creates and binds the POST entity to the request using parameters specified.
|
* creates and binds the POST entity to the request using parameters specified.
|
||||||
*
|
*
|
||||||
* @see PostParam
|
* @see MapEntityParam
|
||||||
*/
|
*/
|
||||||
public void addEntityToRequest(Map<String,String> postParams, HttpRequest request);
|
public void addEntityToRequest(Map<String,String> postParams, HttpRequest request);
|
||||||
|
|
|
@ -36,10 +36,10 @@ import java.lang.annotation.Target;
|
||||||
*/
|
*/
|
||||||
@Target(PARAMETER)
|
@Target(PARAMETER)
|
||||||
@Retention(RUNTIME)
|
@Retention(RUNTIME)
|
||||||
public @interface PostParam {
|
public @interface MapEntityParam {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The key used in a map passed to the {@link PostEntityBinder} associated with the request.
|
* The key used in a map passed to the {@link MapEntityBinder} associated with the request.
|
||||||
*/
|
*/
|
||||||
String value();
|
String value();
|
||||||
}
|
}
|
|
@ -38,8 +38,8 @@ import org.jclouds.http.functions.ParseSax;
|
||||||
import org.jclouds.http.options.HttpRequestOptions;
|
import org.jclouds.http.options.HttpRequestOptions;
|
||||||
import org.jclouds.rest.EntityParam;
|
import org.jclouds.rest.EntityParam;
|
||||||
import org.jclouds.rest.ExceptionParser;
|
import org.jclouds.rest.ExceptionParser;
|
||||||
import org.jclouds.rest.PostBinder;
|
import org.jclouds.rest.MapBinder;
|
||||||
import org.jclouds.rest.PostParam;
|
import org.jclouds.rest.MapEntityParam;
|
||||||
import org.jclouds.rest.RequestFilters;
|
import org.jclouds.rest.RequestFilters;
|
||||||
import org.jclouds.rest.XMLResponseParser;
|
import org.jclouds.rest.XMLResponseParser;
|
||||||
|
|
||||||
|
@ -92,8 +92,8 @@ public interface IntegrationTestClient {
|
||||||
|
|
||||||
@POST
|
@POST
|
||||||
@Path("objects/{id}")
|
@Path("objects/{id}")
|
||||||
@PostBinder(JsonBinder.class)
|
@MapBinder(JsonBinder.class)
|
||||||
Future<String> postJson(@PathParam("id") String id, @PostParam("key") String toPut);
|
Future<String> postJson(@PathParam("id") String id, @MapEntityParam("key") String toPut);
|
||||||
|
|
||||||
@GET
|
@GET
|
||||||
@Path("objects/{id}")
|
@Path("objects/{id}")
|
||||||
|
|
|
@ -89,14 +89,14 @@ public class JaxrsAnnotationProcessorTest {
|
||||||
|
|
||||||
@POST
|
@POST
|
||||||
@Path("{foo}")
|
@Path("{foo}")
|
||||||
public void postWithPath(@PathParam("foo") @PostParam("fooble") String path,
|
public void postWithPath(@PathParam("foo") @MapEntityParam("fooble") String path,
|
||||||
PostEntityBinder content) {
|
MapEntityBinder content) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@POST
|
@POST
|
||||||
@Path("{foo}")
|
@Path("{foo}")
|
||||||
@PostBinder(JsonBinder.class)
|
@MapBinder(JsonBinder.class)
|
||||||
public void postWithMethodBinder(@PathParam("foo") @PostParam("fooble") String path) {
|
public void postWithMethodBinder(@PathParam("foo") @MapEntityParam("fooble") String path) {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -134,11 +134,10 @@ public class JaxrsAnnotationProcessorTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testCreatePostWithPathRequest() throws SecurityException, NoSuchMethodException {
|
public void testCreatePostWithPathRequest() throws SecurityException, NoSuchMethodException {
|
||||||
Method method = TestPost.class
|
Method method = TestPost.class.getMethod("postWithPath", String.class, MapEntityBinder.class);
|
||||||
.getMethod("postWithPath", String.class, PostEntityBinder.class);
|
|
||||||
URI endpoint = URI.create("http://localhost");
|
URI endpoint = URI.create("http://localhost");
|
||||||
HttpRequest httpMethod = factory.create(TestPost.class).createRequest(endpoint, method,
|
HttpRequest httpMethod = factory.create(TestPost.class).createRequest(endpoint, method,
|
||||||
new Object[] { "data", new PostEntityBinder() {
|
new Object[] { "data", new MapEntityBinder() {
|
||||||
|
|
||||||
public void addEntityToRequest(Map<String, String> postParams, HttpRequest request) {
|
public void addEntityToRequest(Map<String, String> postParams, HttpRequest request) {
|
||||||
request.setEntity(postParams.get("fooble"));
|
request.setEntity(postParams.get("fooble"));
|
||||||
|
@ -173,6 +172,33 @@ public class JaxrsAnnotationProcessorTest {
|
||||||
assertEquals(httpMethod.getEntity(), expected);
|
assertEquals(httpMethod.getEntity(), expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class TestPut {
|
||||||
|
|
||||||
|
@PUT
|
||||||
|
@Path("{foo}")
|
||||||
|
@MapBinder(JsonBinder.class)
|
||||||
|
public void putWithMethodBinder(@PathParam("foo") @MapEntityParam("fooble") String path) {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testCreatePutWithMethodBinder() throws SecurityException, NoSuchMethodException {
|
||||||
|
Method method = TestPut.class.getMethod("putWithMethodBinder", String.class);
|
||||||
|
URI endpoint = URI.create("http://localhost");
|
||||||
|
HttpRequest httpMethod = factory.create(TestPut.class).createRequest(endpoint, method,
|
||||||
|
new Object[] { "data", });
|
||||||
|
assertEquals(httpMethod.getEndpoint().getHost(), "localhost");
|
||||||
|
assertEquals(httpMethod.getEndpoint().getPath(), "/data");
|
||||||
|
assertEquals(httpMethod.getMethod(), HttpMethod.PUT);
|
||||||
|
assertEquals(httpMethod.getHeaders().size(), 2);
|
||||||
|
assertEquals(httpMethod.getHeaders().get(HttpHeaders.CONTENT_TYPE), Collections
|
||||||
|
.singletonList("application/json"));
|
||||||
|
String expected = "{\"fooble\":\"data\"}";
|
||||||
|
assertEquals(httpMethod.getHeaders().get(HttpHeaders.CONTENT_LENGTH), Collections
|
||||||
|
.singletonList(expected.getBytes().length + ""));
|
||||||
|
assertEquals(httpMethod.getEntity(), expected);
|
||||||
|
}
|
||||||
|
|
||||||
static class TestRequestFilter1 implements HttpRequestFilter {
|
static class TestRequestFilter1 implements HttpRequestFilter {
|
||||||
|
|
||||||
public void filter(HttpRequest request) throws HttpException {
|
public void filter(HttpRequest request) throws HttpException {
|
||||||
|
@ -446,7 +472,7 @@ public class JaxrsAnnotationProcessorTest {
|
||||||
NoSuchMethodException {
|
NoSuchMethodException {
|
||||||
DateTime date = new DateTime();
|
DateTime date = new DateTime();
|
||||||
GetOptions options = GetOptions.Builder.ifModifiedSince(date);
|
GetOptions options = GetOptions.Builder.ifModifiedSince(date);
|
||||||
HttpRequestOptions[] optionsHolder = new HttpRequestOptions[]{};
|
HttpRequestOptions[] optionsHolder = new HttpRequestOptions[] {};
|
||||||
Method method = TestRequest.class.getMethod("get", String.class, optionsHolder.getClass());
|
Method method = TestRequest.class.getMethod("get", String.class, optionsHolder.getClass());
|
||||||
URI endpoint = URI.create("http://localhost");
|
URI endpoint = URI.create("http://localhost");
|
||||||
HttpRequest httpMethod = factory.create(TestRequest.class).createRequest(endpoint, method,
|
HttpRequest httpMethod = factory.create(TestRequest.class).createRequest(endpoint, method,
|
||||||
|
|
|
@ -1,3 +1,26 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Copyright (C) 2009 Global Cloud Specialists, Inc. <info@globalcloudspecialists.com>
|
||||||
|
*
|
||||||
|
* ====================================================================
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one
|
||||||
|
* or more contributor license agreements. See the NOTICE file
|
||||||
|
* distributed with this work for additional information
|
||||||
|
* regarding copyright ownership. The ASF licenses this file
|
||||||
|
* to you under the Apache License, Version 2.0 (the
|
||||||
|
* "License"); you may not use this file except in compliance
|
||||||
|
* with the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
* ====================================================================
|
||||||
|
*/
|
||||||
package org.jclouds.rackspace.options;
|
package org.jclouds.rackspace.options;
|
||||||
|
|
||||||
import static com.google.common.base.Preconditions.checkNotNull;
|
import static com.google.common.base.Preconditions.checkNotNull;
|
||||||
|
|
|
@ -1,3 +1,26 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Copyright (C) 2009 Global Cloud Specialists, Inc. <info@globalcloudspecialists.com>
|
||||||
|
*
|
||||||
|
* ====================================================================
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one
|
||||||
|
* or more contributor license agreements. See the NOTICE file
|
||||||
|
* distributed with this work for additional information
|
||||||
|
* regarding copyright ownership. The ASF licenses this file
|
||||||
|
* to you under the Apache License, Version 2.0 (the
|
||||||
|
* "License"); you may not use this file except in compliance
|
||||||
|
* with the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
* ====================================================================
|
||||||
|
*/
|
||||||
package org.jclouds.rackspace.options;
|
package org.jclouds.rackspace.options;
|
||||||
|
|
||||||
import static org.jclouds.rackspace.options.BaseListOptions.Builder.changesSince;
|
import static org.jclouds.rackspace.options.BaseListOptions.Builder.changesSince;
|
||||||
|
|
Loading…
Reference in New Issue