View Javadoc
1   /*
2    * Copyright 2022 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty5.handler.codec.http.websocketx;
17  
18  import io.netty5.handler.codec.http.HttpHeaders;
19  
20  import static java.util.Objects.requireNonNull;
21  
22  /**
23   * A websocket handshake was completed on the server-side.
24   */
25  public final class WebSocketServerHandshakeCompletionEvent extends WebSocketHandshakeCompletionEvent {
26      private final String requestUri;
27      private final HttpHeaders requestHeaders;
28      private final String selectedSubprotocol;
29  
30      /**
31       * Create a new event that indicate a successful websocket handshake.
32       *
33       * @param version               the {@link WebSocketVersion} that was used.
34       * @param requestUri            the URI of the request.
35       * @param requestHeaders        the headers of the upgrade request.
36       * @param selectedSubprotocol   the selected sub-protocol, if any.
37       */
38      public WebSocketServerHandshakeCompletionEvent(WebSocketVersion version,
39              String requestUri, HttpHeaders requestHeaders, String selectedSubprotocol) {
40          super(version);
41          this.requestUri = requireNonNull(requestUri, "requestUri");
42          this.requestHeaders = requireNonNull(requestHeaders, "requestHeaders");
43          this.selectedSubprotocol = selectedSubprotocol;
44      }
45  
46      /**
47       * Create a new event that indicate a failed websocket handshake.
48       *
49       * @param cause the cause of the failure.
50       */
51      public WebSocketServerHandshakeCompletionEvent(Throwable cause) {
52          super(cause);
53          requestUri = null;
54          requestHeaders = null;
55          selectedSubprotocol = null;
56      }
57  
58      /**
59       * Return the request uri of the handshake if {@link #isSuccess()} returns {@code true}, {@code null} otherwise.
60       *
61       * @return the uri.
62       */
63      public String requestUri() {
64          return requestUri;
65      }
66  
67      /**
68       * Return the request {@link HttpHeaders} of the handshake if {@link #isSuccess()} returns {@code true},
69       * {@code null} otherwise.
70       *
71       * @return the headers.
72       */
73      public HttpHeaders requestHeaders() {
74          return requestHeaders;
75      }
76  
77      /**
78       * Return the selected sub-protocol of the handshake if {@link #isSuccess()} returns {@code true} and one was
79       * selected, {@code null} otherwise.
80       *
81       * @return the sub-protocol.
82       */
83      public String selectedSubprotocol() {
84          return selectedSubprotocol;
85      }
86  }