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 org.jboss.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 JettyNpnSslSession implements SSLSession {
28  
29      private final SSLEngine engine;
30      private volatile String applicationProtocol;
31  
32      JettyNpnSslSession(SSLEngine engine) {
33          this.engine = engine;
34      }
35  
36      void setApplicationProtocol(String applicationProtocol) {
37          if (applicationProtocol != null) {
38              applicationProtocol = applicationProtocol.replace(':', '_');
39          }
40          this.applicationProtocol = applicationProtocol;
41      }
42  
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      public byte[] getId() {
71          return unwrap().getId();
72      }
73  
74      public SSLSessionContext getSessionContext() {
75          return unwrap().getSessionContext();
76      }
77  
78      public long getCreationTime() {
79          return unwrap().getCreationTime();
80      }
81  
82      public long getLastAccessedTime() {
83          return unwrap().getLastAccessedTime();
84      }
85  
86      public void invalidate() {
87          unwrap().invalidate();
88      }
89  
90      public boolean isValid() {
91          return unwrap().isValid();
92      }
93  
94      public void putValue(String s, Object o) {
95          unwrap().putValue(s, o);
96      }
97  
98      public Object getValue(String s) {
99          return unwrap().getValue(s);
100     }
101 
102     public void removeValue(String s) {
103         unwrap().removeValue(s);
104     }
105 
106     public String[] getValueNames() {
107         return unwrap().getValueNames();
108     }
109 
110     public Certificate[] getPeerCertificates() throws SSLPeerUnverifiedException {
111         return unwrap().getPeerCertificates();
112     }
113 
114     public Certificate[] getLocalCertificates() {
115         return unwrap().getLocalCertificates();
116     }
117 
118     public X509Certificate[] getPeerCertificateChain() throws SSLPeerUnverifiedException {
119         return unwrap().getPeerCertificateChain();
120     }
121 
122     public Principal getPeerPrincipal() throws SSLPeerUnverifiedException {
123         return unwrap().getPeerPrincipal();
124     }
125 
126     public Principal getLocalPrincipal() {
127         return unwrap().getLocalPrincipal();
128     }
129 
130     public String getCipherSuite() {
131         return unwrap().getCipherSuite();
132     }
133 
134     public String getPeerHost() {
135         return unwrap().getPeerHost();
136     }
137 
138     public int getPeerPort() {
139         return unwrap().getPeerPort();
140     }
141 
142     public int getPacketBufferSize() {
143         return unwrap().getPacketBufferSize();
144     }
145 
146     public int getApplicationBufferSize() {
147         return unwrap().getApplicationBufferSize();
148     }
149 }