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