use of org.eclipse.californium.core.server.resources.Resource 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.server.resources.Resource in project camel by apache.
the class CoAPConsumer method doStart.
@Override
protected void doStart() throws Exception {
super.doStart();
String path = endpoint.getUri().getPath();
if (path.startsWith("/")) {
path = path.substring(1);
}
Resource cr = endpoint.getCoapServer().getRoot();
while (!path.isEmpty()) {
int idx = path.indexOf('/');
String part1 = path;
if (idx != -1) {
part1 = path.substring(0, idx);
path = path.substring(idx + 1);
} else {
path = "";
}
Resource child = cr.getChild(part1);
if (child == null) {
child = new CamelCoapResource(part1, this);
cr.add(child);
cr = child;
} else if (path.isEmpty()) {
((CamelCoapResource) child).addConsumer(this);
} else {
cr = child;
}
}
}
Aggregations