Search in sources :

Example 1 with CoapExchange

use of org.eclipse.californium.core.server.resources.CoapExchange in project camel by apache.

the class CamelCoapResource method handleRequest.

@Override
public void handleRequest(Exchange exchange) {
    org.apache.camel.Exchange camelExchange = null;
    CoAPConsumer consumer = null;
    if (possibles != null) {
        consumers.putAll(possibles.get(0).consumers);
    }
    CoapExchange cexchange = new CoapExchange(exchange, this);
    try {
        consumer = consumers.get(exchange.getRequest().getCode().name());
        if (consumer == null) {
            consumer = consumers.get("*");
        }
        camelExchange = consumer.getEndpoint().createExchange();
        consumer.createUoW(camelExchange);
        OptionSet options = exchange.getRequest().getOptions();
        for (String s : options.getUriQuery()) {
            int i = s.indexOf('=');
            if (i == -1) {
                camelExchange.getIn().setHeader(s, "");
            } else {
                camelExchange.getIn().setHeader(s.substring(0, i), s.substring(i + 1));
            }
        }
        if (options.hasContentFormat()) {
            String mt = MediaTypeRegistry.toString(options.getContentFormat());
            camelExchange.getIn().setHeader(org.apache.camel.Exchange.CONTENT_TYPE, mt);
        }
        List<String> path = exchange.getRequest().getOptions().getUriPath();
        LinkedList<Resource> resources = new LinkedList<>();
        Resource r = this;
        while (r != null) {
            resources.push(r);
            r = r.getParent();
        }
        if (resources.getFirst().getName().isEmpty()) {
            resources.removeFirst();
        }
        int res = 0;
        while (!resources.isEmpty() && res < path.size()) {
            r = resources.removeFirst();
            if (r.getName().charAt(0) == '{' && r.getName().charAt(r.getName().length() - 1) == '}') {
                String n = r.getName().substring(1, r.getName().length() - 1);
                camelExchange.getIn().setHeader(n, path.get(res));
            }
            res++;
        }
        byte[] bytes = exchange.getCurrentRequest().getPayload();
        camelExchange.getIn().setBody(bytes);
        consumer.getProcessor().process(camelExchange);
        Message target = camelExchange.hasOut() ? camelExchange.getOut() : camelExchange.getIn();
        int format = MediaTypeRegistry.parse(target.getHeader(org.apache.camel.Exchange.CONTENT_TYPE, String.class));
        cexchange.respond(ResponseCode.CONTENT, target.getBody(byte[].class), format);
    } catch (Exception e) {
        cexchange.respond(ResponseCode.INTERNAL_SERVER_ERROR, e.getMessage());
    } finally {
        if (camelExchange != null) {
            consumer.doneUoW(camelExchange);
        }
    }
}
Also used : Message(org.apache.camel.Message) Resource(org.eclipse.californium.core.server.resources.Resource) CoapResource(org.eclipse.californium.core.CoapResource) LinkedList(java.util.LinkedList) OptionSet(org.eclipse.californium.core.coap.OptionSet) CoapExchange(org.eclipse.californium.core.server.resources.CoapExchange)

Example 2 with CoapExchange

use of org.eclipse.californium.core.server.resources.CoapExchange in project thingsboard by thingsboard.

the class CoapTransportResource method processRequest.

private Optional<SessionId> processRequest(CoapExchange exchange, MsgType type) {
    log.trace("Processing {}", exchange.advanced().getRequest());
    exchange.accept();
    Exchange advanced = exchange.advanced();
    Request request = advanced.getRequest();
    Optional<DeviceCredentialsFilter> credentials = decodeCredentials(request);
    if (!credentials.isPresent()) {
        exchange.respond(ResponseCode.BAD_REQUEST);
        return Optional.empty();
    }
    CoapSessionCtx ctx = new CoapSessionCtx(exchange, adaptor, processor, authService, timeout);
    if (!ctx.login(credentials.get())) {
        exchange.respond(ResponseCode.UNAUTHORIZED);
        return Optional.empty();
    }
    AdaptorToSessionActorMsg msg;
    try {
        switch(type) {
            case GET_ATTRIBUTES_REQUEST:
            case POST_ATTRIBUTES_REQUEST:
            case POST_TELEMETRY_REQUEST:
            case TO_DEVICE_RPC_RESPONSE:
            case TO_SERVER_RPC_REQUEST:
                ctx.setSessionType(SessionType.SYNC);
                msg = adaptor.convertToActorMsg(ctx, type, request);
                break;
            case SUBSCRIBE_ATTRIBUTES_REQUEST:
            case SUBSCRIBE_RPC_COMMANDS_REQUEST:
                ExchangeObserver systemObserver = (ExchangeObserver) observerField.get(advanced);
                advanced.setObserver(new CoapExchangeObserverProxy(systemObserver, ctx));
                ctx.setSessionType(SessionType.ASYNC);
                msg = adaptor.convertToActorMsg(ctx, type, request);
                break;
            case UNSUBSCRIBE_ATTRIBUTES_REQUEST:
            case UNSUBSCRIBE_RPC_COMMANDS_REQUEST:
                ctx.setSessionType(SessionType.ASYNC);
                msg = adaptor.convertToActorMsg(ctx, type, request);
                break;
            default:
                log.trace("[{}] Unsupported msg type: {}", ctx.getSessionId(), type);
                throw new IllegalArgumentException("Unsupported msg type: " + type);
        }
        log.trace("Processing msg: {}", msg);
        processor.process(new BasicToDeviceActorSessionMsg(ctx.getDevice(), msg));
    } catch (AdaptorException e) {
        log.debug("Failed to decode payload {}", e);
        exchange.respond(ResponseCode.BAD_REQUEST, e.getMessage());
        return Optional.empty();
    } catch (IllegalArgumentException | IllegalAccessException e) {
        log.debug("Failed to process payload {}", e);
        exchange.respond(ResponseCode.INTERNAL_SERVER_ERROR, e.getMessage());
    }
    return Optional.of(ctx.getSessionId());
}
Also used : Request(org.eclipse.californium.core.coap.Request) CoapExchangeObserverProxy(org.thingsboard.server.transport.coap.session.CoapExchangeObserverProxy) CoapExchange(org.eclipse.californium.core.server.resources.CoapExchange) Exchange(org.eclipse.californium.core.network.Exchange) DeviceCredentialsFilter(org.thingsboard.server.common.data.security.DeviceCredentialsFilter) AdaptorException(org.thingsboard.server.common.transport.adaptor.AdaptorException) CoapSessionCtx(org.thingsboard.server.transport.coap.session.CoapSessionCtx) ExchangeObserver(org.eclipse.californium.core.network.ExchangeObserver)

Aggregations

CoapExchange (org.eclipse.californium.core.server.resources.CoapExchange)2 LinkedList (java.util.LinkedList)1 Message (org.apache.camel.Message)1 CoapResource (org.eclipse.californium.core.CoapResource)1 OptionSet (org.eclipse.californium.core.coap.OptionSet)1 Request (org.eclipse.californium.core.coap.Request)1 Exchange (org.eclipse.californium.core.network.Exchange)1 ExchangeObserver (org.eclipse.californium.core.network.ExchangeObserver)1 Resource (org.eclipse.californium.core.server.resources.Resource)1 DeviceCredentialsFilter (org.thingsboard.server.common.data.security.DeviceCredentialsFilter)1 AdaptorException (org.thingsboard.server.common.transport.adaptor.AdaptorException)1 CoapExchangeObserverProxy (org.thingsboard.server.transport.coap.session.CoapExchangeObserverProxy)1 CoapSessionCtx (org.thingsboard.server.transport.coap.session.CoapSessionCtx)1