/** * Returns a string that encodes the results of an RPC call. Private overload * that takes a flag signaling the preamble of the response payload. * * @param object the object that we wish to send back to the client * @param wasThrown if true, the object being returned was an exception thrown * by the service method; if false, it was the result of the service * method's invocation * @return a string that encodes the response from a service method * @throws SerializationException if the object cannot be serialized */ private static String encodeResponse(Class<?> responseClass, Object object, boolean wasThrown, int flags, SerializationPolicy serializationPolicy) throws SerializationException { ServerSerializationStreamWriter stream = new ServerSerializationStreamWriter(serializationPolicy, getRpcVersion()); stream.setFlags(flags); stream.prepareToWrite(); if (responseClass != void.class) { stream.serializeValue(object, responseClass); } String bufferStr = (wasThrown ? "//EX" : "//OK") + stream.toString(); return bufferStr; }
@Override void write(ServerSerializationStreamWriter stream, Object instance) throws SerializationException { Object[] vector = (Object[]) instance; stream.writeInt(vector.length); for (int i = 0, n = vector.length; i < n; ++i) { stream.writeObject(vector[i]); } } },
@Override void write(ServerSerializationStreamWriter stream, Object instance) { String[] vector = (String[]) instance; stream.writeInt(vector.length); for (int i = 0, n = vector.length; i < n; ++i) { stream.writeString(vector[i]); } } };
public String serialize(Object message) { try { ServerSerializationStreamWriter streamWriter = new ServerSerializationStreamWriter(serializationPolicy); streamWriter.prepareToWrite(); streamWriter.writeObject(message); return streamWriter.toString(); } catch (SerializationException ex) { logger.log(Level.SEVERE, "Failed to serialize message", ex); return null; } }
@Override protected String createResponse(ServerSerializationStreamWriter stream, Class responseType, Object responseObj, boolean isException) { stream.prepareToWrite(); if (responseType != void.class) { try { stream.serializeValue(responseObj, responseType); } catch (SerializationException e) { responseObj = e; isException = true; } } return (isException ? "//EX" : "//OK") + stream.toString(); }
writeString(encodedData); } catch (IllegalAccessException e) { throw new SerializationException(e); try { value = declField.get(instance); serializeValue(value, declField.getType()); serializeImpl(instance, superClass);
@Override public void writeLong(long value) { if (getVersion() == SERIALIZATION_STREAM_MIN_VERSION) { // Write longs as a pair of doubles for backwards compatibility double[] parts = getAsDoubleArray(value); assert parts != null && parts.length == 2; writeDouble(parts[0]); writeDouble(parts[1]); } else { StringBuilder sb = new StringBuilder(); sb.append('"'); sb.append(Base64Utils.toBase64(value)); sb.append('"'); append(sb.toString()); } }
@Override void write(ServerSerializationStreamWriter stream, Object instance) throws SerializationException { stream.writeObject(instance); } },
/** * This method takes a string and outputs a JavaScript string literal. The * data is surrounded with quotes, and any contained characters that need to * be escaped are mapped onto their escape sequence. * * Assumptions: We are targeting a version of JavaScript that that is later * than 1.3 that supports unicode strings. */ public static String escapeString(String toEscape) { return escapeString(toEscape, false, null); }
@Override void write(ServerSerializationStreamWriter stream, Object instance) { stream.writeString((String) instance); } };
public String serialize(Object message) { try { ServerSerializationStreamWriter streamWriter = new ServerSerializationStreamWriter(serializationPolicy); streamWriter.prepareToWrite(); streamWriter.writeObject(message); return streamWriter.toString(); } catch (SerializationException ex) { logger.log(Level.SEVERE, "Failed to serialize message", ex); return null; } }
writeString(encodedData); } catch (IllegalAccessException e) { throw new SerializationException(e); try { value = declField.get(instance); serializeValue(value, declField.getType()); serializeImpl(instance, superClass);
@Override public void writeLong(long value) { if (getVersion() == SERIALIZATION_STREAM_MIN_VERSION) { // Write longs as a pair of doubles for backwards compatibility double[] parts = getAsDoubleArray(value); assert parts != null && parts.length == 2; writeDouble(parts[0]); writeDouble(parts[1]); } else { StringBuilder sb = new StringBuilder(); sb.append('"'); sb.append(Base64Utils.toBase64(value)); sb.append('"'); append(sb.toString()); } }
@Override void write(ServerSerializationStreamWriter stream, Object instance) throws SerializationException { stream.writeObject(instance); } },
/** * This method takes a string and outputs a JavaScript string literal. The * data is surrounded with quotes, and any contained characters that need to * be escaped are mapped onto their escape sequence. * * This splits strings into 64KB chunks to workaround an issue with the hosted mode client where * the Rhino parser can't handle string nodes larger than 64KB, e.g. {@code "longstring"} is * converted to {@code "long" + "string"}. * * Assumptions: We are targeting a version of JavaScript that that is later * than 1.3 that supports unicode strings. */ public static String escapeStringSplitNodes(String toEscape) { return escapeString(toEscape, true, null); }
@Override void write(ServerSerializationStreamWriter stream, Object instance) { stream.writeString((String) instance); } };
public <T> String serialize(Class<? super T> clazz, T instance, SerializationPolicy serializationPolicy) throws SerializationException { if (instance == null) { return null; }else if(String.class.isAssignableFrom(clazz)){ return (String) instance; } if (serializationPolicy == null) { throw new IllegalArgumentException("SerializationPolicy is null, please call StorageUtils.PolicyLoader.load(...) before"); } ServerSerializationStreamWriter stream = new ServerSerializationStreamWriter(serializationPolicy); stream.setFlags(AbstractSerializationStream.DEFAULT_FLAGS); stream.prepareToWrite(); stream.writeString(SERVER_READABLE_STR_1); stream.writeString(SERVER_READABLE_STR_2); if (clazz != void.class) { stream.serializeValue(instance, clazz); } return stream.toString(); }
public static String serialize(Object message) throws SerializationException { ServerSerializationStreamWriter streamWriter = new ServerSerializationStreamWriter(GwtRpcUtil.getSerializationPolicy()); streamWriter.prepareToWrite(); streamWriter.writeObject(message); return streamWriter.toString(); }
writeString(encodedData); } catch (IllegalAccessException e) { throw new SerializationException(e); try { value = declField.get(instance); serializeValue(value, declField.getType()); serializeImpl(instance, superClass);
@Override public void writeLong(long value) { if (getVersion() == SERIALIZATION_STREAM_MIN_VERSION) { // Write longs as a pair of doubles for backwards compatibility double[] parts = getAsDoubleArray(value); assert parts != null && parts.length == 2; writeDouble(parts[0]); writeDouble(parts[1]); } else { StringBuilder sb = new StringBuilder(); sb.append('"'); sb.append(Base64Utils.toBase64(value)); sb.append('"'); append(sb.toString()); } }