diff options
Diffstat (limited to 'base/common/src/com/netscape/certsrv/client/PKIConnection.java')
-rw-r--r-- | base/common/src/com/netscape/certsrv/client/PKIConnection.java | 48 |
1 files changed, 47 insertions, 1 deletions
diff --git a/base/common/src/com/netscape/certsrv/client/PKIConnection.java b/base/common/src/com/netscape/certsrv/client/PKIConnection.java index 4556f1c6a..2a29db207 100644 --- a/base/common/src/com/netscape/certsrv/client/PKIConnection.java +++ b/base/common/src/com/netscape/certsrv/client/PKIConnection.java @@ -1,7 +1,11 @@ package com.netscape.certsrv.client; import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.net.InetAddress; @@ -28,12 +32,15 @@ import org.apache.http.ProtocolException; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.auth.params.AuthPNames; +import org.apache.http.client.CookieStore; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.client.params.AuthPolicy; import org.apache.http.client.params.HttpClientParams; import org.apache.http.conn.scheme.LayeredSchemeSocketFactory; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.scheme.SchemeSocketFactory; +import org.apache.http.cookie.Cookie; +import org.apache.http.impl.client.BasicCookieStore; import org.apache.http.impl.client.ClientParamsStack; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.client.DefaultRedirectStrategy; @@ -62,7 +69,8 @@ public class PKIConnection { ClientConfig config; - DefaultHttpClient httpClient = new DefaultHttpClient(); + DefaultHttpClient httpClient; + CookieStore cookieStore; ResteasyProviderFactory providerFactory; ClientErrorHandler errorHandler; @@ -71,6 +79,9 @@ public class PKIConnection { public PKIConnection(ClientConfig config) { this.config = config; + httpClient = new DefaultHttpClient(); + cookieStore = httpClient.getCookieStore(); + // Register https scheme. Scheme scheme = new Scheme("https", 443, new JSSProtocolSocketFactory()); httpClient.getConnectionManager().getSchemeRegistry().register(scheme); @@ -345,6 +356,41 @@ public class PKIConnection { } + public void loadCookies(File file) throws IOException, ClassNotFoundException { + + if (verbose) System.out.println("Loading cookies from "+file+":"); + + FileInputStream fis = new FileInputStream(file); + ObjectInputStream ois = new ObjectInputStream(fis); + + BasicCookieStore bcs = (BasicCookieStore)ois.readObject(); + + cookieStore.clear(); + for (Cookie cookie : bcs.getCookies()) { + if (verbose) System.out.println(" "+cookie.getName()+": "+cookie.getValue()); + cookieStore.addCookie(cookie); + } + + ois.close(); + } + + public void saveCookies(File file) throws IOException { + + if (verbose) System.out.println("Storing cookies into "+file+":"); + + BasicCookieStore bcs = new BasicCookieStore(); + + for (Cookie cookie : cookieStore.getCookies()) { + if (verbose) System.out.println(" "+cookie.getName()+": "+cookie.getValue()); + bcs.addCookie(cookie); + } + + FileOutputStream fos = new FileOutputStream(file); + ObjectOutputStream oos = new ObjectOutputStream(fos); + oos.writeObject(bcs); + oos.close(); + } + public <T> T createProxy(Class<T> clazz) throws URISyntaxException { URI uri = new URI(config.getServerURI()+"/rest"); return ProxyFactory.create(clazz, uri, executor, providerFactory); |