001/*
002 * Copyright 2002-2017 the original author or authors.
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 *      https://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
017package org.springframework.web.socket.sockjs.transport.session;
018
019import java.io.IOException;
020import java.util.Map;
021
022import org.springframework.http.server.ServerHttpRequest;
023import org.springframework.http.server.ServerHttpResponse;
024import org.springframework.util.Assert;
025import org.springframework.web.socket.WebSocketHandler;
026import org.springframework.web.socket.sockjs.SockJsTransportFailureException;
027import org.springframework.web.socket.sockjs.frame.SockJsFrame;
028import org.springframework.web.socket.sockjs.frame.SockJsMessageCodec;
029import org.springframework.web.socket.sockjs.transport.SockJsServiceConfig;
030
031/**
032 * A SockJS session for use with streaming HTTP transports.
033 *
034 * @author Rossen Stoyanchev
035 * @since 4.0
036 */
037public abstract class StreamingSockJsSession extends AbstractHttpSockJsSession {
038
039        private int byteCount;
040
041
042        public StreamingSockJsSession(String sessionId, SockJsServiceConfig config,
043                        WebSocketHandler wsHandler, Map<String, Object> attributes) {
044
045                super(sessionId, config, wsHandler, attributes);
046        }
047
048
049        /**
050         * @deprecated as of 4.2, since this method is no longer used.
051         */
052        @Override
053        @Deprecated
054        protected boolean isStreaming() {
055                return true;
056        }
057
058        /**
059         * Get the prelude to write to the response before any other data.
060         * @since 4.2
061         */
062        protected abstract byte[] getPrelude(ServerHttpRequest request);
063
064
065        @Override
066        protected void handleRequestInternal(ServerHttpRequest request, ServerHttpResponse response,
067                        boolean initialRequest) throws IOException {
068
069                byte[] prelude = getPrelude(request);
070                Assert.state(prelude != null, "Prelude expected");
071                response.getBody().write(prelude);
072                response.flush();
073
074                if (initialRequest) {
075                        writeFrame(SockJsFrame.openFrame());
076                }
077                flushCache();
078        }
079
080        @Override
081        protected void flushCache() throws SockJsTransportFailureException {
082                while (!getMessageCache().isEmpty()) {
083                        String message = getMessageCache().poll();
084                        SockJsMessageCodec messageCodec = getSockJsServiceConfig().getMessageCodec();
085                        SockJsFrame frame = SockJsFrame.messageFrame(messageCodec, message);
086                        writeFrame(frame);
087
088                        this.byteCount += (frame.getContentBytes().length + 1);
089                        if (logger.isTraceEnabled()) {
090                                logger.trace(this.byteCount + " bytes written so far, " +
091                                                getMessageCache().size() + " more messages not flushed");
092                        }
093                        if (this.byteCount >= getSockJsServiceConfig().getStreamBytesLimit()) {
094                                logger.trace("Streamed bytes limit reached, recycling current request");
095                                resetRequest();
096                                this.byteCount = 0;
097                                break;
098                        }
099                }
100                scheduleHeartbeat();
101        }
102
103}