View Javadoc
1   /*
2    * Copyright 2016 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.buffer.api.BufferAllocator;
19  
20  import javax.net.ssl.SSLEngine;
21  import javax.net.ssl.SSLSessionContext;
22  import java.util.List;
23  import java.util.concurrent.Executor;
24  
25  import static java.util.Objects.requireNonNull;
26  
27  /**
28   * Adapter class which allows to wrap another {@link SslContext} and init {@link SSLEngine} instances.
29   */
30  public abstract class DelegatingSslContext extends SslContext {
31  
32      private final SslContext ctx;
33  
34      protected DelegatingSslContext(SslContext ctx) {
35          this.ctx = requireNonNull(ctx, "ctx");
36      }
37  
38      @Override
39      public final boolean isClient() {
40          return ctx.isClient();
41      }
42  
43      @Override
44      public final List<String> cipherSuites() {
45          return ctx.cipherSuites();
46      }
47  
48      @Override
49      public final long sessionCacheSize() {
50          return ctx.sessionCacheSize();
51      }
52  
53      @Override
54      public final long sessionTimeout() {
55          return ctx.sessionTimeout();
56      }
57  
58      @Override
59      public final ApplicationProtocolNegotiator applicationProtocolNegotiator() {
60          return ctx.applicationProtocolNegotiator();
61      }
62  
63      @Override
64      public final SSLEngine newEngine(BufferAllocator alloc) {
65          SSLEngine engine = ctx.newEngine(alloc);
66          initEngine(engine);
67          return engine;
68      }
69  
70      @Override
71      public final SSLEngine newEngine(BufferAllocator alloc, String peerHost, int peerPort) {
72          SSLEngine engine = ctx.newEngine(alloc, peerHost, peerPort);
73          initEngine(engine);
74          return engine;
75      }
76  
77      @Override
78      protected final SslHandler newHandler(BufferAllocator alloc, boolean startTls) {
79          SslHandler handler = ctx.newHandler(alloc, startTls);
80          initHandler(handler);
81          return handler;
82      }
83  
84      @Override
85      protected final SslHandler newHandler(BufferAllocator alloc, String peerHost, int peerPort, boolean startTls) {
86          SslHandler handler = ctx.newHandler(alloc, peerHost, peerPort, startTls);
87          initHandler(handler);
88          return handler;
89      }
90  
91      @Override
92      protected SslHandler newHandler(BufferAllocator alloc, boolean startTls, Executor executor) {
93          SslHandler handler = ctx.newHandler(alloc, startTls, executor);
94          initHandler(handler);
95          return handler;
96      }
97  
98      @Override
99      protected SslHandler newHandler(BufferAllocator alloc, String peerHost, int peerPort,
100                                     boolean startTls, Executor executor) {
101         SslHandler handler = ctx.newHandler(alloc, peerHost, peerPort, startTls, executor);
102         initHandler(handler);
103         return handler;
104     }
105 
106     @Override
107     public final SSLSessionContext sessionContext() {
108         return ctx.sessionContext();
109     }
110 
111     /**
112      * Init the {@link SSLEngine}.
113      */
114     protected abstract void initEngine(SSLEngine engine);
115 
116     /**
117      * Init the {@link SslHandler}. This will by default call {@link #initEngine(SSLEngine)}, sub-classes may override
118      * this.
119      */
120     protected void initHandler(SslHandler handler) {
121         initEngine(handler.engine());
122     }
123 }