001: /*
002: * This file is part of the Echo Web Application Framework (hereinafter "Echo").
003: * Copyright (C) 2002-2005 NextApp, Inc.
004: *
005: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006: *
007: * The contents of this file are subject to the Mozilla Public License Version
008: * 1.1 (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: * http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the
015: * License.
016: *
017: * Alternatively, the contents of this file may be used under the terms of
018: * either the GNU General Public License Version 2 or later (the "GPL"), or
019: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020: * in which case the provisions of the GPL or the LGPL are applicable instead
021: * of those above. If you wish to allow use of your version of this file only
022: * under the terms of either the GPL or the LGPL, and not to allow others to
023: * use your version of this file under the terms of the MPL, indicate your
024: * decision by deleting the provisions above and replace them with the notice
025: * and other provisions required by the GPL or the LGPL. If you do not delete
026: * the provisions above, a recipient may use your version of this file under
027: * the terms of any one of the MPL, the GPL or the LGPL.
028: */
029:
030: package nextapp.echo2.webcontainer.syncpeer;
031:
032: import org.w3c.dom.Element;
033:
034: import nextapp.echo2.app.Command;
035: import nextapp.echo2.webcontainer.CommandSynchronizePeer;
036: import nextapp.echo2.webcontainer.RenderContext;
037: import nextapp.echo2.webcontainer.command.BrowserOpenWindowCommand;
038: import nextapp.echo2.webcontainer.command.BrowserRedirectCommand;
039: import nextapp.echo2.webcontainer.command.BrowserSetCookieCommand;
040: import nextapp.echo2.webrender.ServerMessage;
041: import nextapp.echo2.webrender.Service;
042: import nextapp.echo2.webrender.WebRenderServlet;
043: import nextapp.echo2.webrender.service.JavaScriptService;
044:
045: /**
046: * A <code>CommandSynchronizePeer</code> implementation for
047: * browser control commands.
048: * <p>
049: * This class should not be extended or used by classes outside of the
050: * Echo framework.
051: */
052: public class BrowserCommandPeer implements CommandSynchronizePeer {
053:
054: /**
055: * Service to provide supporting JavaScript library.
056: */
057: private static final Service BROWSER_COMMAND_SERVICE = JavaScriptService
058: .forResource("Echo.BrowserCommand",
059: "/nextapp/echo2/webcontainer/resource/js/BrowserCommand.js");
060:
061: static {
062: WebRenderServlet.getServiceRegistry().add(
063: BROWSER_COMMAND_SERVICE);
064: }
065:
066: /**
067: * @see nextapp.echo2.webcontainer.CommandSynchronizePeer#render(
068: * nextapp.echo2.webcontainer.RenderContext, nextapp.echo2.app.Command)
069: */
070: public void render(RenderContext rc, Command command) {
071: if (command instanceof BrowserOpenWindowCommand) {
072: renderOpenWindow(rc, (BrowserOpenWindowCommand) command);
073: } else if (command instanceof BrowserRedirectCommand) {
074: renderRedirect(rc, (BrowserRedirectCommand) command);
075: } else if (command instanceof BrowserSetCookieCommand) {
076: renderSetCookie(rc, (BrowserSetCookieCommand) command);
077: } else {
078: throw new IllegalArgumentException();
079: }
080: }
081:
082: /**
083: * Renders a <code>BrowserSetCookieCommand</code>.
084: *
085: * @param rc the relevant <code>RenderContext</code>
086: * @param command the command
087: */
088: private void renderSetCookie(RenderContext rc,
089: BrowserSetCookieCommand command) {
090: rc.getConnection().getResponse().addCookie(command.getCookie());
091: }
092:
093: /**
094: * Renders a <code>BrowserOpenWindowCommand</code>.
095: *
096: * @param rc the relevant <code>RenderContext</code>
097: * @param command the command
098: */
099: private void renderOpenWindow(RenderContext rc,
100: BrowserOpenWindowCommand command) {
101: ServerMessage serverMessage = rc.getServerMessage();
102: serverMessage.addLibrary(BROWSER_COMMAND_SERVICE.getId());
103: Element openWindowElement = serverMessage.appendPartDirective(
104: ServerMessage.GROUP_ID_POSTUPDATE,
105: "EchoBrowserCommand.MessageProcessor", "open-window");
106: openWindowElement.setAttribute("uri", command.getUri());
107: if (command.getName() != null) {
108: openWindowElement.setAttribute("name", command.getName());
109: }
110: if (command.getFeatures() != null) {
111: openWindowElement.setAttribute("features", command
112: .getFeatures());
113: }
114: openWindowElement.setAttribute("replace",
115: command.isReplace() ? "true" : "false");
116: }
117:
118: /**
119: * Renders a <code>BrowserRedirectCommand</code>.
120: *
121: * @param rc the relevant <code>RenderContext</code>
122: * @param command the command
123: */
124: private void renderRedirect(RenderContext rc,
125: BrowserRedirectCommand command) {
126: ServerMessage serverMessage = rc.getServerMessage();
127: serverMessage.addLibrary(BROWSER_COMMAND_SERVICE.getId());
128: Element redirectElement = serverMessage.appendPartDirective(
129: ServerMessage.GROUP_ID_POSTUPDATE,
130: "EchoBrowserCommand.MessageProcessor", "redirect");
131: redirectElement.setAttribute("uri", command.getUri());
132: }
133: }
|