/** * Flush the response contents to the output channel * * @param channel Channel to be used */ private void writeResponse(Channel channel) { // Convert the response content to a ChannelBuffer. ByteBuf buf = copiedBuffer(responseContent.toString(), CharsetUtil.UTF_8); responseContent.setLength(0); // Decide whether to close the connection or not. boolean close = HttpHeaders.Values.CLOSE.equalsIgnoreCase(request.headers().get(CONNECTION)) || request.getProtocolVersion().equals(HttpVersion.HTTP_1_0) && !HttpHeaders.Values.KEEP_ALIVE.equalsIgnoreCase( request.headers().get(CONNECTION)); // Build the response object. FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, buf); response.headers().set(CONTENT_TYPE, "application/json; charset=UTF-8"); if (!close) { // There's no need to add 'Content-Length' header // if this is the last response. response.headers().set(CONTENT_LENGTH, buf.readableBytes()); } try { // Write the response. ChannelFuture future = channel.writeAndFlush(response); // Close the connection after the write operation is done if necessary. if (close) { future.addListener(ChannelFutureListener.CLOSE); } } catch (Exception e) { logger.error("{}", e); } }
/** * Returns the route URI for the current request * * @param request HttpRequest which contains the request URI of the current session * @return Returns the route * @throws URISyntaxException */ public TrackingType setTrackingType(HttpRequest request) throws URISyntaxException { URI uri = new URI(request.getUri()); if (uri.getPath().startsWith("/trackingPoint")) { return TrackingType.TRACKING_POINT; } else if (uri.getPath().startsWith("/startTracking")) { return TrackingType.START_TRACKING; } else if (uri.getPath().startsWith("/stopTracking")) { return TrackingType.STOP_TRACKING; } else { return TrackingType.UNKNOWN; } }