From b3e4f9e3c1210022027df85ee5c1ce5c1beae49f Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Mon, 30 Aug 2010 20:12:11 +0000 Subject: [PATCH] Improved HTTP cache entry serialization git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@990925 13f79535-47bb-0310-9956-ffa450edef68 --- .../http/client/cache/CachedHeaderGroup.java | 39 ----------- .../http/client/cache/HttpCacheEntry.java | 5 +- .../DefaultHttpCacheEntrySerializer.java | 69 +------------------ 3 files changed, 5 insertions(+), 108 deletions(-) delete mode 100644 httpclient-cache/src/main/java/org/apache/http/client/cache/CachedHeaderGroup.java diff --git a/httpclient-cache/src/main/java/org/apache/http/client/cache/CachedHeaderGroup.java b/httpclient-cache/src/main/java/org/apache/http/client/cache/CachedHeaderGroup.java deleted file mode 100644 index 8140ca6fa..000000000 --- a/httpclient-cache/src/main/java/org/apache/http/client/cache/CachedHeaderGroup.java +++ /dev/null @@ -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 - * . - * - */ -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; - -} diff --git a/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCacheEntry.java b/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCacheEntry.java index dfcd93c6b..bb86626d0 100644 --- a/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCacheEntry.java +++ b/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCacheEntry.java @@ -41,6 +41,7 @@ import org.apache.http.ProtocolVersion; import org.apache.http.StatusLine; import org.apache.http.annotation.Immutable; 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 @@ -58,7 +59,7 @@ public class HttpCacheEntry implements Serializable { private final Date requestDate; private final Date responseDate; private final StatusLine statusLine; - private final CachedHeaderGroup responseHeaders; + private final HeaderGroup responseHeaders; private final Resource resource; private final Set variantURIs; @@ -102,7 +103,7 @@ public class HttpCacheEntry implements Serializable { this.requestDate = requestDate; this.responseDate = responseDate; this.statusLine = statusLine; - this.responseHeaders = new CachedHeaderGroup(); + this.responseHeaders = new HeaderGroup(); this.responseHeaders.setHeaders(responseHeaders); this.resource = resource; this.variantURIs = variants != null ? new HashSet(variants) : new HashSet(); diff --git a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/DefaultHttpCacheEntrySerializer.java b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/DefaultHttpCacheEntrySerializer.java index 762359be7..9b84a7cf4 100644 --- a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/DefaultHttpCacheEntrySerializer.java +++ b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/DefaultHttpCacheEntrySerializer.java @@ -31,21 +31,11 @@ import java.io.InputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; 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.client.cache.HttpCacheEntry; import org.apache.http.client.cache.HttpCacheEntrySerializationException; 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) @@ -58,74 +48,19 @@ import org.apache.http.message.BasicStatusLine; @Immutable public class DefaultHttpCacheEntrySerializer implements HttpCacheEntrySerializer { - /** - * - * @param cacheEntry - * @param os - * @throws IOException - */ public void writeTo(HttpCacheEntry cacheEntry, OutputStream os) throws IOException { - ObjectOutputStream oos = new ObjectOutputStream(os); try { - oos.writeObject(cacheEntry.getRequestDate()); - 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()); + oos.writeObject(cacheEntry); } finally { oos.close(); } } - /** - * - * @param is - * @return the cache entry - * @throws IOException - */ - @SuppressWarnings("unchecked") public HttpCacheEntry readFrom(InputStream is) throws IOException { - ObjectInputStream ois = new ObjectInputStream(is); try { - Date requestDate = (Date)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 variants = (Set) ois.readObject(); - - return new HttpCacheEntry(requestDate, responseDate, statusLine, headers, resource, variants); + return (HttpCacheEntry) ois.readObject(); } catch (ClassNotFoundException ex) { throw new HttpCacheEntrySerializationException("Class not found: " + ex.getMessage(), ex); } finally {