Improved HTTP cache entry serialization
git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@990925 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
30abf15ba6
commit
b3e4f9e3c1
|
@ -1,39 +0,0 @@
|
||||||
/*
|
|
||||||
* ====================================================================
|
|
||||||
* Licensed to the Apache Software Foundation (ASF) under one
|
|
||||||
* or more contributor license agreements. See the NOTICE file
|
|
||||||
* distributed with this work for additional information
|
|
||||||
* regarding copyright ownership. The ASF licenses this file
|
|
||||||
* to you under the Apache License, Version 2.0 (the
|
|
||||||
* "License"); you may not use this file except in compliance
|
|
||||||
* with the License. You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing,
|
|
||||||
* software distributed under the License is distributed on an
|
|
||||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
||||||
* KIND, either express or implied. See the License for the
|
|
||||||
* specific language governing permissions and limitations
|
|
||||||
* under the License.
|
|
||||||
* ====================================================================
|
|
||||||
*
|
|
||||||
* This software consists of voluntary contributions made by many
|
|
||||||
* individuals on behalf of the Apache Software Foundation. For more
|
|
||||||
* information on the Apache Software Foundation, please see
|
|
||||||
* <http://www.apache.org/>.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
package org.apache.http.client.cache;
|
|
||||||
|
|
||||||
import java.io.Serializable;
|
|
||||||
|
|
||||||
import org.apache.http.annotation.NotThreadSafe;
|
|
||||||
import org.apache.http.message.HeaderGroup;
|
|
||||||
|
|
||||||
@NotThreadSafe // because HeaderGroup is @NotThreadSafe
|
|
||||||
class CachedHeaderGroup extends HeaderGroup implements Serializable {
|
|
||||||
|
|
||||||
private static final long serialVersionUID = -4572663568087431896L;
|
|
||||||
|
|
||||||
}
|
|
|
@ -41,6 +41,7 @@ import org.apache.http.ProtocolVersion;
|
||||||
import org.apache.http.StatusLine;
|
import org.apache.http.StatusLine;
|
||||||
import org.apache.http.annotation.Immutable;
|
import org.apache.http.annotation.Immutable;
|
||||||
import org.apache.http.message.BasicHeader;
|
import org.apache.http.message.BasicHeader;
|
||||||
|
import org.apache.http.message.HeaderGroup;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Structure used to store an {@link HttpResponse} in a cache. Some entries can optionally depend
|
* Structure used to store an {@link HttpResponse} in a cache. Some entries can optionally depend
|
||||||
|
@ -58,7 +59,7 @@ public class HttpCacheEntry implements Serializable {
|
||||||
private final Date requestDate;
|
private final Date requestDate;
|
||||||
private final Date responseDate;
|
private final Date responseDate;
|
||||||
private final StatusLine statusLine;
|
private final StatusLine statusLine;
|
||||||
private final CachedHeaderGroup responseHeaders;
|
private final HeaderGroup responseHeaders;
|
||||||
private final Resource resource;
|
private final Resource resource;
|
||||||
private final Set<String> variantURIs;
|
private final Set<String> variantURIs;
|
||||||
|
|
||||||
|
@ -102,7 +103,7 @@ public class HttpCacheEntry implements Serializable {
|
||||||
this.requestDate = requestDate;
|
this.requestDate = requestDate;
|
||||||
this.responseDate = responseDate;
|
this.responseDate = responseDate;
|
||||||
this.statusLine = statusLine;
|
this.statusLine = statusLine;
|
||||||
this.responseHeaders = new CachedHeaderGroup();
|
this.responseHeaders = new HeaderGroup();
|
||||||
this.responseHeaders.setHeaders(responseHeaders);
|
this.responseHeaders.setHeaders(responseHeaders);
|
||||||
this.resource = resource;
|
this.resource = resource;
|
||||||
this.variantURIs = variants != null ? new HashSet<String>(variants) : new HashSet<String>();
|
this.variantURIs = variants != null ? new HashSet<String>(variants) : new HashSet<String>();
|
||||||
|
|
|
@ -31,21 +31,11 @@ import java.io.InputStream;
|
||||||
import java.io.ObjectInputStream;
|
import java.io.ObjectInputStream;
|
||||||
import java.io.ObjectOutputStream;
|
import java.io.ObjectOutputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.util.Date;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import org.apache.http.Header;
|
|
||||||
import org.apache.http.NameValuePair;
|
|
||||||
import org.apache.http.ProtocolVersion;
|
|
||||||
import org.apache.http.StatusLine;
|
|
||||||
import org.apache.http.annotation.Immutable;
|
import org.apache.http.annotation.Immutable;
|
||||||
import org.apache.http.client.cache.HttpCacheEntry;
|
import org.apache.http.client.cache.HttpCacheEntry;
|
||||||
import org.apache.http.client.cache.HttpCacheEntrySerializationException;
|
import org.apache.http.client.cache.HttpCacheEntrySerializationException;
|
||||||
import org.apache.http.client.cache.HttpCacheEntrySerializer;
|
import org.apache.http.client.cache.HttpCacheEntrySerializer;
|
||||||
import org.apache.http.client.cache.Resource;
|
|
||||||
import org.apache.http.message.BasicHeader;
|
|
||||||
import org.apache.http.message.BasicNameValuePair;
|
|
||||||
import org.apache.http.message.BasicStatusLine;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link HttpCacheEntrySerializer} implementation that uses the default (native)
|
* {@link HttpCacheEntrySerializer} implementation that uses the default (native)
|
||||||
|
@ -58,74 +48,19 @@ import org.apache.http.message.BasicStatusLine;
|
||||||
@Immutable
|
@Immutable
|
||||||
public class DefaultHttpCacheEntrySerializer implements HttpCacheEntrySerializer {
|
public class DefaultHttpCacheEntrySerializer implements HttpCacheEntrySerializer {
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @param cacheEntry
|
|
||||||
* @param os
|
|
||||||
* @throws IOException
|
|
||||||
*/
|
|
||||||
public void writeTo(HttpCacheEntry cacheEntry, OutputStream os) throws IOException {
|
public void writeTo(HttpCacheEntry cacheEntry, OutputStream os) throws IOException {
|
||||||
|
|
||||||
ObjectOutputStream oos = new ObjectOutputStream(os);
|
ObjectOutputStream oos = new ObjectOutputStream(os);
|
||||||
try {
|
try {
|
||||||
oos.writeObject(cacheEntry.getRequestDate());
|
oos.writeObject(cacheEntry);
|
||||||
oos.writeObject(cacheEntry.getResponseDate());
|
|
||||||
|
|
||||||
// workaround to nonserializable BasicStatusLine object
|
|
||||||
// TODO: can change to directly serialize once new httpcore is released
|
|
||||||
oos.writeObject(cacheEntry.getStatusLine().getProtocolVersion());
|
|
||||||
oos.writeObject(cacheEntry.getStatusLine().getStatusCode());
|
|
||||||
oos.writeObject(cacheEntry.getStatusLine().getReasonPhrase());
|
|
||||||
|
|
||||||
// workaround to nonserializable BasicHeader object
|
|
||||||
// TODO: can change to directly serialize once new httpcore is released
|
|
||||||
Header[] headers = cacheEntry.getAllHeaders();
|
|
||||||
NameValuePair[] headerNvps = new NameValuePair[headers.length];
|
|
||||||
for(int i = 0; i < headers.length; i++){
|
|
||||||
headerNvps[i] = new BasicNameValuePair(headers[i].getName(), headers[i].getValue());
|
|
||||||
}
|
|
||||||
oos.writeObject(headerNvps);
|
|
||||||
|
|
||||||
oos.writeObject(cacheEntry.getResource());
|
|
||||||
oos.writeObject(cacheEntry.getVariantURIs());
|
|
||||||
} finally {
|
} finally {
|
||||||
oos.close();
|
oos.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @param is
|
|
||||||
* @return the cache entry
|
|
||||||
* @throws IOException
|
|
||||||
*/
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public HttpCacheEntry readFrom(InputStream is) throws IOException {
|
public HttpCacheEntry readFrom(InputStream is) throws IOException {
|
||||||
|
|
||||||
ObjectInputStream ois = new ObjectInputStream(is);
|
ObjectInputStream ois = new ObjectInputStream(is);
|
||||||
try {
|
try {
|
||||||
Date requestDate = (Date)ois.readObject();
|
return (HttpCacheEntry) ois.readObject();
|
||||||
Date responseDate = (Date)ois.readObject();
|
|
||||||
|
|
||||||
// workaround to nonserializable BasicStatusLine object
|
|
||||||
// TODO: can change to directly serialize once new httpcore is released
|
|
||||||
ProtocolVersion pv = (ProtocolVersion) ois.readObject();
|
|
||||||
int status = (Integer) ois.readObject();
|
|
||||||
String reason = (String) ois.readObject();
|
|
||||||
StatusLine statusLine = new BasicStatusLine(pv, status, reason);
|
|
||||||
|
|
||||||
// workaround to nonserializable BasicHeader object
|
|
||||||
// TODO: can change to directly serialize once new httpcore is released
|
|
||||||
NameValuePair[] headerNvps = (NameValuePair[]) ois.readObject();
|
|
||||||
Header[] headers = new Header[headerNvps.length];
|
|
||||||
for(int i = 0; i < headerNvps.length; i++){
|
|
||||||
headers[i] = new BasicHeader(headerNvps[i].getName(), headerNvps[i].getValue());
|
|
||||||
}
|
|
||||||
|
|
||||||
Resource resource = (Resource) ois.readObject();
|
|
||||||
Set<String> variants = (Set<String>) ois.readObject();
|
|
||||||
|
|
||||||
return new HttpCacheEntry(requestDate, responseDate, statusLine, headers, resource, variants);
|
|
||||||
} catch (ClassNotFoundException ex) {
|
} catch (ClassNotFoundException ex) {
|
||||||
throw new HttpCacheEntrySerializationException("Class not found: " + ex.getMessage(), ex);
|
throw new HttpCacheEntrySerializationException("Class not found: " + ex.getMessage(), ex);
|
||||||
} finally {
|
} finally {
|
||||||
|
|
Loading…
Reference in New Issue