public static final Object strToObject(final String value) { if (value == null || !value.startsWith(PREFIX)) { return value; } return Try.apply(() -> { byte[] bytes = BaseEncoding.base64().decode(value.substring(PREFIX.length())); // TODO: sanitization of arguments return new ObjectInputStream(new ByteArrayInputStream(bytes)).readObject(); }) .wrap(x -> new IllegalArgumentException("Can't de-serialize value " + value, x)) .get(); }
public static final String objToStr(final Object value) { if (value instanceof CharSequence || Primitives.isWrapperType(value.getClass())) { return value.toString(); } return Try.apply(() -> { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); ObjectOutputStream stream = new ObjectOutputStream(bytes); stream.writeObject(value); stream.flush(); return PREFIX + BaseEncoding.base64().encode(bytes.toByteArray()); }) .wrap(x -> new IllegalArgumentException("Can't serialize value " + value, x)) .get(); }
public static Object getOrGenId(final Object bean, final Supplier<Long> next) { Field fid = field(bean); Object id = getId(bean, fid); if (id == null && fid.isAnnotationPresent(GeneratedValue.class)) { if (fid.getType() != Long.class) { throw new IllegalArgumentException("Generated value must be of type Long: " + fid); } id = Try.apply(() -> { Long seq = next.get(); fid.set(bean, seq); return seq; }).wrap(x -> new IllegalStateException("Can't generate id for: " + fid, x)) .get(); } return id; }
public static final String objToStr(final Object value) { if (value instanceof CharSequence || Primitives.isWrapperType(value.getClass())) { return value.toString(); } return Try.apply(() -> { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); ObjectOutputStream stream = new ObjectOutputStream(bytes); stream.writeObject(value); stream.flush(); return PREFIX + BaseEncoding.base64().encode(bytes.toByteArray()); }) .wrap(x -> new IllegalArgumentException("Can't serialize value " + value, x)) .get(); }
public static final Object strToObject(final String value) { if (value == null || !value.startsWith(PREFIX)) { return value; } return Try.apply(() -> { byte[] bytes = BaseEncoding.base64().decode(value.substring(PREFIX.length())); // TODO: sanitization of arguments return new ObjectInputStream(new ByteArrayInputStream(bytes)).readObject(); }) .wrap(x -> new IllegalArgumentException("Can't de-serialize value " + value, x)) .get(); }