From b86fe9eec36fdf9f730bbb9e051280a8b4cb162e Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Mon, 14 Feb 2011 15:18:19 +0100 Subject: [PATCH] Issue 125:payload binder --- .../org/jclouds/rest/annotations/Payload.java | 44 +++++++++++++ .../rest/annotations/PayloadParam.java | 42 ++++++++++++ .../rest/annotations/PayloadParams.java | 48 ++++++++++++++ .../rest/binders/BindMapToStringPayload.java | 64 +++++++++++++++++++ 4 files changed, 198 insertions(+) create mode 100644 core/src/main/java/org/jclouds/rest/annotations/Payload.java create mode 100644 core/src/main/java/org/jclouds/rest/annotations/PayloadParam.java create mode 100644 core/src/main/java/org/jclouds/rest/annotations/PayloadParams.java create mode 100644 core/src/main/java/org/jclouds/rest/binders/BindMapToStringPayload.java diff --git a/core/src/main/java/org/jclouds/rest/annotations/Payload.java b/core/src/main/java/org/jclouds/rest/annotations/Payload.java new file mode 100644 index 0000000000..45c42dccf4 --- /dev/null +++ b/core/src/main/java/org/jclouds/rest/annotations/Payload.java @@ -0,0 +1,44 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.rest.annotations; + +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import javax.ws.rs.Path; + +/** + * Designates that this parameter will hold the payload for a PUT or POST command. + * + * @author Adrian Cole + */ +@Target(METHOD) +@Retention(RUNTIME) +public @interface Payload { + + /** + * @see Path#value() + * @see PayloadParam + */ + String value(); +} diff --git a/core/src/main/java/org/jclouds/rest/annotations/PayloadParam.java b/core/src/main/java/org/jclouds/rest/annotations/PayloadParam.java new file mode 100644 index 0000000000..51d7c4ba01 --- /dev/null +++ b/core/src/main/java/org/jclouds/rest/annotations/PayloadParam.java @@ -0,0 +1,42 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.rest.annotations; + +import static java.lang.annotation.ElementType.PARAMETER; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +/** + * Designates that this parameter will hold the payload for a PUT or POST command. + * + * @author Adrian Cole + */ +@Target(PARAMETER) +@Retention(RUNTIME) +public @interface PayloadParam { + + /** + * The key used in a map passed to the {@link MapBinder} or {@link Payload} associated with the + * request. + */ + String value(); +} diff --git a/core/src/main/java/org/jclouds/rest/annotations/PayloadParams.java b/core/src/main/java/org/jclouds/rest/annotations/PayloadParams.java new file mode 100644 index 0000000000..f5019ca8f5 --- /dev/null +++ b/core/src/main/java/org/jclouds/rest/annotations/PayloadParams.java @@ -0,0 +1,48 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.rest.annotations; + +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import javax.ws.rs.QueryParam; + +/** + * Designates that default parameters will be added a map that builds the request entity. + * + * @see QueryParam + * @see Payload + * @see MapBinder + * @author Adrian Cole + */ +@Target( { TYPE, METHOD }) +@Retention(RUNTIME) +public @interface PayloadParams { + + public static final String NULL = "MAP_PAYLOAD_NULL"; + + String[] keys(); + + String[] values() default NULL; +} diff --git a/core/src/main/java/org/jclouds/rest/binders/BindMapToStringPayload.java b/core/src/main/java/org/jclouds/rest/binders/BindMapToStringPayload.java new file mode 100644 index 0000000000..45fce9dc7d --- /dev/null +++ b/core/src/main/java/org/jclouds/rest/binders/BindMapToStringPayload.java @@ -0,0 +1,64 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.rest.binders; + +import java.net.URI; +import java.util.Map; + +import javax.inject.Inject; +import javax.inject.Provider; +import javax.inject.Singleton; +import javax.ws.rs.core.UriBuilder; + +import org.jclouds.http.HttpRequest; +import org.jclouds.io.Payloads; +import org.jclouds.rest.MapBinder; +import org.jclouds.rest.annotations.Payload; +import org.jclouds.rest.internal.GeneratedHttpRequest; + +/** + * + * @author Adrian Cole + */ +@Singleton +public class BindMapToStringPayload implements MapBinder { + protected final Provider uriBuilders; + + @Inject + public BindMapToStringPayload(Provider uriBuilders) { + this.uriBuilders = uriBuilders; + } + + @SuppressWarnings("unchecked") + @Override + public R bindToRequest(R request, Map postParams) { + GeneratedHttpRequest r = GeneratedHttpRequest.class.cast( request); + UriBuilder builder = uriBuilders.get(); + builder.path(r.getJavaMethod().getAnnotation(Payload.class).value()); + URI fake = builder.buildFromMap(postParams); + return (R) request.toBuilder().payload(Payloads.newStringPayload(fake.getPath())).build(); + } + + @Override + public R bindToRequest(R request, Object payload) { + throw new IllegalArgumentException("this is a map binder"); + } + +} \ No newline at end of file