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:
adrian.f.cole 2009-07-21 17:00:15 +00:00
parent c63e532271
commit feb0385c0a
9 changed files with 110 additions and 38 deletions

View File

@ -32,7 +32,7 @@ import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import org.jclouds.http.HttpRequest;
import org.jclouds.rest.PostEntityBinder;
import org.jclouds.rest.MapEntityBinder;
import com.google.gson.Gson;
import com.google.inject.Inject;
@ -43,7 +43,7 @@ import com.google.inject.Inject;
* @author Adrian Cole
* @since 4.0
*/
public class JsonBinder implements PostEntityBinder {
public class JsonBinder implements MapEntityBinder {
@Inject
protected Gson gson;

View File

@ -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>>> methodToIndexOfParamToHostPrefixParamAnnotations = createMethodToIndexOfParamToAnnotation(HostPrefixParam.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);
static Map<Method, Map<Integer, Set<Annotation>>> createMethodToIndexOfParamToAnnotation(
@ -323,31 +323,31 @@ public class JaxrsAnnotationProcessor {
return null;
}
public PostEntityBinder getPostEntityBinderOrNull(Method method, Object[] args) {
public MapEntityBinder getMapEntityBinderOrNull(Method method, Object[] args) {
for (Object arg : args) {
if (arg instanceof Object[]) {
Object[] postBinders = (Object[]) arg;
if (postBinders.length == 0) {
} else if (postBinders.length == 1) {
if (postBinders[0] instanceof PostEntityBinder) {
PostEntityBinder binder = (PostEntityBinder) postBinders[0];
if (postBinders[0] instanceof MapEntityBinder) {
MapEntityBinder binder = (MapEntityBinder) postBinders[0];
injector.injectMembers(binder);
return binder;
}
} else {
if (postBinders[0] instanceof PostEntityBinder) {
if (postBinders[0] instanceof MapEntityBinder) {
throw new IllegalArgumentException(
"we currently do not support multiple varargs postBinders in: "
+ method.getName());
}
}
} else if (arg instanceof PostEntityBinder) {
PostEntityBinder binder = (PostEntityBinder) arg;
} else if (arg instanceof MapEntityBinder) {
MapEntityBinder binder = (MapEntityBinder) arg;
injector.injectMembers(binder);
return binder;
}
}
PostBinder annotation = method.getAnnotation(PostBinder.class);
MapBinder annotation = method.getAnnotation(MapBinder.class);
if (annotation != null) {
return injector.getInstance(annotation.value());
}
@ -392,17 +392,17 @@ public class JaxrsAnnotationProcessor {
HttpRequest request) {
switch (request.getMethod()) {
case POST:
PostEntityBinder postBinder = null;
Map<String, String> postParams = buildPostParams(method, args);
// post binder is only useful if there are parameters. We guard here in case the
// PostEntityBinder is also an EntityBinder. If so, it can be used with or without
case PUT:
MapEntityBinder mapBinder = null;
Map<String, String> mapParams = buildPostParams(method, args);
// 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.
if (postParams.size() > 0
&& (postBinder = this.getPostEntityBinderOrNull(method, args)) != null) {
postBinder.addEntityToRequest(postParams, request);
if (mapParams.size() > 0
&& (mapBinder = this.getMapEntityBinderOrNull(method, args)) != null) {
mapBinder.addEntityToRequest(mapParams, request);
break;
}
case PUT:
HttpRequestOptions options = findOptionsIn(method, args);
if (options != null) {
optionsBinder.addEntityToRequest(options, request);
@ -568,7 +568,7 @@ public class JaxrsAnnotationProcessor {
postParams.put(((PathParam) key).value(), injector.getInstance(extractor.value())
.apply(args[entry.getKey()]));
} else {
String paramKey = ((PostParam) key).value();
String paramKey = ((MapEntityParam) key).value();
String paramValue = args[entry.getKey()].toString();
postParams.put(paramKey, paramValue);
}

View File

@ -36,11 +36,11 @@ import java.lang.annotation.Target;
*/
@Target(METHOD)
@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
*/
Class<? extends PostEntityBinder> value();
Class<? extends MapEntityBinder> value();
}

View File

@ -33,12 +33,12 @@ import org.jclouds.http.HttpRequest;
* @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.
*
* @see PostParam
* @see MapEntityParam
*/
public void addEntityToRequest(Map<String,String> postParams, HttpRequest request);

View File

@ -36,10 +36,10 @@ import java.lang.annotation.Target;
*/
@Target(PARAMETER)
@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();
}

View File

@ -38,8 +38,8 @@ import org.jclouds.http.functions.ParseSax;
import org.jclouds.http.options.HttpRequestOptions;
import org.jclouds.rest.EntityParam;
import org.jclouds.rest.ExceptionParser;
import org.jclouds.rest.PostBinder;
import org.jclouds.rest.PostParam;
import org.jclouds.rest.MapBinder;
import org.jclouds.rest.MapEntityParam;
import org.jclouds.rest.RequestFilters;
import org.jclouds.rest.XMLResponseParser;
@ -92,8 +92,8 @@ public interface IntegrationTestClient {
@POST
@Path("objects/{id}")
@PostBinder(JsonBinder.class)
Future<String> postJson(@PathParam("id") String id, @PostParam("key") String toPut);
@MapBinder(JsonBinder.class)
Future<String> postJson(@PathParam("id") String id, @MapEntityParam("key") String toPut);
@GET
@Path("objects/{id}")

View File

@ -89,14 +89,14 @@ public class JaxrsAnnotationProcessorTest {
@POST
@Path("{foo}")
public void postWithPath(@PathParam("foo") @PostParam("fooble") String path,
PostEntityBinder content) {
public void postWithPath(@PathParam("foo") @MapEntityParam("fooble") String path,
MapEntityBinder content) {
}
@POST
@Path("{foo}")
@PostBinder(JsonBinder.class)
public void postWithMethodBinder(@PathParam("foo") @PostParam("fooble") String path) {
@MapBinder(JsonBinder.class)
public void postWithMethodBinder(@PathParam("foo") @MapEntityParam("fooble") String path) {
}
}
@ -134,11 +134,10 @@ public class JaxrsAnnotationProcessorTest {
}
public void testCreatePostWithPathRequest() throws SecurityException, NoSuchMethodException {
Method method = TestPost.class
.getMethod("postWithPath", String.class, PostEntityBinder.class);
Method method = TestPost.class.getMethod("postWithPath", String.class, MapEntityBinder.class);
URI endpoint = URI.create("http://localhost");
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) {
request.setEntity(postParams.get("fooble"));
@ -173,6 +172,33 @@ public class JaxrsAnnotationProcessorTest {
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 {
public void filter(HttpRequest request) throws HttpException {

View File

@ -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;
import static com.google.common.base.Preconditions.checkNotNull;

View File

@ -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;
import static org.jclouds.rackspace.options.BaseListOptions.Builder.changesSince;