Java Code Examples for io.reactivex.netty.protocol.http.server.HttpServerResponse#close()

The following examples show how to use io.reactivex.netty.protocol.http.server.HttpServerResponse#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RxMovieServer.java    From ribbon with Apache License 2.0 6 votes vote down vote up
private Observable<Void> handleRecommendationsByUserId(HttpServerRequest<ByteBuf> request, HttpServerResponse<ByteBuf> response) {
    System.out.println("HTTP request -> recommendations by user id request: " + request.getPath());
    final String userId = userIdFromPath(request.getPath());
    if (userId == null) {
        response.setStatus(HttpResponseStatus.BAD_REQUEST);
        return response.close();
    }
    if (!userRecommendations.containsKey(userId)) {
        response.setStatus(HttpResponseStatus.NOT_FOUND);
        return response.close();
    }

    StringBuilder builder = new StringBuilder();
    for (String movieId : userRecommendations.get(userId)) {
        System.out.println("    returning: " + movies.get(movieId));
        builder.append(movies.get(movieId)).append('\n');
    }

    ByteBuf byteBuf = UnpooledByteBufAllocator.DEFAULT.buffer();
    byteBuf.writeBytes(builder.toString().getBytes(Charset.defaultCharset()));

    response.write(byteBuf);
    return response.close();
}
 
Example 2
Source File: RxNettyHandler.java    From karyon with Apache License 2.0 6 votes vote down vote up
@Override
public Observable<Void> handle(HttpServerRequest<ByteBuf> request, HttpServerResponse<ByteBuf> response) {
    if (request.getUri().startsWith(healthCheckUri)) {
        return healthCheckEndpoint.handle(request, response);
    } else if (request.getUri().startsWith("/hello/to/")) {
        int prefixLength = "/hello/to".length();
        String userName = request.getPath().substring(prefixLength);
        if (userName.isEmpty() || userName.length() == 1 /*The uri is /hello/to/ but no name */) {
            response.setStatus(HttpResponseStatus.BAD_REQUEST);
            return response.writeStringAndFlush(
                    "{\"Error\":\"Please provide a username to say hello. The URI should be /hello/to/{username}\"}");
        } else {
            String msg = "Hello " + userName.substring(1) /*Remove the / prefix*/ + " from Netflix OSS";
            return response.writeStringAndFlush("{\"Message\":\"" + msg + "\"}");
        }
    } else if (request.getUri().startsWith("/hello")) {
        return response.writeStringAndFlush("{\"Message\":\"Hello newbee from Netflix OSS\"}");
    } else {
        response.setStatus(HttpResponseStatus.NOT_FOUND);
        return response.close();
    }
}
 
Example 3
Source File: HelloWorldEndpoint.java    From karyon with Apache License 2.0 6 votes vote down vote up
public Observable<Void> sayHelloToUser(HttpServerRequest<ByteBuf> request, HttpServerResponse<ByteBuf> response) {
    JSONObject content = new JSONObject();

    int prefixLength = "/hello/to".length();
    String userName = request.getPath().substring(prefixLength);

    try {
        if (userName.isEmpty() || userName.length() == 1 /*The uri is /hello/to/ but no name */) {
            response.setStatus(HttpResponseStatus.BAD_REQUEST);
            content.put("Error", "Please provide a username to say hello. The URI should be /hello/to/{username}");
        } else {
            content.put("Message", "Hello " + userName.substring(1) /*Remove the / prefix*/ + " from Netflix OSS");
        }
    } catch (JSONException e) {
        logger.error("Error creating json response.", e);
        return Observable.error(e);
    }

    response.write(content.toString(), StringTransformer.DEFAULT_INSTANCE);
    return response.close();

}
 
Example 4
Source File: RxMovieServer.java    From ribbon with Apache License 2.0 5 votes vote down vote up
private Observable<Void> handleUpdateRecommendationsForUser(HttpServerRequest<ByteBuf> request, final HttpServerResponse<ByteBuf> response) {
    System.out.println("HTTP request -> update recommendations for user: " + request.getPath());
    final String userId = userIdFromPath(request.getPath());
    if (userId == null) {
        response.setStatus(HttpResponseStatus.BAD_REQUEST);
        return response.close();
    }
    return request.getContent().flatMap(new Func1<ByteBuf, Observable<Void>>() {
        @Override
        public Observable<Void> call(ByteBuf byteBuf) {
            String movieId = byteBuf.toString(Charset.defaultCharset());
            System.out.println(format("    updating: {user=%s, movie=%s}", userId, movieId));
            synchronized (this) {
                Set<String> recommendations;
                if (userRecommendations.containsKey(userId)) {
                    recommendations = userRecommendations.get(userId);
                } else {
                    recommendations = new ConcurrentSet<String>();
                    userRecommendations.put(userId, recommendations);
                }
                recommendations.add(movieId);
            }
            response.setStatus(HttpResponseStatus.OK);
            return response.close();
        }
    });
}
 
Example 5
Source File: SimpleUriRouter.java    From karyon with Apache License 2.0 5 votes vote down vote up
@Override
public Observable<Void> handle(HttpServerRequest<I> request, HttpServerResponse<O> response) {
    HttpKeyEvaluationContext context = new HttpKeyEvaluationContext(response.getChannel());
    for (Route route : routes) {
        if (route.key.apply(request, context)) {
            return route.getHandler().handle(request, response);
        }
    }

    // None of the routes matched.
    response.setStatus(HttpResponseStatus.NOT_FOUND);
    return response.close();
}
 
Example 6
Source File: HelloWorldEndpoint.java    From karyon with Apache License 2.0 5 votes vote down vote up
public Observable<Void> sayHello(HttpServerResponse<ByteBuf> response) {
    JSONObject content = new JSONObject();
    try {
        content.put("Message", "Hello from Netflix OSS");
        response.write(content.toString(), StringTransformer.DEFAULT_INSTANCE);
        return response.close();
    } catch (JSONException e) {
        logger.error("Error creating json response.", e);
        return Observable.error(e);
    }
}
 
Example 7
Source File: HealthCheckHandlerTest.java    From Prana with Apache License 2.0 4 votes vote down vote up
@Override
public Observable<Void> handle(HttpServerRequest<ByteBuf> request, HttpServerResponse<ByteBuf> response) {
    response.setStatus(HttpResponseStatus.OK);
    return response.close();
}
 
Example 8
Source File: HealthCheckEndpoint.java    From karyon with Apache License 2.0 4 votes vote down vote up
@Override
public Observable<Void> handle(HttpServerRequest<ByteBuf> request, HttpServerResponse<ByteBuf> response) {
    int httpStatus = healthCheckHandler.getStatus();
    response.setStatus(HttpResponseStatus.valueOf(httpStatus));
    return response.close();
}
 
Example 9
Source File: TestRouteHello.java    From WSPerfLab with Apache License 2.0 4 votes vote down vote up
public Observable<Void> handle(HttpServerRequest<ByteBuf> request, HttpServerResponse<ByteBuf> response) {
    response.flushOnlyOnChannelReadComplete(true);
    response.getHeaders().set(HttpHeaders.Names.CONTENT_LENGTH, HELLO_WORLD_LENGTH_STR);
    response.write(response.getAllocator().buffer(HELLO_WORLD_LENGTH).writeBytes(MSG));
    return response.close();
}