Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 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.web.bindery.requestfactory.server;
import com.google.gwt.user.server.Base64Utils;
import com.google.web.bindery.autobean.shared.AutoBean;
import com.google.web.bindery.autobean.shared.AutoBeanCodex;
import com.google.web.bindery.autobean.shared.AutoBeanUtils;
import com.google.web.bindery.autobean.shared.AutoBeanVisitor;
import com.google.web.bindery.autobean.shared.Splittable;
import com.google.web.bindery.autobean.shared.ValueCodex;
import com.google.web.bindery.autobean.vm.AutoBeanFactorySource;
import com.google.web.bindery.autobean.vm.Configuration;
import com.google.web.bindery.autobean.vm.impl.TypeUtils;
import com.google.web.bindery.requestfactory.shared.BaseProxy;
import com.google.web.bindery.requestfactory.shared.EntityProxyId;
import com.google.web.bindery.requestfactory.shared.InstanceRequest;
import com.google.web.bindery.requestfactory.shared.Request;
import com.google.web.bindery.requestfactory.shared.RequestContext;
import com.google.web.bindery.requestfactory.shared.ServerFailure;
import com.google.web.bindery.requestfactory.shared.WriteOperation;
import com.google.web.bindery.requestfactory.shared.impl.BaseProxyCategory;
import com.google.web.bindery.requestfactory.shared.impl.Constants;
import com.google.web.bindery.requestfactory.shared.impl.EntityCodex;
import com.google.web.bindery.requestfactory.shared.impl.EntityProxyCategory;
import com.google.web.bindery.requestfactory.shared.impl.SimpleProxyId;
import com.google.web.bindery.requestfactory.shared.impl.ValueProxyCategory;
import com.google.web.bindery.requestfactory.shared.messages.IdMessage;
import com.google.web.bindery.requestfactory.shared.messages.IdMessage.Strength;
import com.google.web.bindery.requestfactory.shared.messages.InvocationMessage;
import com.google.web.bindery.requestfactory.shared.messages.MessageFactory;
import com.google.web.bindery.requestfactory.shared.messages.OperationMessage;
import com.google.web.bindery.requestfactory.shared.messages.RequestMessage;
import com.google.web.bindery.requestfactory.shared.messages.ResponseMessage;
import com.google.web.bindery.requestfactory.shared.messages.ServerFailureMessage;
import com.google.web.bindery.requestfactory.shared.messages.ViolationMessage;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import javax.validation.ConstraintViolation;
/**
* Processes request payloads from a RequestFactory client. This implementation
* is stateless. A single instance may be reused and is thread-safe.
*/
public class SimpleRequestProcessor {
/**
* This parameterization is so long, it improves readability to have a
* specific type.
*
* FIXME: IDs used as keys in this map can be mutated (turning an ephemeral
* ID to a persisted ID in Resolver#resolveClientProxy) in a way that can
* change their hashCode value and equals behavior, therefore breaking the
* Map contract. We should find a way to only put immutable IDs here, or
* change SimpleProxyId so that its hashCode value and equals behavior don't
* change, or possibly remove and re-add the entry when the ID is modified
* (as this is something entirely under our control).
*/
@SuppressWarnings("serial")
static class IdToEntityMap extends HashMap, AutoBean extends BaseProxy>> {
}
/**
* Allows the creation of properly-configured AutoBeans without having to
* create an AutoBeanFactory with the desired annotations.
*/
static final Configuration CONFIGURATION = new Configuration.Builder().setCategories(
EntityProxyCategory.class, ValueProxyCategory.class, BaseProxyCategory.class).setNoWrap(
EntityProxyId.class).build();
/**
* Vends message objects.
*/
static final MessageFactory FACTORY = AutoBeanFactorySource.create(MessageFactory.class);
static String fromBase64(String encoded) {
try {
return new String(Base64Utils.fromBase64(encoded), "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new UnexpectedException(e);
}
}
static String toBase64(String data) {
try {
return Base64Utils.toBase64(data.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new UnexpectedException(e);
}
}
private ExceptionHandler exceptionHandler = new DefaultExceptionHandler();
private final ServiceLayer service;
public SimpleRequestProcessor(ServiceLayer serviceLayer) {
this.service = serviceLayer;
}
/**
* Process a payload sent by a RequestFactory client.
*
* @param payload the payload sent by the client
* @return a payload to return to the client
*/
public String process(String payload) {
RequestMessage req = AutoBeanCodex.decode(FACTORY, RequestMessage.class, payload).as();
AutoBean responseBean = FACTORY.response();
try {
process(req, responseBean.as());
} catch (ReportableException e) {
// Create a new response envelope, since the state is unknown
responseBean = FACTORY.response();
responseBean.as().setGeneralFailure(createFailureMessage(e).as());
}
// Return a JSON-formatted payload
return AutoBeanCodex.encode(responseBean).getPayload();
}
public void setExceptionHandler(ExceptionHandler exceptionHandler) {
this.exceptionHandler = exceptionHandler;
}
/**
* Encode a list of objects into a self-contained message that can be used for
* out-of-band communication.
*/
Splittable createOobMessage(List domainValues) {
RequestState state = new RequestState(service);
List encodedValues = new ArrayList(domainValues.size());
for (T domainValue : domainValues) {
Object clientValue;
if (domainValue == null) {
clientValue = null;
} else {
Class> clientType =
service.resolveClientType(domainValue.getClass(), BaseProxy.class, true);
clientValue =
state.getResolver().resolveClientValue(domainValue, clientType,
Collections. emptySet());
}
encodedValues.add(EntityCodex.encode(state, clientValue));
}
IdToEntityMap map = new IdToEntityMap();
map.putAll(state.beans);
List operations = new ArrayList();
createReturnOperations(operations, state, map);
InvocationMessage invocation = FACTORY.invocation().as();
invocation.setParameters(encodedValues);
AutoBean bean = FACTORY.request();
RequestMessage resp = bean.as();
resp.setInvocations(Collections.singletonList(invocation));
resp.setOperations(operations);
return AutoBeanCodex.encode(bean);
}
/**
* Decode an out-of-band message.
*/
List decodeOobMessage(Class domainClass, Splittable payload) {
Class> proxyType = service.resolveClientType(domainClass, BaseProxy.class, true);
RequestState state = new RequestState(service);
RequestMessage message = AutoBeanCodex.decode(FACTORY, RequestMessage.class, payload).as();
processOperationMessages(state, message);
List