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

com.fullcontact.rpc.jersey.JerseyStreamObserver Maven / Gradle / Ivy

There is a newer version: 0.1.3
Show newest version
package com.fullcontact.rpc.jersey;

import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.Message;
import com.google.protobuf.util.JsonFormat;
import io.grpc.stub.StreamObserver;

import javax.ws.rs.container.AsyncResponse;
import javax.ws.rs.core.Response;

/**
 * gRPC StreamObserver which publishes to a Jersey AsyncResponse
 *
 * Currently does not handle multiple (streaming) messages
 *
 * @author Michael Rose (xorlev)
 */
public class JerseyStreamObserver implements StreamObserver {
    private static final JsonFormat.Printer PRINTER = JsonFormat.printer().includingDefaultValueFields();
    private final AsyncResponse asyncResponse;

    public JerseyStreamObserver(AsyncResponse asyncResponse) {
        this.asyncResponse = asyncResponse;
    }

    @Override
    public void onNext(V value) {
        // TODO, content-negotiated handler
        try {
            asyncResponse.resume(PRINTER.print(value));
        }
        catch(InvalidProtocolBufferException e) {
            onError(e);
        }
    }

    @Override
    public void onError(Throwable t) {
        if(t instanceof InvalidProtocolBufferException) {
            asyncResponse.resume(Response.status(Response.Status.BAD_REQUEST).entity(t.getMessage()).build());
        } else {
            asyncResponse.resume(GrpcErrorUtil.createJerseyResponse(t));
        }
    }

    @Override
    public void onCompleted() {

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy