View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
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   * Utility methods for converting a {@link URI} between a HTTP(S) and WS(S) URI.
27   */
28  public final class WSURI
29  {
30      /**
31       * Convert to HTTP <code>http</code> or <code>https</code> scheme URIs.
32       * <p>
33       * Converting <code>ws</code> and <code>wss</code> URIs to their HTTP equivalent
34       * 
35       * @param inputUri
36       *            the input URI
37       * @return the HTTP scheme URI for the input URI.
38       * @throws URISyntaxException
39       *             if unable to convert the input URI
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              // leave alone
49              httpScheme = wsScheme;
50          }
51          else if ("ws".equalsIgnoreCase(wsScheme))
52          {
53              // convert to http
54              httpScheme = "http";
55          }
56          else if ("wss".equalsIgnoreCase(wsScheme))
57          {
58              // convert to https
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       * Convert to WebSocket <code>ws</code> or <code>wss</code> scheme URIs
71       * <p>
72       * Converting <code>http</code> and <code>https</code> URIs to their WebSocket equivalent
73       * 
74       * @param inputUrl
75       *            the input URI
76       * @return the WebSocket scheme URI for the input URI.
77       * @throws URISyntaxException
78       *             if unable to convert the input URI
79       */
80      public static URI toWebsocket(CharSequence inputUrl) throws URISyntaxException
81      {
82          return toWebsocket(new URI(inputUrl.toString()));
83      }
84  
85      /**
86       * Convert to WebSocket <code>ws</code> or <code>wss</code> scheme URIs
87       * <p>
88       * Converting <code>http</code> and <code>https</code> URIs to their WebSocket equivalent
89       * 
90       * @param inputUrl
91       *            the input URI
92       * @param query
93       *            the optional query string
94       * @return the WebSocket scheme URI for the input URI.
95       * @throws URISyntaxException
96       *             if unable to convert the input URI
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      * Convert to WebSocket <code>ws</code> or <code>wss</code> scheme URIs
109      * 
110      * <p>
111      * Converting <code>http</code> and <code>https</code> URIs to their WebSocket equivalent
112      * 
113      * @param inputUri
114      *            the input URI
115      * @return the WebSocket scheme URI for the input URI.
116      * @throws URISyntaxException
117      *             if unable to convert the input URI
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             // keep as-is
127             wsScheme = httpScheme;
128         }
129         else if ("http".equalsIgnoreCase(httpScheme))
130         {
131             // convert to ws
132             wsScheme = "ws";
133         }
134         else if ("https".equalsIgnoreCase(httpScheme))
135         {
136             // convert to wss
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 }