use of org.xnio.streams.ChannelInputStream in project divolte-collector by divolte.
the class MappingTestServer method handleEvent.
private void handleEvent(final HttpServerExchange exchange) throws Exception {
try (final ChannelInputStream cis = new ChannelInputStream(exchange.getRequestChannel())) {
final JsonNode payload = EVENT_PARAMETERS_READER.readTree(cis);
final String generatedPageViewId = DivolteIdentifier.generate().value;
final DivolteEvent.BrowserEventData browserEventData = new DivolteEvent.BrowserEventData(get(payload, "page_view_id", String.class).orElse(generatedPageViewId), get(payload, "location", String.class), get(payload, "referer", String.class), get(payload, "viewport_pixel_width", Integer.class), get(payload, "viewport_pixel_height", Integer.class), get(payload, "screen_pixel_width", Integer.class), get(payload, "screen_pixel_height", Integer.class), get(payload, "device_pixel_ratio", Integer.class));
final Instant now = Instant.now();
final DivolteEvent divolteEvent = DivolteEvent.createBrowserEvent(exchange, get(payload, "corrupt", Boolean.class).orElse(false), get(payload, "party_id", String.class).flatMap(DivolteIdentifier::tryParse).orElse(DivolteIdentifier.generate()), get(payload, "session_id", String.class).flatMap(DivolteIdentifier::tryParse).orElse(DivolteIdentifier.generate()), get(payload, "event_id", String.class).orElse(generatedPageViewId + "0"), now, now, get(payload, "new_party_id", Boolean.class).orElse(false), get(payload, "first_in_session", Boolean.class).orElse(false), get(payload, "event_type", String.class), () -> get(payload, "parameters", JsonNode.class), browserEventData);
get(payload, "remote_host", String.class).ifPresent(ip -> {
try {
final InetAddress address = InetAddress.getByName(ip);
// We have no way of knowing the port
exchange.setSourceAddress(new InetSocketAddress(address, 0));
} catch (final UnknownHostException e) {
log.warn("Could not parse remote host: " + ip, e);
}
});
exchange.putAttachment(DUPLICATE_EVENT_KEY, get(payload, "duplicate", Boolean.class).orElse(false));
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "application/json");
exchange.getResponseChannel().write(ByteBuffer.wrap(mapper.newRecordFromExchange(divolteEvent).toString().getBytes(StandardCharsets.UTF_8)));
exchange.endExchange();
}
}
use of org.xnio.streams.ChannelInputStream in project iri by iotaledger.
the class API method processRequest.
private void processRequest(final HttpServerExchange exchange) throws IOException {
final ChannelInputStream cis = new ChannelInputStream(exchange.getRequestChannel());
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "application/json");
final long beginningTime = System.currentTimeMillis();
final String body = IOUtils.toString(cis, StandardCharsets.UTF_8);
final AbstractResponse response;
if (!exchange.getRequestHeaders().contains("X-IOTA-API-Version")) {
response = ErrorResponse.create("Invalid API Version");
} else if (body.length() > maxBodyLength) {
response = ErrorResponse.create("Request too long");
} else {
response = process(body, exchange.getSourceAddress());
}
sendResponse(exchange, response, beginningTime);
}
Aggregations