use of com.robo4j.socket.http.dto.ClassGetSetDTO in project robo4j by Robo4J.
the class RoboRequestCallable method call.
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public HttpResponseProcess call() throws Exception {
final HttpResponseProcessBuilder resultBuilder = HttpResponseProcessBuilder.Builder();
final ServerPathConfig pathConfig = serverContext.getPathConfig(decoratedRequest.getPathMethod());
if (isValidPath(pathConfig)) {
resultBuilder.setMethod(pathConfig.getMethod());
resultBuilder.setPath(pathConfig.getPath());
switch(pathConfig.getMethod()) {
case GET:
if (pathConfig.getPath().equals(UTF8_SOLIDUS)) {
resultBuilder.setCode(StatusCode.OK);
resultBuilder.setResult(factory.processGet(context));
} else {
resultBuilder.setTarget(pathConfig.getRoboUnit().getId());
final Object unitDescription;
// the system needs to have one more worker thread to evaluate Future get
final HttpRequestDenominator denominator = (HttpRequestDenominator) decoratedRequest.getDenominator();
final Set<String> requestAttributes = denominator.getAttributes().get(HttpPathUtils.ATTRIBUTES_PATH_VALUE);
if (requestAttributes == null) {
unitDescription = factory.processGet(pathConfig);
} else if (requestAttributes.isEmpty()) {
RoboReference<?> unit = context.getReference(pathConfig.getRoboUnit().getId());
PathAttributeListDTO pathAttributes = new PathAttributeListDTO();
unit.getKnownAttributes().forEach(a -> {
PathAttributeDTO attributeDescriptor = new PathAttributeDTO();
attributeDescriptor.setName(a.getAttributeName());
attributeDescriptor.setValue(a.getAttributeType().getCanonicalName());
pathAttributes.addAttribute(attributeDescriptor);
});
unitDescription = ReflectUtils.createJson(pathAttributes);
} else {
RoboReference<?> unit = context.getReference(pathConfig.getRoboUnit().getId());
List<PathAttributeDTO> attributes = new ArrayList<>();
for (AttributeDescriptor attr : unit.getKnownAttributes()) {
if (requestAttributes.contains(attr.getAttributeName())) {
PathAttributeDTO attribute = new PathAttributeDTO();
String valueString = String.valueOf(unit.getAttribute(attr).get());
attribute.setValue(valueString);
attribute.setName(attr.getAttributeName());
attributes.add(attribute);
}
}
if (attributes.size() == 1) {
Map<String, ClassGetSetDTO> responseAttributeDescriptorMap = ReflectUtils.getFieldsTypeMap(PathAttributeDTO.class);
unitDescription = JsonUtil.toJson(responseAttributeDescriptorMap, attributes.get(0));
} else {
unitDescription = JsonUtil.toJsonArray(attributes);
}
}
resultBuilder.setCode(StatusCode.OK);
resultBuilder.setResult(unitDescription);
}
break;
case POST:
if (pathConfig.getPath().equals(UTF8_SOLIDUS)) {
resultBuilder.setCode(StatusCode.BAD_REQUEST);
} else {
resultBuilder.setTarget(pathConfig.getRoboUnit().getId());
Object respObj = factory.processPost(pathConfig.getRoboUnit(), decoratedRequest.getMessage());
if (respObj == null) {
resultBuilder.setCode(StatusCode.BAD_REQUEST);
} else {
resultBuilder.setCode(StatusCode.ACCEPTED);
resultBuilder.setResult(respObj);
}
}
break;
default:
resultBuilder.setCode(StatusCode.BAD_REQUEST);
SimpleLoggingUtil.debug(getClass(), "not implemented method: " + decoratedRequest.getPathMethod());
}
} else {
resultBuilder.setCode(StatusCode.BAD_REQUEST);
}
return resultBuilder.build();
}
use of com.robo4j.socket.http.dto.ClassGetSetDTO in project robo4j by Robo4J.
the class ReflectUtils method createJson.
public static String createJson(Object obj) {
final Map<String, ClassGetSetDTO> map = getFieldsTypeMap(obj.getClass());
String preKey = map.keySet().isEmpty() ? null : map.keySet().iterator().next();
if (map.size() == 1 && preKey != null && preKey.equals(StringConstants.EMPTY)) {
ClassGetSetDTO descriptor = map.values().iterator().next();
switch(descriptor.getCollection()) {
case MAP:
return new StringBuilder().append(map.entrySet().stream().map(entry -> new MapEntryDTO(entry.getKey(), ReflectUtils.getJsonValue(entry.getValue(), obj))).filter(entry -> Objects.nonNull(entry.getValue())).map(entry -> JsonElementStringBuilder.Builder().add(entry.getValue()).build()).collect(Collectors.joining(UTF8_COMMA))).toString();
case ARRAY:
case LIST:
return new StringBuilder().append(map.entrySet().stream().map(entry -> new MapEntryDTO(entry.getKey(), ReflectUtils.getJsonValue(entry.getValue(), obj))).filter(entry -> Objects.nonNull(entry.getValue())).map(entry -> JsonElementStringBuilder.Builder().add(entry.getValue()).build()).collect(Collectors.joining(UTF8_COMMA))).toString();
}
}
return createJson(map, obj);
}
use of com.robo4j.socket.http.dto.ClassGetSetDTO in project robo4j by Robo4J.
the class ReflectUtils method processCollectionToJson.
@SuppressWarnings("unchecked")
private static String processCollectionToJson(ClassGetSetDTO getterDTO, TypeMapper typeMapper, Object obj) {
JsonElementStringBuilder result = JsonElementStringBuilder.Builder();
JsonTypeAdapter jsonTypeAdapter = getAdapterByClazz(getterDTO.getValueClass(), typeMapper);
switch(getterDTO.getCollection()) {
case ARRAY:
Object[] arrayObjects = (Object[]) obj;
String arrayValue = Stream.of(arrayObjects).map(element -> jsonTypeAdapter.adapt(element)).collect(Collectors.joining(UTF8_COMMA));
// formatter:off
result.add(UTF8_SQUARE_BRACKET_LEFT).add(arrayValue).add(UTF8_SQUARE_BRACKET_RIGHT);
break;
case LIST:
List<Object> objects = (List<Object>) obj;
String listValue = objects.stream().map(element -> jsonTypeAdapter.adapt(element)).collect(Collectors.joining(UTF8_COMMA));
// formatter:off
result.add(UTF8_SQUARE_BRACKET_LEFT).add(listValue).add(UTF8_SQUARE_BRACKET_RIGHT);
// formatter:on
break;
case MAP:
Map<Object, Object> objectMap = (Map<Object, Object>) obj;
result.add(UTF8_CURLY_BRACKET_LEFT).add(objectMap.entrySet().stream().map(entry -> JsonElementStringBuilder.Builder().addQuotationWithDelimiter(UTF8_COLON, entry.getKey()).add(jsonTypeAdapter.adapt(entry.getValue())).build()).collect(Collectors.joining(UTF8_COMMA))).add(UTF8_CURLY_BRACKET_RIGHT);
break;
}
return result.build();
}
use of com.robo4j.socket.http.dto.ClassGetSetDTO in project robo4j by Robo4J.
the class HttpPathUtilTests method createJsonArrayByList.
@Test
void createJsonArrayByList() {
PathAttributeDTO attributeDTO = new PathAttributeDTO("number", "42");
Map<String, ClassGetSetDTO> descriptorMap = ReflectUtils.getFieldsTypeMap(PathAttributeDTO.class);
System.out.println("result: " + JsonUtil.toJson(descriptorMap, attributeDTO));
}
use of com.robo4j.socket.http.dto.ClassGetSetDTO in project robo4j by Robo4J.
the class ReflectUtils method adjustRoboJsonDocumentCast.
private static Object adjustRoboJsonDocumentCast(ClassGetSetDTO classGetSetDTO, JsonDocument document, String key) {
if (classGetSetDTO.getCollection() != null) {
switch(classGetSetDTO.getCollection()) {
case ARRAY:
Class<?> arrayClass = classGetSetDTO.getValueClass();
List<?> list = ((JsonDocument) document.getKey(key)).getArray().stream().map(arrayClass::cast).collect(Collectors.toCollection(LinkedList::new));
return listToArray(list);
case LIST:
Class<?> listClass = classGetSetDTO.getValueClass();
// @formatter:off
return ((JsonDocument) document.getKey(key)).getArray().stream().filter(Objects::nonNull).map(e -> adjustRoboClassCast(listClass, e)).collect(Collectors.toList());
// @formatter:on
case MAP:
JsonDocument mapDocument = (JsonDocument) document.getKey(key);
Map<String, Object> documentMap = mapDocument.getMap();
if (documentMap.isEmpty()) {
return documentMap;
} else {
// @formatter:off
return documentMap.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> {
if (e.getValue() != null && e.getValue() instanceof JsonDocument) {
Class<?> mapValueClazz = classGetSetDTO.getValueClass();
return adjustRoboClassCast(mapValueClazz, e.getValue());
}
return e.getValue();
}));
// @formatter:on
}
default:
throw new RoboReflectException("wrong collection: " + classGetSetDTO);
}
} else {
return adjustRoboClassCast(classGetSetDTO.getValueClass(), document.getKey(key));
}
}
Aggregations