@Override public String getServerName() { return request.context().channel().localAddress().toString(); }
@Path("/display/*") @GET public void display(RakamHttpRequest request) { if (!database.isPresent()) { throw new RakamException("Custom page feature is not implemented", NOT_IMPLEMENTED); } String path = request.path().substring(21); String[] projectCustomPage = path.split("/", 3); byte[] bytes; try { InputStream file = database.get().getFile(Integer.parseInt(projectCustomPage[0]), projectCustomPage[1], projectCustomPage[2]); if (file == null) { request.response(NOT_FOUND.reasonPhrase(), NOT_FOUND).end(); } bytes = ByteStreams.toByteArray(file); } catch (IOException e) { throw Throwables.propagate(e); } HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK); response.headers().set(CONTENT_TYPE, "text/html"); HttpHeaders.setContentLength(response, bytes.length); request.context().write(response); request.context().write(Unpooled.wrappedBuffer(bytes)); ChannelFuture lastContentFuture = request.context().writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT); if (!HttpHeaders.isKeepAlive(request)) { lastContentFuture.addListener(ChannelFutureListener.CLOSE); } }
private List<Cookie> mapProperties(String project, User req, RakamHttpRequest request) { InetAddress socketAddress = ((InetSocketAddress) request.context().channel() .remoteAddress()).getAddress(); List<Cookie> cookies = null; BatchUserOperationRequest op = new BatchUserOperationRequest(req.api, ImmutableList.of(new BatchUserOperations(req.id, req.properties, null, null, null, null))); for (UserPropertyMapper mapper : mappers) { try { List<Cookie> map = mapper.map(project, op.data, new HttpRequestParams(request), socketAddress); if (map != null) { if (cookies == null) { cookies = new ArrayList<>(); } cookies.addAll(map); } } catch (Exception e) { LOGGER.error(e, "Error while mapping user properties in " + mapper.getClass().toString()); return null; } } return cookies; }
@ApiOperation(value = "Event statistics", authorizations = @Authorization(value = "read_key") ) @JsonRequest @Path("/statistics") public CompletableFuture<QueryResult> getEventStatistics(RakamHttpRequest request, @Named("project") RequestContext context, @ApiParam(value = "collections", required = false) Set<String> collections, @ApiParam(value = "dimension", required = false) String dimension, @ApiParam("startDate") LocalDate startDate, @ApiParam("endDate") LocalDate endDate, @ApiParam(value = "timezone", required = false) ZoneId timezone) { timezone = timezone == null ? ZoneOffset.UTC : timezone; CompletableFuture<QueryResult> eventStatistics = eventExplorer.getEventStatistics(context, Optional.ofNullable(collections), Optional.ofNullable(dimension), startDate, endDate, Optional.ofNullable(timezone).orElse(ZoneOffset.UTC)); request.context().channel().closeFuture() .addListener(future -> { try { eventStatistics.complete(QueryResult.empty()); } catch (Throwable e) { e.printStackTrace(); } }); return eventStatistics; }
@ApiOperation(value = "Batch operation on a single user properties", request = SingleUserBatchOperationRequest.class, response = Integer.class, authorizations = {@Authorization(value = "write_key")}) @ApiResponses(value = {@ApiResponse(code = 404, message = "User does not exist.")}) @Path("/batch") @JsonRequest public void batchSingleUserOperations(RakamHttpRequest request) { request.bodyHandler(s -> { SingleUserBatchOperationRequest req; try { req = JsonHelper.read(s, SingleUserBatchOperationRequest.class); } catch (Exception e) { returnError(request, e.getMessage(), BAD_REQUEST); return; } String project = apiKeyService.getProjectOfApiKey(req.api.apiKey, WRITE_KEY); InetAddress socketAddress = ((InetSocketAddress) request.context().channel() .remoteAddress()).getAddress(); DefaultFullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, wrappedBuffer(OK_MESSAGE)); List<Cookie> cookies = mapEvent(mapper -> mapper.map(project, req.data, new HttpRequestParams(request), socketAddress)); service.batch(project, req.data); setBrowser(request, response); if (cookies != null && !cookies.isEmpty()) { response.headers().add(SET_COOKIE, STRICT.encode(cookies)); } request.response(response).end(); }); }
@Override public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception { RakamHttpRequest rakamHttpRequest = ctx.channel().attr(CONNECTION_ATTR).get(); Channel channel = rakamHttpRequest.context().channel(); FullHttpResponse resp = (FullHttpResponse) msg;
InetAddress socketAddress = ((InetSocketAddress) request.context().channel() .remoteAddress()).getAddress();
public ChannelHandlerContext context() { return request.context(); } }
public String getRemoteAddress() { return remoteAddress == null ? ((InetSocketAddress) context().channel().remoteAddress()).getHostString() : remoteAddress; }
public void handle(RakamHttpRequest request) { String path = cleanPath(request.path()); int lastIndex = path.length() - 1; if (lastIndex > 0 && path.charAt(lastIndex) == '/') { path = path.substring(0, lastIndex); } HttpMethod method = request.getMethod(); final HttpRequestHandler handler = routes.get(new PatternBinding(method, path)); if (handler != null) { if (handler instanceof WebSocketService) { request.context().attr(PATH).set(path); } handler.handle(request); } else { for (Map.Entry<PatternBinding, HttpRequestHandler> prefixRoute : prefixRoutes) { if (method.equals(prefixRoute.getKey().method) && path.startsWith(prefixRoute.getKey().pattern)) { prefixRoute.getValue().handle(request); return; } } noMatch.handle(request); } }
@Override public void handle(RakamHttpRequest request) { WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory( getWebSocketLocation(request), null, true); handshaker = wsFactory.newHandshaker(request.getRequest()); if (handshaker == null) { WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(request.context().channel()); } else { HttpRequest request1 = request.getRequest(); DefaultFullHttpRequest defaultFullHttpRequest = new DefaultFullHttpRequest(request1.getProtocolVersion(), request1.getMethod(), request1.getUri()); defaultFullHttpRequest.headers().set(request1.headers()); handshaker.handshake(request.context().channel(), defaultFullHttpRequest); onOpen(new WebSocketRequest(request)); } }