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;
018
019import java.util.Arrays;
020import java.util.Collections;
021import java.util.HashMap;
022import java.util.List;
023import java.util.Map;
024
025import org.springframework.http.HttpMethod;
026
027/**
028 * SockJS transport types.
029 *
030 * <p>JSONP support will be removed as of Spring Framework 5.1, use others transports instead.
031 *
032 * @author Rossen Stoyanchev
033 * @author Sebastien Deleuze
034 * @since 4.0
035 */
036public enum TransportType {
037
038        WEBSOCKET("websocket", HttpMethod.GET, "origin"),
039
040        XHR("xhr", HttpMethod.POST, "cors", "jsessionid", "no_cache"),
041
042        XHR_SEND("xhr_send", HttpMethod.POST, "cors", "jsessionid", "no_cache"),
043
044        @Deprecated
045        JSONP("jsonp", HttpMethod.GET, "jsessionid", "no_cache"),
046
047        @Deprecated
048        JSONP_SEND("jsonp_send", HttpMethod.POST, "jsessionid", "no_cache"),
049
050        XHR_STREAMING("xhr_streaming", HttpMethod.POST, "cors", "jsessionid", "no_cache"),
051
052        EVENT_SOURCE("eventsource", HttpMethod.GET, "origin", "jsessionid", "no_cache"),
053
054        HTML_FILE("htmlfile", HttpMethod.GET, "cors", "jsessionid", "no_cache");
055
056
057        private static final Map<String, TransportType> TRANSPORT_TYPES;
058
059        static {
060                Map<String, TransportType> transportTypes = new HashMap<String, TransportType>();
061                for (TransportType type : values()) {
062                        transportTypes.put(type.value, type);
063                }
064                TRANSPORT_TYPES = Collections.unmodifiableMap(transportTypes);
065        }
066
067        public static TransportType fromValue(String value) {
068                return TRANSPORT_TYPES.get(value);
069        }
070
071
072        private final String value;
073
074        private final HttpMethod httpMethod;
075
076        private final List<String> headerHints;
077
078
079        TransportType(String value, HttpMethod httpMethod, String... headerHints) {
080                this.value = value;
081                this.httpMethod = httpMethod;
082                this.headerHints = Arrays.asList(headerHints);
083        }
084
085
086        public String value() {
087                return this.value;
088        }
089
090        public HttpMethod getHttpMethod() {
091                return this.httpMethod;
092        }
093
094        public boolean sendsNoCacheInstruction() {
095                return this.headerHints.contains("no_cache");
096        }
097
098        public boolean sendsSessionCookie() {
099                return this.headerHints.contains("jsessionid");
100        }
101
102        public boolean supportsCors() {
103                return this.headerHints.contains("cors");
104        }
105
106        public boolean supportsOrigin() {
107                return this.headerHints.contains("cors") || this.headerHints.contains("origin");
108        }
109
110
111        @Override
112        public String toString() {
113                return this.value;
114        }
115
116}