Example usage for io.netty.handler.codec.http QueryStringDecoder QueryStringDecoder

List of usage examples for io.netty.handler.codec.http QueryStringDecoder QueryStringDecoder

Introduction

In this page you can find the example usage for io.netty.handler.codec.http QueryStringDecoder QueryStringDecoder.

Prototype

public QueryStringDecoder(URI uri, Charset charset, int maxParams, boolean semicolonIsNormalChar) 

Source Link

Document

Creates a new decoder that decodes the specified URI encoded in the specified charset.

Usage

From source file:de.cubeisland.engine.core.webapi.HttpRequestHandler.java

License:Open Source License

@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();/*from   www  .  j  ava 2s  . c  o m*/
    }

    if (message.getDecoderResult().isFailure()) {
        this.error(ctx, 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(),
            core.getCommandManager().getProviderManager());
    User authUser = null;
    if (!authorized) {
        if (!core.getModuleManager().getServiceManager().isImplemented(Permission.class)) {
            this.error(ctx, AUTHENTICATION_FAILURE, new ApiRequestException("Authentication deactivated", 200));
            return;
        }
        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;
        }
        User exactUser = core.getUserManager().findExactUser(user);
        if (exactUser == null || !exactUser.isPasswordSet()
                || !CubeEngine.getUserManager().checkPassword(exactUser, pass)) {
            this.error(ctx, AUTHENTICATION_FAILURE,
                    new ApiRequestException("Could not complete authentication", 200));
            return;
        }
        authUser = exactUser;
    }
    String path = qsDecoder.path().trim();
    if (path.length() == 0 || "/".equals(path)) {
        this.error(ctx, 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(core, 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);
}

From source file:de.cubeisland.engine.core.webapi.WebSocketRequestHandler.java

License:Open Source License

private void handleTextWebSocketFrame(final ChannelHandlerContext ctx, TextWebSocketFrame frame) {
    // TODO log exceptions!!!
    JsonNode jsonNode;/*from  w  ww .j  av a2  s  .  com*/
    try {
        jsonNode = objectMapper.readTree(frame.text());
    } catch (IOException e) {
        this.log.info("the frame data was no valid json!");
        return;
    }
    JsonNode action = jsonNode.get("action");
    JsonNode msgid = jsonNode.get("msgid");
    ObjectNode responseNode = objectMapper.createObjectNode();
    if (action == null) {
        responseNode.put("response", "No action");
    } else {
        JsonNode data = jsonNode.get("data");
        switch (action.asText()) {
        case "http":
            QueryStringDecoder qsDecoder = new QueryStringDecoder(normalizePath(data.get("uri").asText()),
                    this.UTF8, true, 100);

            JsonNode reqMethod = data.get("method");
            RequestMethod method = reqMethod != null ? getByName(reqMethod.asText()) : GET;
            JsonNode reqdata = data.get("body");
            ApiHandler handler = this.server.getApiHandler(normalizePath(qsDecoder.path()));
            if (handler == null) {
                responseNode.put("response", "Unknown route");
                break;
            }
            Parameters params = new Parameters(qsDecoder.parameters(),
                    core.getCommandManager().getProviderManager());
            ApiRequest request = new ApiRequest((InetSocketAddress) ctx.channel().remoteAddress(), method,
                    params, EMPTY_HEADERS, reqdata, authUser);
            ApiResponse response = handler.execute(request);
            if (msgid != null) {
                responseNode.set("response", objectMapper.valueToTree(response.getContent()));
            }
            break;
        case "subscribe":
            this.server.subscribe(data.asText().trim(), this);
            break;
        case "unsubscribe":
            this.server.unsubscribe(data.asText().trim(), this);
            break;
        default:
            responseNode.put("response", action.asText() + " -- " + data.asText());
        }

    }
    if (msgid != null && responseNode.elements().hasNext()) {
        responseNode.put("msgid", msgid);
        ctx.writeAndFlush(responseNode);
    }
}

From source file:io.soliton.protobuf.json.JsonRpcServerHandler.java

License:Apache License

/**
 * Determines whether the response to the request should be pretty-printed.
 *
 * @param request the HTTP request./*from w w  w . ja va 2 s. com*/
 * @return {@code true} if the response should be pretty-printed.
 */
private boolean shouldPrettyPrint(HttpRequest request) {
    QueryStringDecoder decoder = new QueryStringDecoder(request.getUri(), Charsets.UTF_8, true, 2);
    Map<String, List<String>> parameters = decoder.parameters();
    if (parameters.containsKey(PP_PARAMETER)) {
        return parseBoolean(parameters.get(PP_PARAMETER).get(0));
    } else if (parameters.containsKey(PRETTY_PRINT_PARAMETER)) {
        return parseBoolean(parameters.get(PRETTY_PRINT_PARAMETER).get(0));
    }
    return true;
}