net.phoenix.thrift.hello.SpringHelloService.java Source code

Java tutorial

Introduction

Here is the source code for net.phoenix.thrift.hello.SpringHelloService.java

Source

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package net.phoenix.thrift.hello;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;

import net.phoenix.thrift.annotation.Processor;
import net.phoenix.thrift.gen.HelloService;
import net.phoenix.thrift.gen.Hello.HelloRequest;
import net.phoenix.thrift.gen.Hello.HelloResponse;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.thrift.TException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.google.protobuf.InvalidProtocolBufferException;

/**
 *
 * @author Shamphone Lee<shamphone@gmail.com>
 *
 */
@Component("HelloService")
@Processor(processor = HelloService.Processor.class)
public class SpringHelloService implements HelloService.Iface {

    @Value("${test.lob}")
    private String lobData;

    /* (non-Javadoc)
     * @see knowledge.pub.HelloService.Iface#hello(java.nio.ByteBuffer)
     */
    @Override
    public ByteBuffer hello(ByteBuffer hello_request) throws TException {
        HelloRequest request;
        try {
            request = HelloRequest.parseFrom(
                    ArrayUtils.subarray(hello_request.array(), hello_request.position(), hello_request.limit()));
        } catch (InvalidProtocolBufferException e) {
            throw new TException(e);
        }
        HelloResponse.Builder response = HelloResponse.newBuilder();
        response.setMessage("Hello " + request.getName());
        return ByteBuffer.wrap(response.build().toByteArray());
    }

    /* (non-Javadoc)
     * @see knowledge.pub.HelloService.Iface#hello2(java.lang.String)
     */
    @Override
    public String hello2(String name) throws TException {
        return name + " World";
    }

    /* (non-Javadoc)
     * @see knowledge.pub.HelloService.Iface#testBinary(java.nio.ByteBuffer)
     */
    @Override
    public ByteBuffer testBinary(ByteBuffer name) throws TException {
        try {

            String result = new String(name.array(), name.position(), name.limit() - name.position(), "UTF-8");
            result = "Hello " + result;
            return ByteBuffer.wrap(result.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            throw new TException(e);
        }
    }

    /* (non-Javadoc)
     * @see knowledge.pub.HelloService.Iface#testLOB(java.lang.String)
     */
    @Override
    public String testLOB(String content) throws TException {
        return content + this.lobData;
    }

}