moved contents of wsf into greaze-server

This commit is contained in:
Inderjeet Singh 2010-11-14 08:32:56 +00:00
parent 4966bf0a53
commit d69290f3e9
21 changed files with 8 additions and 1076 deletions

View File

@ -28,14 +28,14 @@ import com.google.greaze.definition.webservice.ResponseSpec;
import com.google.greaze.definition.webservice.WebServiceCallSpec;
import com.google.greaze.definition.webservice.WebServiceRequest;
import com.google.greaze.definition.webservice.WebServiceResponse;
import com.google.greaze.webservice.server.RequestReceiver;
import com.google.greaze.webservice.server.ResponseSender;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.example.model.Cart;
import com.google.gson.example.model.Order;
import com.google.gson.example.model.TypedKeys;
import com.google.gson.example.service.SampleJsonService;
import com.google.gson.wsf.server.procedural.RequestReceiver;
import com.google.gson.wsf.server.procedural.ResponseSender;
/**
* A dispatcher for all the procedural calls

View File

@ -26,17 +26,17 @@ import com.google.greaze.definition.rest.RestCallSpec;
import com.google.greaze.definition.rest.RestRequest;
import com.google.greaze.definition.rest.RestResponse;
import com.google.greaze.definition.rest.ValueBasedId;
import com.google.greaze.rest.server.Repository;
import com.google.greaze.rest.server.RepositoryInMemory;
import com.google.greaze.rest.server.ResponseBuilderMap;
import com.google.greaze.rest.server.RestRequestReceiver;
import com.google.greaze.rest.server.RestResponseBuilder;
import com.google.greaze.rest.server.RestResponseSender;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.example.model.Cart;
import com.google.gson.example.model.Order;
import com.google.gson.example.service.ServicePaths;
import com.google.gson.rest.server.Repository;
import com.google.gson.rest.server.RepositoryInMemory;
import com.google.gson.rest.server.ResponseBuilderMap;
import com.google.gson.rest.server.RestRequestReceiver;
import com.google.gson.rest.server.RestResponseBuilder;
import com.google.gson.rest.server.RestResponseSender;
/**
* A dispatcher for all the REST requests

View File

@ -1,79 +0,0 @@
/*
* 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.server;
import java.lang.reflect.Type;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.google.greaze.definition.rest.HasId;
import com.google.greaze.definition.rest.ID;
import com.google.greaze.definition.rest.IDFactory;
/**
* This class provides a type-safe map to access values associated with Ids
*
* @author inder
*
* @param <T> the type of the objects being kept in the map
*/
public class IdMap<I extends ID, T extends HasId<I>> {
public static final Logger LOG = Logger.getLogger(IdMap.class.getName());
public static final long ID_START_VALUE = 1L;
protected final Map<I, T> map;
private volatile long nextAvailableId;
private final IDFactory<I> idFactory;
/**
* Use create(Type) instead of constructor
*/
protected IdMap(Class<? super I> classOfI, Type typeOfId) {
map = new ConcurrentHashMap<I, T>();
nextAvailableId = ID_START_VALUE;
this.idFactory = new IDFactory<I>(classOfI, typeOfId);
}
public T get(I id) {
return map.get(id);
}
public T put(T obj) {
map.put(obj.getId(), obj);
return obj;
}
public void delete(I id) {
T removed = map.remove(id);
if (removed == null) {
LOG.log(Level.WARNING, "Attempted to delete non-existent id: {0}", id);
}
}
public boolean exists(I id) {
return map.containsKey(id);
}
public synchronized I getNextId() {
long id = nextAvailableId++;
return idFactory.createId(id);
}
public static <II extends ID, S extends HasId<II>> IdMap<II, S> create(Class<? super II> classOfII, Type typeOfId) {
return new IdMap<II, S>(classOfII, typeOfId);
}
}

View File

@ -1,52 +0,0 @@
/*
* 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.server;
import java.util.HashMap;
import java.util.Map;
import com.google.greaze.definition.rest.ID;
import com.google.greaze.definition.rest.MetaData;
import com.google.greaze.definition.rest.RestResource;
/**
* A map of resources to their MetaData
*
* @author inder
*
* @param <R> the rest resource for whic the metadata is being stored
*/
public class MetaDataMap<I extends ID, R extends RestResource<I, R>> {
private final Map<I, MetaData<I, R>> map;
public MetaDataMap() {
this.map = new HashMap<I, MetaData<I, R>>();
}
public MetaData<I, R> get(I resourceId) {
MetaData<I, R> metaData = map.get(resourceId);
if (metaData == null) {
metaData = MetaData.create();
map.put(resourceId, metaData);
}
return metaData;
}
@Override
public String toString() {
return String.format("%s", map);
}
}

