All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.jsync.sockjs.impl.HtmlFileTransport Maven / Gradle / Ivy

There is a newer version: 1.10.13
Show newest version
/*
 * Copyright (c) 2011-2013 The original author or authors
 * ------------------------------------------------------
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Apache License v2.0 which accompanies this distribution.
 *
 *     The Eclipse Public License is available at
 *     http://www.eclipse.org/legal/epl-v10.html
 *
 *     The Apache License v2.0 is available at
 *     http://www.opensource.org/licenses/apache2.0.php
 *
 * You may elect to redistribute this code under either of these licenses.
 */

package io.jsync.sockjs.impl;

import io.jsync.Handler;
import io.jsync.buffer.Buffer;
import io.jsync.http.HttpServerRequest;
import io.jsync.http.RouteMatcher;
import io.jsync.impl.AsyncInternal;
import io.jsync.json.JsonObject;
import io.jsync.logging.Logger;
import io.jsync.logging.impl.LoggerFactory;
import io.jsync.sockjs.SockJSSocket;

import java.util.Map;

/**
 * @author Tim Fox
 */
class HtmlFileTransport extends BaseTransport {

    private static final Logger log = LoggerFactory.getLogger(HtmlFileTransport.class);

    private static final String HTML_FILE_TEMPLATE;

    static {
        String str =
                "\n" +
                        "\n" +
                        "  \n" +
                        "  \n" +
                        "

Don't panic!

\n" + " "; String str2 = str.replace("{{ callback }}", ""); StringBuilder sb = new StringBuilder(str); int extra = 1024 - str2.length(); for (int i = 0; i < extra; i++) { sb.append(' '); } sb.append("\r\n"); HTML_FILE_TEMPLATE = sb.toString(); } HtmlFileTransport(AsyncInternal async, RouteMatcher rm, String basePath, Map sessions, final JsonObject config, final Handler sockHandler) { super(async, sessions, config); String htmlFileRE = basePath + COMMON_PATH_ELEMENT_RE + "htmlfile"; rm.getWithRegEx(htmlFileRE, new Handler() { public void handle(final HttpServerRequest req) { if (log.isTraceEnabled()) log.trace("HtmlFile, get: " + req.uri()); String callback = req.params().get("callback"); if (callback == null) { callback = req.params().get("c"); if (callback == null) { req.response().setStatusCode(500); req.response().end("\"callback\" parameter required\n"); return; } } String sessionID = req.params().get("param0"); Session session = getSession(config.getLong("session_timeout"), config.getLong("heartbeat_period"), sessionID, sockHandler); session.setInfo(req.localAddress(), req.remoteAddress(), req.uri(), req.headers()); session.register(new HtmlFileListener(config.getInteger("max_bytes_streaming"), req, callback, session)); } }); } private class HtmlFileListener extends BaseListener { final int maxBytesStreaming; final HttpServerRequest req; final String callback; final Session session; boolean headersWritten; int bytesSent; boolean closed; HtmlFileListener(int maxBytesStreaming, HttpServerRequest req, String callback, Session session) { this.maxBytesStreaming = maxBytesStreaming; this.req = req; this.callback = callback; this.session = session; addCloseHandler(req.response(), session); } public void sendFrame(String body) { if (log.isTraceEnabled()) log.trace("HtmlFile, sending frame"); if (!headersWritten) { String htmlFile = HTML_FILE_TEMPLATE.replace("{{ callback }}", callback); req.response().headers().set("Content-Type", "text/html; charset=UTF-8"); setNoCacheHeaders(req); req.response().setChunked(true); setJSESSIONID(config, req); req.response().write(htmlFile); headersWritten = true; } body = escapeForJavaScript(body); StringBuilder sb = new StringBuilder(); sb.append("\r\n"); Buffer buff = new Buffer(sb.toString()); req.response().write(buff); bytesSent += buff.length(); if (bytesSent >= maxBytesStreaming) { if (log.isTraceEnabled()) log.trace("More than maxBytes sent so closing connection"); // Reset and close the connection close(); } } public void close() { if (!closed) { try { session.resetListener(); req.response().end(); req.response().close(); closed = true; } catch (IllegalStateException e) { // Underlying connection might already be closed - that's fine } } } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy