use of org.cubeengine.module.apiserver.exception.ApiRequestException in project modules-extra by CubeEngine.
the class HttpRequestHandler method channelRead0.
@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest message) throws Exception {
InetSocketAddress inetSocketAddress = (InetSocketAddress) ctx.channel().remoteAddress();
this.log.info("{} connected...", inetSocketAddress.getAddress().getHostAddress());
if (!this.server.isAddressAccepted(inetSocketAddress.getAddress())) {
this.log.info("Access denied!");
ctx.channel().close();
}
if (message.getDecoderResult().isFailure()) {
this.error(ctx, RequestStatus.UNKNOWN_ERROR);
this.log.info(message.getDecoderResult().cause(), "The decoder failed on this request...");
return;
}
boolean authorized = this.server.isAuthorized(inetSocketAddress.getAddress());
QueryStringDecoder qsDecoder = new QueryStringDecoder(message.getUri(), this.UTF8, true, 100);
final Parameters params = new Parameters(qsDecoder.parameters(), cm.getProviders());
User authUser = null;
if (!authorized) {
String user = params.get("user", String.class);
String pass = params.get("pass", String.class);
if (user == null || pass == null) {
this.error(ctx, AUTHENTICATION_FAILURE, new ApiRequestException("Could not complete authentication", 200));
return;
}
Optional<User> byName = Sponge.getServiceManager().provide(UserStorageService.class).get().get(user);
if (!byName.isPresent()) {
this.error(ctx, AUTHENTICATION_FAILURE, new ApiRequestException("Could not complete authentication", 200));
return;
}
UUID id = byName.get().getUniqueId();
// TODO make properly async
CompletableFuture<Boolean> cf = am.isPasswordSet(id).thenCompose(isSet -> am.checkPassword(id, pass).thenApply(correctPassword -> !isSet || !correctPassword));
Boolean authFailed = cf.get();
if (authFailed) {
this.error(ctx, AUTHENTICATION_FAILURE, new ApiRequestException("Could not complete authentication", 200));
return;
}
authUser = byName.get();
}
String path = qsDecoder.path().trim();
if (path.length() == 0 || "/".equals(path)) {
this.error(ctx, RequestStatus.ROUTE_NOT_FOUND);
return;
}
path = normalizePath(path);
// is this request intended to initialize a websockets connection?
if (WEBSOCKET_ROUTE.equals(path)) {
WebSocketRequestHandler handler;
if (!(ctx.pipeline().last() instanceof WebSocketRequestHandler)) {
handler = new WebSocketRequestHandler(cm, server, objectMapper, authUser);
ctx.pipeline().addLast("wsEncoder", new TextWebSocketFrameEncoder(objectMapper));
ctx.pipeline().addLast("handler", handler);
} else {
handler = (WebSocketRequestHandler) ctx.pipeline().last();
}
this.log.info("received a websocket request...");
handler.doHandshake(ctx, message);
return;
}
this.handleHttpRequest(ctx, message, path, params, authUser);
}
use of org.cubeengine.module.apiserver.exception.ApiRequestException in project modules-extra by CubeEngine.
the class HttpRequestHandler method handleHttpRequest.
private void handleHttpRequest(ChannelHandlerContext context, FullHttpRequest message, String path, Parameters params, User authUser) {
ApiHandler handler = this.server.getApiHandler(path);
if (handler == null) {
this.error(context, RequestStatus.ROUTE_NOT_FOUND);
return;
}
JsonNode data = null;
ByteBuf requestContent = message.content();
if (!requestContent.equals(EMPTY_BUFFER)) {
try {
byte[] bytes = new byte[requestContent.readableBytes()];
requestContent.readBytes(bytes);
data = this.objectMapper.readTree(bytes);
} catch (Exception ex) {
this.log.debug(ex, "Failed to parse the request body!");
this.error(context, RequestStatus.MALFORMED_DATA);
return;
}
}
final RequestMethod method = RequestMethod.getByName(message.getMethod().name());
ApiRequest apiRequest = new ApiRequest((InetSocketAddress) context.channel().remoteAddress(), ((InetSocketAddress) context.channel().localAddress()), method, params, message.headers(), data, authUser);
try {
this.success(context, handler.execute(apiRequest));
} catch (ApiRequestException e) {
this.error(context, RequestStatus.REQUEST_EXCEPTION, e);
} catch (Throwable t) {
this.error(context, RequestStatus.UNKNOWN_ERROR);
}
}
Aggregations