1 9 package org.jboss.portal.portlet.servlet; 10 11 import java.io.ByteArrayOutputStream ; 12 import java.io.IOException ; 13 import java.io.PrintWriter ; 14 import java.io.StringWriter ; 15 import java.util.Iterator ; 16 17 import javax.portlet.PortletModeException; 18 import javax.portlet.PortletURL; 19 import javax.portlet.WindowStateException; 20 import javax.servlet.ServletException ; 21 import javax.servlet.http.HttpServletResponse ; 22 23 import org.jboss.portal.portlet.PortletUtils; 24 import org.jboss.portal.server.Instance; 25 import org.jboss.portal.server.InstanceContext; 26 import org.jboss.portal.server.PortalRequest; 27 import org.jboss.portal.server.PortalResponse; 28 import org.jboss.portal.server.Window; 29 import org.jboss.portal.server.WindowContext; 30 import org.jboss.portal.server.output.ActionResult; 31 import org.jboss.portal.server.output.ErrorResult; 32 import org.jboss.portal.server.output.FragmentResult; 33 import org.jboss.portal.server.output.HTTPRedirectionResult; 34 import org.jboss.portal.server.output.NoResult; 35 import org.jboss.portal.server.output.Output; 36 import org.jboss.portal.server.output.Result; 37 import org.jboss.portal.server.plugins.mode.Mode; 38 import org.jboss.portal.server.plugins.windowstate.WindowState; 39 import org.jboss.portal.server.servlet.AbstractMainServlet; 40 import org.jboss.portal.server.user.UserContext; 41 42 46 public class PortletServlet extends AbstractMainServlet 47 { 48 49 protected void render(PortalRequest req, PortalResponse resp) throws IOException , ServletException 50 { 51 Result tmp = resp.getTargetResult(); 52 if (tmp instanceof HTTPRedirectionResult) 53 { 54 HTTPRedirectionResult redirection = (HTTPRedirectionResult)tmp; 55 resp.sendRedirect(redirection.getLocation()); 56 } 57 else if (tmp == null || tmp instanceof ActionResult || tmp instanceof NoResult) 58 { 59 resp.setContentType("text/html"); 60 PrintWriter writer = resp.getWriter(); 61 writer.print("<html><body>"); 62 writer.print("<h1>Hello</h1>"); 63 writer.print("<table border=\"1\">"); 64 65 UserContext userContext = (UserContext)req.getSession().getAttribute("USER_CONTEXT"); 66 67 for (Iterator i = resp.getRenderResults();i.hasNext();) 68 { 69 Result result = (Result)i.next(); 70 if (result instanceof FragmentResult) 71 { 72 FragmentResult fragment = (FragmentResult)result; 73 WindowContext windowContext = (WindowContext)result.getProducer(); 74 Window window = (Window)req.getServer().getObject(windowContext.getID()); 75 Instance instance = window.getInstance(); 76 InstanceContext instanceCtx = (InstanceContext)userContext.getContext(instance); 77 if (instanceCtx != null) 78 { 79 if (windowContext != null) 80 { 81 writer.print("<tr><td>"); 82 83 String title = fragment.getTitle(); 84 if (title == null) 85 { 86 title = instance.getName() + " - " + window.getName(); 87 } 88 writer.write("<div style=\"font-weight:bold;\">" + title + "</div>"); 89 90 writer.write("<div>" + windowContext.getWindowState()); 91 PortletURL renderURL = PortletUtils.createRenderURL( 95 resp, 96 window, 97 "text/html"); 98 for (Iterator j = window.getSupportedWindowStates().iterator();j.hasNext();) 99 { 100 try 101 { 102 WindowState windowState = (WindowState)j.next(); 103 renderURL.setWindowState(PortletUtils.decodeWindowState(windowState.toString())); 104 writer.write(" - <a HREF=\"" + renderURL.toString() + "\">" + windowState.toString() + "</a>"); 105 } 106 catch (WindowStateException ignore) 107 { 108 } 110 } 111 writer.write("</div>"); 112 113 writer.write("<div>" + windowContext.getMode()); 114 renderURL = PortletUtils.createRenderURL( 115 resp, 116 window, 117 "text/html"); 118 for (Iterator j = window.getSupportedModes("text/html").iterator();j.hasNext();) 119 { 120 try 121 { 122 Mode mode = (Mode)j.next(); 123 renderURL.setPortletMode(PortletUtils.decodePortletMode(mode.toString())); 124 writer.write(" - <a HREF=\"" + renderURL.toString() + "\">" + mode.toString() + "</a>"); 125 } 126 catch (PortletModeException ignore) 127 { 128 } 130 } 131 writer.write("</div>"); 132 133 StringWriter chars = fragment.getChars(); 134 if (chars != null) 135 { 136 writer.write(chars.toString()); 137 } 138 ByteArrayOutputStream baos = fragment.getBytes(); 139 if (baos != null) 140 { 141 writer.write(baos.toString()); 142 } 143 writer.print("</td></tr>"); 144 } 145 else 146 { 147 writer.print("<tr><td>"); 148 writer.write("window " + window.getName() + " is null"); 149 writer.print("</td></tr>"); 150 } 151 } 152 else 153 { 154 writer.print("<tr><td>"); 155 writer.write("instance " + instance.getName() + " is null"); 156 writer.print("</td></tr>"); 157 } 158 } 159 else 160 { 161 System.out.println("TODO NOT DATA " + result); 162 } 163 } 164 writer.print("</table>"); 165 writer.print("</body></html>"); 166 writer.close(); 167 } 168 else if (tmp instanceof ErrorResult) 169 { 170 ErrorResult error = (ErrorResult)tmp; 171 Throwable t = error.getThrowable(); 172 173 while (t.getCause() != null) 175 { 176 t = t.getCause(); 177 } 178 179 log.error("An error occured", t); 180 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, t.getMessage()); 181 } 182 else 183 { 184 throw new ServletException ("Unknown result"); 185 } 186 } 187 } 188 | Popular Tags |