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