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

com.uber.tchannel.messages.Serializer Maven / Gradle / Ivy

/*
 * Copyright (c) 2015 Uber Technologies, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.uber.tchannel.messages;

import com.uber.tchannel.headers.ArgScheme;
import com.uber.tchannel.headers.TransportHeaders;
import io.netty.buffer.ByteBuf;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Map;

public class Serializer {
    Map serializers;

    public Serializer(Map serializers) {
        this.serializers = serializers;
    }

    public String decodeEndpoint(RawMessage message) {
        return this.getSerializer(message).decodeEndpoint(message.getArg1());
    }

    public Map decodeHeaders(RawMessage message) {
        return this.getSerializer(message).decodeHeaders(message.getArg2());
    }

    public  T decodeBody(RawMessage message, Class bodyType) {
        return this.getSerializer(message).decodeBody(message.getArg3(), bodyType);
    }

    public ByteBuf encodeEndpoint(String method, ArgScheme argScheme) {
        return this.getSerializer(argScheme).encodeEndpoint(method);
    }

    public ByteBuf encodeHeaders(Map applicationHeaders, ArgScheme argScheme) {
        return this.getSerializer(argScheme).encodeHeaders(applicationHeaders);
    }

    public ByteBuf encodeBody(Object body, ArgScheme argScheme) {
        return this.getSerializer(argScheme).encodeBody(body);
    }

    private SerializerInterface getSerializer(RawMessage message) {
        Map transportHeaders = message.getTransportHeaders();
        ArgScheme argScheme = ArgScheme.toScheme(transportHeaders.get(TransportHeaders.ARG_SCHEME_KEY));
        return this.serializers.get(argScheme);
    }

    private SerializerInterface getSerializer(ArgScheme argScheme) {
        return this.serializers.get(argScheme);
    }

    public interface SerializerInterface {

        @NotNull String decodeEndpoint(@NotNull ByteBuf arg1);

        @NotNull Map decodeHeaders(@NotNull ByteBuf arg2);

        @Nullable  T decodeBody(@NotNull ByteBuf arg3, @NotNull Class bodyType);

        ByteBuf encodeEndpoint(String method);

        ByteBuf encodeHeaders(Map applicationHeaders);

        ByteBuf encodeBody(Object body);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy