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  
17  package io.netty.handler.ssl;
18  
19  import javax.net.ssl.SSLEngine;
20  import javax.net.ssl.SSLPeerUnverifiedException;
21  import javax.net.ssl.SSLSession;
22  import javax.net.ssl.SSLSessionContext;
23  import javax.security.cert.X509Certificate;
24  import java.security.Principal;
25  import java.security.cert.Certificate;
26  
27  final class JdkSslSession implements SSLSession {
28      private final SSLEngine engine;
29      private volatile String applicationProtocol;
30  
31      JdkSslSession(SSLEngine engine) {
32          this.engine = engine;
33      }
34  
35      void setApplicationProtocol(String applicationProtocol) {
36          if (applicationProtocol != null) {
37              applicationProtocol = applicationProtocol.replace(':', '_');
38          }
39          this.applicationProtocol = applicationProtocol;
40      }
41  
42      @Override
43      public String getProtocol() {
44          final String protocol = unwrap().getProtocol();
45          final String applicationProtocol = this.applicationProtocol;
46  
47          if (applicationProtocol == null) {
48              if (protocol != null) {
49                  return protocol.replace(':', '_');
50              } else {
51                  return null;
52              }
53          }
54  
55          final StringBuilder buf = new StringBuilder(32);
56          if (protocol != null) {
57              buf.append(protocol.replace(':', '_'));
58              buf.append(':');
59          } else {
60              buf.append("null:");
61          }
62          buf.append(applicationProtocol);
63          return buf.toString();
64      }
65  
66      private SSLSession unwrap() {
67          return engine.getSession();
68      }
69  
70      @Override
71      public byte[] getId() {
72          return unwrap().getId();
73      }
74  
75      @Override
76      public SSLSessionContext getSessionContext() {
77          return unwrap().getSessionContext();
78      }
79  
80      @Override
81      public long getCreationTime() {
82          return unwrap().getCreationTime();
83      }
84  
85      @Override
86      public long getLastAccessedTime() {
87          return unwrap().getLastAccessedTime();
88      }
89  
90      @Override
91      public void invalidate() {
92          unwrap().invalidate();
93      }
94  
95      @Override
96      public boolean isValid() {
97          return unwrap().isValid();
98      }
99  
100     @Override
101     public void putValue(String s, Object o) {
102         unwrap().putValue(s, o);
103     }
104 
105     @Override
106     public Object getValue(String s) {
107         return unwrap().getValue(s);
108     }
109 
110     @Override
111     public void removeValue(String s) {
112         unwrap().removeValue(s);
113     }
114 
115     @Override
116     public String[] getValueNames() {
117         return unwrap().getValueNames();
118     }
119 
120     @Override
121     public Certificate[] getPeerCertificates() throws SSLPeerUnverifiedException {
122         return unwrap().getPeerCertificates();
123     }
124 
125     @Override
126     public Certificate[] getLocalCertificates() {
127         return unwrap().getLocalCertificates();
128     }
129 
130     @Override
131     public X509Certificate[] getPeerCertificateChain() throws SSLPeerUnverifiedException {
132         return unwrap().getPeerCertificateChain();
133     }
134 
135     @Override
136     public Principal getPeerPrincipal() throws SSLPeerUnverifiedException {
137         return unwrap().getPeerPrincipal();
138     }
139 
140     @Override
141     public Principal getLocalPrincipal() {
142         return unwrap().getLocalPrincipal();
143     }
144 
145     @Override
146     public String getCipherSuite() {
147         return unwrap().getCipherSuite();
148     }
149 
150     @Override
151     public String getPeerHost() {
152         return unwrap().getPeerHost();
153     }
154 
155     @Override
156     public int getPeerPort() {
157         return unwrap().getPeerPort();
158     }
159 
160     @Override
161     public int getPacketBufferSize() {
162         return unwrap().getPacketBufferSize();
163     }
164 
165     @Override
166     public int getApplicationBufferSize() {
167         return unwrap().getApplicationBufferSize();
168     }
169 }