001/* 002 * Copyright 2002-2019 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.handler; 018 019import java.util.Map; 020 021import org.springframework.http.MediaType; 022import org.springframework.http.server.ServerHttpRequest; 023import org.springframework.web.socket.WebSocketHandler; 024import org.springframework.web.socket.sockjs.frame.DefaultSockJsFrameFormat; 025import org.springframework.web.socket.sockjs.frame.SockJsFrameFormat; 026import org.springframework.web.socket.sockjs.transport.SockJsServiceConfig; 027import org.springframework.web.socket.sockjs.transport.SockJsSession; 028import org.springframework.web.socket.sockjs.transport.TransportType; 029import org.springframework.web.socket.sockjs.transport.session.StreamingSockJsSession; 030 031/** 032 * A TransportHandler for sending messages via Server-Sent Events: 033 * <a href="https://dev.w3.org/html5/eventsource/">https://dev.w3.org/html5/eventsource/</a>. 034 * 035 * @author Rossen Stoyanchev 036 * @since 4.0 037 */ 038public class EventSourceTransportHandler extends AbstractHttpSendingTransportHandler { 039 040 @Override 041 public TransportType getTransportType() { 042 return TransportType.EVENT_SOURCE; 043 } 044 045 @Override 046 protected MediaType getContentType() { 047 return new MediaType("text", "event-stream", UTF8_CHARSET); 048 } 049 050 @Override 051 public boolean checkSessionType(SockJsSession session) { 052 return (session instanceof EventSourceStreamingSockJsSession); 053 } 054 055 @Override 056 public StreamingSockJsSession createSession( 057 String sessionId, WebSocketHandler handler, Map<String, Object> attributes) { 058 059 return new EventSourceStreamingSockJsSession(sessionId, getServiceConfig(), handler, attributes); 060 } 061 062 @Override 063 protected SockJsFrameFormat getFrameFormat(ServerHttpRequest request) { 064 return new DefaultSockJsFrameFormat("data: %s\r\n\r\n"); 065 } 066 067 068 private static class EventSourceStreamingSockJsSession extends StreamingSockJsSession { 069 070 public EventSourceStreamingSockJsSession(String sessionId, SockJsServiceConfig config, 071 WebSocketHandler wsHandler, Map<String, Object> attributes) { 072 073 super(sessionId, config, wsHandler, attributes); 074 } 075 076 @Override 077 protected byte[] getPrelude(ServerHttpRequest request) { 078 return new byte[] {'\r', '\n'}; 079 } 080 } 081 082}