1 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 ; 33 import java.io.IOException ; 34 import java.io.PrintWriter ; 35 import java.io.StringWriter ; 36 import java.io.ByteArrayOutputStream ; 37 import java.util.Iterator ; 38 import java.util.Enumeration ; 39 import java.util.List ; 40 import java.util.ArrayList ; 41 42 46 public class TestServlet extends AbstractMainServlet 47 { 48 49 protected void invoke(Invocation invocation) throws ServletException , IOException 50 { 51 PortalRequest req = (PortalRequest)invocation.getAttachment(AttachmentKey.PORTAL_REQUEST); 52 53 String appName = req.getHeader("appName"); 55 Enumeration e = req.getHeaders("componentName"); 56 if (appName != null && e.hasMoreElements()) 57 { 58 List windowNames = new ArrayList (); 59 while (e.hasMoreElements()) 60 { 61 windowNames.add("/" + appName + "/" + e.nextElement() + "/default"); 62 } 63 invocation.setAttachment(TestAttachmentKey.TEST_WINDOWS, windowNames.toArray(new String [windowNames.size()])); 64 } 65 66 super.invoke(invocation); 68 } 69 70 protected void render(PortalRequest req, PortalResponse resp) throws IOException , ServletException 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 writer = resp.getWriter(); 82 writer.print("<results>"); 83 84 UserContext userContext = (UserContext)req.getSession().getAttribute("USER_CONTEXT"); 85 86 for (Iterator 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 chars = fragment.getChars(); 106 if (chars != null) 107 { 108 writer.write(chars.toString()); 109 } 110 ByteArrayOutputStream 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 } 124 } 125 else 126 { 127 } 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 t = error.getThrowable(); 144 145 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 resp.setContentType("text/xml"); 158 PrintWriter 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 writer = resp.getWriter(); 176 writer.print("<unknown/>"); 177 writer.close(); 178 } 179 } 180 181 } 182 | Popular Tags |