org.projectodd.sockjs.HtmlfileHandler Maven / Gradle / Ivy
/**
* Copyright (C) 2014 Red Hat, Inc, and individual contributors.
* Copyright (C) 2011-2012 VMware, Inc.
*/
package org.projectodd.sockjs;
import java.util.Arrays;
/**
* Handlers from sockjs-node's trans-htmlfile.coffee
*/
public class HtmlfileHandler {
private static final String IFRAME_TEMPLATE =
"\n" +
"\n" +
" \n" +
" \n" +
"Don't panic!
\n" +
" ";
public DispatchFunction htmlfile = new DispatchFunction() {
@Override
public Object handle(SockJsRequest req, SockJsResponse res, Object data) throws SockJsException {
String callback = req.getQueryParameter("c");
if (callback == null) {
callback = req.getQueryParameter("callback");
}
if (callback == null) {
throw new DispatchException(500, "\"callback\" parameter required");
}
if (callback.matches("[^a-zA-Z0-9-_.]")) {
throw new DispatchException(500, "invalid \"callback\" parameter");
}
res.setHeader("Content-Type", "text/html; charset=UTF-8");
res.writeHead(200);
char[] safariPadding = new char[1024 - IFRAME_TEMPLATE.length() + 14];
Arrays.fill(safariPadding, ' ');
String iframeTemplate = IFRAME_TEMPLATE + new String(safariPadding) + "\r\n\r\n";
res.write(iframeTemplate.replace("{{ callback }}", callback));
Transport.register(req, server, new HtmlFileReceiver(req, res, server.options));
return true;
}
};
public HtmlfileHandler(SockJsServer server) {
this.server = server;
}
private SockJsServer server;
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy