Files
Athou_commafeed/src/main/java/com/commafeed/backend/HttpGetter.java

140 lines
4.2 KiB
Java
Raw Normal View History

2013-04-03 15:53:57 +02:00
package com.commafeed.backend;
2013-04-11 12:49:54 +02:00
import java.io.IOException;
import org.apache.commons.lang.StringUtils;
import org.apache.http.Header;
2013-04-03 15:53:57 +02:00
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
2013-04-03 15:53:57 +02:00
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
2013-04-11 12:49:54 +02:00
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpResponseException;
2013-04-03 15:53:57 +02:00
import org.apache.http.client.methods.HttpGet;
2013-04-05 23:02:12 +02:00
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.client.params.HttpClientParams;
2013-04-03 15:53:57 +02:00
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
2013-04-05 23:02:12 +02:00
import org.apache.http.params.HttpParams;
2013-04-03 15:53:57 +02:00
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.util.EntityUtils;
public class HttpGetter {
public HttpResult getBinary(String url) throws ClientProtocolException,
IOException, NotModifiedException {
return getBinary(url, null, null);
2013-04-03 15:53:57 +02:00
}
/**
*
* @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;
2013-04-03 15:53:57 +02:00
2013-04-13 07:01:28 +02:00
DefaultHttpClient httpclient = new DefaultHttpClient();
2013-04-05 23:02:12 +02:00
HttpParams params = httpclient.getParams();
HttpClientParams.setCookiePolicy(params, CookiePolicy.IGNORE_COOKIES);
HttpProtocolParams.setContentCharset(params, "UTF-8");
2013-04-09 11:50:21 +02:00
HttpConnectionParams.setConnectionTimeout(params, 4000);
HttpConnectionParams.setSoTimeout(params, 4000);
2013-04-03 15:53:57 +02:00
try {
HttpGet httpget = new HttpGet(url);
2013-04-08 14:45:32 +02:00
httpget.addHeader("Pragma", "No-cache");
httpget.addHeader("Cache-Control", "no-cache");
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);
2013-04-03 15:53:57 +02:00
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;
2013-04-03 15:53:57 +02:00
if (entity != null) {
content = EntityUtils.toByteArray(entity);
}
result = new HttpResult(content, lastModifiedHeader == null ? null
: lastModifiedHeader.getValue(), eTagHeader == null ? null
: eTagHeader.getValue());
2013-04-03 15:53:57 +02:00
} finally {
httpclient.getConnectionManager().shutdown();
}
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;
2013-04-03 15:53:57 +02:00
}
}