1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.websocket.api.util;
20
21 import java.net.URI;
22 import java.net.URISyntaxException;
23 import java.util.Objects;
24
25
26
27
28 public final class WSURI
29 {
30
31
32
33
34
35
36
37
38
39
40
41 public static URI toHttp(final URI inputUri) throws URISyntaxException
42 {
43 Objects.requireNonNull(inputUri,"Input URI must not be null");
44 String wsScheme = inputUri.getScheme();
45 String httpScheme = null;
46 if ("http".equalsIgnoreCase(wsScheme) || "https".equalsIgnoreCase(wsScheme))
47 {
48
49 httpScheme = wsScheme;
50 }
51 else if ("ws".equalsIgnoreCase(wsScheme))
52 {
53
54 httpScheme = "http";
55 }
56 else if ("wss".equalsIgnoreCase(wsScheme))
57 {
58
59 httpScheme = "https";
60 }
61 else
62 {
63 throw new URISyntaxException(inputUri.toString(),"Unrecognized WebSocket scheme");
64 }
65
66 return new URI(httpScheme,inputUri.getUserInfo(),inputUri.getHost(),inputUri.getPort(),inputUri.getPath(),inputUri.getQuery(),inputUri.getFragment());
67 }
68
69
70
71
72
73
74
75
76
77
78
79
80 public static URI toWebsocket(CharSequence inputUrl) throws URISyntaxException
81 {
82 return toWebsocket(new URI(inputUrl.toString()));
83 }
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98 public static URI toWebsocket(CharSequence inputUrl, String query) throws URISyntaxException
99 {
100 if (query == null)
101 {
102 return toWebsocket(new URI(inputUrl.toString()));
103 }
104 return toWebsocket(new URI(inputUrl.toString() + '?' + query));
105 }
106
107
108
109
110
111
112
113
114
115
116
117
118
119 public static URI toWebsocket(final URI inputUri) throws URISyntaxException
120 {
121 Objects.requireNonNull(inputUri,"Input URI must not be null");
122 String httpScheme = inputUri.getScheme();
123 String wsScheme = null;
124 if ("ws".equalsIgnoreCase(httpScheme) || "wss".equalsIgnoreCase(httpScheme))
125 {
126
127 wsScheme = httpScheme;
128 }
129 else if ("http".equalsIgnoreCase(httpScheme))
130 {
131
132 wsScheme = "ws";
133 }
134 else if ("https".equalsIgnoreCase(httpScheme))
135 {
136
137 wsScheme = "wss";
138 }
139 else
140 {
141 throw new URISyntaxException(inputUri.toString(),"Unrecognized HTTP scheme");
142 }
143 return new URI(wsScheme,inputUri.getUserInfo(),inputUri.getHost(),inputUri.getPort(),inputUri.getPath(),inputUri.getQuery(),inputUri.getFragment());
144 }
145 }