1 29 30 package nextapp.echo2.webcontainer.syncpeer; 31 32 import org.w3c.dom.Element ; 33 34 import nextapp.echo2.app.Command; 35 import nextapp.echo2.webcontainer.CommandSynchronizePeer; 36 import nextapp.echo2.webcontainer.RenderContext; 37 import nextapp.echo2.webcontainer.command.BrowserOpenWindowCommand; 38 import nextapp.echo2.webcontainer.command.BrowserRedirectCommand; 39 import nextapp.echo2.webcontainer.command.BrowserSetCookieCommand; 40 import nextapp.echo2.webrender.ServerMessage; 41 import nextapp.echo2.webrender.Service; 42 import nextapp.echo2.webrender.WebRenderServlet; 43 import nextapp.echo2.webrender.service.JavaScriptService; 44 45 52 public class BrowserCommandPeer 53 implements CommandSynchronizePeer { 54 55 58 private static final Service BROWSER_COMMAND_SERVICE = JavaScriptService.forResource("Echo.BrowserCommand", 59 "/nextapp/echo2/webcontainer/resource/js/BrowserCommand.js"); 60 61 static { 62 WebRenderServlet.getServiceRegistry().add(BROWSER_COMMAND_SERVICE); 63 } 64 65 69 public void render(RenderContext rc, Command command) { 70 if (command instanceof BrowserOpenWindowCommand) { 71 renderOpenWindow(rc, (BrowserOpenWindowCommand) command); 72 } else if (command instanceof BrowserRedirectCommand) { 73 renderRedirect(rc, (BrowserRedirectCommand) command); 74 } else if (command instanceof BrowserSetCookieCommand) { 75 renderSetCookie(rc, (BrowserSetCookieCommand) command); 76 } else { 77 throw new IllegalArgumentException (); 78 } 79 } 80 81 87 private void renderSetCookie(RenderContext rc, BrowserSetCookieCommand command) { 88 rc.getConnection().getResponse().addCookie(command.getCookie()); 89 } 90 91 97 private void renderOpenWindow(RenderContext rc, BrowserOpenWindowCommand command) { 98 ServerMessage serverMessage = rc.getServerMessage(); 99 serverMessage.addLibrary(BROWSER_COMMAND_SERVICE.getId()); 100 Element openWindowElement = serverMessage.appendPartDirective(ServerMessage.GROUP_ID_POSTUPDATE, 101 "EchoBrowserCommand.MessageProcessor", "open-window"); 102 openWindowElement.setAttribute("uri", command.getUri()); 103 if (command.getName() != null) { 104 openWindowElement.setAttribute("name", command.getName()); 105 } 106 if (command.getFeatures() != null) { 107 openWindowElement.setAttribute("features", command.getFeatures()); 108 } 109 openWindowElement.setAttribute("replace", command.isReplace() ? "true" : "false"); 110 } 111 112 118 private void renderRedirect(RenderContext rc, BrowserRedirectCommand command) { 119 ServerMessage serverMessage = rc.getServerMessage(); 120 serverMessage.addLibrary(BROWSER_COMMAND_SERVICE.getId()); 121 Element redirectElement = serverMessage.appendPartDirective(ServerMessage.GROUP_ID_POSTUPDATE, 122 "EchoBrowserCommand.MessageProcessor", "redirect"); 123 redirectElement.setAttribute("uri", command.getUri()); 124 } 125 } 126 | Popular Tags |