mirror of
https://github.com/Athou/commafeed.git
synced 2026-03-21 21:37:29 +00:00
speed feeds refresh up by using http headers
This commit is contained in:
@@ -2,9 +2,14 @@ package com.commafeed.backend;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpHeaders;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.HttpStatus;
|
||||
import org.apache.http.client.ClientProtocolException;
|
||||
import org.apache.http.client.HttpResponseException;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.params.CookiePolicy;
|
||||
import org.apache.http.client.params.HttpClientParams;
|
||||
@@ -16,13 +21,28 @@ import org.apache.http.util.EntityUtils;
|
||||
|
||||
public class HttpGetter {
|
||||
|
||||
public String get(String url) throws Exception {
|
||||
return new String(getBinary(url), "UTF-8");
|
||||
public HttpResult getBinary(String url) throws ClientProtocolException,
|
||||
IOException, NotModifiedException {
|
||||
return getBinary(url, null, null);
|
||||
}
|
||||
|
||||
public byte[] getBinary(String url) throws ClientProtocolException,
|
||||
IOException {
|
||||
byte[] content = null;
|
||||
/**
|
||||
*
|
||||
* @param url
|
||||
* the url to retrive
|
||||
* @param lastModified
|
||||
* header we got last time we queried that url, or null
|
||||
* @param eTag
|
||||
* header we got last time we queried that url, or null
|
||||
* @return
|
||||
* @throws ClientProtocolException
|
||||
* @throws IOException
|
||||
* @throws NotModifiedException
|
||||
* if the url hasn't changed since we asked for it last time
|
||||
*/
|
||||
public HttpResult getBinary(String url, String lastModified, String eTag)
|
||||
throws ClientProtocolException, IOException, NotModifiedException {
|
||||
HttpResult result = null;
|
||||
|
||||
DefaultHttpClient httpclient = new DefaultHttpClient();
|
||||
HttpParams params = httpclient.getParams();
|
||||
@@ -35,14 +55,85 @@ public class HttpGetter {
|
||||
HttpGet httpget = new HttpGet(url);
|
||||
httpget.addHeader("Pragma", "No-cache");
|
||||
httpget.addHeader("Cache-Control", "no-cache");
|
||||
HttpResponse response = httpclient.execute(httpget);
|
||||
|
||||
if (lastModified != null) {
|
||||
httpget.addHeader(HttpHeaders.IF_MODIFIED_SINCE, lastModified);
|
||||
}
|
||||
if (eTag != null) {
|
||||
httpget.addHeader(HttpHeaders.IF_NONE_MATCH, eTag);
|
||||
}
|
||||
|
||||
HttpResponse response = null;
|
||||
try {
|
||||
response = httpclient.execute(httpget);
|
||||
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_MODIFIED) {
|
||||
throw new NotModifiedException();
|
||||
}
|
||||
} catch (HttpResponseException e) {
|
||||
if (e.getStatusCode() == HttpStatus.SC_NOT_MODIFIED) {
|
||||
throw new NotModifiedException();
|
||||
}
|
||||
}
|
||||
Header lastModifiedHeader = response
|
||||
.getFirstHeader(HttpHeaders.LAST_MODIFIED);
|
||||
Header eTagHeader = response.getFirstHeader(HttpHeaders.ETAG);
|
||||
HttpEntity entity = response.getEntity();
|
||||
|
||||
String lastModifiedResponse = lastModifiedHeader == null ? null
|
||||
: lastModifiedHeader.getValue();
|
||||
String eTagResponse = eTagHeader == null ? null : eTagHeader
|
||||
.getValue();
|
||||
|
||||
if (lastModified != null
|
||||
&& StringUtils.equals(lastModified, lastModifiedResponse)) {
|
||||
throw new NotModifiedException();
|
||||
}
|
||||
|
||||
if (eTag != null && StringUtils.equals(eTag, eTagResponse)) {
|
||||
throw new NotModifiedException();
|
||||
}
|
||||
|
||||
byte[] content = null;
|
||||
if (entity != null) {
|
||||
content = EntityUtils.toByteArray(entity);
|
||||
}
|
||||
result = new HttpResult(content, lastModifiedHeader == null ? null
|
||||
: lastModifiedHeader.getValue(), eTagHeader == null ? null
|
||||
: eTagHeader.getValue());
|
||||
} finally {
|
||||
httpclient.getConnectionManager().shutdown();
|
||||
}
|
||||
return content;
|
||||
return result;
|
||||
}
|
||||
|
||||
public static class HttpResult {
|
||||
|
||||
private byte[] content;
|
||||
private String lastModifiedSince;
|
||||
private String eTag;
|
||||
|
||||
public HttpResult(byte[] content, String lastModifiedSince, String eTag) {
|
||||
this.content = content;
|
||||
this.lastModifiedSince = lastModifiedSince;
|
||||
this.eTag = eTag;
|
||||
}
|
||||
|
||||
public byte[] getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
public String getLastModifiedSince() {
|
||||
return lastModifiedSince;
|
||||
}
|
||||
|
||||
public String geteTag() {
|
||||
return eTag;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class NotModifiedException extends Exception {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user