camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Malathi G <malathi...@gmail.com>
Subject XML to JSON using XMLJSON - BufferDataException
Date Mon, 11 Nov 2013 09:19:27 GMT
Hi,

I am trying to convert XML into JSON format using XMLJson in TCP client
server program. I am having three files - TCPClient, TCPServer and CamelDemo
class. In between client and server, i am using CamelDemo class for
conversion. It shows BufferDataFormat Exception in CamelDemo class. This is
my code  


*TCPClient Code:*
public class TCPClient {
	public static void main(String[] args) throws IOException {
		Socket clientsoc = null;
		PrintWriter out = null;
		String inputMsg = null;
		try {
			clientsoc = new Socket("localhost", 6789);
	                  
inputMsg="<Root><firstName>xxx</firstName><lastName>yyy</lastName></Root>";
                                      out = new
PrintWriter(clientsoc.getOutputStream(), true);
			out.println(inputMsg);

			InputStream is = clientsoc.getInputStream();
			InputStreamReader isr = new InputStreamReader(is);
			BufferedReader br = new BufferedReader(isr);
			String receivedMsg = br.readLine();
			System.out.println("Message received  from server is "
					+ receivedMsg);

		}
		catch (Exception e) {
			e.printStackTrace();

		} finally {
			out.close();
			clientsoc.close();
			out = null;
			inputMsg = null;
		}
	}
}


