2 * Copyright (C) 2009 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.apache.harmony.xnet.provider.jsse;
19 import javax.net.ssl.SSLSession;
22 * Caches server sessions. Indexes by session ID. Users typically look up
23 * sessions using the ID provided by an SSL client.
25 public class ServerSessionContext extends AbstractSessionContext {
27 private SSLServerSessionCache persistentCache;
29 public ServerSessionContext() {
32 // TODO make sure SSL_CTX does not automaticaly clear sessions we want it to cache
33 // SSL_CTX_set_session_cache_mode(sslCtxNativePointer, SSL_SESS_CACHE_NO_AUTO_CLEAR);
35 // TODO remove SSL_CTX session cache limit so we can manage it
36 // SSL_CTX_sess_set_cache_size(sslCtxNativePointer, 0);
38 // TODO override trimToSize and removeEldestEntry to use
39 // SSL_CTX_sessions to remove from native cache
42 public void setPersistentCache(SSLServerSessionCache persistentCache) {
43 this.persistentCache = persistentCache;
46 protected void sessionRemoved(SSLSession session) {}
49 public SSLSession getSession(byte[] sessionId) {
50 SSLSession session = super.getSession(sessionId);
51 if (session != null) {
55 // Check persistent cache.
56 if (persistentCache != null) {
57 byte[] data = persistentCache.getSessionData(sessionId);
59 session = toSession(data, null, -1);
60 if (session != null && session.isValid()) {
61 super.putSession(session);
71 void putSession(SSLSession session) {
72 super.putSession(session);
74 // TODO: In background thread.
75 if (persistentCache != null) {
76 byte[] data = toBytes(session);
78 persistentCache.putSessionData(session, data);