Search in sources :

Example 1 with Request

use of io.s4.message.Request in project core by s4.

the class ObjectBuilder method main.

public static void main(String[] argv) throws Exception {
    ObjectBuilder b = new ObjectBuilder();
    String s = "{a:5, b:100}";
    Object out = b.fromJson(s, TEST.class);
    System.out.println(out.toString());
    TEST t = new TEST(1, 2);
    System.out.println(b.toJson(t));
    String[] query = { "name", "count", "freq" };
    String[] target = { "ACDW", "11" };
    io.s4.message.Request.ClientRInfo rinfo = new io.s4.message.Request.ClientRInfo();
    rinfo.setRequesterUUID(UUID.randomUUID());
    Request req = new io.s4.message.SinglePERequest(Arrays.asList(target), Arrays.asList(query), rinfo);
    System.out.println(req.toString());
    InstanceCreator<io.s4.message.Request.RInfo> infoCreator = new InstanceCreator<io.s4.message.Request.RInfo>() {

        public io.s4.message.Request.RInfo createInstance(Type type) {
            return new io.s4.message.Request.ClientRInfo();
        }
    };
    Gson gson = (new GsonBuilder()).registerTypeAdapter(io.s4.message.Request.RInfo.class, infoCreator).registerTypeAdapter(Object.class, new ObjectTypeAdapter()).create();
    System.out.println("gson: " + gson.toJson(req));
    System.out.println("gson reversed: " + gson.fromJson(gson.toJson(req), SinglePERequest.class));
    System.out.println(b.toJson(req));
    System.out.println(b.toJson(Arrays.asList(query)));
    System.out.println("----------------------------------------------");
    ArrayList<SSTest> list = new ArrayList<SSTest>();
    SSTest ss1 = new SSTest();
    ss1.str = "list-element-1";
    SSTest ss2 = new SSTest();
    ss2.str = "list-element-2";
    list.add(ss1);
    list.add(ss2);
    Map<String, Object> listmap = new HashMap<String, Object>();
    listmap.put("ll", list);
    MapTest mt = new MapTest();
    mt.map = listmap;
    Object listmapobj = listmap;
    System.out.println("list: " + gson.toJson(list));
    System.out.println("listmap: " + gson.toJson(listmap));
    System.out.println("listmapobj: " + gson.toJson(listmapobj));
    System.out.println("mapobject: " + gson.toJson(mt));
}
Also used : SinglePERequest(io.s4.message.SinglePERequest) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) GsonBuilder(com.google.gson.GsonBuilder) SinglePERequest(io.s4.message.SinglePERequest) Request(io.s4.message.Request) Type(java.lang.reflect.Type) InstanceCreator(com.google.gson.InstanceCreator) JsonObject(com.google.gson.JsonObject)

Example 2 with Request

use of io.s4.message.Request in project core by s4.

the class Response method partition.

public List<CompoundKeyInfo> partition(int partCount) {
    // partition id is available from the request info object
    int p = this.getRInfo().getPartition();
    List<CompoundKeyInfo> partitionInfoList = null;
    if (p >= 0 && p < partCount) {
        CompoundKeyInfo partitionInfo = new CompoundKeyInfo();
        partitionInfo.setPartitionId(p);
        partitionInfoList = new ArrayList<CompoundKeyInfo>();
        partitionInfoList.add(partitionInfo);
    }
    return partitionInfoList;
}
Also used : CompoundKeyInfo(io.s4.dispatcher.partitioner.CompoundKeyInfo)

Example 3 with Request

use of io.s4.message.Request in project rest.li by linkedin.

the class SslRequestHandler method write.

/**
   * Override this method to set the handlers for SSL connection the first time this channel
   * is used to make a request. Once used, the scheme of the request on this channel cannot be changed.
   */
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    if (msg instanceof Request) {
        Request request = (Request) msg;
        URI uri = request.getURI();
        String scheme = uri.getScheme();
        if (_firstTimeScheme == null) {
            // handler to the channel pipeline
            if (scheme.equalsIgnoreCase(HTTPS_SCHEME)) {
                if (_sslHandler == null) {
                    throw new IllegalStateException("The client hasn't been configured with SSLContext " + "- cannot make an https request to " + uri);
                }
                /** Note: {@link SslHandler} will initiate a handshake upon being added to the pipeline. */
                ctx.pipeline().addFirst(SSL_HANDLER, _sslHandler);
            }
            _firstTimeScheme = scheme;
        } else if (!scheme.equalsIgnoreCase(_firstTimeScheme)) {
            throw new IllegalStateException(String.format("Cannot switch scheme from %s to %s for %s", _firstTimeScheme, scheme, ctx.channel().remoteAddress()));
        }
    }
    ctx.write(msg, promise);
}
Also used : Request(com.linkedin.r2.message.Request) URI(java.net.URI)

Example 4 with Request

use of io.s4.message.Request in project rest.li by linkedin.

the class Http2InitializerHandler method write.

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    if (!(msg instanceof RequestWithCallback)) {
        ctx.write(msg, promise);
        return;
    }
    if (_setupComplete) {
        ctx.write(msg);
    } else {
        Request request = ((RequestWithCallback) msg).request();
        URI uri = request.getURI();
        String scheme = uri.getScheme();
        if (scheme.equalsIgnoreCase(HttpScheme.HTTPS.toString())) {
            configureHttpsPipeline(ctx);
        } else if (scheme.equalsIgnoreCase(HttpScheme.HTTP.toString())) {
            configureHttpPipeline(ctx, request);
        } else {
            throw new IllegalArgumentException("Invalid scheme " + scheme + ", expected either http or https");
        }
        ctx.write(msg);
    }
}
Also used : RequestWithCallback(com.linkedin.r2.transport.common.bridge.common.RequestWithCallback) Request(com.linkedin.r2.message.Request) URI(java.net.URI)

Example 5 with Request

use of io.s4.message.Request in project rest.li by linkedin.

the class Http2SchemeHandler method write.

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    if (!(msg instanceof RequestWithCallback)) {
        ctx.write(msg, promise);
        return;
    }
    Request request = ((RequestWithCallback) msg).request();
    URI uri = request.getURI();
    String scheme = uri.getScheme();
    if (!scheme.equalsIgnoreCase(_scheme)) {
        // Specified scheme does not match the existing scheme for the pipeline. Returns channel back to the pool
        // and throws exception to the caller.
        ((RequestWithCallback) msg).handle().release();
        throw new IllegalStateException(String.format("Cannot switch scheme from %s to %s for %s", _scheme, scheme, ctx.channel().remoteAddress()));
    }
    ctx.write(msg, promise);
}
Also used : RequestWithCallback(com.linkedin.r2.transport.common.bridge.common.RequestWithCallback) Request(com.linkedin.r2.message.Request) URI(java.net.URI)

Aggregations

Request (com.linkedin.r2.message.Request)4 RequestWithCallback (com.linkedin.r2.transport.common.bridge.common.RequestWithCallback)3 URI (java.net.URI)3 CompoundKeyInfo (io.s4.dispatcher.partitioner.CompoundKeyInfo)2 ArrayList (java.util.ArrayList)2 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 InstanceCreator (com.google.gson.InstanceCreator)1 JsonObject (com.google.gson.JsonObject)1 RestRequest (com.linkedin.r2.message.rest.RestRequest)1 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)1 ByteBuf (io.netty.buffer.ByteBuf)1 ChannelPromise (io.netty.channel.ChannelPromise)1 Http2Connection (io.netty.handler.codec.http2.Http2Connection)1 Http2ConnectionEncoder (io.netty.handler.codec.http2.Http2ConnectionEncoder)1 Http2Headers (io.netty.handler.codec.http2.Http2Headers)1 PromiseCombiner (io.netty.util.concurrent.PromiseCombiner)1 KeyInfo (io.s4.dispatcher.partitioner.KeyInfo)1 Request (io.s4.message.Request)1 SinglePERequest (io.s4.message.SinglePERequest)1