/** * Decode an AutoBeanCodex payload. * * @param <T> the expected return type * @param factory an AutoBeanFactory capable of producing {@code AutoBean<T>} * @param clazz the expected return type * @param payload a payload string previously generated by * {@link #encode(AutoBean)}{@link Splittable#getPayload() * .getPayload()}. * @return an AutoBean containing the payload contents */ public static <T> AutoBean<T> decode(AutoBeanFactory factory, Class<T> clazz, String payload) { Splittable data = StringQuoter.split(payload); return decode(factory, clazz, data); }
/** * Return a JSON object literal with the contents of the store. */ public String encode() { // Backwards-compat: never store a '0' (see constructor) assert nextId >= 0; messageBean.as().setSyntheticId(nextId + 1); return AutoBeanCodex.encode(messageBean).getPayload(); }
/** * 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<ResponseMessage> 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(); }
/** * 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<ResponseMessage> 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(); }
/** * EntityCodex support. */ public <Q extends BaseProxy> AutoBean<Q> getBeanForPayload(Splittable serializedProxyId) { IdMessage idMessage = AutoBeanCodex.decode(MessageFactoryHolder.FACTORY, IdMessage.class, serializedProxyId).as(); @SuppressWarnings("unchecked") AutoBean<Q> toReturn = (AutoBean<Q>) getBeansForPayload(Collections.singletonList(idMessage)).get(0); return toReturn; }
private Splittable nonCollectionEncode(Object obj) { if (obj instanceof Collection) { // TODO: Allow for the encoding of nested collections. See issue 5974. // Once we do this, this can turn into an assert. throw new RuntimeException( "Unable to encode request as JSON payload; Request methods must have parameters of the form List<T> or Set<T>, where T is a scalar (non-collection) type."); } Splittable value; if (obj instanceof Enum && getAutoBeanFactory() instanceof EnumMap) { value = ValueCodex.encode(((EnumMap) getAutoBeanFactory()).getToken((Enum<?>) obj)); } else if (ValueCodex.canDecode(obj.getClass())) { value = ValueCodex.encode(obj); } else { // XXX user-provided implementation of interface? value = AutoBeanCodex.encode(AutoBeanUtils.getAutoBean(obj)); } return value; } }
/** * 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<ResponseMessage> 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(); }
/** * EntityCodex support. */ public <Q extends BaseProxy> AutoBean<Q> getBeanForPayload(Splittable serializedProxyId) { IdMessage ref = AutoBeanCodex.decode(MessageFactoryHolder.FACTORY, IdMessage.class, serializedProxyId).as(); @SuppressWarnings("unchecked") SimpleProxyId<Q> id = (SimpleProxyId<Q>) getId(ref); return getProxyForReturnPayloadGraph(id); }
domainReturnValue = AutoBeanCodex.encode(createFailureMessage(e)); ok = false;
/** * 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<ResponseMessage> 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(); }
/** * Load the OperationMessage containing the object state from the backing * store. */ private OperationMessage getOperation(String key) { Splittable data = store.get(key); if (data == null) { throw new NoDataException(); } OperationMessage op = AutoBeanCodex.decode(MessageFactoryHolder.FACTORY, OperationMessage.class, data).as(); return op; }
public String makePayload() { RequestData data = state.invocations.get(0).getRequestData(); AutoBean<JsonRpcRequest> bean = MessageFactoryHolder.FACTORY.jsonRpcRequest(); JsonRpcRequest request = bean.as(); request.setVersion("2.0"); request.setApiVersion(data.getApiVersion()); request.setId(payloadId++); Map<String, Splittable> params = new HashMap<String, Splittable>(); for (Map.Entry<String, Object> entry : data.getNamedParameters().entrySet()) { Object obj = entry.getValue(); Splittable value = encode(obj); params.put(entry.getKey(), value); } if (data.getRequestResource() != null) { params.put("resource", encode(data.getRequestResource())); } request.setParams(params); request.setMethod(data.getOperation()); return AutoBeanCodex.encode(bean).getPayload(); }
/** * Construct a DefaultProxyStore using the a value returned from * {@link #encode()}. * * @param payload a String previously returned from {@link #encode()} * @throws IllegalArgumentException if the payload cannot be parsed */ public DefaultProxyStore(String payload) throws IllegalArgumentException { messageBean = AutoBeanCodex.decode(MessageFactoryHolder.FACTORY, OperationMessage.class, payload); OperationMessage message = messageBean.as(); if (!EXPECTED_VERSION.equals(message.getVersion())) { throw new IllegalArgumentException("Unexpected version string in payload " + message.getVersion()); } map = message.getPropertyMap(); // #encode() saves nextId + 1, so we remove 1 here; see rationale below. nextId = message.getSyntheticId() - 1; if (nextId < 0) { /* * In case this is a message generated by a pre-2.5 DefaultProxyStore, * getSyntheticId will be '0', so we initialize the nextId to our best * guess (that only works because the store is append-only, and it could * break if nextId() was called on the original DefaultProxyStore more * times than put()). * Post-2.5 DefaultProxyStore never stores a '0' (see #encode()). */ nextId = map.size(); } }
/** * Assemble all of the state that has been accumulated in this context. This includes: * <ul> * <li>Diffs accumulated on objects passed to {@link #edit}. * <li>Invocations accumulated as Request subtypes passed to {@link #addInvocation}. * </ul> */ public String makePayload() { // Get the factory from the runtime-specific holder. MessageFactory f = MessageFactoryHolder.FACTORY; List<OperationMessage> operations = makePayloadOperations(); List<InvocationMessage> invocationMessages = makePayloadInvocations(); // Create the outer envelope message AutoBean<RequestMessage> bean = f.request(); RequestMessage requestMessage = bean.as(); requestMessage.setRequestFactory(getRequestFactory().getFactoryTypeToken()); if (!invocationMessages.isEmpty()) { requestMessage.setInvocations(invocationMessages); } if (!operations.isEmpty()) { requestMessage.setOperations(operations); } return AutoBeanCodex.encode(bean).getPayload(); }
/** * Decode an out-of-band message. */ <T> List<T> decodeOobMessage(Class<T> 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<Object> decoded = decodeInvocationArguments(state, message.getInvocations().get(0).getParameters(), new Class<?>[] {proxyType}, new Type[] {domainClass}); @SuppressWarnings("unchecked") List<T> toReturn = (List<T>) decoded; return toReturn; }
private void serializeOneProxy(SimpleProxyId<?> idForSerialization, AutoBean<? extends BaseProxy> bean) { AutoBean<OperationMessage> op = makeOperationMessage(serializedId(BaseProxyCategory.stableId(bean)), bean, false); store.put(getRequestFactory().getHistoryToken(idForSerialization), AutoBeanCodex.encode(op)); } }
public void processPayload(final Receiver<Void> receiver, String payload) { ResponseMessage response = AutoBeanCodex.decode(MessageFactoryHolder.FACTORY, ResponseMessage.class, payload).as(); if (response.getGeneralFailure() != null) { ServerFailureMessage failure = response.getGeneralFailure(); } else { ServerFailureMessage failure = AutoBeanCodex.decode(MessageFactoryHolder.FACTORY, ServerFailureMessage.class, response.getInvocationResults().get(i)).as(); state.invocations.get(i).onFail(
resp.setInvocations(Collections.singletonList(invocation)); resp.setOperations(operations); return AutoBeanCodex.encode(bean);
/** * Decode an AutoBeanCodex payload. * * @param <T> the expected return type * @param factory an AutoBeanFactory capable of producing {@code AutoBean<T>} * @param clazz the expected return type * @param payload a payload string previously generated by * {@link #encode(AutoBean)}{@link Splittable#getPayload() * .getPayload()}. * @return an AutoBean containing the payload contents */ public static <T> AutoBean<T> decode(AutoBeanFactory factory, Class<T> clazz, String payload) { Splittable data = StringQuoter.split(payload); return decode(factory, clazz, data); }
/** * EntityCodex support. */ public Splittable getSerializedProxyId(SimpleProxyId<?> stableId) { AutoBean<IdMessage> bean = MessageFactoryHolder.FACTORY.id(); IdMessage ref = bean.as(); ref.setServerId(stableId.getServerId()); ref.setTypeToken(getRequestFactory().getTypeToken(stableId.getProxyClass())); if (stableId.isSynthetic()) { ref.setStrength(Strength.SYNTHETIC); ref.setSyntheticId(stableId.getSyntheticId()); } else if (stableId.isEphemeral()) { ref.setStrength(Strength.EPHEMERAL); ref.setClientId(stableId.getClientId()); } return AutoBeanCodex.encode(bean); }