Example usage for io.netty.channel ChannelHandlerContext fireExceptionCaught

List of usage examples for io.netty.channel ChannelHandlerContext fireExceptionCaught

Introduction

In this page you can find the example usage for io.netty.channel ChannelHandlerContext fireExceptionCaught.

Prototype

@Override
    ChannelHandlerContext fireExceptionCaught(Throwable cause);

Source Link

Usage

From source file:org.freeswitch.esl.client.internal.AbstractEslClientHandler.java

License:Apache License

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable e) throws Exception {

    for (final CompletableFuture<EslMessage> apiCall : apiCalls) {
        apiCall.completeExceptionally(e.getCause());
    }/*from w w  w.  j  a v a 2s  . c om*/

    for (final CompletableFuture<EslEvent> backgroundJob : backgroundJobs.values()) {
        backgroundJob.completeExceptionally(e.getCause());
    }

    ctx.close();

    ctx.fireExceptionCaught(e);

}

From source file:org.jfxvnc.net.rfb.codec.handshaker.RfbClient33Decoder.java

License:Apache License

protected void decodeErrorMessage(ChannelHandlerContext ctx, ByteBuf in) {
    if (!in.isReadable()) {
        ctx.fireExceptionCaught(new ProtocolException("decode error message failed"));
        return;/*ww w.  ja va  2 s .co m*/
    }

    byte[] reason = new byte[actualReadableBytes()];
    in.readBytes(reason);
    String error = new String(reason, ASCII);
    ctx.fireExceptionCaught(new ProtocolException(error.trim()));
}

From source file:org.jfxvnc.net.rfb.codec.handshaker.RfbClient38Decoder.java

License:Apache License

private void decodeErrorMessage(ChannelHandlerContext ctx, ByteBuf in) {

    int length = in.readInt();
    if (length == 0) {
        ctx.fireExceptionCaught(new ProtocolException("decode error message failed"));
        return;/*from www . ja v a2  s  .  co m*/
    }
    byte[] text = new byte[length];
    in.readBytes(text);
    ctx.fireExceptionCaught(new ProtocolException(new String(text, ASCII)));
}

From source file:org.jfxvnc.net.rfb.codec.ProtocolHandshakeHandler.java

License:Apache License

private void handleServerVersion(final ChannelHandlerContext ctx, ProtocolVersion version) {

    logger.debug("server version: {}", version);
    if (version.isGreaterThan(config.versionProperty().get())) {
        logger.debug("set client version: {}", config.versionProperty().get());
        version = config.versionProperty().get();
    }//from  w ww.  java 2 s  .c  o  m

    RfbClientHandshakerFactory hsFactory = new RfbClientHandshakerFactory();
    handshaker = hsFactory.newRfbClientHandshaker(version);
    handshaker.handshake(ctx.channel()).addListener((future) -> {
        if (!future.isSuccess()) {
            ctx.fireExceptionCaught(future.cause());
        } else {
            ctx.pipeline().fireUserEventTriggered(ProtocolState.HANDSHAKE_STARTED);
        }

    });
}

From source file:org.jfxvnc.net.rfb.codec.ProtocolHandshakeHandler.java

License:Apache License

private void handleSecurityTypes(final ChannelHandlerContext ctx, SecurityTypesEvent msg) {

    SecurityType[] supportTypes = msg.getSecurityTypes();
    if (supportTypes.length == 0) {
        ctx.fireExceptionCaught(new ProtocolException("no security types supported"));
        return;/* ww w. j  a  v a 2  s  . c o  m*/
    }

    SecurityType userSecType = config.securityProperty().get();
    boolean isSupported = Arrays.stream(supportTypes).anyMatch(i -> i == userSecType);
    if (!isSupported) {
        ctx.fireExceptionCaught(new ProtocolException(
                String.format("Authentication: '%s' is not supported. The server supports only (%s)",
                        userSecType, Arrays.toString(supportTypes))));
        return;
    }

    if (userSecType == SecurityType.NONE) {
        logger.debug("none security type available");
        ctx.writeAndFlush(Unpooled.buffer(1).writeByte(userSecType.getType()));
        ctx.pipeline().fireUserEventTriggered(ProtocolState.SECURITY_COMPLETE);
        return;
    }

    RfbSecurityHandshakerFactory secFactory = new RfbSecurityHandshakerFactory();

    secHandshaker = secFactory.newRfbSecurityHandshaker(userSecType);
    if (secHandshaker == null) {
        ctx.fireExceptionCaught(
                new ProtocolException(String.format("Authentication: '%s' is not supported yet", userSecType)));
        return;
    }
    secHandshaker.handshake(ctx.channel(), msg.isResponse()).addListener((future) -> {
        if (future.isSuccess()) {
            ctx.pipeline().fireUserEventTriggered(ProtocolState.SECURITY_STARTED);
        } else {
            ctx.fireExceptionCaught(future.cause());
        }
    });

}

From source file:org.jfxvnc.net.rfb.codec.ProtocolHandshakeHandler.java

License:Apache License

private void handleSecurityMessage(final ChannelHandlerContext ctx, final RfbSecurityMessage msg) {
    msg.setCredentials(config);//from  ww w  . ja v a  2 s .  c o  m
    ctx.writeAndFlush(msg).addListener((future) -> {
        if (secHandshaker != null && !secHandshaker.isHandshakeComplete()) {
            secHandshaker.finishHandshake(ctx.channel(), msg);
        }
        if (!future.isSuccess()) {
            ctx.fireExceptionCaught(future.cause());
        }

    });
}

From source file:org.jfxvnc.net.rfb.codec.ProtocolHandshakeHandler.java

License:Apache License

private void handleSecurityResult(final ChannelHandlerContext ctx, final SecurityResultEvent msg) {
    if (msg.isPassed()) {
        logger.info("security passed: {}", msg);
        boolean sharedFlag = config.sharedProperty().get();
        ctx.writeAndFlush(new SharedEvent(sharedFlag)).addListener((future) -> {
            if (!future.isSuccess()) {
                ctx.fireExceptionCaught(future.cause());
            } else {
                ctx.pipeline().fireUserEventTriggered(ProtocolState.SECURITY_COMPLETE);
            }//from  www . j  a va 2 s .co  m
        });
        return;
    }
    ctx.pipeline().fireUserEventTriggered(ProtocolState.SECURITY_FAILED);
    if (msg.getThrowable() != null) {
        ctx.fireExceptionCaught(msg.getThrowable());
    }

}

From source file:org.jmqtt.broker.handler.NettyMQTTHandler.java

License:Open Source License

@Override
public void channelRead(ChannelHandlerContext ctx, Object message) {
    AbstractPacket msg = (AbstractPacket) message;
    LOG.info("Received a message of type {}", MqttUtils.msgType2String(msg.getMessageType()));
    try {//from w  w  w.j av a  2  s. c  o m
        switch (msg.getMessageType()) {
        case CONNECT:
            processor.processConnect(ctx.channel(), (ConnectPacket) msg);
            break;
        case PUBLISH:
            processor.processPublish(ctx.channel(), (PublishPacket) msg);
            break;
        case PUBACK:
            processor.processPubAck(ctx.channel(), (PubAckPacket) msg);
            break;
        case PUBREC:
            processor.processPubRec(ctx.channel(), (PubRecPacket) msg);
            break;
        case PUBREL:
            processor.processPubRel(ctx.channel(), (PubRelPacket) msg);
            break;
        case PUBCOMP:
            processor.processPubComp(ctx.channel(), (PubCompPacket) msg);
            break;
        case SUBSCRIBE:
            processor.processSubscribe(ctx.channel(), (SubscribePacket) msg);
            break;
        case UNSUBSCRIBE:
            processor.processUnsubscribe(ctx.channel(), (UnsubscribePacket) msg);
            break;
        case PINGREQ:
            PingRespPacket pingResp = new PingRespPacket();
            ctx.writeAndFlush(pingResp);
            break;
        case DISCONNECT:
            processor.processDisconnect(ctx.channel());
            break;
        }
    } catch (Exception ex) {
        LOG.error("Bad error in processing the message", ex);
        ctx.fireExceptionCaught(ex);
    }
}

From source file:org.kaaproject.kaa.server.transports.http.transport.netty.DefaultHandler.java

License:Apache License

@Override
protected void channelRead0(final ChannelHandlerContext ctx, final AbstractCommand msg) throws Exception {
    Callable<AbstractCommand> callable = msg;

    final Future<AbstractCommand> future = executor.submit(callable);

    future.addListener(new GenericFutureListener<Future<Object>>() {
        @Override//from   w  w w.  ja va  2 s.co m
        public void operationComplete(final Future<Object> future) throws Exception {
            LOG.trace("DefaultHandler().operationComplete...");
            if (future.isSuccess()) {
                ctx.writeAndFlush(future.get());
            } else {
                ctx.fireExceptionCaught(future.cause());
            }
        }
    });
}

From source file:org.nosceon.titanite.HttpServerHandler.java

License:Apache License

@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof WebSocketFrame) {
        websocketHandler.handleWebSocketFrame(ctx, (WebSocketFrame) msg);
        return;//from ww  w. j  a  v  a 2s  .  c o m
    }

    if (msg instanceof HttpRequest) {
        if (is100ContinueExpected((HttpRequest) msg)) {
            ctx.writeAndFlush(CONTINUE).addListener(future -> {
                if (!future.isSuccess()) {
                    ctx.fireExceptionCaught(future.cause());
                }
            });
        }

        this.request = (HttpRequest) msg;
        this.qsd = new QueryStringDecoder(request.getUri());
        this.routing = router.find(request.getMethod(), qsd.path());
        this.bodyParser = newBodyParser(routing, request);
        this.bodyParser.initialize(ctx, request);
    }

    if (msg instanceof HttpContent) {
        HttpContent chunk = (HttpContent) msg;

        if (bodyParser != null) {
            bodyParser.offer(chunk);
        }

        if (chunk instanceof LastHttpContent) {

            Map<String, CookieParam> cookies = Optional.ofNullable(request.headers().get(COOKIE))
                    .map(CookieDecoder::decode)
                    .map(s -> s.stream()
                            .collect(toMap(io.netty.handler.codec.http.Cookie::getName, CookieParam::new)))
                    .orElseGet(Collections::emptyMap);

            request.headers().set(HttpHeaders.Names.CONTENT_LENGTH, bodyParser.size());

            Request req = new Request(secure, Method.valueOf(request.getMethod().name()), qsd.path(),
                    new HeaderParams(request), new CookieParams(cookies), new PathParams(routing.pathParams()),
                    new QueryParams(qsd.parameters()), bodyParser.body());

            if (bodyParser.isMaximumExceeded()) {
                requestEntityTooLarge().apply(request, websocketHandler, isKeepAlive(request), req, ctx);
            } else {
                completedFuture(req).thenCompose(r -> routing.handler().apply(r)).whenComplete((resp, ex) -> {
                    releaseBodyParser();
                    Response response = resp;
                    if (ex != null) {
                        if (ex instanceof CompletionException) {
                            ex = lookupCause(ex);
                        }

                        if (ex instanceof InternalRuntimeException) {
                            ex = lookupCause(ex);
                        }

                        if (ex instanceof HttpServerException) {
                            response = ((HttpServerException) ex).getResponse();

                            if (response.status() >= 500) {
                                Titanite.LOG.error("error processing request", ex);
                            }
                        } else {
                            Titanite.LOG.error("error processing request", ex);
                            response = internalServerError();
                        }
                    }
                    response.apply(request, websocketHandler, isKeepAlive(request), req, ctx);
                });
            }

        }
    }

}