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