プログラマとプロマネのあいだ

プログラマもやるし、プロマネもやるし、たまに似非アーキとか営業っぽいこともやる

Restlet-1.0b9の初歩を試す(XML編)。

http://d.hatena.ne.jp/taka_2/20060424#p2の続き。
特にハマるようなところもなく、ごく普通。つか、久々にJavaのDOM API使ったよ。。
サンプルコードは↓のURL(で見られるハズ)
http://proxy.f2.ymdb.yahoofs.jp/bc/5e69f8d4/bc/public/RestletSample.zip?bc2xxTEBvwq3Mfj4
やっぱダメっぽいなぁ。やっぱりhttp://briefcase.yahoo.co.jp/taka_javaからたどるしかないのか?

  • RestletXmlServer.java
    • Stringをtext/xmlとして提供するRestlet
  • RestletXmlServer2.java
    • org.w3c.dom.Documentをtext/xmlとして提供するRestlet
  • RestletXmlClient.java
    • text/xmlを提供するRESTサービスにアクセスして、org.w3c.dom.Documentを取り出すクライアント

RestletXmlServer.java

import org.restlet.AbstractRestlet;
import org.restlet.Manager;
import org.restlet.Restlet;
import org.restlet.RestletCall;
import org.restlet.data.MediaTypes;
import org.restlet.data.Protocols;

import com.noelios.restlet.data.StringRepresentation;

public class RestletXmlServer
{
	public static void main(String[] args) throws Exception
	{
		// Creating a minimal Restlet returning "Hello World"
		Restlet handler = new AbstractRestlet()
		{
			public void handle(RestletCall call)
			{
				call.setOutput(new StringRepresentation("<?xml version = \"1.0\" ?><root><aaa><bbb>Hello, World!</bbb><bbb>Hello, World!!</bbb></aaa></root>", MediaTypes.TEXT_XML));
			}
		};

		// Create the HTTP server and listen on port 8182
		Manager.createServer(Protocols.HTTP, "My server", handler, null, 8182).start();
	}
}

RestletXmlServer2.java

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.restlet.AbstractRestlet;
import org.restlet.Manager;
import org.restlet.Restlet;
import org.restlet.RestletCall;
import org.restlet.data.MediaTypes;
import org.restlet.data.Protocols;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

import com.noelios.restlet.data.DomRepresentation;
import com.noelios.restlet.data.StringRepresentation;

public class RestletXmlServer2
{
	public static void main(String[] args) throws Exception
	{
		// Creating a minimal Restlet returning "Hello World"
		Restlet handler = new AbstractRestlet()
		{
			public void handle(RestletCall call)
			{
				try
				{
					DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
					DocumentBuilder db = dbf.newDocumentBuilder();
					Document document = db.newDocument();

					// Create /root
					Node rootNode = document.createElement("root");
					document.appendChild(rootNode);

					// Create /root/aaa
					Node aaaNode = document.createElement("aaa");
					rootNode.appendChild(aaaNode);

					// Create /root/aaa/bbb
					Node bbbNode1 = document.createElement("bbb");
					Node bbbChildNode1 = document.createTextNode("Hello, World!");
					bbbNode1.appendChild(bbbChildNode1);
					aaaNode.appendChild(bbbNode1);

					// Create /root/aaa/bbb
					Node bbbNode2 = document.createElement("bbb");
					Node bbbChildNode2 = document.createTextNode("Hello, World!!");
					bbbNode2.appendChild(bbbChildNode2);
					aaaNode.appendChild(bbbNode2);
					
					DomRepresentation domRepresentation = new DomRepresentation(MediaTypes.TEXT_XML, document);
					call.setOutput(domRepresentation);
				}
				catch(Exception e)
				{
					call.setOutput(new StringRepresentation(e.getMessage(), MediaTypes.TEXT_PLAIN));
					e.printStackTrace();
				}
			}
		};

		// Create the HTTP server and listen on port 8182
		Manager.createServer(Protocols.HTTP, "My server", handler, null, 8182).start();
	}
}

RestletXmlClient.java

import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.restlet.Manager;
import org.restlet.connector.Client;
import org.restlet.data.Protocols;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class RestletXmlClient {
	public static void main(String[] args) throws Exception
	{
		// Outputting the content of a Web page
		Client client = Manager.createClient(Protocols.HTTP, "My client");
		InputStream iStream = client.get("http://localhost:8182/").getOutput().getStream();
		
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = dbf.newDocumentBuilder();
		Document document = db.parse(iStream);
		printNodes(document.getDocumentElement());
	}

	private static void printNodes(Node node)
	{
		NodeList nodeList = node.getChildNodes();
		for(int i=0; i<nodeList.getLength(); i++)
		{
			Node currentNode = nodeList.item(i);
			if(currentNode.getNodeType() == Node.ELEMENT_NODE)
			{
				printNodes(currentNode);
			}
			else
			{
				System.out.println(currentNode.getNodeValue());
			}
		}
	}
}