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