001/* 002 * Copyright 2002-2018 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.HttpStatus; 022import org.springframework.http.MediaType; 023import org.springframework.http.server.ServerHttpRequest; 024import org.springframework.http.server.ServerHttpResponse; 025import org.springframework.util.StringUtils; 026import org.springframework.web.socket.CloseStatus; 027import org.springframework.web.socket.WebSocketHandler; 028import org.springframework.web.socket.sockjs.SockJsException; 029import org.springframework.web.socket.sockjs.SockJsTransportFailureException; 030import org.springframework.web.socket.sockjs.frame.DefaultSockJsFrameFormat; 031import org.springframework.web.socket.sockjs.frame.SockJsFrameFormat; 032import org.springframework.web.socket.sockjs.transport.SockJsSession; 033import org.springframework.web.socket.sockjs.transport.TransportType; 034import org.springframework.web.socket.sockjs.transport.session.AbstractHttpSockJsSession; 035import org.springframework.web.socket.sockjs.transport.session.PollingSockJsSession; 036import org.springframework.web.util.JavaScriptUtils; 037 038/** 039 * A TransportHandler that sends messages via JSONP polling. 040 * 041 * @author Rossen Stoyanchev 042 * @since 4.0 043 * @deprecated Will be removed as of Spring Framework 5.1, use others transports instead. 044 */ 045@Deprecated 046public class JsonpPollingTransportHandler extends AbstractHttpSendingTransportHandler { 047 048 @Override 049 public TransportType getTransportType() { 050 return TransportType.JSONP; 051 } 052 053 @Override 054 protected MediaType getContentType() { 055 return new MediaType("application", "javascript", UTF8_CHARSET); 056 } 057 058 @Override 059 public boolean checkSessionType(SockJsSession session) { 060 return session instanceof PollingSockJsSession; 061 } 062 063 @Override 064 public PollingSockJsSession createSession( 065 String sessionId, WebSocketHandler handler, Map<String, Object> attributes) { 066 067 return new PollingSockJsSession(sessionId, getServiceConfig(), handler, attributes); 068 } 069 070 @Override 071 public void handleRequestInternal(ServerHttpRequest request, ServerHttpResponse response, 072 AbstractHttpSockJsSession sockJsSession) throws SockJsException { 073 074 try { 075 String callback = getCallbackParam(request); 076 if (!StringUtils.hasText(callback)) { 077 response.setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR); 078 response.getBody().write("\"callback\" parameter required".getBytes(UTF8_CHARSET)); 079 return; 080 } 081 } 082 catch (Throwable ex) { 083 sockJsSession.tryCloseWithSockJsTransportError(ex, CloseStatus.SERVER_ERROR); 084 throw new SockJsTransportFailureException("Failed to send error", sockJsSession.getId(), ex); 085 } 086 087 super.handleRequestInternal(request, response, sockJsSession); 088 } 089 090 @Override 091 protected SockJsFrameFormat getFrameFormat(ServerHttpRequest request) { 092 // We already validated the parameter above... 093 String callback = getCallbackParam(request); 094 095 return new DefaultSockJsFrameFormat("/**/" + callback + "(\"%s\");\r\n") { 096 @Override 097 protected String preProcessContent(String content) { 098 return JavaScriptUtils.javaScriptEscape(content); 099 } 100 }; 101 } 102 103}