KickJava   Java API By Example, From Geeks To Geeks.

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


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.server.servlet.AbstractMainServlet;
14 import org.jboss.portal.server.PortalRequest;
15 import org.jboss.portal.server.PortalResponse;
16 import org.jboss.portal.server.WindowContext;
17 import org.jboss.portal.server.Window;
18 import org.jboss.portal.server.Instance;
19 import org.jboss.portal.server.InstanceContext;
20 import org.jboss.portal.server.invocation.Invocation;
21 import org.jboss.portal.server.invocation.AttachmentKey;
22 import org.jboss.portal.server.user.UserContext;
23 import org.jboss.portal.server.output.Output;
24 import org.jboss.portal.server.output.Result;
25 import org.jboss.portal.server.output.HTTPRedirectionResult;
26 import org.jboss.portal.server.output.ActionResult;
27 import org.jboss.portal.server.output.NoResult;
28 import org.jboss.portal.server.output.FragmentResult;
29 import org.jboss.portal.server.output.ErrorResult;
30 import org.jboss.portal.common.util.Exceptions;
31
32 import javax.servlet.ServletException JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.io.PrintWriter JavaDoc;
35 import java.io.StringWriter JavaDoc;
36 import java.io.ByteArrayOutputStream JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.Enumeration JavaDoc;
39 import java.util.List JavaDoc;
40 import java.util.ArrayList JavaDoc;
41
42 /**
43  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
44  * @version $Revision: 1.4 $
45  */

46 public class TestServlet extends AbstractMainServlet
47 {
48
49    protected void invoke(Invocation invocation) throws ServletException JavaDoc, IOException JavaDoc
50    {
51       PortalRequest req = (PortalRequest)invocation.getAttachment(AttachmentKey.PORTAL_REQUEST);
52
53       // Set participant of the test if any
54
String JavaDoc appName = req.getHeader("appName");
55       Enumeration JavaDoc e = req.getHeaders("componentName");
56       if (appName != null && e.hasMoreElements())
57       {
58          List JavaDoc windowNames = new ArrayList JavaDoc();
59          while (e.hasMoreElements())
60          {
61             windowNames.add("/" + appName + "/" + e.nextElement() + "/default");
62          }
63          invocation.setAttachment(TestAttachmentKey.TEST_WINDOWS, windowNames.toArray(new String JavaDoc[windowNames.size()]));
64       }
65
66       // Perform invocation
67
super.invoke(invocation);
68    }
69
70    protected void render(PortalRequest req, PortalResponse resp) throws IOException JavaDoc, ServletException JavaDoc
71    {
72       Result tmp = resp.getTargetResult();
73       if (tmp instanceof HTTPRedirectionResult)
74       {
75          HTTPRedirectionResult redirection = (HTTPRedirectionResult)tmp;
76          resp.sendRedirect(redirection.getLocation());
77       }
78       else if (tmp == null || tmp instanceof ActionResult || tmp instanceof NoResult)
79       {
80          resp.setContentType("text/xml");
81          PrintWriter JavaDoc writer = resp.getWriter();
82          writer.print("<results>");
83
84          UserContext userContext = (UserContext)req.getSession().getAttribute("USER_CONTEXT");
85
86          for (Iterator JavaDoc i = resp.getRenderResults();i.hasNext();)
87          {
88             Result result = (Result)i.next();
89             if (result instanceof FragmentResult)
90             {
91                FragmentResult fragment = (FragmentResult)result;
92                WindowContext windowContext = (WindowContext)result.getProducer();
93                Window window = (Window)req.getServer().getObject(windowContext.getID());
94                Instance instance = window.getInstance();
95                InstanceContext instanceCtx = (InstanceContext)userContext.getContext(instance);
96                if (instanceCtx != null)
97                {
98                   if (windowContext != null)
99                   {
100                      writer.print("<result>");
101                      writer.print("<id>");
102                      writer.print(instance.getComponent().getName());
103                      writer.print("</id>");
104                      writer.print("<content>");
105                      StringWriter JavaDoc chars = fragment.getChars();
106                      if (chars != null)
107                      {
108                         writer.write(chars.toString());
109                      }
110                      ByteArrayOutputStream JavaDoc baos = fragment.getBytes();
111                      if (baos != null)
112                      {
113                         writer.write(baos.toString());
114                      }
115                      writer.print("</content>");
116                      writer.print("</result>");
117                   }
118                   else
119                   {
120                      // writer.print("<content>");
121
// writer.write("window " + window.getKey() + " is null");
122
// writer.print("</td></tr>");
123
}
124                }
125                else
126                {
127                   // writer.print("<tr><td>");
128
// writer.write("instance " + instance.getKey() + " is null");
129
// writer.print("</td></tr>");
130
}
131             }
132             else
133             {
134                System.out.println("TODO NO DATA");
135             }
136          }
137          writer.print("</results>");
138          writer.close();
139       }
140       else if (tmp instanceof ErrorResult)
141       {
142          ErrorResult error = (ErrorResult)tmp;
143          Throwable JavaDoc t = error.getThrowable();
144
145          // Log the message
146
if (t != null)
147          {
148             t = Exceptions.unwrap(t);
149             log.error("An error occured during the request : " + error.getMessage(), t);
150          }
151          else
152          {
153             log.error("An error occured during the request : " + error.getMessage());
154          }
155
156          //
157
resp.setContentType("text/xml");
158          PrintWriter JavaDoc writer = resp.getWriter();
159          writer.print("<error>");
160          writer.print("<message>");
161          writer.print(error.getMessage());
162          writer.print("</message>");
163          if (t != null)
164          {
165             writer.print("<throwable>");
166             t.printStackTrace(writer);
167             writer.print("</throwable>");
168          }
169          writer.print("</error>");
170          writer.close();
171       }
172       else
173       {
174          resp.setContentType("text/xml");
175          PrintWriter JavaDoc writer = resp.getWriter();
176          writer.print("<unknown/>");
177          writer.close();
178       }
179    }
180
181 }
182
Popular Tags