Search in sources :

Example 1 with OptionSet

use of org.eclipse.californium.core.coap.OptionSet 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 OptionSet

use of org.eclipse.californium.core.coap.OptionSet in project leshan by eclipse.

the class ObserveTest method sendNotification.

private void sendNotification(Connector connector, byte[] payload, Response firstCoapResponse, int contentFormat) {
    // create observe response
    Response response = new Response(org.eclipse.californium.core.coap.CoAP.ResponseCode.CONTENT);
    response.setType(Type.NON);
    response.setPayload(payload);
    response.setMID(firstCoapResponse.getMID() + 1);
    response.setToken(firstCoapResponse.getToken());
    OptionSet options = new OptionSet().setContentFormat(contentFormat).setObserve(firstCoapResponse.getOptions().getObserve() + 1);
    response.setOptions(options);
    EndpointContext context = new AddressEndpointContext(helper.server.getUnsecuredAddress().getAddress(), helper.server.getUnsecuredAddress().getPort());
    response.setDestinationContext(context);
    // serialize response
    UdpDataSerializer serializer = new UdpDataSerializer();
    RawData data = serializer.serializeResponse(response);
    // send it
    connector.send(data);
}
Also used : Response(org.eclipse.californium.core.coap.Response) LwM2mResponse(org.eclipse.leshan.core.response.LwM2mResponse) ObserveResponse(org.eclipse.leshan.core.response.ObserveResponse) ReadResponse(org.eclipse.leshan.core.response.ReadResponse) RawData(org.eclipse.californium.elements.RawData) AddressEndpointContext(org.eclipse.californium.elements.AddressEndpointContext) EndpointContext(org.eclipse.californium.elements.EndpointContext) AddressEndpointContext(org.eclipse.californium.elements.AddressEndpointContext) OptionSet(org.eclipse.californium.core.coap.OptionSet) UdpDataSerializer(org.eclipse.californium.core.network.serialization.UdpDataSerializer)

Aggregations

OptionSet (org.eclipse.californium.core.coap.OptionSet)2 LinkedList (java.util.LinkedList)1 Message (org.apache.camel.Message)1 CoapResource (org.eclipse.californium.core.CoapResource)1 Response (org.eclipse.californium.core.coap.Response)1 UdpDataSerializer (org.eclipse.californium.core.network.serialization.UdpDataSerializer)1 CoapExchange (org.eclipse.californium.core.server.resources.CoapExchange)1 Resource (org.eclipse.californium.core.server.resources.Resource)1 AddressEndpointContext (org.eclipse.californium.elements.AddressEndpointContext)1 EndpointContext (org.eclipse.californium.elements.EndpointContext)1 RawData (org.eclipse.californium.elements.RawData)1 LwM2mResponse (org.eclipse.leshan.core.response.LwM2mResponse)1 ObserveResponse (org.eclipse.leshan.core.response.ObserveResponse)1 ReadResponse (org.eclipse.leshan.core.response.ReadResponse)1