Search in sources :

Example 1 with ClassGetSetDTO

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();
}
Also used : HttpPathUtils(com.robo4j.socket.http.util.HttpPathUtils) SimpleLoggingUtil(com.robo4j.logging.SimpleLoggingUtil) HttpRequestDenominator(com.robo4j.socket.http.message.HttpRequestDenominator) ServerPathConfig(com.robo4j.socket.http.units.ServerPathConfig) Set(java.util.Set) Callable(java.util.concurrent.Callable) PathAttributeDTO(com.robo4j.socket.http.dto.PathAttributeDTO) PathAttributeListDTO(com.robo4j.socket.http.dto.PathAttributeListDTO) AttributeDescriptor(com.robo4j.AttributeDescriptor) HttpDecoratedRequest(com.robo4j.socket.http.message.HttpDecoratedRequest) ArrayList(java.util.ArrayList) JsonUtil(com.robo4j.socket.http.util.JsonUtil) Objects(java.util.Objects) ServerContext(com.robo4j.socket.http.units.ServerContext) List(java.util.List) UTF8_SOLIDUS(com.robo4j.util.Utf8Constant.UTF8_SOLIDUS) StatusCode(com.robo4j.socket.http.enums.StatusCode) Map(java.util.Map) RoboContext(com.robo4j.RoboContext) ClassGetSetDTO(com.robo4j.socket.http.dto.ClassGetSetDTO) RoboReference(com.robo4j.RoboReference) ReflectUtils(com.robo4j.socket.http.util.ReflectUtils) ServerPathConfig(com.robo4j.socket.http.units.ServerPathConfig) AttributeDescriptor(com.robo4j.AttributeDescriptor) PathAttributeDTO(com.robo4j.socket.http.dto.PathAttributeDTO) RoboReference(com.robo4j.RoboReference) PathAttributeListDTO(com.robo4j.socket.http.dto.PathAttributeListDTO) ArrayList(java.util.ArrayList) List(java.util.List) HttpRequestDenominator(com.robo4j.socket.http.message.HttpRequestDenominator) Map(java.util.Map)

Example 2 with ClassGetSetDTO

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);
}
Also used : Array(java.lang.reflect.Array) StringConstants(com.robo4j.util.StringConstants) UTF8_COLON(com.robo4j.util.Utf8Constant.UTF8_COLON) HashMap(java.util.HashMap) UTF8_COMMA(com.robo4j.util.Utf8Constant.UTF8_COMMA) LinkedHashMap(java.util.LinkedHashMap) Matcher(java.util.regex.Matcher) Map(java.util.Map) JsonGenericTypeAdapter(com.robo4j.socket.http.json.JsonGenericTypeAdapter) LinkedList(java.util.LinkedList) Method(java.lang.reflect.Method) JsonTypeAdapter(com.robo4j.socket.http.json.JsonTypeAdapter) UTF8_SQUARE_BRACKET_LEFT(com.robo4j.util.Utf8Constant.UTF8_SQUARE_BRACKET_LEFT) Collection(java.util.Collection) WITHOUT_QUOTATION_TYPES(com.robo4j.socket.http.util.JsonUtil.WITHOUT_QUOTATION_TYPES) Set(java.util.Set) Collectors(java.util.stream.Collectors) UTF8_CURLY_BRACKET_RIGHT(com.robo4j.util.Utf8Constant.UTF8_CURLY_BRACKET_RIGHT) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) Pattern(java.util.regex.Pattern) UTF8_SQUARE_BRACKET_RIGHT(com.robo4j.util.Utf8Constant.UTF8_SQUARE_BRACKET_RIGHT) ClassGetSetDTO(com.robo4j.socket.http.dto.ClassGetSetDTO) JsonDocument(com.robo4j.socket.http.json.JsonDocument) UTF8_CURLY_BRACKET_LEFT(com.robo4j.util.Utf8Constant.UTF8_CURLY_BRACKET_LEFT) ClassGetSetDTO(com.robo4j.socket.http.dto.ClassGetSetDTO)

