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.io.IOException;
020
021import org.springframework.http.HttpStatus;
022import org.springframework.http.MediaType;
023import org.springframework.http.converter.FormHttpMessageConverter;
024import org.springframework.http.server.ServerHttpRequest;
025import org.springframework.http.server.ServerHttpResponse;
026import org.springframework.util.MultiValueMap;
027import org.springframework.util.StringUtils;
028import org.springframework.web.socket.WebSocketHandler;
029import org.springframework.web.socket.sockjs.SockJsException;
030import org.springframework.web.socket.sockjs.frame.SockJsMessageCodec;
031import org.springframework.web.socket.sockjs.transport.TransportHandler;
032import org.springframework.web.socket.sockjs.transport.TransportType;
033import org.springframework.web.socket.sockjs.transport.session.AbstractHttpSockJsSession;
034
035/**
036 * A {@link TransportHandler} that receives messages over HTTP.
037 *
038 * @author Rossen Stoyanchev
039 * @deprecated Will be removed as of Spring Framework 5.1, use others transports instead.
040 */
041@Deprecated
042public class JsonpReceivingTransportHandler extends AbstractHttpReceivingTransportHandler {
043
044        private final FormHttpMessageConverter formConverter = new FormHttpMessageConverter();
045
046
047        @Override
048        public TransportType getTransportType() {
049                return TransportType.JSONP_SEND;
050        }
051
052        @Override
053        public void handleRequestInternal(ServerHttpRequest request, ServerHttpResponse response,
054                        WebSocketHandler wsHandler, AbstractHttpSockJsSession sockJsSession) throws SockJsException {
055
056                super.handleRequestInternal(request, response, wsHandler, sockJsSession);
057                try {
058                        response.getBody().write("ok".getBytes(UTF8_CHARSET));
059                }
060                catch (IOException ex) {
061                        throw new SockJsException("Failed to write to the response body", sockJsSession.getId(), ex);
062                }
063        }
064
065        @Override
066        protected String[] readMessages(ServerHttpRequest request) throws IOException {
067                SockJsMessageCodec messageCodec = getServiceConfig().getMessageCodec();
068                MediaType contentType = request.getHeaders().getContentType();
069                if (contentType != null && MediaType.APPLICATION_FORM_URLENCODED.isCompatibleWith(contentType)) {
070                        MultiValueMap<String, String> map = this.formConverter.read(null, request);
071                        String d = map.getFirst("d");
072                        return (StringUtils.hasText(d) ? messageCodec.decode(d) : null);
073                }
074                else {
075                        return messageCodec.decodeInputStream(request.getBody());
076                }
077        }
078
079        @Override
080        protected HttpStatus getResponseStatus() {
081                return HttpStatus.OK;
082        }
083
084}