Home | History | Annotate | Download | only in net
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      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
      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,
     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.
     15  */
     16 
     17 package android.net;
     18 
     19 import org.apache.harmony.xnet.provider.jsse.FileClientSessionCache;
     20 import org.apache.harmony.xnet.provider.jsse.SSLClientSessionCache;
     21 
     22 import android.content.Context;
     23 import android.util.Log;
     24 
     25 import java.io.File;
     26 import java.io.IOException;
     27 
     28 /**
     29  * File-based cache of established SSL sessions.  When re-establishing a
     30  * connection to the same server, using an SSL session cache can save some time,
     31  * power, and bandwidth by skipping directly to an encrypted stream.
     32  * This is a persistent cache which can span executions of the application.
     33  *
     34  * @see SSLCertificateSocketFactory
     35  */
     36 public final class SSLSessionCache {
     37     private static final String TAG = "SSLSessionCache";
     38     /* package */ final SSLClientSessionCache mSessionCache;
     39 
     40     /**
     41      * Create a session cache using the specified directory.
     42      * Individual session entries will be files within the directory.
     43      * Multiple instances for the same directory share data internally.
     44      *
     45      * @param dir to store session files in (created if necessary)
     46      * @throws IOException if the cache can't be opened
     47      */
     48     public SSLSessionCache(File dir) throws IOException {
     49         mSessionCache = FileClientSessionCache.usingDirectory(dir);
     50     }
     51 
     52     /**
     53      * Create a session cache at the default location for this app.
     54      * Multiple instances share data internally.
     55      *
     56      * @param context for the application
     57      */
     58     public SSLSessionCache(Context context) {
     59         File dir = context.getDir("sslcache", Context.MODE_PRIVATE);
     60         SSLClientSessionCache cache = null;
     61         try {
     62             cache = FileClientSessionCache.usingDirectory(dir);
     63         } catch (IOException e) {
     64             Log.w(TAG, "Unable to create SSL session cache in " + dir, e);
     65         }
     66         mSessionCache = cache;
     67     }
     68 }
     69