Search in sources :

Example 1 with SetHeadersInitializer

use of net.morimekta.providence.tools.rpc.utils.SetHeadersInitializer in project providence by morimekta.

the class RPCOptions method getHandler.

public PServiceCallHandler getHandler() {
    Serializer serializer = getSerializer(format);
    URI uri = URI.create(endpoint);
    if (uri.getScheme() == null || uri.getScheme().length() == 0) {
        throw new ArgumentException("No protocol on URI: " + endpoint);
    }
    if (uri.getScheme().startsWith("thrift")) {
        if (// Must have host and port.
        (uri.getPort() < 1) || (uri.getHost() == null || uri.getHost().length() == 0) || // No path, query or fragment.
        (uri.getFragment() != null && uri.getFragment().length() > 0) || (uri.getQuery() != null && uri.getQuery().length() > 0) || (uri.getPath() != null && uri.getPath().length() > 0)) {
            throw new ArgumentException("Illegal thrift URI: " + endpoint);
        }
        InetSocketAddress address = new InetSocketAddress(uri.getHost(), uri.getPort());
        switch(uri.getScheme()) {
            case "thrift":
                return new SocketClientHandler(serializer, address, connect_timeout, read_timeout);
            case "thrift+nonblocking":
                return new NonblockingSocketClientHandler(serializer, address, connect_timeout, read_timeout);
            default:
                throw new ArgumentException("Unknown thrift protocol " + uri.getScheme());
        }
    }
    GenericUrl url = new GenericUrl(endpoint);
    Map<String, String> hdrs = new HashMap<>();
    for (String hdr : headers) {
        String[] parts = hdr.split("[:]", 2);
        if (parts.length != 2) {
            throw new ArgumentException("Invalid headers param: " + hdr);
        }
        hdrs.put(parts[0].trim(), parts[1].trim());
    }
    HttpTransport transport = new NetHttpTransport();
    HttpRequestFactory factory = transport.createRequestFactory(new SetHeadersInitializer(hdrs, connect_timeout, read_timeout));
    SerializerProvider serializerProvider = new ThriftSerializerProvider(serializer.mediaType());
    return new HttpClientHandler(() -> url, factory, serializerProvider);
}
Also used : SocketClientHandler(net.morimekta.providence.thrift.client.SocketClientHandler) NonblockingSocketClientHandler(net.morimekta.providence.thrift.client.NonblockingSocketClientHandler) NonblockingSocketClientHandler(net.morimekta.providence.thrift.client.NonblockingSocketClientHandler) HashMap(java.util.HashMap) HttpRequestFactory(com.google.api.client.http.HttpRequestFactory) InetSocketAddress(java.net.InetSocketAddress) GenericUrl(com.google.api.client.http.GenericUrl) URI(java.net.URI) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) HttpTransport(com.google.api.client.http.HttpTransport) SetHeadersInitializer(net.morimekta.providence.tools.rpc.utils.SetHeadersInitializer) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) ArgumentException(net.morimekta.console.args.ArgumentException) ThriftSerializerProvider(net.morimekta.providence.thrift.ThriftSerializerProvider) SerializerProvider(net.morimekta.providence.serializer.SerializerProvider) HttpClientHandler(net.morimekta.providence.client.HttpClientHandler) ThriftSerializerProvider(net.morimekta.providence.thrift.ThriftSerializerProvider) Serializer(net.morimekta.providence.serializer.Serializer)

Aggregations

GenericUrl (com.google.api.client.http.GenericUrl)1 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)1 HttpTransport (com.google.api.client.http.HttpTransport)1 NetHttpTransport (com.google.api.client.http.javanet.NetHttpTransport)1 InetSocketAddress (java.net.InetSocketAddress)1 URI (java.net.URI)1 HashMap (java.util.HashMap)1 ArgumentException (net.morimekta.console.args.ArgumentException)1 HttpClientHandler (net.morimekta.providence.client.HttpClientHandler)1 Serializer (net.morimekta.providence.serializer.Serializer)1 SerializerProvider (net.morimekta.providence.serializer.SerializerProvider)1 ThriftSerializerProvider (net.morimekta.providence.thrift.ThriftSerializerProvider)1 NonblockingSocketClientHandler (net.morimekta.providence.thrift.client.NonblockingSocketClientHandler)1 SocketClientHandler (net.morimekta.providence.thrift.client.SocketClientHandler)1 SetHeadersInitializer (net.morimekta.providence.tools.rpc.utils.SetHeadersInitializer)1