All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.netflix.karyon.examples.hellonoss.server.rxnetty.RxNettyHandler Maven / Gradle / Ivy

package com.netflix.karyon.examples.hellonoss.server.rxnetty;

import com.netflix.karyon.transport.http.health.HealthCheckEndpoint;
import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.reactivex.netty.protocol.http.server.HttpServerRequest;
import io.reactivex.netty.protocol.http.server.HttpServerResponse;
import io.reactivex.netty.protocol.http.server.RequestHandler;
import rx.Observable;

/**
 * @author Nitesh Kant
 */
public class RxNettyHandler implements RequestHandler {

    private final String healthCheckUri;
    private final HealthCheckEndpoint healthCheckEndpoint;

    public RxNettyHandler(String healthCheckUri, HealthCheckEndpoint healthCheckEndpoint) {
        this.healthCheckUri = healthCheckUri;
        this.healthCheckEndpoint = healthCheckEndpoint;
    }

    @Override
    public Observable handle(HttpServerRequest request, HttpServerResponse 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();
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy