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);
}
}
}
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);
}
Aggregations