ph.com.nightowlstudios.service.ServiceUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of edge Show documentation
Show all versions of edge Show documentation
A simple library for building REST API using Vertx.
package ph.com.nightowlstudios.service;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import org.apache.commons.lang3.StringUtils;
import ph.com.nightowlstudios.dto.DTO;
import ph.com.nightowlstudios.entity.Table;
import java.time.Instant;
import java.util.*;
/**
* @author Joseph Harvey Angeles - yev
* @since 4/4/21
**/
public class ServiceUtils {
public static final String PAYLOAD = "payload";
public static final String TYPE = "type";
public static final String NIL_TYPE = "nil";
private ServiceUtils() {
}
static JsonObject buildRequestPayload(Object... payload) {
JsonArray array = new JsonArray();
JsonArray types = new JsonArray();
Arrays
.stream(payload)
.forEachOrdered(obj -> {
if (isEntity(obj.getClass()) || isDTO(obj.getClass())) {
array.add(JsonObject.mapFrom(obj));
types.add(obj.getClass().getName());
} else if (obj.getClass().getName().equals(UUID.class.getName())) {
array.add(((UUID) obj).toString()); // Add as string, receiver will just unwrap base on type.
types.add(UUID.class.getName()); // Type as UUID to match exact method parameter of receiver.
} else {
array.add(obj);
types.add(obj.getClass().getName());
}
});
return new JsonObject()
.put(PAYLOAD, array)
.put(TYPE, types);
}
@SuppressWarnings("unchecked")
static Optional unwrapRequestResponse(JsonObject body) throws Exception {
String typeName = body.getString(TYPE);
return !typeName.equalsIgnoreCase(NIL_TYPE)
? Optional.ofNullable((R) Class.forName(typeName).cast(fromReplyPayload(body)))
: Optional.empty();
}
@SuppressWarnings("unchecked")
private static Object fromReplyPayload(JsonObject body) throws Exception {
String typeName = body.getString(TYPE);
Class> theClass = Class.forName(typeName);
if (isEntity(Class.forName(typeName)) || isDTO(Class.forName(typeName))) {
JsonObject payload = body.getJsonObject(PAYLOAD);
return payload.mapTo(theClass);
} else if (UUID.class.getName().equals(typeName)) {
return UUID.fromString(body.getString(PAYLOAD));
} else if (String.class.getName().equals(typeName)) {
return body.getString(PAYLOAD);
} else if (Integer.class.getName().equals(typeName)) {
return body.getInteger(PAYLOAD);
} else if (Long.class.getName().equals(typeName)) {
return body.getLong(PAYLOAD);
} else if (Instant.class.getName().equals(typeName)) {
return body.getInstant(PAYLOAD);
} else if (Double.class.getName().equals(typeName)) {
return body.getDouble(PAYLOAD);
} else if (Float.class.getName().equals(typeName)) {
return body.getFloat(PAYLOAD);
} else if (Number.class.getName().equals(typeName)) {
return body.getNumber(PAYLOAD);
} else if (Buffer.class.getName().equals(typeName)) {
return body.getBuffer(PAYLOAD);
} else if (Byte.class.getName().equals(typeName)) {
return body.getBinary(PAYLOAD);
} else if (typeName.toLowerCase().endsWith("list")) {
List list = new ArrayList<>();
JsonArray array = body.getJsonArray(PAYLOAD);
for (int i = 0; i < array.size(); i++) {
list.add(fromReplyPayload(array.getJsonObject(i)));
}
return list;
} else {
return body.getValue(PAYLOAD);
}
}
static Object[] extractRequestPayloadParameters(JsonObject body) throws ClassNotFoundException {
JsonArray payload = body.getJsonArray(PAYLOAD);
JsonArray types = body.getJsonArray(TYPE);
List