replaced the use of RestServerConfig with ServerConfig.

Added an example query QueryOrdersByName and its use in a client.
This commit is contained in:
Inderjeet Singh 2010-11-11 00:22:56 +00:00
parent a12286698b
commit 4d873d5dd7
3 changed files with 40 additions and 24 deletions

View File

@ -22,6 +22,7 @@ import com.google.gson.rest.definition.RestCallSpec;
import com.google.gson.rest.definition.RestRequest; import com.google.gson.rest.definition.RestRequest;
import com.google.gson.rest.definition.RestResource; import com.google.gson.rest.definition.RestResource;
import com.google.gson.rest.definition.RestResponse; import com.google.gson.rest.definition.RestResponse;
import com.google.gson.webservice.client.ServerConfig;
import com.google.gson.webservice.definition.WebServiceSystemException; import com.google.gson.webservice.definition.WebServiceSystemException;
import java.io.IOException; import java.io.IOException;
@ -37,15 +38,15 @@ import java.util.logging.Logger;
* @author inder * @author inder
*/ */
public class RestClientStub { public class RestClientStub {
private final RestServerConfig config; private final ServerConfig config;
private final Logger logger; private final Logger logger;
private final Level logLevel; private final Level logLevel;
public RestClientStub(RestServerConfig serverConfig) { public RestClientStub(ServerConfig serverConfig) {
this(serverConfig, null); this(serverConfig, null);
} }
public RestClientStub(RestServerConfig serverConfig, Level logLevel) { public RestClientStub(ServerConfig serverConfig, Level logLevel) {
this.config = serverConfig; this.config = serverConfig;
this.logger = logLevel == null ? null : Logger.getLogger(RestClientStub.class.getName()); this.logger = logLevel == null ? null : Logger.getLogger(RestClientStub.class.getName());
this.logLevel = logLevel; this.logLevel = logLevel;

View File

@ -18,15 +18,17 @@ import com.google.gson.Gson;
import com.google.gson.example.model.Cart; import com.google.gson.example.model.Cart;
import com.google.gson.example.model.LineItem; import com.google.gson.example.model.LineItem;
import com.google.gson.example.model.Order; import com.google.gson.example.model.Order;
import com.google.gson.example.model.QueryOrdersByItemName;
import com.google.gson.rest.client.ResourceDepotClient; import com.google.gson.rest.client.ResourceDepotClient;
import com.google.gson.rest.client.RestClientStub; import com.google.gson.rest.client.RestClientStub;
import com.google.gson.rest.client.RestServerConfig;
import com.google.gson.rest.definition.ValueBasedId; import com.google.gson.rest.definition.ValueBasedId;
import com.google.gson.rest.query.client.ResourceQueryClient;
import com.google.gson.webservice.client.ServerConfig;
import com.google.gson.webservice.client.WebServiceClient;
import com.google.gson.webservice.definition.CallPath; import com.google.gson.webservice.definition.CallPath;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.logging.Level;
/** /**
* A sample client for the rest resource for {@link Order} * A sample client for the rest resource for {@link Order}
@ -36,11 +38,17 @@ import java.util.logging.Level;
public class OrderClient { public class OrderClient {
public static final CallPath CALL_PATH = new CallPath("/rest/order"); public static final CallPath CALL_PATH = new CallPath("/rest/order");
private final ResourceDepotClient<ValueBasedId<Order>, Order> restClient; private final ResourceDepotClient<ValueBasedId<Order>, Order> restClient;
private final ResourceQueryClient<
ValueBasedId<Order>, Order, QueryOrdersByItemName> queryClient;
public OrderClient() { public OrderClient() {
RestServerConfig serverConfig = new RestServerConfig("http://localhost"); ServerConfig serverConfig = new ServerConfig("http://localhost");
RestClientStub stub = new RestClientStub(serverConfig, Level.INFO); Gson gson = new Gson();
restClient = new ResourceDepotClient<ValueBasedId<Order>, Order>( restClient = new ResourceDepotClient<ValueBasedId<Order>, Order>(
stub, CALL_PATH, Order.class, new Gson()); new RestClientStub(serverConfig), CALL_PATH, Order.class, new Gson());
ServerConfig wsServerConfig = new ServerConfig("http://localhost");
queryClient = new ResourceQueryClient<ValueBasedId<Order>, Order, QueryOrdersByItemName>(
new WebServiceClient(wsServerConfig), CALL_PATH, gson);
} }
public Order placeOrder(Cart cart) { public Order placeOrder(Cart cart) {
@ -48,11 +56,19 @@ public class OrderClient {
return restClient.post(order); return restClient.post(order);
} }
private List<Order> query(String itemName) {
return queryClient.query(new QueryOrdersByItemName(itemName));
}
public static void main(String[] args) { public static void main(String[] args) {
OrderClient client = new OrderClient(); OrderClient client = new OrderClient();
List<LineItem> lineItems = new ArrayList<LineItem>(); List<LineItem> lineItems = new ArrayList<LineItem>();
lineItems.add(new LineItem("item1", 2, 1000000L, "USD")); String itemName = "item1";
lineItems.add(new LineItem(itemName, 2, 1000000L, "USD"));
Cart cart = new Cart(lineItems, "first last", "4111-1111-1111-1111"); Cart cart = new Cart(lineItems, "first last", "4111-1111-1111-1111");
client.placeOrder(cart); Order order = client.placeOrder(cart);
System.out.println("Placed order: " + order);
List<Order> queriedOrder = client.query(itemName);
System.out.println("Queried order by item name ( " + itemName + "): " + order);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2008-2010 Google Inc. * Copyright (C) 2010 Google Inc.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -13,26 +13,25 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package com.google.gson.rest.client; package com.google.gson.example.model;
/** /**
* Configuration needed to access a Gson REST service. * A query for orders by item name
* *
* @author inder * @author Inderjeet Singh
*/ */
public final class RestServerConfig { public class QueryOrdersByItemName {
private final String serviceBaseUrl; private final String itemName;
public RestServerConfig(String serviceBaseUrl) { public QueryOrdersByItemName() {
this.serviceBaseUrl = serviceBaseUrl; this(null);
} }
public String getServiceBaseUrl() { public QueryOrdersByItemName(String itemName) {
return serviceBaseUrl; this.itemName = itemName;
} }
@Override public String getItemName() {
public String toString() { return itemName;
return serviceBaseUrl;
} }
} }