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

The following examples show how to use io.reactivex.netty.protocol.http.server.HttpServerResponse#writeStringAndFlush() . 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: 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 2
Source File: HystrixMetricsStreamHandler.java    From ReactiveLab with Apache License 2.0 5 votes vote down vote up
public HystrixMetricsStreamHandler(String hystrixPrefix, long intervalInMillis) {
    this.hystrixPrefix = hystrixPrefix;
    this.interval = intervalInMillis;
    this.appHandler = (HttpServerRequest<I> request, HttpServerResponse<O> response) -> {
        return response.writeStringAndFlush("Only supported path is /" + hystrixPrefix);
    };
}
 
Example 3
Source File: StartMockService.java    From WSPerfLab with Apache License 2.0 5 votes vote down vote up
private static Observable<Void> handleRequest(HttpServerRequest<ByteBuf> request, HttpServerResponse<ByteBuf> response) {

        if (request.getUri().startsWith("/hello")) {
            return response.writeStringAndFlush("Hello world!");
        }

        List<String> _id = request.getQueryParameters().get("id");
        if (_id == null || _id.size() != 1) {
            return writeError(request, response,
                              "Please provide a numerical 'id' value. It can be a random number (uuid). Received => "
                              + _id);
        }
        long id = Long.parseLong(String.valueOf(_id.get(0)));

        int delay = getParameter(request, "delay", 50); // default to 50ms server-side delay
        int itemSize = getParameter(request, "itemSize", 128); // default to 128 bytes item size (assuming ascii text)
        int numItems = getParameter(request, "numItems", 10); // default to 10 items in a list

        // no more than 100 items
        if (numItems < 1 || numItems > 100) {
            return writeError(request, response, "Please choose a 'numItems' value from 1 to 100.");
        }

        // no larger than 50KB per item
        if (itemSize < 1 || itemSize > 1024 * 50) {
            return writeError(request, response, "Please choose an 'itemSize' value from 1 to 1024*50 (50KB).");
        }

        // no larger than 60 second delay
        if (delay < 0 || delay > 60000) {
            return writeError(request, response, "Please choose a 'delay' value from 0 to 60000 (60 seconds).");
        }

        response.setStatus(HttpResponseStatus.OK);
        return MockResponse.generateJson(id, delay, itemSize, numItems)
                           .doOnNext(json -> counter.add(CounterEvent.BYTES, json.readableBytes()))
                           .flatMap(response::writeAndFlush)
                           .doOnTerminate(response::close);
    }
 
Example 4
Source File: AbstractMiddleTierService.java    From ReactiveLab with Apache License 2.0 4 votes vote down vote up
protected static Observable<Void> writeError(HttpServerRequest<?> request, HttpServerResponse<?> response, String message) {
    System.err.println("Server => Error [" + request.getPath() + "] => " + message);
    response.setStatus(HttpResponseStatus.BAD_REQUEST);
    return response.writeStringAndFlush("Error 500: " + message + "\n");
}
 
Example 5
Source File: TestRouteWithSimpleFaultTolerance.java    From ReactiveLab with Apache License 2.0 4 votes vote down vote up
private static Observable<Void> writeError(HttpServerRequest<?> request, HttpServerResponse<?> response, String message) {
    System.err.println("Server => Error [" + request.getPath() + "] => " + message);
    response.setStatus(HttpResponseStatus.BAD_REQUEST);
    return response.writeStringAndFlush("Error 500: " + message + "\n");
}
 
Example 6
Source File: TestRouteBasic.java    From ReactiveLab with Apache License 2.0 4 votes vote down vote up
private static Observable<Void> writeError(HttpServerRequest<?> request, HttpServerResponse<?> response, String message) {
    System.err.println("Server => Error [" + request.getPath() + "] => " + message);
    response.setStatus(HttpResponseStatus.BAD_REQUEST);
    return response.writeStringAndFlush("Error 500: " + message + "\n");
}
 
Example 7
Source File: TestRouteWithHystrix.java    From ReactiveLab with Apache License 2.0 4 votes vote down vote up
private static Observable<Void> writeError(HttpServerRequest<?> request, HttpServerResponse<?> response, String message) {
    System.err.println("Server => Error [" + request.getPath() + "] => " + message);
    response.setStatus(HttpResponseStatus.BAD_REQUEST);
    return response.writeStringAndFlush("Error 500: " + message + "\n");
}
 
Example 8
Source File: StartGatewayServer.java    From ReactiveLab with Apache License 2.0 4 votes vote down vote up
public static Observable<Void> writeError(HttpServerRequest<?> request, HttpServerResponse<ByteBuf> response, String message) {
    System.err.println("Server => Error [" + request.getPath() + "] => " + message);
    response.setStatus(HttpResponseStatus.BAD_REQUEST);
    return response.writeStringAndFlush("Error 500: " + message);
}
 
Example 9
Source File: TestRouteBasic.java    From WSPerfLab with Apache License 2.0 4 votes vote down vote up
private static Observable<Void> writeError(HttpServerRequest<?> request, HttpServerResponse<?> response, String message) {
    System.err.println("Server => Error [" + request.getPath() + "] => " + message);
    response.setStatus(HttpResponseStatus.BAD_REQUEST);
    return response.writeStringAndFlush("Error 500: " + message + "\n");
}
 
Example 10
Source File: StartMockService.java    From WSPerfLab with Apache License 2.0 4 votes vote down vote up
protected static Observable<Void> writeError(HttpServerRequest<?> request, HttpServerResponse<?> response, String message) {
    System.err.println("Server => Error [" + request.getPath() + "] => " + message);
    response.setStatus(HttpResponseStatus.BAD_REQUEST);
    counter.increment(CounterEvent.HTTP_ERROR);
    return response.writeStringAndFlush("Error 500: " + message + '\n');
}