View File

@ -1,48 +0,0 @@
/*
* 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.server;
import com.google.greaze.definition.rest.HasId;
import com.google.greaze.definition.rest.ID;
/**
* An interface for a repository of rest resources. Meant for abstracting the server-side
* storage of rest resources.
*
* @author inder
*
* @param <R> the type of rest resource
*/
public interface Repository<I extends ID, R extends HasId<I>> {
public R get(I resourceId);
/**
* if resource.getId() == null, inserts the resource after assigning it a new id.
* Otherwise, updates the resource ensuring that it pre-exists.
*/
public R put(R resource);
public void delete(I resourceId);
public boolean exists(I resourceId);
/**
* Ensures that the specified resource has a valid id that will be used when it is saved
*/
public I assignId(R resource);
public I getNextId();
}

View File

@ -1,95 +0,0 @@
/*
* 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.server;
import com.google.common.base.Preconditions;
import com.google.greaze.definition.rest.ID;
import com.google.greaze.definition.rest.MetaData;
import com.google.greaze.definition.rest.RestResource;
/**
* An in-memory map of rest resources
*
* @author inder
*
* @param <R> Type variable for the resource
*/
public class RepositoryInMemory<I extends ID, R extends RestResource<I, R>> implements Repository<I, R> {
private static final String METADATA_KEY_IS_FRESHLY_ASSIGNED_ID = "isFreshlyAssignedId";
private final IdMap<I, R> resources;
private final MetaDataMap<I, R> metaDataMap;
public RepositoryInMemory(Class<? super I> classOfI, Class<? super R> classOfResource) {
this.resources = IdMap.create(classOfI, classOfResource);
this.metaDataMap = new MetaDataMap<I, R>();
}
@Override
public R get(I resourceId) {
return resources.get(resourceId);
}
public boolean isFreshlyAssignedId(I resourceId) {
MetaData<I, R> metaData = metaDataMap.get(resourceId);
if (metaData == null) {
return false;
}
return metaData.getBoolean(METADATA_KEY_IS_FRESHLY_ASSIGNED_ID);
}
@Override
public R put(R resource) {
if (!resource.hasId()) {
// insert semantics
assignId(resource);
} else {
I id = resource.getId();
if (!isFreshlyAssignedId(id)) {
// update semantics
Preconditions.checkState(resources.exists(resource.getId()));
}
}
resource = resources.put(resource);
metaDataMap.get(resource.getId()).remove(METADATA_KEY_IS_FRESHLY_ASSIGNED_ID);
return resource;
}
@Override
public void delete(I resourceId) {
resources.delete(resourceId);
}
@Override
public boolean exists(I resourceId) {
return resources.exists(resourceId);
}
@Override
public I getNextId() {
return resources.getNextId();
}
@Override
public I assignId(R resource) {
if (resource.getId() == null) {
I id = resources.getNextId();
resource.setId(id);
metaDataMap.get(id).putBoolean(METADATA_KEY_IS_FRESHLY_ASSIGNED_ID, true);
}
return resource.getId();
}
}

View File

@ -1,57 +0,0 @@
/*
* 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.server;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
import com.google.greaze.definition.rest.ID;
import com.google.greaze.definition.rest.RestCallSpec;
import com.google.greaze.definition.rest.RestResource;
/**
* A map of {@link RestCallSpec}, {@link RestResponseBuilder} to help figure out which
* {@link RestResponseBuilder} to use for a {@link RestCallSpec}.
*
* @author Inderjeet Singh
*/
public final class ResponseBuilderMap {
public static final class Builder {
private final Map<Type, RestResponseBuilder<?, ?>> map =
new HashMap<Type, RestResponseBuilder<?, ?>>();
public <I extends ID, R extends RestResource<I, R>> Builder set(
Type resourceType, RestResponseBuilder<I, R> responseBuilder) {
map.put(resourceType, responseBuilder);
return this;
}
public ResponseBuilderMap build() {
return new ResponseBuilderMap(map);
}
}
private final Map<Type, RestResponseBuilder<?, ?>> map;
public ResponseBuilderMap(Map<Type, RestResponseBuilder<?, ?>> map) {
this.map = map;
}
public RestResponseBuilder<?, ?> get(Type resourceType) {
return map.get(resourceType);
}
}