Example 3 with ClassGetSetDTO

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();
}
Also used : Array(java.lang.reflect.Array) StringConstants(com.robo4j.util.StringConstants) UTF8_COLON(com.robo4j.util.Utf8Constant.UTF8_COLON) HashMap(java.util.HashMap) UTF8_COMMA(com.robo4j.util.Utf8Constant.UTF8_COMMA) LinkedHashMap(java.util.LinkedHashMap) Matcher(java.util.regex.Matcher) Map(java.util.Map) JsonGenericTypeAdapter(com.robo4j.socket.http.json.JsonGenericTypeAdapter) LinkedList(java.util.LinkedList) Method(java.lang.reflect.Method) JsonTypeAdapter(com.robo4j.socket.http.json.JsonTypeAdapter) UTF8_SQUARE_BRACKET_LEFT(com.robo4j.util.Utf8Constant.UTF8_SQUARE_BRACKET_LEFT) Collection(java.util.Collection) WITHOUT_QUOTATION_TYPES(com.robo4j.socket.http.util.JsonUtil.WITHOUT_QUOTATION_TYPES) Set(java.util.Set) Collectors(java.util.stream.Collectors) UTF8_CURLY_BRACKET_RIGHT(com.robo4j.util.Utf8Constant.UTF8_CURLY_BRACKET_RIGHT) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) Pattern(java.util.regex.Pattern) UTF8_SQUARE_BRACKET_RIGHT(com.robo4j.util.Utf8Constant.UTF8_SQUARE_BRACKET_RIGHT) ClassGetSetDTO(com.robo4j.socket.http.dto.ClassGetSetDTO) JsonDocument(com.robo4j.socket.http.json.JsonDocument) UTF8_CURLY_BRACKET_LEFT(com.robo4j.util.Utf8Constant.UTF8_CURLY_BRACKET_LEFT) LinkedList(java.util.LinkedList) List(java.util.List) JsonTypeAdapter(com.robo4j.socket.http.json.JsonTypeAdapter) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 4 with ClassGetSetDTO

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));
}
Also used : ClassGetSetDTO(com.robo4j.socket.http.dto.ClassGetSetDTO) PathAttributeDTO(com.robo4j.socket.http.dto.PathAttributeDTO) Test(org.junit.jupiter.api.Test)

Example 5 with ClassGetSetDTO

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));
    }
}
Also used : Array(java.lang.reflect.Array) StringConstants(com.robo4j.util.StringConstants) UTF8_COLON(com.robo4j.util.Utf8Constant.UTF8_COLON) HashMap(java.util.HashMap) UTF8_COMMA(com.robo4j.util.Utf8Constant.UTF8_COMMA) LinkedHashMap(java.util.LinkedHashMap) Matcher(java.util.regex.Matcher) Map(java.util.Map) JsonGenericTypeAdapter(com.robo4j.socket.http.json.JsonGenericTypeAdapter) LinkedList(java.util.LinkedList) Method(java.lang.reflect.Method) JsonTypeAdapter(com.robo4j.socket.http.json.JsonTypeAdapter) UTF8_SQUARE_BRACKET_LEFT(com.robo4j.util.Utf8Constant.UTF8_SQUARE_BRACKET_LEFT) Collection(java.util.Collection) WITHOUT_QUOTATION_TYPES(com.robo4j.socket.http.util.JsonUtil.WITHOUT_QUOTATION_TYPES) Set(java.util.Set) Collectors(java.util.stream.Collectors) UTF8_CURLY_BRACKET_RIGHT(com.robo4j.util.Utf8Constant.UTF8_CURLY_BRACKET_RIGHT) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) Pattern(java.util.regex.Pattern) UTF8_SQUARE_BRACKET_RIGHT(com.robo4j.util.Utf8Constant.UTF8_SQUARE_BRACKET_RIGHT) ClassGetSetDTO(com.robo4j.socket.http.dto.ClassGetSetDTO) JsonDocument(com.robo4j.socket.http.json.JsonDocument) UTF8_CURLY_BRACKET_LEFT(com.robo4j.util.Utf8Constant.UTF8_CURLY_BRACKET_LEFT) Objects(java.util.Objects) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) JsonDocument(com.robo4j.socket.http.json.JsonDocument)

Aggregations

ClassGetSetDTO (com.robo4j.socket.http.dto.ClassGetSetDTO)7 JsonDocument (com.robo4j.socket.http.json.JsonDocument)5 UTF8_CURLY_BRACKET_LEFT (com.robo4j.util.Utf8Constant.UTF8_CURLY_BRACKET_LEFT)5 UTF8_CURLY_BRACKET_RIGHT (com.robo4j.util.Utf8Constant.UTF8_CURLY_BRACKET_RIGHT)5 UTF8_SQUARE_BRACKET_LEFT (com.robo4j.util.Utf8Constant.UTF8_SQUARE_BRACKET_LEFT)5 UTF8_SQUARE_BRACKET_RIGHT (com.robo4j.util.Utf8Constant.UTF8_SQUARE_BRACKET_RIGHT)5 List (java.util.List)5 Map (java.util.Map)5 Objects (java.util.Objects)5 Set (java.util.Set)5 JsonGenericTypeAdapter (com.robo4j.socket.http.json.JsonGenericTypeAdapter)4 JsonTypeAdapter (com.robo4j.socket.http.json.JsonTypeAdapter)4 WITHOUT_QUOTATION_TYPES (com.robo4j.socket.http.util.JsonUtil.WITHOUT_QUOTATION_TYPES)4 StringConstants (com.robo4j.util.StringConstants)4 UTF8_COLON (com.robo4j.util.Utf8Constant.UTF8_COLON)4 UTF8_COMMA (com.robo4j.util.Utf8Constant.UTF8_COMMA)4 Array (java.lang.reflect.Array)4 Method (java.lang.reflect.Method)4 Collection (java.util.Collection)4 HashMap (java.util.HashMap)4