Skip to content

Instantly share code, notes, and snippets.

@forty
Created December 10, 2011 19:18
Show Gist options
  • Save forty/1456005 to your computer and use it in GitHub Desktop.
Save forty/1456005 to your computer and use it in GitHub Desktop.
import im.scala.xmlstream.views.EndElementView;
import im.scala.xmlstream.views.StartElementView;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.concurrent.Executors;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
public class Main {
/**
* Should write to standard out:
*
* <pre>
* Start: uri / test
* Start: uri / test2
* End: uri / test2
* End: uri / test
* </pre>
*/
public static void main(String[] args) throws Exception {
// Configure the client.
ServerBootstrap bootstrap = new ServerBootstrap(
new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()));
// Set up the pipeline factory.
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipeline = Channels.pipeline();
pipeline.addLast("XMLDecoder", new XMLStreamDecoder(
new DefaultXMLEventMaker() {
public void startElement(XMLEventSender sender,
StartElementView view) {
sender.sendEvent("Start: "
+ view.getNamespaceURI() + " / "
+ view.getLocalName());
}
public void endElement(XMLEventSender sender,
EndElementView view) {
sender.sendEvent("End: "
+ view.getNamespaceURI() + " / "
+ view.getLocalName());
}
}));
pipeline.addLast("PrintToConsole",
new SimpleChannelUpstreamHandler() {
public void messageReceived(
ChannelHandlerContext ctx, MessageEvent e)
throws Exception {
System.out.println(e.getMessage());
}
});
return pipeline;
}
});
// Start the connection attempt.
bootstrap.bind(new InetSocketAddress(1234));
// Client test
Socket sock = new Socket("localhost", 1234);
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
sock.getOutputStream()));
out.write("<test xmlns='uri'><test2 /></test>");
out.flush();
out.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment