View Javadoc
1   /*
2    * Copyright 2017 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.ssl;
17  
18  import io.netty5.handler.codec.ProtocolEvent;
19  
20  import javax.net.ssl.SSLSession;
21  
22  import static java.util.Objects.requireNonNull;
23  
24  /**
25   * A {@link ProtocolEvent} for a completed SSL related event.
26   */
27  public abstract class SslCompletionEvent implements ProtocolEvent {
28      private final SSLSession session;
29      private final Throwable cause;
30  
31      SslCompletionEvent(SSLSession session) {
32          this.session = session;
33          cause = null;
34      }
35  
36      SslCompletionEvent(SSLSession session, Throwable cause) {
37          this.session = session;
38          this.cause = requireNonNull(cause, "cause");
39      }
40  
41      /**
42       * Return the {@link Throwable} if {@link #isSuccess()} returns {@code false}
43       * and so the completion failed.
44       */
45      public final Throwable cause() {
46          return cause;
47      }
48  
49      /**
50       * Returns the {@link SSLSession} or {@code null} if none existed yet.
51       *
52       * @return the session.
53       */
54      public SSLSession session() {
55          return session;
56      }
57  
58      @Override
59      public String toString() {
60          final Throwable cause = cause();
61          return cause == null? getClass().getSimpleName() + "(SUCCESS)" :
62                  getClass().getSimpleName() +  '(' + cause + ')';
63      }
64  }