diff --git a/apis/chef/src/main/java/org/jclouds/chef/ChefAsyncClient.java b/apis/chef/src/main/java/org/jclouds/chef/ChefAsyncClient.java index df50ba9fb6..16f97f8a0b 100644 --- a/apis/chef/src/main/java/org/jclouds/chef/ChefAsyncClient.java +++ b/apis/chef/src/main/java/org/jclouds/chef/ChefAsyncClient.java @@ -32,7 +32,6 @@ import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.core.MediaType; -import org.jclouds.chef.binders.AdminFlagFromCreateClientOptions; import org.jclouds.chef.binders.BindChecksumsToJsonPayload; import org.jclouds.chef.binders.BindCreateClientOptionsToJsonPayload; import org.jclouds.chef.binders.BindGenerateKeyForClientToJsonPayload; @@ -165,8 +164,7 @@ public interface ChefAsyncClient { @POST @Path("/clients") @MapBinder(BindCreateClientOptionsToJsonPayload.class) - ListenableFuture createClient(@PayloadParam("name") String clientname, - @PayloadParam("admin") @ParamParser(AdminFlagFromCreateClientOptions.class) CreateClientOptions options); + ListenableFuture createClient(@PayloadParam("name") String clientname, CreateClientOptions options); /** * @see ChefClient#generateKeyForClient diff --git a/apis/chef/src/main/java/org/jclouds/chef/ChefClient.java b/apis/chef/src/main/java/org/jclouds/chef/ChefClient.java index 43eba7d37d..b1a87a7971 100644 --- a/apis/chef/src/main/java/org/jclouds/chef/ChefClient.java +++ b/apis/chef/src/main/java/org/jclouds/chef/ChefClient.java @@ -23,10 +23,6 @@ import java.util.List; import java.util.Set; import java.util.concurrent.TimeUnit; -import javax.ws.rs.POST; -import javax.ws.rs.Path; - -import org.jclouds.chef.binders.AdminFlagFromCreateClientOptions; import org.jclouds.chef.domain.Client; import org.jclouds.chef.domain.CookbookVersion; import org.jclouds.chef.domain.DatabagItem; @@ -40,13 +36,8 @@ import org.jclouds.concurrent.Timeout; import org.jclouds.http.HttpResponseException; import org.jclouds.rest.AuthorizationException; import org.jclouds.rest.annotations.BinderParam; -import org.jclouds.rest.annotations.MapBinder; -import org.jclouds.rest.annotations.ParamParser; -import org.jclouds.rest.annotations.PayloadParam; import org.jclouds.rest.binders.BindToJsonPayload; -import com.google.common.util.concurrent.ListenableFuture; - /** * Provides synchronous access to Chef. *

diff --git a/apis/chef/src/main/java/org/jclouds/chef/binders/AdminFlagFromCreateClientOptions.java b/apis/chef/src/main/java/org/jclouds/chef/binders/AdminFlagFromCreateClientOptions.java deleted file mode 100644 index 5ab1a9f3ca..0000000000 --- a/apis/chef/src/main/java/org/jclouds/chef/binders/AdminFlagFromCreateClientOptions.java +++ /dev/null @@ -1,38 +0,0 @@ -/** - * - * 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.chef.binders; - -import javax.inject.Singleton; - -import org.jclouds.chef.options.CreateClientOptions; - -import com.google.common.base.Function; - -/** - * @author Ignasi Barrera - */ -@Singleton -public class AdminFlagFromCreateClientOptions implements Function { - - public String apply(Object from) { - return String.valueOf(((CreateClientOptions) from).isAdmin()); - } - -} \ No newline at end of file diff --git a/apis/chef/src/main/java/org/jclouds/chef/binders/BindCreateClientOptionsToJsonPayload.java b/apis/chef/src/main/java/org/jclouds/chef/binders/BindCreateClientOptionsToJsonPayload.java index 34a6397549..41a9e5296b 100644 --- a/apis/chef/src/main/java/org/jclouds/chef/binders/BindCreateClientOptionsToJsonPayload.java +++ b/apis/chef/src/main/java/org/jclouds/chef/binders/BindCreateClientOptionsToJsonPayload.java @@ -18,6 +18,10 @@ */ package org.jclouds.chef.binders; +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.base.Preconditions.checkState; + import java.util.Map; import javax.inject.Inject; @@ -26,9 +30,10 @@ import org.jclouds.chef.options.CreateClientOptions; import org.jclouds.http.HttpRequest; import org.jclouds.json.Json; import org.jclouds.rest.binders.BindToJsonPayload; +import org.jclouds.rest.internal.GeneratedHttpRequest; -import com.google.common.base.Function; -import com.google.common.collect.Maps; +import com.google.common.base.Predicates; +import com.google.common.collect.Iterables; /** * Bind the parameters of a {@link CreateClientOptions} to the payload, taking care of transforming @@ -44,19 +49,30 @@ public class BindCreateClientOptionsToJsonPayload extends BindToJsonPayload } @Override - public R bindToRequest(R request, Map postParams) + public R bindToRequest(R request, Map postParams) { + checkArgument(checkNotNull(request, "request") instanceof GeneratedHttpRequest, + "this binder is only valid for GeneratedHttpRequests"); + GeneratedHttpRequest gRequest = (GeneratedHttpRequest) request; + checkState(gRequest.getArgs() != null, "args should be initialized at this point"); + + String name = checkNotNull(postParams.remove("name"), "name"); + CreateClientOptions options = (CreateClientOptions) Iterables.find(gRequest.getArgs(), + Predicates.instanceOf(CreateClientOptions.class)); + + return bindToRequest(request, new CreateClientParams(name, options)); + } + + @SuppressWarnings("unused") + private static class CreateClientParams { - Map params = - Maps.transformValues(postParams, new Function() { - @Override - public Object apply(String input) { - // Transform boolean values to Boolean objects so they are serialized as boolean - return input.equals("true") || input.equals("false") ? Boolean.valueOf(input) - : input; - } - }); - - return bindToRequest(request, (Object) params); + private String name; + + private boolean admin; + + public CreateClientParams(String name, CreateClientOptions options) { + this.name = name; + this.admin = options.isAdmin(); + } } }