use of org.eclipse.californium.core.network.Exchange 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());
}
Aggregations