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

245 lines
7.3 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;
2013-04-19 13:24:46 +02:00
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
2013-04-11 12:49:54 +02:00
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;
2013-04-19 13:24:46 +02:00
import org.apache.http.client.HttpClient;
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-19 13:24:46 +02:00
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.conn.ssl.X509HostnameVerifier;
2013-05-02 09:32:27 +02:00
import org.apache.http.impl.client.DecompressingHttpClient;
2013-04-03 15:53:57 +02:00
import org.apache.http.impl.client.DefaultHttpClient;
2013-04-19 11:49:22 +02:00
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
2013-04-03 15:53:57 +02:00
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;
2013-04-19 13:24:46 +02:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2013-04-03 15:53:57 +02:00
public class HttpGetter {
2013-04-19 13:24:46 +02:00
private static Logger log = LoggerFactory.getLogger(HttpGetter.class);
private static SSLContext SSL_CONTEXT = null;
static {
try {
SSL_CONTEXT = SSLContext.getInstance("TLS");
SSL_CONTEXT.init(new KeyManager[0],
new TrustManager[] { new DefaultTrustManager() },
new SecureRandom());
} catch (Exception e) {
log.error("Could not configure ssl context");
}
}
private static final X509HostnameVerifier VERIFIER = new DefaultHostnameVerifier();
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-19 09:37:07 +02:00
long start = System.currentTimeMillis();
2013-04-03 15:53:57 +02:00
2013-04-19 13:24:46 +02:00
HttpClient client = newClient();
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 {
2013-04-19 13:24:46 +02:00
response = client.execute(httpget);
int code = response.getStatusLine().getStatusCode();
if (code == HttpStatus.SC_NOT_MODIFIED) {
throw new NotModifiedException();
} else if (code >= 300) {
throw new HttpResponseException(code,
"Server returned HTTP error code " + code);
}
} catch (HttpResponseException e) {
if (e.getStatusCode() == HttpStatus.SC_NOT_MODIFIED) {
throw new NotModifiedException();
} else {
throw e;
}
}
Header lastModifiedHeader = response
.getFirstHeader(HttpHeaders.LAST_MODIFIED);
Header eTagHeader = response.getFirstHeader(HttpHeaders.ETAG);
String lastModifiedResponse = lastModifiedHeader == null ? null
: lastModifiedHeader.getValue();
if (lastModified != null
&& StringUtils.equals(lastModified, lastModifiedResponse)) {
throw new NotModifiedException();
}
2013-04-19 11:49:22 +02:00
String eTagResponse = eTagHeader == null ? null : eTagHeader
.getValue();
if (eTag != null && StringUtils.equals(eTag, eTagResponse)) {
throw new NotModifiedException();
}
2013-04-19 11:49:22 +02:00
HttpEntity entity = response.getEntity();
byte[] content = null;
2013-04-03 15:53:57 +02:00
if (entity != null) {
content = EntityUtils.toByteArray(entity);
}
2013-04-19 13:24:46 +02:00
2013-04-19 09:37:07 +02:00
long duration = System.currentTimeMillis() - start;
result = new HttpResult(content, lastModifiedHeader == null ? null
: lastModifiedHeader.getValue(), eTagHeader == null ? null
2013-04-19 09:37:07 +02:00
: eTagHeader.getValue(), duration);
2013-04-03 15:53:57 +02:00
} finally {
2013-04-19 13:24:46 +02:00
client.getConnectionManager().shutdown();
2013-04-03 15:53:57 +02:00
}
return result;
}
public static class HttpResult {
private byte[] content;
private String lastModifiedSince;
private String eTag;
2013-04-19 09:37:07 +02:00
private long duration;
2013-04-19 09:37:07 +02:00
public HttpResult(byte[] content, String lastModifiedSince,
String eTag, long duration) {
this.content = content;
this.lastModifiedSince = lastModifiedSince;
this.eTag = eTag;
2013-04-19 09:37:07 +02:00
this.duration = duration;
}
public byte[] getContent() {
return content;
}
public String getLastModifiedSince() {
return lastModifiedSince;
}
public String geteTag() {
return eTag;
}
2013-04-19 09:37:07 +02:00
public long getDuration() {
return duration;
}
}
2013-04-19 13:24:46 +02:00
private static HttpClient newClient() {
DefaultHttpClient client = new DefaultHttpClient();
SSLSocketFactory ssf = new SSLSocketFactory(SSL_CONTEXT, VERIFIER);
ClientConnectionManager ccm = client.getConnectionManager();
SchemeRegistry sr = ccm.getSchemeRegistry();
sr.register(new Scheme("https", 443, ssf));
HttpParams params = client.getParams();
HttpClientParams.setCookiePolicy(params, CookiePolicy.IGNORE_COOKIES);
HttpProtocolParams.setContentCharset(params, "UTF-8");
HttpConnectionParams.setConnectionTimeout(params, 4000);
HttpConnectionParams.setSoTimeout(params, 4000);
client.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0,
false));
2013-05-02 09:32:27 +02:00
return new DecompressingHttpClient(client);
2013-04-19 13:24:46 +02:00
}
public static class NotModifiedException extends Exception {
private static final long serialVersionUID = 1L;
2013-04-03 15:53:57 +02:00
}
2013-04-19 13:24:46 +02:00
private static class DefaultTrustManager implements X509TrustManager {
@Override
public void checkClientTrusted(X509Certificate[] arg0, String arg1)
throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] arg0, String arg1)
throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
}
private static class DefaultHostnameVerifier implements
X509HostnameVerifier {
@Override
public void verify(String string, SSLSocket ssls) throws IOException {
}
@Override
public void verify(String string, X509Certificate xc)
throws SSLException {
}
@Override
public void verify(String string, String[] strings, String[] strings1)
throws SSLException {
}
@Override
public boolean verify(String string, SSLSession ssls) {
return true;
}
};
2013-04-03 15:53:57 +02:00
}