Package org.apache.camel.component.netty

Source Code of org.apache.camel.component.netty.NettyCustomPipelineFactoryAsynchTest$TestServerChannelPipelineFactory

/**
* 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 org.apache.camel.component.netty;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.Produce;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.netty.handlers.ClientChannelHandler;
import org.apache.camel.component.netty.handlers.ServerChannelHandler;
import org.apache.camel.impl.JndiRegistry;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.handler.codec.frame.DelimiterBasedFrameDecoder;
import org.jboss.netty.handler.codec.frame.Delimiters;
import org.jboss.netty.handler.codec.string.StringDecoder;
import org.jboss.netty.handler.codec.string.StringEncoder;
import org.jboss.netty.util.CharsetUtil;
import org.junit.Test;

public class NettyCustomPipelineFactoryAsynchTest extends BaseNettyTest {

    @Produce(uri = "direct:start")
    protected ProducerTemplate producerTemplate;
    private TestClientChannelPipelineFactory clientPipelineFactory;
    private TestServerChannelPipelineFactory serverPipelineFactory;
    private String response;
   
    @Override
    protected JndiRegistry createRegistry() throws Exception {
        JndiRegistry registry = super.createRegistry();
        clientPipelineFactory = new TestClientChannelPipelineFactory();
        serverPipelineFactory = new TestServerChannelPipelineFactory();
        registry.bind("cpf", clientPipelineFactory);
        registry.bind("spf", serverPipelineFactory);
        return registry;
    }
   
    @Override
    public boolean isUseRouteBuilder() {
        return false;
    }

    private void sendRequest() throws Exception {
        // Async request
        response = (String) producerTemplate.requestBody(
            "netty:tcp://localhost:{{port}}?clientPipelineFactory=#cpf&textline=true",
            "Forest Gump describing Vietnam...");       
    }
   
    @Test
    public void testCustomClientPipelineFactory() throws Exception {
        context.addRoutes(new RouteBuilder() {
            public void configure() {
                from("netty:tcp://localhost:{{port}}?serverPipelineFactory=#spf&textline=true")
                    .process(new Processor() {
                        public void process(Exchange exchange) throws Exception {
                            exchange.getOut().setBody("Forrest Gump: We was always taking long walks, and we was always looking for a guy named 'Charlie'");                          
                        }
                    });               
            }
        });
        context.start();
       
        sendRequest();
        context.stop();
       
        assertEquals("Forrest Gump: We was always taking long walks, and we was always looking for a guy named 'Charlie'", response);
        assertEquals(true, clientPipelineFactory.isFactoryInvoked());
        assertEquals(true, serverPipelineFactory.isFactoryInvoked());
    }
   
    public class TestClientChannelPipelineFactory extends ClientPipelineFactory {
        private int maxLineSize = 1024;
        private boolean invoked;

        @Override
        public ChannelPipeline getPipeline(NettyProducer producer) throws Exception {
            invoked = true;
           
            ChannelPipeline channelPipeline = Channels.pipeline();

            channelPipeline.addLast("decoder-DELIM", new DelimiterBasedFrameDecoder(maxLineSize, true, Delimiters.lineDelimiter()));
            channelPipeline.addLast("decoder-SD", new StringDecoder(CharsetUtil.UTF_8));
            channelPipeline.addLast("encoder-SD", new StringEncoder(CharsetUtil.UTF_8));           
            channelPipeline.addLast("handler", new ClientChannelHandler(producer));

            return channelPipeline;
        }
       
        public boolean isFactoryInvoked() {
            return invoked;
        }
    }
   
    public class TestServerChannelPipelineFactory extends ServerPipelineFactory {
        private int maxLineSize = 1024;
        private boolean invoked;

        @Override
        public ChannelPipeline getPipeline(NettyConsumer consumer) throws Exception {
            invoked = true;
           
            ChannelPipeline channelPipeline = Channels.pipeline();

            channelPipeline.addLast("encoder-SD", new StringEncoder(CharsetUtil.UTF_8));
            channelPipeline.addLast("decoder-DELIM", new DelimiterBasedFrameDecoder(maxLineSize, true, Delimiters.lineDelimiter()));
            channelPipeline.addLast("decoder-SD", new StringDecoder(CharsetUtil.UTF_8));
            channelPipeline.addLast("handler", new ServerChannelHandler(consumer));

            return channelPipeline;
        }
       
        public boolean isFactoryInvoked() {
            return invoked;
        }
       
    }
}
TOP

Related Classes of org.apache.camel.component.netty.NettyCustomPipelineFactoryAsynchTest$TestServerChannelPipelineFactory

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.