diff --git a/wsclient/src/main/java/com/google/gson/rest/client/ResourceDepotClient.java b/wsclient/src/main/java/com/google/gson/rest/client/ResourceDepotClient.java index 86a075fb..c9e790f2 100644 --- a/wsclient/src/main/java/com/google/gson/rest/client/ResourceDepotClient.java +++ b/wsclient/src/main/java/com/google/gson/rest/client/ResourceDepotClient.java @@ -33,7 +33,8 @@ import com.google.gson.webservice.definition.HttpMethod; * * @author Inderjeet Singh */ -public class ResourceDepotClient> implements ResourceDepot { +public class ResourceDepotClient> + implements ResourceDepot { private final RestClientStub stub; private final RestCallSpec callSpec; private final Type resourceType; diff --git a/wsclient/src/main/java/com/google/gson/rest/query/client/ResourceQueryClient.java b/wsclient/src/main/java/com/google/gson/rest/query/client/ResourceQueryClient.java new file mode 100644 index 00000000..ff107f55 --- /dev/null +++ b/wsclient/src/main/java/com/google/gson/rest/query/client/ResourceQueryClient.java @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2010 Google Inc. + * + * 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 com.google.gson.rest.query.client; + +import java.util.List; + +import com.google.gson.Gson; +import com.google.gson.rest.definition.ID; +import com.google.gson.rest.definition.RestResource; +import com.google.gson.rest.query.ResourceQuery; +import com.google.gson.rest.query.TypedKeysQuery; +import com.google.gson.webservice.client.WebServiceClient; +import com.google.gson.webservice.definition.CallPath; +import com.google.gson.webservice.definition.HeaderMap; +import com.google.gson.webservice.definition.HttpMethod; +import com.google.gson.webservice.definition.procedural.RequestBody; +import com.google.gson.webservice.definition.procedural.ResponseBody; +import com.google.gson.webservice.definition.procedural.WebServiceCallSpec; +import com.google.gson.webservice.definition.procedural.WebServiceRequest; +import com.google.gson.webservice.definition.procedural.WebServiceResponse; + +/** + * A client to invoke {@link ResourceQuery}s associated with a REST resource + * + * @author Inderjeet Singh + * + * @param ID type of the REST resource + * @param type of the REST resource + * @param Query parameters + */ +public class ResourceQueryClient, Q> + implements ResourceQuery { + + private final WebServiceClient stub; + private final WebServiceCallSpec callSpec; + private final Gson gson; + + /** + * @param stub stub containing server info to access the rest client + * @param callPath relative path to the resource + */ + public ResourceQueryClient(WebServiceClient stub, CallPath callPath, Gson gson) { + this(stub, generateCallSpec(callPath), gson); + } + + protected ResourceQueryClient(WebServiceClient stub, WebServiceCallSpec callSpec, Gson gson) { + this.stub = stub; + this.callSpec = callSpec; + this.gson = gson; + } + + private static WebServiceCallSpec generateCallSpec(CallPath callPath) { + return new WebServiceCallSpec.Builder(callPath).supportsHttpMethod(HttpMethod.GET).build(); + } + + @SuppressWarnings({"unchecked", "rawtypes"}) + @Override + public List query(Q query) { + HeaderMap requestHeaders = + new HeaderMap.Builder(callSpec.getRequestSpec().getHeadersSpec()).build(); + RequestBody requestBody = + new RequestBody.Builder(callSpec.getRequestSpec().getBodySpec()) + .build(); + WebServiceRequest request = new WebServiceRequest(HttpMethod.GET, requestHeaders, requestBody); + WebServiceResponse response = stub.getResponse(callSpec, request, gson); + ResponseBody body = response.getBody(); + List list = body.get(TypedKeysQuery.RESOURCE_LIST); + return list; + } +} diff --git a/wsdef/src/main/java/com/google/gson/rest/definition/Queryable.java b/wsdef/src/main/java/com/google/gson/rest/definition/Queryable.java deleted file mode 100644 index fc372b40..00000000 --- a/wsdef/src/main/java/com/google/gson/rest/definition/Queryable.java +++ /dev/null @@ -1,13 +0,0 @@ -// Copyright 2010 Google Inc. All Rights Reserved. - -package com.google.gson.rest.definition; - -/** - * Implement this interface in a service to indicate that it allows querying - * by this type - * - * @author Inderjeet Singh - */ -public interface Queryable { - public RESULTS query(QUERY query); -} diff --git a/wsdef/src/main/java/com/google/gson/rest/query/ResourceQuery.java b/wsdef/src/main/java/com/google/gson/rest/query/ResourceQuery.java new file mode 100644 index 00000000..63b5f63e --- /dev/null +++ b/wsdef/src/main/java/com/google/gson/rest/query/ResourceQuery.java @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2010 Google Inc. + * + * 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 com.google.gson.rest.query; + +import java.util.List; + +import com.google.gson.rest.definition.ID; +import com.google.gson.rest.definition.RestResource; + +/** + * A query for a list of rest resources. + * + * @author Inderjeet Singh + */ +public interface ResourceQuery, QUERY> { + /** + * Returns a list of resources matching the query + */ + public List query(QUERY query); +} diff --git a/wsdef/src/main/java/com/google/gson/rest/query/TypedKeysQuery.java b/wsdef/src/main/java/com/google/gson/rest/query/TypedKeysQuery.java new file mode 100644 index 00000000..29a2aa79 --- /dev/null +++ b/wsdef/src/main/java/com/google/gson/rest/query/TypedKeysQuery.java @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2010 Google Inc. + * + * 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 com.google.gson.rest.query; + +import java.util.List; + +import com.google.gson.rest.definition.ID; +import com.google.gson.rest.definition.RestResource; +import com.google.gson.webservice.definition.TypedKey; + +/** + * List of {@link TypedKey}s associated with REST queries + * + * @author Inderjeet Singh + * + * @param ID type of the REST resource + * @param The type of the REST resource + */ +@SuppressWarnings("rawtypes") +public class TypedKeysQuery> { + + // TODO(inder): This should really be TypedKey> + public static TypedKey RESOURCE_LIST = new TypedKey("resourceList", List.class); +}