View File

@ -1,96 +0,0 @@
/*
* Copyright (C) 2008 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.server;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.Type;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import com.google.greaze.definition.HeaderMap;
import com.google.greaze.definition.HeaderMapSpec;
import com.google.greaze.definition.HttpMethod;
import com.google.greaze.definition.WebServiceSystemException;
import com.google.greaze.definition.rest.ID;
import com.google.greaze.definition.rest.RestRequest;
import com.google.greaze.definition.rest.RestRequestSpec;
import com.google.greaze.definition.rest.RestResource;
import com.google.gson.Gson;
import com.google.gson.JsonParseException;
/**
* Receives and parses a request at the server side on a {@link HttpServletRequest}.
*
* @author inder
*/
public final class RestRequestReceiver<I extends ID, R extends RestResource<I, R>> {
private final Gson gson;
private final RestRequestSpec spec;
public RestRequestReceiver(Gson gson, RestRequestSpec spec) {
this.gson = gson;
this.spec = spec;
}
public RestRequest<I, R> receive(HttpServletRequest request, I resourceId) {
try {
HeaderMap requestParams = buildRequestParams(request);
R requestBody = buildRequestBody(request);
HttpMethod method = HttpMethod.getMethod(request.getMethod());
String simulatedMethod = request.getHeader(HttpMethod.SIMULATED_METHOD_HEADER);
if (simulatedMethod != null && !simulatedMethod.equals("")) {
method = HttpMethod.getMethod(simulatedMethod);
}
return new RestRequest<I, R>(method, requestParams, resourceId, requestBody, spec.getResourceType());
} catch (IOException e) {
throw new WebServiceSystemException(e);
} catch (JsonParseException e) {
// Not a Web service request
throw new WebServiceSystemException(e);
}
}
private HeaderMap buildRequestParams(HttpServletRequest request) {
HeaderMapSpec paramsSpec = this.spec.getHeadersSpec();
HeaderMap.Builder paramsBuilder = new HeaderMap.Builder(paramsSpec);
for (Map.Entry<String, Type> param : paramsSpec.entrySet()) {
String name = param.getKey();
Type type = param.getValue();
String header = request.getHeader(name);
if (header == null || header.equals("")) {
// check parameter map for the value
header = request.getParameter(name);
}
if (header != null && !header.equals("")) {
Object value = gson.fromJson(header, type);
paramsBuilder.put(name, value);
}
}
return paramsBuilder.build();
}
private R buildRequestBody(HttpServletRequest request) throws IOException {
Reader reader = new BufferedReader(new InputStreamReader(request.getInputStream()));
R requestBody = gson.fromJson(reader, spec.getResourceType());
return requestBody;
}
}

View File

