001: /*
002: * Copyright 2005 Joe Walker
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.directwebremoting.dwrp;
017:
018: import java.io.IOException;
019:
020: import javax.servlet.http.HttpServletResponse;
021:
022: import org.directwebremoting.ScriptBuffer;
023: import org.directwebremoting.extend.ConverterManager;
024: import org.directwebremoting.extend.EnginePrivate;
025: import org.directwebremoting.extend.MarshallException;
026: import org.directwebremoting.extend.ScriptBufferUtil;
027: import org.directwebremoting.util.MimeConstants;
028:
029: /**
030: * A ScriptConduit for use with HTML/SCRIPT wrapped Javascript output.
031: * <p>Scripts begin with an html and script tags. The scripts have been altered
032: * to include an 'execute-in-parent-context' wrapper.
033: * <p>If this conduit is used, the output should be directed to an iframe. No
034: * polling should be required.
035: * <p>This conduit works with IE 6/7 since the 4k buffer drawback (see
036: * {@link PlainScriptConduit} and {@link Html4kScriptConduit} does not prevent
037: * the execution of script elements.
038: * @author Joe Walker [joe at getahead dot ltd dot uk]
039: */
040: public class HtmlScriptConduit extends BaseScriptConduit {
041: /**
042: * Simple ctor
043: * @param response Used to flush output
044: * @param batchId The id of the batch that we are responding to
045: * @param converterManager How we convert objects to script
046: * @throws IOException If stream actions fail
047: */
048: public HtmlScriptConduit(HttpServletResponse response,
049: String batchId, ConverterManager converterManager,
050: boolean jsonOutput) throws IOException {
051: super (response, batchId, converterManager, jsonOutput);
052: }
053:
054: /* (non-Javadoc)
055: * @see org.directwebremoting.dwrp.BaseCallMarshaller#getOutboundMimeType()
056: */
057: @Override
058: protected String getOutboundMimeType() {
059: return MimeConstants.MIME_HTML;
060: }
061:
062: /* (non-Javadoc)
063: * @see org.directwebremoting.dwrp.BaseScriptConduit#beginStream()
064: */
065: @Override
066: public void beginStream() {
067: synchronized (out) {
068: out.println("<html><body>");
069: out.println("<script type=\"text/javascript\">");
070: out.println(EnginePrivate.remoteBeginIFrameResponse(
071: batchId, true));
072: out.println("</script>");
073: }
074: }
075:
076: /* (non-Javadoc)
077: * @see org.directwebremoting.dwrp.BaseScriptConduit#endStream()
078: */
079: @Override
080: public void endStream() {
081: synchronized (out) {
082: out.println("<script type=\"text/javascript\">");
083: out.println(EnginePrivate.remoteEndIFrameResponse(batchId,
084: true));
085: out.println("</script>");
086: out.println("</body></html>");
087: }
088: }
089:
090: /* (non-Javadoc)
091: * @see org.directwebremoting.ScriptConduit#addScript(org.directwebremoting.ScriptBuffer)
092: */
093: @Override
094: public boolean addScript(ScriptBuffer scriptBuffer)
095: throws IOException, MarshallException {
096: String script = ScriptBufferUtil.createOutput(scriptBuffer,
097: converterManager, jsonOutput);
098:
099: synchronized (out) {
100: // TODO: We don't think the comments are needed because the exec is automatic
101: out.println("<script type=\"text/javascript\">");
102: // out.println(ProtocolConstants.SCRIPT_START_MARKER);
103: out.println(EnginePrivate.remoteEval(script));
104: // out.println(ProtocolConstants.SCRIPT_END_MARKER);
105: out.println("</script>");
106:
107: return flush();
108: }
109: }
110: }
|