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.client;
018
019import java.net.URI;
020import java.util.UUID;
021
022import org.springframework.util.IdGenerator;
023import org.springframework.util.JdkIdGenerator;
024import org.springframework.web.socket.sockjs.transport.TransportType;
025import org.springframework.web.util.UriComponentsBuilder;
026
027/**
028 * Container for the base URL of a SockJS endpoint with additional helper methods
029 * to derive related SockJS URLs: specifically, the {@link #getInfoUrl() info}
030 * and {@link #getTransportUrl(TransportType) transport} URLs.
031 *
032 * @author Rossen Stoyanchev
033 * @since 4.1
034 */
035public class SockJsUrlInfo {
036
037        private static final IdGenerator idGenerator = new JdkIdGenerator();
038
039
040        private final URI sockJsUrl;
041
042        private String serverId;
043
044        private String sessionId;
045
046        private UUID uuid;
047
048
049        public SockJsUrlInfo(URI sockJsUrl) {
050                this.sockJsUrl = sockJsUrl;
051        }
052
053
054        public URI getSockJsUrl() {
055                return this.sockJsUrl;
056        }
057
058        public String getServerId() {
059                if (this.serverId == null) {
060                        this.serverId = String.valueOf(Math.abs(getUuid().getMostSignificantBits()) % 1000);
061                }
062                return this.serverId;
063        }
064
065        public String getSessionId() {
066                if (this.sessionId == null) {
067                        this.sessionId = getUuid().toString().replace("-", "");
068                }
069                return this.sessionId;
070        }
071
072        protected UUID getUuid() {
073                if (this.uuid == null) {
074                        this.uuid = idGenerator.generateId();
075                }
076                return this.uuid;
077        }
078
079        public URI getInfoUrl() {
080                return UriComponentsBuilder.fromUri(this.sockJsUrl)
081                                .scheme(getScheme(TransportType.XHR))
082                                .pathSegment("info")
083                                .build(true).toUri();
084        }
085
086        public URI getTransportUrl(TransportType transportType) {
087                return UriComponentsBuilder.fromUri(this.sockJsUrl)
088                                .scheme(getScheme(transportType))
089                                .pathSegment(getServerId())
090                                .pathSegment(getSessionId())
091                                .pathSegment(transportType.toString())
092                                .build(true).toUri();
093        }
094
095        private String getScheme(TransportType transportType) {
096                String scheme = this.sockJsUrl.getScheme();
097                if (TransportType.WEBSOCKET.equals(transportType)) {
098                        if (!scheme.startsWith("ws")) {
099                                scheme = ("https".equals(scheme) ? "wss" : "ws");
100                        }
101                }
102                else {
103                        if (!scheme.startsWith("http")) {
104                                scheme = ("wss".equals(scheme) ? "https" : "http");
105                        }
106                }
107                return scheme;
108        }
109
110
111        @Override
112        public String toString() {
113                return "SockJsUrlInfo[url=" + this.sockJsUrl + "]";
114        }
115
116}