*TCPServer Code:*
public class TCPServer 
{
    public static void main(String[] args) 
    {
        Socket socket =null;
        try
        {
 
            int port = 6123;
            ServerSocket serverSocket = new ServerSocket(port);
            System.out.println("Server Started and listening to the port
6123");
            while(true) 
            {
                socket = serverSocket.accept();
                InputStream is = socket.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                String receivedMsg = br.readLine();
                System.out.println("Message received from client is
"+receivedMsg);

                String returnMessage=receivedMsg;
                PrintWriter out = new PrintWriter(socket.getOutputStream(),
true);
                out.println(returnMessage);
                System.out.println("Message sent to the client is
"+returnMessage);
                out.flush();
            }
        }
        catch (Exception e) 
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                socket.close();
            }
            catch(Exception e){}
        }
    }
}


*CamelDemo Code:*
public class CamelDemo {
	static CamelContext context = null;
	public static void main(String[] s) {
	       try {
                                   context = new DefaultCamelContext();
		         final XmlJsonDataFormat xmlJsonFormat=new XmlJsonDataFormat();
		         xmlJsonFormat.setEncoding("UTF-8");
		         xmlJsonFormat.setForceTopLevelObject(true);
	                      xmlJsonFormat.setTrimSpaces(true);
		         xmlJsonFormat.setRootName("Root");
		         xmlJsonFormat.setSkipNamespaces(true);
		         xmlJsonFormat.setRemoveNamespacePrefixes(true);
		         xmlJsonFormat.setExpandableProperties(Arrays.asList("d", "e"));
			
		Component component = new org.apache.camel.component.mina.MinaComponent();
		        context.addComponent("tcp", component);
		        context.addRoutes(new RouteBuilder() {
		        public void configure() {
			from("mina:tcp://localhost:6789?textline=false&sync=true")
				.marshal(xmlJsonFormat)
				.process(new Processor() {
				// consumer
			           public void process(Exchange e) {
			                   try {
					          // Request message from client
					         String inMsg = (String) e.getIn().getBody();
				                     System.out.println("******"+inMsg+"******");
					       // Configuring Destination
  				                   Endpoint endpoint =
context.getEndpoint("mina:tcp://127.0.0.1:6123?textline=false&sync=true");
					Exchange exchange = endpoint.createExchange(ExchangePattern.InOut);
                                                                 Message in
= exchange.getIn();
					in.setBody(inMsg);
                                                                Producer
producer = endpoint.createProducer();
                                                               
producer.start();
                                                               
producer.process(exchange);

					// Response message from destination
					Message out = exchange.getOut();
					e.getOut().setBody(out);
				             producer.stop();
			                 } catch (Exception e1) {
						e1.printStackTrace();
							}

				      }
				});//.to("mina:tcp://127.0.0.1:6123?sync=true");
				}

			});

			context.start();
			Thread.sleep(10000000);
			context.stop();

		} catch (Throwable ex) {
			ex.printStackTrace();
		}
	}
}


*Exception:*
14:04:16.350 [SocketAcceptorIoProcessor-0.0] DEBUG
o.a.m.filter.executor.ExecutorFilter - Launching thread for /127.0.0.1:51299
14:04:16.350 [Camel (camel-1) thread #3 - MinaThreadPool] DEBUG
o.a.m.filter.executor.ExecutorFilter - Exiting since queue is empty for
/127.0.0.1:51299
14:04:16.350 [SocketAcceptorIoProcessor-0.0] DEBUG
o.a.m.filter.executor.ExecutorFilter - Launching thread for /127.0.0.1:51299
14:04:16.350 [Camel (camel-1) thread #3 - MinaThreadPool] DEBUG
o.a.c.component.mina.MinaConsumer - Closing session as an exception was
thrown from MINA
14:04:16.366 [Camel (camel-1) thread #3 - MinaThreadPool] WARN 
o.a.c.c.m.MinaConsumer$ReceiveHandler - [/127.0.0.1:51299] Unexpected
exception from exceptionCaught handler.
org.apache.camel.CamelException:
org.apache.mina.filter.codec.ProtocolDecoderException:
org.apache.mina.common.BufferDataException: dataLength: 1012035439 (Hexdump:
3C 52 6F 6F 74 3E 3C 66 69 72 73 74 4E 61 6D 65 3E 78 78 78 3C 2F 66 69 72
73 74 4E 61 6D 65 3E 3C 6C 61 73 74 4E 61 6D 65 3E 79 79 79 3C 2F 6C 61 73
74 4E 61 6D 65 3E 3C 2F 52 6F 6F 74 3E 0D 0A)
	at
org.apache.camel.component.mina.MinaConsumer$ReceiveHandler.exceptionCaught(MinaConsumer.java:91)
~[camel-mina-2.11.1.jar:2.11.1]
	at
org.apache.mina.common.support.AbstractIoFilterChain$TailFilter.exceptionCaught(AbstractIoFilterChain.java:564)
~[mina-core-1.1.7.jar:na]
	at
org.apache.mina.common.support.AbstractIoFilterChain.callNextExceptionCaught(AbstractIoFilterChain.java:345)
[mina-core-1.1.7.jar:na]
	at
org.apache.mina.common.support.AbstractIoFilterChain.access$1000(AbstractIoFilterChain.java:53)
[mina-core-1.1.7.jar:na]
	at
org.apache.mina.common.support.AbstractIoFilterChain$EntryImpl$1.exceptionCaught(AbstractIoFilterChain.java:643)
[mina-core-1.1.7.jar:na]
	at
org.apache.mina.filter.executor.ExecutorFilter.processEvent(ExecutorFilter.java:224)
[mina-core-1.1.7.jar:na]
	at
org.apache.mina.filter.executor.ExecutorFilter$ProcessEventsRunnable.run(ExecutorFilter.java:264)
[mina-core-1.1.7.jar:na]
	at
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
[na:1.6.0_14]
	at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
[na:1.6.0_14]
	at java.lang.Thread.run(Thread.java:619) [na:1.6.0_14]
Caused by: org.apache.mina.filter.codec.ProtocolDecoderException:
org.apache.mina.common.BufferDataException: dataLength: 1012035439 (Hexdump:
3C 52 6F 6F 74 3E 3C 66 69 72 73 74 4E 61 6D 65 3E 78 78 78 3C 2F 66 69 72
73 74 4E 61 6D 65 3E 3C 6C 61 73 74 4E 61 6D 65 3E 79 79 79 3C 2F 6C 61 73
74 4E 61 6D 65 3E 3C 2F 52 6F 6F 74 3E 0D 0A)
	at
org.apache.mina.filter.codec.ProtocolCodecFilter.messageReceived(ProtocolCodecFilter.java:165)
~[mina-core-1.1.7.jar:na]
	at
org.apache.mina.common.support.AbstractIoFilterChain.callNextMessageReceived(AbstractIoFilterChain.java:299)
[mina-core-1.1.7.jar:na]
	at
org.apache.mina.common.support.AbstractIoFilterChain.access$1100(AbstractIoFilterChain.java:53)
[mina-core-1.1.7.jar:na]
	at
org.apache.mina.common.support.AbstractIoFilterChain$EntryImpl$1.messageReceived(AbstractIoFilterChain.java:648)
[mina-core-1.1.7.jar:na]
	at
org.apache.mina.common.support.AbstractIoFilterChain$HeadFilter.messageReceived(AbstractIoFilterChain.java:499)
~[mina-core-1.1.7.jar:na]
	at
org.apache.mina.common.support.AbstractIoFilterChain.callNextMessageReceived(AbstractIoFilterChain.java:299)
[mina-core-1.1.7.jar:na]
	at
org.apache.mina.common.support.AbstractIoFilterChain.fireMessageReceived(AbstractIoFilterChain.java:293)
[mina-core-1.1.7.jar:na]
	at
org.apache.mina.transport.socket.nio.SocketIoProcessor.read(SocketIoProcessor.java:228)
~[mina-core-1.1.7.jar:na]
	at
org.apache.mina.transport.socket.nio.SocketIoProcessor.process(SocketIoProcessor.java:198)
~[mina-core-1.1.7.jar:na]
	at
org.apache.mina.transport.socket.nio.SocketIoProcessor.access$400(SocketIoProcessor.java:45)
~[mina-core-1.1.7.jar:na]
	at
org.apache.mina.transport.socket.nio.SocketIoProcessor$Worker.run(SocketIoProcessor.java:485)
~[mina-core-1.1.7.jar:na]
	at
org.apache.mina.util.NamePreservingRunnable.run(NamePreservingRunnable.java:51)
~[mina-core-1.1.7.jar:na]
	... 3 common frames omitted
Caused by: org.apache.mina.common.BufferDataException: dataLength:
1012035439
	at
org.apache.mina.common.ByteBuffer.prefixedDataAvailable(ByteBuffer.java:1631)
~[mina-core-1.1.7.jar:na]
	at
org.apache.mina.filter.codec.serialization.ObjectSerializationDecoder.doDecode(ObjectSerializationDecoder.java:88)
~[mina-core-1.1.7.jar:na]
	at
org.apache.mina.filter.codec.CumulativeProtocolDecoder.decode(CumulativeProtocolDecoder.java:133)
~[mina-core-1.1.7.jar:na]
	at
org.apache.mina.filter.codec.ProtocolCodecFilter.messageReceived(ProtocolCodecFilter.java:158)
~[mina-core-1.1.7.jar:na]
	... 14 common frames omitted
14:04:16.366 [Camel (camel-1) thread #3 - MinaThreadPool] DEBUG
o.a.m.filter.executor.ExecutorFilter - Exiting since queue is empty for
/127.0.0.1:51299

Please help me to solve this exception



--
View this message in context: http://camel.465427.n5.nabble.com/XML-to-JSON-using-XMLJSON-BufferDataException-tp5743019.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Mime
View raw message