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/PRE wrapped Javascript output.
031: * <p>Scripts begin with an html, body and pre tag followed by plain Javascript
032: * without 'execute-in-parent-context' wrapping, but with script-start and
033: * script-end markers.
034: * <p>If this conduit is used the client should direct the output to an iframe
035: * and then poll, looking for new data into the iframe. The html tags should be
036: * removed and script between script-start and script-end tags eval()ed.
037: * <p>This conduit also sends 4k of whitespace data on each flush. This causes
038: * IE to recognize new content. This would be a significant network overhead
039: * so it is important to use gzip on the connection. This complexity has caused
040: * us to turn this conduit off at the moment.
041: * @author Joe Walker [joe at getahead dot ltd dot uk]
042: */
043: public class Html4kScriptConduit extends BaseScriptConduit {
044: /**
045: * Simple ctor
046: * @param response Used to flush output
047: * @param batchId The id of the batch that we are responding to
048: * @param converterManager How we convert objects to script
049: * @throws IOException If stream actions fail
050: */
051: public Html4kScriptConduit(HttpServletResponse response,
052: String batchId, ConverterManager converterManager,
053: boolean jsonOutput) throws IOException {
054: super (response, batchId, converterManager, jsonOutput);
055: }
056:
057: /* (non-Javadoc)
058: * @see org.directwebremoting.dwrp.BaseCallMarshaller#getOutboundMimeType()
059: */
060: @Override
061: protected String getOutboundMimeType() {
062: return MimeConstants.MIME_HTML;
063: }
064:
065: /* (non-Javadoc)
066: * @see org.directwebremoting.dwrp.BaseScriptConduit#beginStream()
067: */
068: @Override
069: public void beginStream() {
070: synchronized (out) {
071: out.println("<html><body><pre>");
072:
073: out.println(ProtocolConstants.SCRIPT_START_MARKER);
074: out.println(EnginePrivate.remoteBeginIFrameResponse(
075: batchId, false));
076: out.println(ProtocolConstants.SCRIPT_END_MARKER);
077: }
078: }
079:
080: /* (non-Javadoc)
081: * @see org.directwebremoting.dwrp.BaseScriptConduit#endStream()
082: */
083: @Override
084: public void endStream() {
085: synchronized (out) {
086: out.println(ProtocolConstants.SCRIPT_START_MARKER);
087: out.println(EnginePrivate.remoteEndIFrameResponse(batchId,
088: false));
089: out.println(ProtocolConstants.SCRIPT_END_MARKER);
090:
091: out.println("</pre></body></html>");
092: }
093: }
094:
095: /* (non-Javadoc)
096: * @see org.directwebremoting.ScriptConduit#addScript(org.directwebremoting.ScriptBuffer)
097: */
098: @Override
099: public boolean addScript(ScriptBuffer scriptBuffer)
100: throws IOException, MarshallException {
101: String script = ScriptBufferUtil.createOutput(scriptBuffer,
102: converterManager, jsonOutput);
103:
104: synchronized (out) {
105: out.println(ProtocolConstants.SCRIPT_START_MARKER);
106: out.println(script);
107: out.println(ProtocolConstants.SCRIPT_END_MARKER);
108:
109: out.print(FOUR_K_FLUSH_DATA);
110: return flush();
111: }
112: }
113:
114: /**
115: * The slab of data we send to IE to get it to stream
116: */
117: protected static final String FOUR_K_FLUSH_DATA;
118: static {
119: StringBuffer buffer = new StringBuffer(409600);
120: for (int i = 0; i < 4096; i++) {
121: buffer.append(" ");
122: }
123: FOUR_K_FLUSH_DATA = buffer.toString();
124: }
125: }
|