Search in sources :

Example 1 with PathAttributeDTO

use of com.robo4j.socket.http.dto.PathAttributeDTO 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 PathAttributeDTO

use of com.robo4j.socket.http.dto.PathAttributeDTO 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 3 with PathAttributeDTO

use of com.robo4j.socket.http.dto.PathAttributeDTO in project robo4j by Robo4J.

the class ReflectUtilTests method pathAttributesListToJsonTest.

@Test
void pathAttributesListToJsonTest() {
    final String expectedJson = "{\"attributes\":[{\"name\":\"name\",\"value\":\"java.lang.String\"},{\"name\":\"values\",\"value\":\"java.util.HashMap\"}]}";
    PathAttributeListDTO attributes = new PathAttributeListDTO();
    attributes.addAttribute(new PathAttributeDTO("name", "java.lang.String"));
    attributes.addAttribute(new PathAttributeDTO("values", "java.util.HashMap"));
    String result = ReflectUtils.createJson(attributes);
    System.out.println("result:" + result);
    assertEquals(expectedJson, result);
}
Also used : PathAttributeListDTO(com.robo4j.socket.http.dto.PathAttributeListDTO) PathAttributeDTO(com.robo4j.socket.http.dto.PathAttributeDTO) Test(org.junit.jupiter.api.Test)

Example 4 with PathAttributeDTO

use of com.robo4j.socket.http.dto.PathAttributeDTO in project robo4j by Robo4J.

the class ReflectUtilTests method complexObjectToJsonTest.

@Test
void complexObjectToJsonTest() {
    String expectedJson = "{\"name\":\"object\",\"value\":42,\"textList\":[\"one\",\"two\"],\"dictionary\":{\"one\":\"one1\",\"two\":\"two2\"},\"attributes\":{\"one\":{\"name\":\"name\",\"value\":\"test name\"},\"two\":{\"name\":\"value\",\"value\":\"42\"}}}";
    TestListMapValues obj = new TestListMapValues();
    obj.setName("object");
    obj.setValue(42);
    obj.setTextList(Arrays.asList("one", "two"));
    Map<String, String> textMap = new HashMap<>();
    textMap.put("one", "one1");
    textMap.put("two", "two2");
    obj.setDictionary(textMap);
    Map<String, PathAttributeDTO> attributes = new HashMap<>();
    attributes.put("one", new PathAttributeDTO("name", "test name"));
    attributes.put("two", new PathAttributeDTO("value", "42"));
    obj.setAttributes(attributes);
    String result = ReflectUtils.createJson(obj);
    System.out.println("result:" + result);
    assertEquals(expectedJson, result);
}
Also used : HashMap(java.util.HashMap) PathAttributeDTO(com.robo4j.socket.http.dto.PathAttributeDTO) Test(org.junit.jupiter.api.Test)

Example 5 with PathAttributeDTO

use of com.robo4j.socket.http.dto.PathAttributeDTO in project robo4j by Robo4J.

the class ReflectUtilTests method createJsonByObjectListCollectionTest.

@Test
void createJsonByObjectListCollectionTest() {
    Throwable exception = assertThrows(RoboReflectException.class, () -> {
        List<PathAttributeDTO> list = new ArrayList<>();
        list.add(new PathAttributeDTO("one", "1"));
        list.add(new PathAttributeDTO("two", "2"));
        ReflectUtils.createJson(list);
    });
    assertTrue(exception.getMessage().startsWith("object getter value"));
}
Also used : ArrayList(java.util.ArrayList) PathAttributeDTO(com.robo4j.socket.http.dto.PathAttributeDTO) Test(org.junit.jupiter.api.Test)

Aggregations

PathAttributeDTO (com.robo4j.socket.http.dto.PathAttributeDTO)6 Test (org.junit.jupiter.api.Test)5 ClassGetSetDTO (com.robo4j.socket.http.dto.ClassGetSetDTO)2 PathAttributeListDTO (com.robo4j.socket.http.dto.PathAttributeListDTO)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 AttributeDescriptor (com.robo4j.AttributeDescriptor)1 RoboContext (com.robo4j.RoboContext)1 RoboReference (com.robo4j.RoboReference)1 SimpleLoggingUtil (com.robo4j.logging.SimpleLoggingUtil)1 StatusCode (com.robo4j.socket.http.enums.StatusCode)1 HttpDecoratedRequest (com.robo4j.socket.http.message.HttpDecoratedRequest)1 HttpRequestDenominator (com.robo4j.socket.http.message.HttpRequestDenominator)1 ServerContext (com.robo4j.socket.http.units.ServerContext)1 ServerPathConfig (com.robo4j.socket.http.units.ServerPathConfig)1 HttpPathUtils (com.robo4j.socket.http.util.HttpPathUtils)1 JsonUtil (com.robo4j.socket.http.util.JsonUtil)1 ReflectUtils (com.robo4j.socket.http.util.ReflectUtils)1 UTF8_SOLIDUS (com.robo4j.util.Utf8Constant.UTF8_SOLIDUS)1 List (java.util.List)1