Search in sources :

Example 1 with RoboReference

use of com.robo4j.RoboReference 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 RoboReference

use of com.robo4j.RoboReference in project robo4j by Robo4J.

the class AccelerometerExample method main.

public static void main(String[] args) throws RoboBuilderException, IOException {
    RoboBuilder builder = new RoboBuilder();
    InputStream settings = Thread.currentThread().getContextClassLoader().getResourceAsStream("accelerometerexample.xml");
    if (settings == null) {
        System.out.println("Could not find the settings for the GyroExample!");
        System.exit(2);
    }
    builder.add(settings);
    builder.add(AccelerometerProcessor.class, ID_PROCESSOR);
    RoboContext ctx = builder.build();
    System.out.println("State before start:");
    System.out.println(SystemUtil.printStateReport(ctx));
    ctx.start();
    System.out.println("State after start:");
    System.out.println(SystemUtil.printStateReport(ctx));
    RoboReference<AccelerometerRequest> accelerometer = ctx.getReference("accelerometer");
    RoboReference<AccelerometerEvent> processor = ctx.getReference(ID_PROCESSOR);
    System.out.println("Press <Enter> to start!");
    System.in.read();
    accelerometer.sendMessage(new AccelerometerRequest(processor, true, (Float3D) -> true));
    System.out.println("Will report angular changes indefinitely.\nPress <Enter> to quit!");
    System.in.read();
}
Also used : SystemUtil(com.robo4j.util.SystemUtil) IOException(java.io.IOException) RoboContext(com.robo4j.RoboContext) RoboBuilder(com.robo4j.RoboBuilder) RoboBuilderException(com.robo4j.RoboBuilderException) InputStream(java.io.InputStream) RoboReference(com.robo4j.RoboReference) InputStream(java.io.InputStream) RoboBuilder(com.robo4j.RoboBuilder) RoboContext(com.robo4j.RoboContext)

Example 3 with RoboReference

use of com.robo4j.RoboReference in project robo4j by Robo4J.

the class DatagramPathUtils method updateDatagramServerContextPaths.

public static void updateDatagramServerContextPaths(final RoboContext context, final ServerContext serverContext, final Collection<HttpPathMethodDTO> paths) {
    final Map<PathHttpMethod, ServerPathConfig> resultPaths = paths.stream().map(e -> {
        RoboReference<Object> reference = context.getReference(e.getRoboUnit());
        return new ServerPathConfig(toPath(SystemPath.UNITS.getPath(), e.getRoboUnit()), reference, e.getMethod(), e.getCallbacks());
    }).collect(Collectors.toMap(e -> new PathHttpMethod(e.getPath(), null), e -> e));
    serverContext.addPaths(resultPaths);
}
Also used : ClientContext(com.robo4j.socket.http.units.ClientContext) Collection(java.util.Collection) ServerPathConfig(com.robo4j.socket.http.units.ServerPathConfig) HttpPathUtils.toPath(com.robo4j.socket.http.util.HttpPathUtils.toPath) PathHttpMethod(com.robo4j.socket.http.units.PathHttpMethod) Collectors(java.util.stream.Collectors) ServerContext(com.robo4j.socket.http.units.ServerContext) HttpPathMethodDTO(com.robo4j.socket.http.dto.HttpPathMethodDTO) Map(java.util.Map) RoboContext(com.robo4j.RoboContext) ClientPathConfig(com.robo4j.socket.http.units.ClientPathConfig) SystemPath(com.robo4j.socket.http.enums.SystemPath) RoboReference(com.robo4j.RoboReference) RoboReference(com.robo4j.RoboReference) ServerPathConfig(com.robo4j.socket.http.units.ServerPathConfig) PathHttpMethod(com.robo4j.socket.http.units.PathHttpMethod)

Example 4 with RoboReference

use of com.robo4j.RoboReference in project robo4j by Robo4J.

the class HttpPathUtils method updateHttpServerContextPaths.

public static void updateHttpServerContextPaths(final RoboContext context, final ServerContext serverContext, final Collection<HttpPathMethodDTO> paths) {
    final Map<PathHttpMethod, ServerPathConfig> resultPaths = paths.stream().map(e -> {
        RoboReference<Object> reference = context.getReference(e.getRoboUnit());
        return HttpPathUtils.toHttpPathConfig(e, reference);
    }).collect(Collectors.toMap(e -> new PathHttpMethod(e.getPath(), e.getMethod()), e -> e));
    resultPaths.put(new PathHttpMethod(Utf8Constant.UTF8_SOLIDUS, HttpMethod.GET), new ServerPathConfig(Utf8Constant.UTF8_SOLIDUS, null, HttpMethod.GET));
    serverContext.addPaths(resultPaths);
}
Also used : StringConstants(com.robo4j.util.StringConstants) JsonReader(com.robo4j.socket.http.json.JsonReader) ClientContext(com.robo4j.socket.http.units.ClientContext) Collection(java.util.Collection) ServerPathConfig(com.robo4j.socket.http.units.ServerPathConfig) Set(java.util.Set) PathHttpMethod(com.robo4j.socket.http.units.PathHttpMethod) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) ServerContext(com.robo4j.socket.http.units.ServerContext) List(java.util.List) HttpPathMethodDTO(com.robo4j.socket.http.dto.HttpPathMethodDTO) Stream(java.util.stream.Stream) Map(java.util.Map) HttpMethod(com.robo4j.socket.http.HttpMethod) RoboContext(com.robo4j.RoboContext) ClientPathConfig(com.robo4j.socket.http.units.ClientPathConfig) SystemPath(com.robo4j.socket.http.enums.SystemPath) Utf8Constant(com.robo4j.util.Utf8Constant) JsonDocument(com.robo4j.socket.http.json.JsonDocument) Collections(java.util.Collections) RoboReference(com.robo4j.RoboReference) RoboReference(com.robo4j.RoboReference) ServerPathConfig(com.robo4j.socket.http.units.ServerPathConfig) PathHttpMethod(com.robo4j.socket.http.units.PathHttpMethod)

Aggregations

RoboContext (com.robo4j.RoboContext)4 RoboReference (com.robo4j.RoboReference)4 ServerContext (com.robo4j.socket.http.units.ServerContext)3 ServerPathConfig (com.robo4j.socket.http.units.ServerPathConfig)3 Map (java.util.Map)3 HttpPathMethodDTO (com.robo4j.socket.http.dto.HttpPathMethodDTO)2 SystemPath (com.robo4j.socket.http.enums.SystemPath)2 ClientContext (com.robo4j.socket.http.units.ClientContext)2 ClientPathConfig (com.robo4j.socket.http.units.ClientPathConfig)2 PathHttpMethod (com.robo4j.socket.http.units.PathHttpMethod)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 List (java.util.List)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 AttributeDescriptor (com.robo4j.AttributeDescriptor)1 RoboBuilder (com.robo4j.RoboBuilder)1 RoboBuilderException (com.robo4j.RoboBuilderException)1 SimpleLoggingUtil (com.robo4j.logging.SimpleLoggingUtil)1 HttpMethod (com.robo4j.socket.http.HttpMethod)1