@ -1,68 +0,0 @@
/*
* 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.server;
import com.google.greaze.definition.HttpMethod;
import com.google.greaze.definition.rest.ID;
import com.google.greaze.definition.rest.RestCallSpec;
import com.google.greaze.definition.rest.RestRequest;
import com.google.greaze.definition.rest.RestResource;
import com.google.greaze.definition.rest.RestResponse;
public class RestResponseBuilder<I extends ID, R extends RestResource<I, R>> {
protected final Repository<I, R> resources;
public RestResponseBuilder(Repository<I, R> resources) {
this.resources = resources;
}
public void buildResponse(RestCallSpec callSpec, RestRequest<I, R> request,
RestResponse.Builder<I, R> responseBuilder) {
HttpMethod method = request.getMethod();
R responseBody = null;
switch (method) {
case GET:
responseBody = get(request.getId());
break;
case POST:
responseBody = post(request.getBody());
break;
case DELETE:
delete(request.getId());
break;
case PUT:
default:
throw new IllegalStateException("Unexpected method: " + method);
}
responseBuilder.setBody(responseBody);
}
public R get(I resourceId) {
return resources.get(resourceId);
}
public R post(R resource) {
return resources.put(resource);
}
public void delete(I resourceId) {
resources.delete(resourceId);
}
public R put(R resource) {
return resources.put(resource);
}
}

View File

@ -1,75 +0,0 @@
/*
* Copyright (C) 2008 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.server;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Map;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletResponse;
import com.google.greaze.definition.ContentBodySpec;
import com.google.greaze.definition.HeaderMap;
import com.google.greaze.definition.HeaderMapSpec;
import com.google.greaze.definition.rest.ID;
import com.google.greaze.definition.rest.RestResource;
import com.google.greaze.definition.rest.RestResponse;
import com.google.gson.Gson;
/**
* Sends a JSON web service response on {@link HttpServletResponse}.
*
* @author inder
*/
public final class RestResponseSender<I extends ID, R extends RestResource<I, R>> {
private static final Logger logger = Logger.getLogger(RestResponseSender.class.getCanonicalName());
private Gson gson;
public RestResponseSender(Gson gson) {
this.gson = gson;
}
public void send(HttpServletResponse conn, RestResponse<I, R> response) {
try {
sendHeaders(conn, response.getHeaders());
sendBody(conn, response.getBody());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private void sendHeaders(HttpServletResponse conn, HeaderMap responseParams) {
HeaderMapSpec spec = responseParams.getSpec();
for (Map.Entry<String, Object> param : responseParams.entrySet()) {
String paramName = param.getKey();
Object paramValue = param.getValue();
Type paramType = spec.getTypeFor(paramName);
String json = gson.toJson(paramValue, paramType);
logger.fine("RESPONSE HEADER:{" + paramName + ", " + json + "}");
conn.addHeader(paramName, json);
}
}
private void sendBody(HttpServletResponse conn, R responseBody) throws IOException {
conn.setContentType(ContentBodySpec.JSON_CONTENT_TYPE);
conn.setCharacterEncoding(ContentBodySpec.JSON_CHARACTER_ENCODING);
String json = gson.toJson(responseBody);
logger.fine("RESPONSE BODY:" + json);
conn.getWriter().append(json);
}
}

View File

@ -1,45 +0,0 @@
/*
* Copyright (C) 2008 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.wsf.inject;
import javax.servlet.http.HttpServletRequest;
import com.google.greaze.definition.CallPath;
import com.google.inject.Inject;
import com.google.inject.Provider;
/**
* Guice provider for {@link CallPath} for an incoming web service request.
*
* @author inder
*/
public final class CallPathProvider implements Provider<CallPath> {
private final CallPath callPath;
@Inject
public CallPathProvider(HttpServletRequest request) {
this(request.getPathInfo());
}
public CallPathProvider(String pathInfo) {
this.callPath = new CallPath(pathInfo);
}
@Override
public CallPath get() {
return callPath;
}
}

View File

@ -1,41 +0,0 @@
/*
* Copyright (C) 2008 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.wsf.inject.server.procedural;
import com.google.greaze.definition.webservice.RequestBodySpec;
import com.google.greaze.definition.webservice.RequestSpec;
import com.google.inject.Inject;
import com.google.inject.Provider;
/**
* Guice provider for the {@link RequestBodySpec} to map to the incoming requests.
*
* @author inder
*/
public final class RequestBodySpecProvider implements Provider<RequestBodySpec> {
private final RequestSpec requestSpec;
@Inject
public RequestBodySpecProvider(RequestSpec requestSpec) {
this.requestSpec = requestSpec;
}
@Override
public RequestBodySpec get() {
return requestSpec.getBodySpec();
}
}

View File

@ -1,41 +0,0 @@
/*
* Copyright (C) 2008 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.wsf.inject.server.procedural;
import com.google.greaze.definition.webservice.RequestSpec;
import com.google.greaze.definition.webservice.WebServiceCallSpec;
import com.google.inject.Inject;
import com.google.inject.Provider;
/**
* Guice provider for the {@link RequestSpec} to map to the incoming requests.
*
* @author inder
*/
public final class RequestSpecProvider implements Provider<RequestSpec> {
private final WebServiceCallSpec webServiceCallSpec;
@Inject
public RequestSpecProvider(WebServiceCallSpec webServiceCallSpec) {
this.webServiceCallSpec = webServiceCallSpec;
}
@Override
public RequestSpec get() {
return webServiceCallSpec.getRequestSpec();
}
}

View File

@ -1,57 +0,0 @@
/*
* Copyright (C) 2008 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.wsf.inject.server.procedural;
import javax.servlet.http.HttpServletRequest;
import com.google.greaze.definition.webservice.WebServiceCall;
import com.google.greaze.definition.webservice.WebServiceCallSpec;
import com.google.greaze.definition.webservice.WebServiceRequest;
import com.google.greaze.definition.webservice.WebServiceResponse;
import com.google.gson.Gson;
import com.google.gson.wsf.server.procedural.RequestReceiver;
import com.google.gson.wsf.server.procedural.WebServiceCallServerBuilder;
import com.google.inject.Inject;
import com.google.inject.Provider;
/**
* Guice provider for {@link WebServiceCall} to be used by a server-side implementation.
*
* @author inder
*/
public final class WebServiceCallServerProvider implements Provider<WebServiceCallServerBuilder> {
private final Gson gson;
private final WebServiceCallSpec callSpec;
private final HttpServletRequest request;
@Inject
public WebServiceCallServerProvider(Gson gson, HttpServletRequest request,
WebServiceCallSpec callSpec) {
this.callSpec = callSpec;
this.gson = gson;
this.request = request;
}
@Override
public WebServiceCallServerBuilder get() {
RequestReceiver receiver = new RequestReceiver(gson, callSpec.getRequestSpec());
WebServiceRequest wsRequest = receiver.receive(request);
WebServiceResponse.Builder responseBuilder =
new WebServiceResponse.Builder(callSpec.getResponseSpec());
return new WebServiceCallServerBuilder(callSpec, wsRequest, responseBuilder);
}
}

View File

@ -1,43 +0,0 @@
/*
* Copyright (C) 2008 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.wsf.inject.server.procedural;
import com.google.greaze.definition.CallPath;
import com.google.greaze.definition.webservice.WebServiceCallSpec;
import com.google.greaze.definition.webservice.WebServiceSpec;
import com.google.inject.Inject;
import com.google.inject.Provider;
/**
* Guice provider for {@link WebServiceCallSpec}.
*
* @author inder
*/
public final class WebServiceCallSpecProvider implements Provider<WebServiceCallSpec> {
private final CallPath callPath;
private final WebServiceSpec webServiceSpec;
@Inject
WebServiceCallSpecProvider(CallPath callPath, WebServiceSpec webServiceSpec) {
this.callPath = callPath;
this.webServiceSpec = webServiceSpec;
}
@Override
public WebServiceCallSpec get() {
return webServiceSpec.getCalls().get(callPath);
}
}

View File

@ -1,100 +0,0 @@
/*
* Copyright (C) 2008 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.wsf.server.procedural;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.Type;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import com.google.greaze.definition.HeaderMap;
import com.google.greaze.definition.HeaderMapSpec;
import com.google.greaze.definition.HttpMethod;
import com.google.greaze.definition.WebServiceSystemException;
import com.google.greaze.definition.webservice.RequestBody;
import com.google.greaze.definition.webservice.RequestBodySpec;
import com.google.greaze.definition.webservice.RequestSpec;
import com.google.greaze.definition.webservice.WebServiceRequest;
import com.google.gson.Gson;
import com.google.gson.JsonParseException;
/**
* Receives and parses a request at the server side on a {@link HttpServletRequest}.
*
* @author inder
*/
public final class RequestReceiver {
private final Gson gson;
private final RequestSpec spec;
public RequestReceiver(Gson gson, RequestSpec spec) {
this.gson = gson;
this.spec = spec;
}
public WebServiceRequest receive(HttpServletRequest request) {
try {
HeaderMap requestParams = buildRequestParams(request);
RequestBody requestBody = buildRequestBody(request);
HttpMethod method = HttpMethod.getMethod(request.getMethod());
return new WebServiceRequest(method, requestParams, requestBody);
} catch (IOException e) {
throw new WebServiceSystemException(e);
} catch (JsonParseException e) {
// Not a Web service request
throw new WebServiceSystemException(e);
}
}
private HeaderMap buildRequestParams(HttpServletRequest request) {
HeaderMapSpec paramsSpec = this.spec.getHeadersSpec();
HeaderMap.Builder paramsBuilder = new HeaderMap.Builder(paramsSpec);
for (Map.Entry<String, Type> param : paramsSpec.entrySet()) {
String name = param.getKey();
Type type = param.getValue();
String header = request.getHeader(name);
if (header == null || header.equals("")) {
// check parameter map for the value
header = request.getParameter(name);
}
if (header != null && !header.equals("")) {
Object value = gson.fromJson(header, type);
paramsBuilder.put(name, value);
}
}
return paramsBuilder.build();
}
private RequestBody buildRequestBody(HttpServletRequest request) throws IOException {
RequestBodySpec bodySpec = spec.getBodySpec();
if (bodySpec.size() == 0) {
return createEmptyRequestBody(bodySpec);
}
Reader reader = new BufferedReader(new InputStreamReader(request.getInputStream()));
RequestBody requestBody = gson.fromJson(reader, RequestBody.class);
return requestBody;
}
private RequestBody createEmptyRequestBody(RequestBodySpec bodySpec) {
return new RequestBody.Builder(bodySpec).build();
}
}

View File

@ -1,30 +0,0 @@
/*
* 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.wsf.server.procedural;
import com.google.greaze.definition.webservice.WebServiceCallSpec;
import com.google.greaze.definition.webservice.WebServiceRequest;
import com.google.greaze.definition.webservice.WebServiceResponse;
/**
* An interface describing a class that can build a response
*
* @author inder
*/
public interface ResponseBuilder {
public void buildResponse(WebServiceCallSpec callSpec, WebServiceRequest request,
WebServiceResponse.Builder responseBuilder);
}

View File

@ -1,73 +0,0 @@
/*
* Copyright (C) 2008 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.wsf.server.procedural;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Map;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletResponse;
import com.google.greaze.definition.HeaderMap;
import com.google.greaze.definition.HeaderMapSpec;
import com.google.greaze.definition.webservice.ResponseBody;
import com.google.greaze.definition.webservice.WebServiceResponse;
import com.google.gson.Gson;
/**
* Sends a JSON web service response on {@link HttpServletResponse}.
*
* @author inder
*/
public final class ResponseSender {
private static final Logger logger = Logger.getLogger(ResponseSender.class.getCanonicalName());
private Gson gson;
public ResponseSender(Gson gson) {
this.gson = gson;
}
public void send(HttpServletResponse conn, WebServiceResponse response) {
try {
sendHeaders(conn, response.getHeaders());
sendBody(conn, response.getBody());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private void sendHeaders(HttpServletResponse conn, HeaderMap responseParams) {
HeaderMapSpec spec = responseParams.getSpec();
for (Map.Entry<String, Object> param : responseParams.entrySet()) {
String paramName = param.getKey();
Object paramValue = param.getValue();
Type paramType = spec.getTypeFor(paramName);
String json = gson.toJson(paramValue, paramType);
logger.fine("RESPONSE HEADER:{" + paramName + ", " + json + "}");
conn.addHeader(paramName, json);
}
}
private void sendBody(HttpServletResponse conn, ResponseBody responseBody) throws IOException {
conn.setContentType(responseBody.getContentType());
conn.setCharacterEncoding(responseBody.getCharacterEncoding());
String json = gson.toJson(responseBody);
logger.fine("RESPONSE BODY:" + json);
conn.getWriter().append(json);
}
}

View File

@ -1,29 +0,0 @@
/*
* Copyright (C) 2009 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.wsf.server.procedural;
import com.google.greaze.definition.webservice.WebServiceCallSpec;
import com.google.greaze.definition.webservice.WebServiceRequest;
import com.google.greaze.definition.webservice.WebServiceResponse.Builder;
public class WebServiceCallServerBuilder {
public WebServiceCallServerBuilder(
@SuppressWarnings("unused") WebServiceCallSpec callSpec,
@SuppressWarnings("unused") WebServiceRequest wsRequest,
@SuppressWarnings("unused") Builder responseBuilder) {
}
}

View File

@ -1,21 +0,0 @@
<assembly>
<id>release</id>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<includes>
<include>README*</include>
<include>LICENSE*</include>
</includes>
</fileSet>
<fileSet>
<directory>target</directory>
<outputDirectory></outputDirectory>
<includes>
<include>wsf-*.jar</include>
</includes>
</fileSet>
</fileSets>
</assembly>

View File

@ -1,18 +0,0 @@
<archive>
<addMavenDescriptor>true</addMavenDescriptor>
<compress/>
<forced/>
<index/>
<manifest>
<addClasspath/>
<addDefaultImplementationEntries/>
<addDefaultSpecificationEntries/>
<addExtensions/>
<classpathMavenRepositoryLayout/>
<classpathPrefix/>
<mainClass/>
<packageName/>
</manifest>
<manifestFile/>
<pomPropertiesFile/>
</archive>