KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > junit > ServerResponseFactory


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Forums JBoss Portlet *
6  * *
7  * Distributable under LGPL license. *
8  * See terms of license at gnu.org. *
9  * *
10  *****************************************/

11 package org.jboss.portal.junit;
12
13 import org.jboss.portal.common.util.XML;
14 import org.jboss.portal.common.util.Tools;
15 import org.jboss.portal.junit.response.ResultSetResponse;
16 import org.jboss.portal.junit.response.ErrorResponse;
17 import org.w3c.dom.Document JavaDoc;
18 import org.w3c.dom.Element JavaDoc;
19 import org.apache.commons.httpclient.HttpMethod;
20 import org.xml.sax.SAXException JavaDoc;
21
22 import javax.xml.parsers.DocumentBuilder JavaDoc;
23 import javax.xml.parsers.ParserConfigurationException JavaDoc;
24 import java.io.ByteArrayInputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.List JavaDoc;
29
30 import junit.framework.AssertionFailedError;
31
32 /**
33  * Create a server response with the result of the http response.
34  *
35  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
36  * @version $Revision: 1.6 $
37  */

38 public class ServerResponseFactory
39 {
40
41    public static ServerResponse create(HttpMethod method) throws IOException JavaDoc
42    {
43       try
44       {
45          int status = method.getStatusCode();
46          if (status == 200)
47          {
48             String JavaDoc s = method.getResponseBodyAsString();
49             DocumentBuilder JavaDoc builder = XML.getDocumentBuilderFactory().newDocumentBuilder();
50             Document JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(s.getBytes("UTF-8")));
51             Element JavaDoc root = doc.getDocumentElement();
52             if ("results".equals(root.getNodeName()))
53             {
54                Map JavaDoc results = new HashMap JavaDoc();
55                List JavaDoc portletElts = XML.getChildren(root, "result");
56                for (int i = 0;i < portletElts.size();i++)
57                {
58                   Element JavaDoc portletElt = (Element JavaDoc)portletElts.get(i);
59                   Element JavaDoc idElt = XML.getUniqueChild(portletElt, "id", true);
60                   Element JavaDoc contentElt = XML.getUniqueChild(portletElt, "content", true);
61                   String JavaDoc id = XML.asString(idElt);
62                   String JavaDoc content = XML.asString(contentElt);
63                   Result result = ResultCodec.unmarshallResult(content);
64                   results.put(id, result);
65                }
66                return new ResultSetResponse(results);
67             }
68             else if ("error".equals(root.getNodeName()))
69             {
70                Element JavaDoc messageElt = XML.getUniqueChild(root, "message", true);
71                String JavaDoc message = XML.asString(messageElt);
72                return new ErrorResponse(message);
73             }
74          }
75          return null;
76       }
77       catch (ParserConfigurationException JavaDoc e)
78       {
79          AssertionFailedError error = new AssertionFailedError();
80          error.initCause(error);
81          throw error;
82       }
83       catch (SAXException JavaDoc e)
84       {
85          AssertionFailedError error = new AssertionFailedError();
86          error.initCause(error);
87          throw error;
88       }
89    }
90 }
91
Popular Tags