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.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 streaming HTTP transports. 032 * 033 * @author Rossen Stoyanchev 034 * @since 4.0 035 */ 036public abstract class StreamingSockJsSession extends AbstractHttpSockJsSession { 037 038 private int byteCount; 039 040 041 public StreamingSockJsSession(String sessionId, SockJsServiceConfig config, 042 WebSocketHandler wsHandler, Map<String, Object> attributes) { 043 044 super(sessionId, config, wsHandler, attributes); 045 } 046 047 048 /** 049 * Get the prelude to write to the response before any other data. 050 * @since 4.2 051 */ 052 protected abstract byte[] getPrelude(ServerHttpRequest request); 053 054 055 @Override 056 protected void handleRequestInternal(ServerHttpRequest request, ServerHttpResponse response, 057 boolean initialRequest) throws IOException { 058 059 byte[] prelude = getPrelude(request); 060 response.getBody().write(prelude); 061 response.flush(); 062 063 if (initialRequest) { 064 writeFrame(SockJsFrame.openFrame()); 065 } 066 flushCache(); 067 } 068 069 @Override 070 protected void flushCache() throws SockJsTransportFailureException { 071 while (!getMessageCache().isEmpty()) { 072 String message = getMessageCache().poll(); 073 SockJsMessageCodec messageCodec = getSockJsServiceConfig().getMessageCodec(); 074 SockJsFrame frame = SockJsFrame.messageFrame(messageCodec, message); 075 writeFrame(frame); 076 077 this.byteCount += (frame.getContentBytes().length + 1); 078 if (logger.isTraceEnabled()) { 079 logger.trace(this.byteCount + " bytes written so far, " + 080 getMessageCache().size() + " more messages not flushed"); 081 } 082 if (this.byteCount >= getSockJsServiceConfig().getStreamBytesLimit()) { 083 logger.trace("Streamed bytes limit reached, recycling current request"); 084 resetRequest(); 085 this.byteCount = 0; 086 break; 087 } 088 } 089 scheduleHeartbeat(); 090 } 091 092}