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

io.jsync.sockjs.impl.RawWebSocketTransport 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.Async;
import io.jsync.Handler;
import io.jsync.MultiMap;
import io.jsync.buffer.Buffer;
import io.jsync.http.HttpServerRequest;
import io.jsync.http.RouteMatcher;
import io.jsync.http.ServerWebSocket;
import io.jsync.http.impl.WebSocketMatcher;
import io.jsync.logging.Logger;
import io.jsync.logging.impl.LoggerFactory;
import io.jsync.sockjs.SockJSSocket;

import java.net.InetSocketAddress;

/**
 * @author Tim Fox
 */
class RawWebSocketTransport {

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

    RawWebSocketTransport(final Async async, WebSocketMatcher wsMatcher, RouteMatcher rm, String basePath,
                          final Handler sockHandler) {

        String wsRE = basePath + "/websocket";

        wsMatcher.addRegEx(wsRE, new Handler() {

            public void handle(final WebSocketMatcher.Match match) {
                SockJSSocket sock = new RawWSSockJSSocket(async, match.ws);
                sockHandler.handle(sock);
            }
        });

        rm.getWithRegEx(wsRE, new Handler() {
            public void handle(HttpServerRequest request) {
                request.response().setStatusCode(400);
                request.response().end("Can \"Upgrade\" only to \"WebSocket\".");
            }
        });

        rm.allWithRegEx(wsRE, new Handler() {
            public void handle(HttpServerRequest request) {
                request.response().headers().set("Allow", "GET");
                request.response().setStatusCode(405);
                request.response().end();
            }
        });
    }

    private static class RawWSSockJSSocket extends SockJSSocketBase {

        private ServerWebSocket ws;
        private MultiMap headers;

        RawWSSockJSSocket(Async async, ServerWebSocket ws) {
            super(async);
            this.ws = ws;
            ws.closeHandler(new Handler() {
                @Override
                public void handle(Void v) {
                    // Make sure the writeHandler gets unregistered
                    RawWSSockJSSocket.super.close();
                }
            });
        }

        public SockJSSocket dataHandler(Handler handler) {
            ws.dataHandler(handler);
            return this;
        }

        public SockJSSocket pause() {
            ws.pause();
            return this;
        }

        public SockJSSocket resume() {
            ws.resume();
            return this;
        }

        public SockJSSocket write(Buffer data) {
            ws.write(data);
            return this;
        }

        public SockJSSocket setWriteQueueMaxSize(int maxQueueSize) {
            ws.setWriteQueueMaxSize(maxQueueSize);
            return this;
        }

        public boolean writeQueueFull() {
            return ws.writeQueueFull();
        }

        public SockJSSocket drainHandler(Handler handler) {
            ws.drainHandler(handler);
            return this;
        }

        public SockJSSocket exceptionHandler(Handler handler) {
            ws.exceptionHandler(handler);
            return this;
        }

        public SockJSSocket endHandler(Handler endHandler) {
            ws.endHandler(endHandler);
            return this;
        }

        public void close() {
            super.close();
            ws.close();
        }

        @Override
        public InetSocketAddress remoteAddress() {
            return ws.remoteAddress();
        }

        @Override
        public InetSocketAddress localAddress() {
            return ws.localAddress();
        }

        @Override
        public MultiMap headers() {
            if (headers == null) {
                headers = BaseTransport.removeCookieHeaders(ws.headers());
            }
            return headers;
        }

        @Override
        public String uri() {
            return ws.uri();
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy