Package com.facebook.swift.headerexample

Source Code of com.facebook.swift.headerexample.HeaderUsageExampleHandler

package com.facebook.swift.headerexample;

import com.facebook.nifty.core.RequestContext;
import com.facebook.nifty.core.RequestContexts;
import com.facebook.nifty.header.transport.THeaderTransport;
import com.facebook.swift.service.ThriftMethod;
import com.facebook.swift.service.ThriftService;
import org.apache.thrift.transport.TTransport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@ThriftService
public class HeaderUsageExampleHandler
{
    private static final Logger LOG = LoggerFactory.getLogger(HeaderUsageExampleHandler.class);

    @ThriftMethod
    public void headerUsageExampleMethod() {
        RequestContext requestContext = RequestContexts.getCurrentContext();

        // Display headers sent by client
        TTransport inputTransport = requestContext.getInputProtocol().getTransport();
        if (inputTransport instanceof THeaderTransport) {
            LOG.info("headers received from the client in the request:");
            THeaderTransport headerTransport = (THeaderTransport) inputTransport;
            for (String key : headerTransport.getReadHeaders().keySet()) {
                LOG.info("header '" + key + "' => '" + headerTransport.getReadHeaders().get(key) + "'");
            }
        }

        // Send some headers back to the client
        TTransport outputTransport = requestContext.getOutputProtocol().getTransport();
        if (outputTransport instanceof THeaderTransport) {
            LOG.info("adding headers to the response");
            THeaderTransport headerTransport = (THeaderTransport) inputTransport;
            headerTransport.setHeader("header_from", "server");
        }
    }
}
TOP

Related Classes of com.facebook.swift.headerexample.HeaderUsageExampleHandler

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.