Search in sources :

Example 51 with QueryStringDecoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.QueryStringDecoder in project BRFS by zhangnianli.

the class NettyHttpRequestHandler method requestReceived.

public void requestReceived(ChannelHandlerContext ctx, FullHttpRequest request) {
    LOG.debug("ctx{}, handle request[{}:{}]", ctx.channel(), request.method(), request.uri());
    MessageHandler handler = methodToOps.get(request.method());
    if (handler == null) {
        LOG.error("Exception context[{}] method[{}] is unknown", ctx.toString(), request.method());
        ResponseSender.sendError(ctx, HttpResponseStatus.METHOD_NOT_ALLOWED, HttpResponseStatus.METHOD_NOT_ALLOWED.reasonPhrase());
        return;
    }
    String path = new QueryStringDecoder(request.uri(), CharsetUtil.UTF_8, true).path();
    String uri = request.uri();
    byte[] content = new byte[request.content().readableBytes()];
    request.content().readBytes(content);
    HttpMessage message = new HttpMessage() {

        @Override
        public String getPath() {
            return path;
        }

        @Override
        public Map<String, String> getParams() {
            return HttpParamsDecoder.decodeFromUri(uri);
        }

        @Override
        public byte[] getContent() {
            return content;
        }
    };
    executors.submit(new Runnable() {

        @Override
        public void run() {
            try {
                if (!handler.isValidRequest(message)) {
                    LOG.error("Exception context[{}] method[{}] invalid request message[{}]", ctx.toString(), message.getPath());
                    ResponseSender.sendError(ctx, HttpResponseStatus.BAD_REQUEST, HttpResponseStatus.BAD_REQUEST.reasonPhrase());
                    return;
                }
                handler.handle(message, new DefaultNettyHandleResultCallback(ctx));
            } catch (Exception e) {
                LOG.error("message handle error", e);
                ResponseSender.sendError(ctx, HttpResponseStatus.INTERNAL_SERVER_ERROR, e.toString());
            }
        }
    });
}
Also used : QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) MessageHandler(com.bonree.brfs.common.net.http.MessageHandler) HttpMessage(com.bonree.brfs.common.net.http.HttpMessage)

Example 52 with QueryStringDecoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.QueryStringDecoder in project moco by dreamhead.

the class ActualWebSocketServer method connectRequest.

public void connectRequest(final ChannelHandlerContext ctx, final FullHttpRequest request) {
    QueryStringDecoder decoder = new QueryStringDecoder(request.uri());
    final String actual = decoder.path();
    if (!uri.equals(actual)) {
        ctx.writeAndFlush(new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST));
        return;
    }
    WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(this.uri, null, false);
    WebSocketServerHandshaker handshaker = wsFactory.newHandshaker(request);
    Channel channel = ctx.channel();
    if (handshaker == null) {
        WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(channel);
        return;
    }
    handshaker.handshake(channel, request);
    connect(channel);
    sendConnected(channel);
}
Also used : QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) WebSocketServerHandshaker(io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker) Channel(io.netty.channel.Channel) WebSocketServerHandshakerFactory(io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory)

Example 53 with QueryStringDecoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.QueryStringDecoder in project carbon-apimgt by wso2.

the class InboundWebSocketProcessor method validateOAuthHeader.

/**
 * Validates access_token query param and reset OAuth header in the handshake request.
 *
 * @param req                   Handshake request
 * @param inboundMessageContext InboundMessageContext
 * @return if validation success
 * @throws APISecurityException if an error occurs
 */
private boolean validateOAuthHeader(FullHttpRequest req, InboundMessageContext inboundMessageContext) throws APISecurityException {
    if (!inboundMessageContext.getRequestHeaders().containsKey(HttpHeaders.AUTHORIZATION)) {
        QueryStringDecoder decoder = new QueryStringDecoder(inboundMessageContext.getFullRequestPath());
        Map<String, List<String>> requestMap = decoder.parameters();
        if (requestMap.containsKey(APIConstants.AUTHORIZATION_QUERY_PARAM_DEFAULT)) {
            inboundMessageContext.getHeadersToAdd().put(HttpHeaders.AUTHORIZATION, APIConstants.CONSUMER_KEY_SEGMENT + StringUtils.SPACE + requestMap.get(APIConstants.AUTHORIZATION_QUERY_PARAM_DEFAULT).get(0));
            InboundWebsocketProcessorUtil.removeTokenFromQuery(requestMap, inboundMessageContext);
            req.setUri(inboundMessageContext.getFullRequestPath());
        } else {
            return false;
        }
    }
    return true;
}
Also used : QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) List(java.util.List) ArrayList(java.util.ArrayList)

Example 54 with QueryStringDecoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.QueryStringDecoder in project traccar by tananaev.

the class LeafSpyProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    FullHttpRequest request = (FullHttpRequest) msg;
    QueryStringDecoder decoder = new QueryStringDecoder(request.uri());
    Map<String, List<String>> params = decoder.parameters();
    if (params.isEmpty()) {
        decoder = new QueryStringDecoder(request.content().toString(StandardCharsets.US_ASCII), false);
        params = decoder.parameters();
    }
    Position position = new Position(getProtocolName());
    position.setValid(true);
    for (Map.Entry<String, List<String>> entry : params.entrySet()) {
        for (String value : entry.getValue()) {
            switch(entry.getKey()) {
                case "pass":
                    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, value);
                    if (deviceSession == null) {
                        sendResponse(channel, HttpResponseStatus.BAD_REQUEST);
                        return null;
                    }
                    position.setDeviceId(deviceSession.getDeviceId());
                    break;
                case "Lat":
                    position.setLatitude(Double.parseDouble(value));
                    break;
                case "Long":
                    position.setLongitude(Double.parseDouble(value));
                    break;
                case "RPM":
                    position.set(Position.KEY_RPM, Integer.parseInt(value));
                    position.setSpeed(convertSpeed(Double.parseDouble(value) / 63, "kmh"));
                    break;
                case "Elv":
                    position.setAltitude(Double.parseDouble(value));
                    break;
                case "SOC":
                    position.set(Position.KEY_BATTERY_LEVEL, Double.parseDouble(value));
                    break;
                case "user":
                    position.set(Position.KEY_DRIVER_UNIQUE_ID, value);
                    break;
                case "ChrgMode":
                    position.set(Position.KEY_CHARGE, Integer.parseInt(value) != 0);
                    break;
                case "Odo":
                    position.set(Position.KEY_OBD_ODOMETER, Integer.parseInt(value) * 1000);
                    break;
                default:
                    try {
                        position.set(entry.getKey(), Double.parseDouble(value));
                    } catch (NumberFormatException e) {
                        switch(value) {
                            case "true":
                                position.set(entry.getKey(), true);
                                break;
                            case "false":
                                position.set(entry.getKey(), false);
                                break;
                            default:
                                position.set(entry.getKey(), value);
                                break;
                        }
                    }
                    break;
            }
        }
    }
    if (position.getFixTime() == null) {
        position.setTime(new Date());
    }
    if (position.getLatitude() == 0 && position.getLongitude() == 0) {
        getLastLocation(position, position.getDeviceTime());
    }
    if (position.getDeviceId() != 0) {
        sendResponse(channel, HttpResponseStatus.OK, Unpooled.copiedBuffer("\"status\":\"0\"", StandardCharsets.US_ASCII));
        return position;
    } else {
        sendResponse(channel, HttpResponseStatus.BAD_REQUEST);
        return null;
    }
}
Also used : QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) List(java.util.List) Map(java.util.Map) Date(java.util.Date)

Example 55 with QueryStringDecoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.QueryStringDecoder in project traccar by tananaev.

the class OpenGtsProtocolDecoder method decode.

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
    FullHttpRequest request = (FullHttpRequest) msg;
    QueryStringDecoder decoder = new QueryStringDecoder(request.uri());
    Map<String, List<String>> params = decoder.parameters();
    Position position = new Position(getProtocolName());
    for (Map.Entry<String, List<String>> entry : params.entrySet()) {
        String value = entry.getValue().get(0);
        switch(entry.getKey()) {
            case "id":
                DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, value);
                if (deviceSession == null) {
                    sendResponse(channel, HttpResponseStatus.BAD_REQUEST);
                    return null;
                }
                position.setDeviceId(deviceSession.getDeviceId());
                break;
            case "gprmc":
                Parser parser = new Parser(PATTERN, value);
                if (!parser.matches()) {
                    sendResponse(channel, HttpResponseStatus.BAD_REQUEST);
                    return null;
                }
                DateBuilder dateBuilder = new DateBuilder().setTime(parser.nextInt(), parser.nextInt(), parser.nextInt());
                position.setValid(parser.next().equals("A"));
                position.setLatitude(parser.nextCoordinate());
                position.setLongitude(parser.nextCoordinate());
                position.setSpeed(parser.nextDouble());
                position.setCourse(parser.nextDouble(0));
                dateBuilder.setDateReverse(parser.nextInt(), parser.nextInt(), parser.nextInt());
                position.setTime(dateBuilder.getDate());
                break;
            case "alt":
                position.setAltitude(Double.parseDouble(value));
                break;
            case "batt":
                position.set(Position.KEY_BATTERY_LEVEL, Double.parseDouble(value));
                break;
            default:
                break;
        }
    }
    if (position.getDeviceId() != 0) {
        sendResponse(channel, HttpResponseStatus.OK);
        return position;
    } else {
        sendResponse(channel, HttpResponseStatus.BAD_REQUEST);
        return null;
    }
}
Also used : QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DeviceSession(org.traccar.DeviceSession) Position(org.traccar.model.Position) DateBuilder(org.traccar.helper.DateBuilder) List(java.util.List) Map(java.util.Map) Parser(org.traccar.helper.Parser)

Aggregations

QueryStringDecoder (io.netty.handler.codec.http.QueryStringDecoder)69 List (java.util.List)29 Test (org.junit.Test)15 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)14 Map (java.util.Map)10 HashMap (java.util.HashMap)9 IOException (java.io.IOException)8 URI (java.net.URI)7 ByteBuf (io.netty.buffer.ByteBuf)6 HttpContent (io.netty.handler.codec.http.HttpContent)6 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)6 HttpPostRequestDecoder (io.netty.handler.codec.http.multipart.HttpPostRequestDecoder)6 ArrayList (java.util.ArrayList)6 HttpMethod (io.netty.handler.codec.http.HttpMethod)5 HttpRequest (io.netty.handler.codec.http.HttpRequest)5 DeviceSession (org.traccar.DeviceSession)5 Position (org.traccar.model.Position)5 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)4 Channel (io.netty.channel.Channel)3 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)3