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

266 lines
8.4 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;
import org.apache.http.impl.client.SystemDefaultHttpClient;
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);
2013-07-02 16:17:34 +02:00
private static final String USER_AGENT = "CommaFeed/1.0 (http://www.commafeed.com)";
private static final String ACCEPT_LANGUAGE = "en";
private static final String PRAGMA_NO_CACHE = "No-cache";
private static final String CACHE_CONTROL_NO_CACHE = "no-cache";
private static final String UTF8 = "UTF-8";
private static final String HTTPS = "https";
2013-04-19 13:24:46 +02:00
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, int timeout) throws ClientProtocolException,
IOException, NotModifiedException {
return getBinary(url, null, null, timeout);
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, int timeout)
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
HttpClient client = newClient(timeout);
2013-04-03 15:53:57 +02:00
try {
HttpGet httpget = new HttpGet(url);
2013-07-02 16:17:34 +02:00
httpget.addHeader(HttpHeaders.ACCEPT_LANGUAGE, ACCEPT_LANGUAGE);
httpget.addHeader(HttpHeaders.PRAGMA, PRAGMA_NO_CACHE);
httpget.addHeader(HttpHeaders.CACHE_CONTROL, CACHE_CONTROL_NO_CACHE);
httpget.addHeader(HttpHeaders.USER_AGENT, USER_AGENT);
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) {
2013-07-03 07:56:52 +02:00
throw new NotModifiedException("304 http code");
} else if (code >= 300) {
throw new HttpResponseException(code,
"Server returned HTTP error code " + code);
}
} catch (HttpResponseException e) {
if (e.getStatusCode() == HttpStatus.SC_NOT_MODIFIED) {
2013-07-03 07:56:52 +02:00
throw new NotModifiedException("304 http code");
} else {
throw e;
}
}
Header lastModifiedHeader = response
.getFirstHeader(HttpHeaders.LAST_MODIFIED);
Header eTagHeader = response.getFirstHeader(HttpHeaders.ETAG);
String lastModifiedResponse = lastModifiedHeader == null ? null
2013-07-03 08:09:42 +02:00
: StringUtils.trimToNull(lastModifiedHeader.getValue());
if (lastModified != null
&& StringUtils.equals(lastModified, lastModifiedResponse)) {
2013-07-03 07:56:52 +02:00
throw new NotModifiedException("lastModifiedHeader is the same");
}
2013-07-03 08:09:42 +02:00
String eTagResponse = eTagHeader == null ? null : StringUtils.trimToNull(eTagHeader.getValue());
if (eTag != null && StringUtils.equals(eTag, eTagResponse)) {
2013-07-03 07:56:52 +02:00
throw new NotModifiedException("eTagHeader is the same");
}
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;
2013-06-11 07:18:04 +02:00
Header contentType = entity.getContentType();
result = new HttpResult(content, contentType == null ? null
: contentType.getValue(), 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;
2013-06-11 07:18:04 +02:00
private String contentType;
private String lastModifiedSince;
private String eTag;
2013-04-19 09:37:07 +02:00
private long duration;
2013-06-11 07:18:04 +02:00
public HttpResult(byte[] content, String contentType,
String lastModifiedSince, String eTag, long duration) {
this.content = content;
2013-06-11 07:18:04 +02:00
this.contentType = contentType;
this.lastModifiedSince = lastModifiedSince;
this.eTag = eTag;
2013-04-19 09:37:07 +02:00
this.duration = duration;
}
public byte[] getContent() {
return content;
}
2013-06-11 07:18:04 +02:00
public String getContentType() {
return contentType;
}
public String getLastModifiedSince() {
return lastModifiedSince;
}
public String geteTag() {
return eTag;
}
2013-04-19 09:37:07 +02:00
public long getDuration() {
return duration;
}
}
public static HttpClient newClient(int timeout) {
DefaultHttpClient client = new SystemDefaultHttpClient();
2013-04-19 13:24:46 +02:00
SSLSocketFactory ssf = new SSLSocketFactory(SSL_CONTEXT, VERIFIER);
ClientConnectionManager ccm = client.getConnectionManager();
SchemeRegistry sr = ccm.getSchemeRegistry();
2013-07-02 16:17:34 +02:00
sr.register(new Scheme(HTTPS, 443, ssf));
2013-04-19 13:24:46 +02:00
HttpParams params = client.getParams();
HttpClientParams.setCookiePolicy(params, CookiePolicy.IGNORE_COOKIES);
2013-07-02 16:17:34 +02:00
HttpProtocolParams.setContentCharset(params, UTF8);
HttpConnectionParams.setConnectionTimeout(params, timeout);
HttpConnectionParams.setSoTimeout(params, timeout);
2013-04-19 13:24:46 +02:00
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-07-03 08:09:42 +02:00
public NotModifiedException(String message) {
2013-07-03 07:56:52 +02:00
super(message);
}
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
}