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