001/*
002 * Copyright 2002-2016 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.web.socket.WebSocketHandler;
025import org.springframework.web.socket.sockjs.SockJsTransportFailureException;
026import org.springframework.web.socket.sockjs.frame.SockJsFrame;
027import org.springframework.web.socket.sockjs.frame.SockJsMessageCodec;
028import org.springframework.web.socket.sockjs.transport.SockJsServiceConfig;
029
030/**
031 * A SockJS session for use with polling HTTP transports.
032 *
033 * @author Rossen Stoyanchev
034 * @since 4.0
035 */
036public class PollingSockJsSession extends AbstractHttpSockJsSession {
037
038        public PollingSockJsSession(String sessionId, SockJsServiceConfig config,
039                        WebSocketHandler wsHandler, Map<String, Object> attributes) {
040
041                super(sessionId, config, wsHandler, attributes);
042        }
043
044
045        @Override
046        protected void handleRequestInternal(ServerHttpRequest request, ServerHttpResponse response,
047                        boolean initialRequest) throws IOException {
048
049                if (initialRequest) {
050                        writeFrame(SockJsFrame.openFrame());
051                }
052                else if (!getMessageCache().isEmpty()) {
053                        flushCache();
054                }
055                else {
056                        scheduleHeartbeat();
057                }
058        }
059
060        @Override
061        protected void flushCache() throws SockJsTransportFailureException {
062                String[] messages = new String[getMessageCache().size()];
063                for (int i = 0; i < messages.length; i++) {
064                        messages[i] = getMessageCache().poll();
065                }
066                SockJsMessageCodec messageCodec = getSockJsServiceConfig().getMessageCodec();
067                SockJsFrame frame = SockJsFrame.messageFrame(messageCodec, messages);
068                writeFrame(frame);
069        }
070
071        @Override
072        protected void writeFrame(SockJsFrame frame) throws SockJsTransportFailureException {
073                super.writeFrame(frame);
074                resetRequest();
075        }
076
077}