Search in sources :

Example 1 with WebSocketFrame

use of org.jboss.netty.handler.codec.http.websocketx.WebSocketFrame in project load-balancer by RestComm.

the class HttpRequestHandler method messageReceived.

@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
    Object msg = e.getMessage();
    if (msg instanceof WebSocketFrame) {
        handleWebSocketFrame(ctx, e);
    } else if (msg instanceof HttpRequest) {
        request = (HttpRequest) e.getMessage();
        Integer apiPort = balancerRunner.balancerContext.lbConfig.getCommonConfiguration().getStatisticPort();
        if (apiPort != null) {
            try {
                URI uri = new URI(request.getUri());
                if (((InetSocketAddress) ctx.getChannel().getLocalAddress()).getPort() == apiPort) {
                    String currUriPath = uri.getPath().toLowerCase();
                    switch(currUriPath) {
                        case "/lbnoderegex":
                            if (addRegex(e))
                                writeResponse(e, HttpResponseStatus.OK, "Regex set");
                            else
                                writeResponse(e, HttpResponseStatus.BAD_REQUEST, "Regex NOT set, use : 127.0.0.1:2006/lbnoderegex?regex=(.*)(\\d+)(.*)&ip=127.0.0.1&port=5060");
                            return;
                        case "/lbloglevel":
                            changeLogLevel(e);
                            writeResponse(e, HttpResponseStatus.OK, "Log level changed");
                            return;
                        case "/lbstat":
                            writeStatisticResponse(e);
                            return;
                        case "/lbinfo":
                            writeInfoResponse(e);
                            return;
                        case "/lbstop":
                            if (!balancerRunner.balancerContext.securityRequired || (balancerRunner.balancerContext.securityRequired && checkCredentials(e))) {
                                writeResponse(e, HttpResponseStatus.LOCKED, IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream("bye.html")));
                                new GracefulShutdown(balancerRunner).start();
                            } else {
                                writeResponse(e, HttpResponseStatus.UNAUTHORIZED, "Authorization required : Incorrect login or password" + " request example : 127.0.0.1:2006/lbstop?login=daddy&password=123456");
                            }
                            return;
                        default:
                            writeResponse(e, HttpResponseStatus.INTERNAL_SERVER_ERROR, "Server error");
                            return;
                    }
                }
            } catch (URISyntaxException e1) {
            // ignore exception
            }
        }
    }
    if (balancerRunner.balancerContext.lbConfig.getHttpConfiguration().getHttpsPort() != null && (((InetSocketAddress) ctx.getChannel().getLocalAddress()).getPort() == balancerRunner.balancerContext.lbConfig.getHttpConfiguration().getHttpPort())) {
        // redirect http request send 3xx response
        sendRedirectResponse(e, request);
        return;
    }
    if (msg instanceof HttpRequest) {
        balancerRunner.balancerContext.httpRequests.incrementAndGet();
        balancerRunner.balancerContext.httpRequestsProcessedByMethod.get(request.getMethod().getName()).incrementAndGet();
    }
    balancerRunner.balancerContext.httpBytesToServer.addAndGet(request.getContent().capacity());
    String telestaxHeader = request.headers().get("TelestaxProxy");
    if (telestaxHeader != null && telestaxHeader.equalsIgnoreCase("true")) {
        balancerRunner.getLatestInvocationContext().balancerAlgorithm.proxyMessage(ctx, e);
    } else {
        handleHttpRequest(ctx, e);
    }
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) InetSocketAddress(java.net.InetSocketAddress) JsonObject(com.google.gson.JsonObject) StatisticObject(org.mobicents.tools.sip.balancer.StatisticObject) NodesInfoObject(org.mobicents.tools.sip.balancer.NodesInfoObject) TextWebSocketFrame(org.jboss.netty.handler.codec.http.websocketx.TextWebSocketFrame) WebSocketFrame(org.jboss.netty.handler.codec.http.websocketx.WebSocketFrame) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) GracefulShutdown(org.mobicents.tools.sip.balancer.GracefulShutdown)

Example 2 with WebSocketFrame

use of org.jboss.netty.handler.codec.http.websocketx.WebSocketFrame in project http-kit by http-kit.

the class WebSocketClientHandler method messageReceived.

@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
    Channel ch = ctx.getChannel();
    if (!handshaker.isHandshakeComplete()) {
        handshaker.finishHandshake(ch, (HttpResponse) e.getMessage());
        latch.countDown();
        return;
    }
    if (e.getMessage() instanceof HttpResponse) {
        HttpResponse response = (HttpResponse) e.getMessage();
        throw new Exception("Unexpected HttpResponse (status=" + response.getStatus() + ", content=" + response.getContent().toString(CharsetUtil.UTF_8) + ')');
    }
    WebSocketFrame frame = (WebSocketFrame) e.getMessage();
    if (frame != null)
        queue.offer(frame);
}
Also used : HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) WebSocketFrame(org.jboss.netty.handler.codec.http.websocketx.WebSocketFrame)

Aggregations

WebSocketFrame (org.jboss.netty.handler.codec.http.websocketx.WebSocketFrame)2 JsonObject (com.google.gson.JsonObject)1 InetSocketAddress (java.net.InetSocketAddress)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 HttpRequest (org.jboss.netty.handler.codec.http.HttpRequest)1 HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)1 TextWebSocketFrame (org.jboss.netty.handler.codec.http.websocketx.TextWebSocketFrame)1 GracefulShutdown (org.mobicents.tools.sip.balancer.GracefulShutdown)1 NodesInfoObject (org.mobicents.tools.sip.balancer.NodesInfoObject)1 StatisticObject (org.mobicents.tools.sip.balancer.StatisticObject)1