View Javadoc
1   /*
2    * Copyright 2014 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 org.apache.tomcat.jni.SSL;
19  import org.apache.tomcat.jni.SSLContext;
20  
21  
22  /**
23   * {@link OpenSslSessionContext} implementation which offers extra methods which are only useful for the server-side.
24   */
25  public final class OpenSslServerSessionContext extends OpenSslSessionContext {
26      OpenSslServerSessionContext(long context) {
27          super(context);
28      }
29  
30      @Override
31      public void setSessionTimeout(int seconds) {
32          if (seconds < 0) {
33              throw new IllegalArgumentException();
34          }
35          SSLContext.setSessionCacheTimeout(context, seconds);
36      }
37  
38      @Override
39      public int getSessionTimeout() {
40          return (int) SSLContext.getSessionCacheTimeout(context);
41      }
42  
43      @Override
44      public void setSessionCacheSize(int size) {
45          if (size < 0) {
46              throw new IllegalArgumentException();
47          }
48          SSLContext.setSessionCacheSize(context, size);
49      }
50  
51      @Override
52      public int getSessionCacheSize() {
53          return (int) SSLContext.getSessionCacheSize(context);
54      }
55  
56      @Override
57      public void setSessionCacheEnabled(boolean enabled) {
58          long mode = enabled ? SSL.SSL_SESS_CACHE_SERVER : SSL.SSL_SESS_CACHE_OFF;
59          SSLContext.setSessionCacheMode(context, mode);
60      }
61  
62      @Override
63      public boolean isSessionCacheEnabled() {
64          return SSLContext.getSessionCacheMode(context) == SSL.SSL_SESS_CACHE_SERVER;
65      }
66  
67      /**
68       * Set the context within which session be reused (server side only)
69       * See <a href="http://www.openssl.org/docs/ssl/SSL_CTX_set_session_id_context.html">
70       *     man SSL_CTX_set_session_id_context</a>
71       *
72       * @param sidCtx can be any kind of binary data, it is therefore possible to use e.g. the name
73       *               of the application and/or the hostname and/or service name
74       * @return {@code true} if success, {@code false} otherwise.
75       */
76      public boolean setSessionIdContext(byte[] sidCtx) {
77          return SSLContext.setSessionIdContext(context, sidCtx);
78      }
79  }