/*
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2007-2008 Sun Microsystems, Inc. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can obtain
* a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
* or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
* Sun designates this particular file as subject to the "Classpath" exception
* as provided by Sun in the GPL Version 2 section of the License file that
* accompanied this code. If applicable, add the following below the License
* Header, with the fields enclosed by brackets [] replaced by your own
* identifying information: "Portions Copyrighted [year]
* [name of copyright owner]"
*
* Contributor(s):
*
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*
*/
package org.atmosphere.gwt.client.impl;
import com.google.gwt.core.client.JsArrayString;
import com.google.gwt.dom.client.BodyElement;
import com.google.gwt.dom.client.IFrameElement;
import com.google.gwt.dom.client.Node;
import com.google.gwt.dom.client.NodeList;
import com.google.gwt.user.client.rpc.SerializationException;
import com.google.gwt.user.client.rpc.StatusCodeException;
import org.atmosphere.gwt.client.AtmosphereClient;
import org.atmosphere.gwt.client.AtmosphereClientException;
import org.atmosphere.gwt.client.AtmosphereListener;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author p.havelaar
*/
public class IFrameCometTransport extends BaseCometTransport {
private String domain;
private IFrameElement iframe;
private BodyElement body;
private boolean connected;
private boolean expectingDisconnection;
@Override
public void initiate(AtmosphereClient client, AtmosphereListener listener) {
super.initiate(client, listener);
}
@Override
public void connect(int connectionCount) {
domain = getDomain(getDocumentDomain(), client.getUrl());
StringBuilder html = new StringBuilder("<html>");
if (domain != null) {
html.append("<script>document.domain='").append(domain).append("'</script>");
}
html.append("<iframe src=''></iframe></html>");
iframe = createIFrame(html.toString());
expectingDisconnection = false;
String url = getUrl(connectionCount);
if (domain != null) {
url += "&d=" + domain;
}
iframe.setSrc(url);
}
@Override
public void disconnect() {
// TODO this does not seem to close the connection immediately.
expectingDisconnection = true;
super.disconnect();
iframe.setSrc("");
// perhaps this will fix disconnecting properly
destroyIFrame();
iframe = null;
body = null;
if (connected) {
onDisconnected();
}
connected = false;
}
protected IFrameElement createIFrame(String html) {
return createIFrameImpl(this, html);
}
protected void destroyIFrame() {}
private static native IFrameElement createIFrameImpl(IFrameCometTransport client, String domain) /*-{
var div = document.createElement('div'),
iframe = document.createElement("iframe"),
span = document.createElement('span');
iframe.id = '--endless-iframe-comet-transport';
iframe.setAttribute('name', '--endless-iframe-comet-transport');
iframe.name = '--endless-iframe-comet-transport';
iframe.style.visibility = 'hidden';
span.innerHTML += '<script>document.domain=' + domain + '</script>';
div.id = '--endless-iframe-comet-transport-div';
div.style.position = 'absolute';
div.style.left = iframe.style.top = '0px';
div.style.width = iframe.style.height = '1px';
div.style.visibility = 'hidden';
div.appendChild(span);
div.appendChild(iframe);
document.getElementsByTagName('body')[0].appendChild(div);
window.m = $entry(function(message) {
client.@org.atmosphere.gwt.client.impl.IFrameCometTransport::onMessages(Lcom/google/gwt/core/client/JsArrayString;)(arguments);
});
window.c = $entry(function(heartbeat, connectionID) {
client.@org.atmosphere.gwt.client.impl.IFrameCometTransport::onConnected(II)(heartbeat, connectionID);
});
window.d = $entry(function() {
client.@org.atmosphere.gwt.client.impl.IFrameCometTransport::onDisconnected()();
});
window.e = $entry(function(statusCode, message) {
client.@org.atmosphere.gwt.client.impl.IFrameCometTransport::onError(ILjava/lang/String;)(statusCode, message);
});
window.h = $entry(function() {
client.@org.atmosphere.gwt.client.impl.IFrameCometTransport::onHeartbeat()();
});
window.r = $entry(function() {
client.@org.atmosphere.gwt.client.impl.IFrameCometTransport::onRefresh()();
});
// no $entry() because no user code is reachable
window.t = function() {
client.@org.atmosphere.gwt.client.impl.IFrameCometTransport::onTerminate()();
};
return iframe;
}-*/;
private native static String getDocumentDomain() /*-{
return $doc.domain;
}-*/;
private native static String getDomain(String documentDomain, String url) /*-{
var urlParts = /(^https?:)?(\/\/(([^:\/\?#]+)(:(\d+))?))?([^\?#]*)/.exec(url);
var urlDomain = urlParts[4];
if (!urlDomain || documentDomain == urlDomain) {
return null;
}
var documentDomainParts = documentDomain.split('.');
var urlDomainParts = urlDomain.split('.');
var d = documentDomainParts.length - 1;
var u = urlDomainParts.length - 1;
var resultDomainParts = [];
while (d >= 0 && u >= 0 && documentDomainParts[d] == urlDomainParts[u]) {
resultDomainParts.push(urlDomainParts[u]);
d--;
u--;
}
return resultDomainParts.reverse().join('.')
}-*/;
private void collect() {
if (body != null) {
NodeList<Node> childNodes = body.getChildNodes();
if (childNodes.getLength() > 1) {
body.removeChild(childNodes.getItem(0));
}
}
}
@SuppressWarnings("unused")
private void onMessages(JsArrayString arguments) {
collect();
int length = arguments.length();
List<Serializable> messages = new ArrayList<Serializable>(length);
for (int i = 0; i < length; i++) {
String message = arguments.get(i);
switch (message.charAt(0)) {
case ']':
messages.add(message.substring(1));
break;
case '[':
case 'R':
case 'r':
case 'f':
try {
messages.add(parse(message));
}
catch (SerializationException e) {
listener.onError(e, true);
}
break;
default:
listener.onError(new AtmosphereClientException("Invalid message received: " + message), true);
}
}
listener.onMessage(messages);
}
@SuppressWarnings("unused")
private void onConnected(int heartbeat, int connectionID) {
connected = true;
body = iframe.getContentDocument().getBody();
collect();
this.connectionId = connectionID;
listener.onConnected(heartbeat, connectionID);
}
private void onDisconnected() {
connected = false;
body = null;
if (expectingDisconnection) {
listener.onDisconnected();
}
else {
listener.onError(new AtmosphereClientException("Unexpected disconnection"), false);
}
}
@SuppressWarnings("unused")
private void onError(int statusCode, String message) {
listener.onError(new StatusCodeException(statusCode, message), false);
}
@SuppressWarnings("unused")
private void onHeartbeat() {
collect();
listener.onHeartbeat();
}
@SuppressWarnings("unused")
private void onRefresh() {
collect();
listener.onRefresh();
}
@SuppressWarnings("unused")
private void onTerminate() {
collect();
disconnect();
}
}