Refactored create options parsing to be generic

This commit is contained in:
Ignasi Barrera 2011-09-23 17:36:06 +02:00
parent cbaf2a4605
commit 96dd74cc67
4 changed files with 31 additions and 64 deletions

View File

@ -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<Client> createClient(@PayloadParam("name") String clientname,
@PayloadParam("admin") @ParamParser(AdminFlagFromCreateClientOptions.class) CreateClientOptions options);
ListenableFuture<Client> createClient(@PayloadParam("name") String clientname, CreateClientOptions options);
/**
* @see ChefClient#generateKeyForClient

View File

@ -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.
* <p/>

View File

@ -1,38 +0,0 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* 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<Object, String> {
public String apply(Object from) {
return String.valueOf(((CreateClientOptions) from).isAdmin());
}
}

View File

@ -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 extends HttpRequest> R bindToRequest(R request, Map<String, String> postParams)
public <R extends HttpRequest> R bindToRequest(R request, Map<String, String> 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<String, Object> params =
Maps.transformValues(postParams, new Function<String, Object>() {
@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();
}
}
}