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.Arrays;
021import java.util.Map;
022
023import org.springframework.http.MediaType;
024import org.springframework.http.server.ServerHttpRequest;
025import org.springframework.web.socket.WebSocketHandler;
026import org.springframework.web.socket.sockjs.frame.DefaultSockJsFrameFormat;
027import org.springframework.web.socket.sockjs.frame.SockJsFrameFormat;
028import org.springframework.web.socket.sockjs.transport.SockJsServiceConfig;
029import org.springframework.web.socket.sockjs.transport.SockJsSession;
030import org.springframework.web.socket.sockjs.transport.TransportHandler;
031import org.springframework.web.socket.sockjs.transport.TransportType;
032import org.springframework.web.socket.sockjs.transport.session.StreamingSockJsSession;
033
034/**
035 * A {@link TransportHandler} that sends messages over an HTTP streaming request.
036 *
037 * @author Rossen Stoyanchev
038 * @since 4.0
039 */
040public class XhrStreamingTransportHandler extends AbstractHttpSendingTransportHandler {
041
042        private static final byte[] PRELUDE = new byte[2049];
043
044        static {
045                Arrays.fill(PRELUDE, (byte) 'h');
046                PRELUDE[2048] = '\n';
047        }
048
049
050        @Override
051        public TransportType getTransportType() {
052                return TransportType.XHR_STREAMING;
053        }
054
055        @Override
056        protected MediaType getContentType() {
057                return new MediaType("application", "javascript", StandardCharsets.UTF_8);
058        }
059
060        @Override
061        public boolean checkSessionType(SockJsSession session) {
062                return (session instanceof XhrStreamingSockJsSession);
063        }
064
065        @Override
066        public StreamingSockJsSession createSession(
067                        String sessionId, WebSocketHandler handler, Map<String, Object> attributes) {
068
069                return new XhrStreamingSockJsSession(sessionId, getServiceConfig(), handler, attributes);
070        }
071
072        @Override
073        protected SockJsFrameFormat getFrameFormat(ServerHttpRequest request) {
074                return new DefaultSockJsFrameFormat("%s\n");
075        }
076
077
078        private class XhrStreamingSockJsSession extends StreamingSockJsSession {
079
080                public XhrStreamingSockJsSession(String sessionId, SockJsServiceConfig config,
081                                WebSocketHandler wsHandler, Map<String, Object> attributes) {
082
083                        super(sessionId, config, wsHandler, attributes);
084                }
085
086                @Override
087                protected byte[] getPrelude(ServerHttpRequest request) {
088                        return PRELUDE;
089                }
090        }
091
092}