Skip to content

Commit

Permalink
Use CookieSpecs.STANDARD for all HTTP requests
Browse files Browse the repository at this point in the history
  • Loading branch information
tkuhn committed Feb 9, 2021
1 parent 780c227 commit ef10be6
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/main/java/org/nanopub/NanopubImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import org.apache.commons.lang3.tuple.Pair;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.eclipse.rdf4j.RDF4JException;
Expand Down Expand Up @@ -237,7 +239,8 @@ public NanopubImpl(URL url) throws MalformedNanopubException, RDF4JException, IO
private HttpResponse getNanopub(URL url) throws IOException {
HttpGet get = new HttpGet(url.toString());
get.setHeader("Accept", "application/trig; q=1, application/x-trig; q=1, text/x-nquads; q=0.1, application/trix; q=0.1");
HttpResponse response = HttpClientBuilder.create().build().execute(get);
RequestConfig requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build();
HttpResponse response = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build().execute(get);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 404 || statusCode == 410) {
throw new FileNotFoundException(response.getStatusLine().getReasonPhrase());
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/nanopub/extra/security/IntroNanopub.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
Expand All @@ -35,7 +36,8 @@ public class IntroNanopub {

static {
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(1000)
.setConnectionRequestTimeout(100).setSocketTimeout(1000).build();
.setConnectionRequestTimeout(100).setSocketTimeout(1000)
.setCookieSpec(CookieSpecs.STANDARD).build();
defaultHttpClient = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build();
}

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/nanopub/extra/server/FetchIndex.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.Set;

import org.apache.http.client.HttpClient;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.conn.ConnectionPoolTimeoutException;
import org.apache.http.impl.client.HttpClientBuilder;
Expand Down Expand Up @@ -83,7 +84,8 @@ public FetchIndex(String indexUri, OutputStream out, RDFFormat format, boolean w
}
nanopubCount = 0;
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(2000)
.setConnectionRequestTimeout(100).setSocketTimeout(2000).build();
.setConnectionRequestTimeout(100).setSocketTimeout(2000)
.setCookieSpec(CookieSpecs.STANDARD).build();
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
connManager.setDefaultMaxPerRoute(10);
connManager.setMaxTotal(1000);
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/nanopub/extra/server/GetNanopub.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
Expand Down Expand Up @@ -142,7 +143,8 @@ public static Nanopub get(String artifactCode, ServerInfo serverInfo)
public static Nanopub get(String artifactCode, String serverUrl)
throws IOException, RDF4JException, MalformedNanopubException {
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(1000)
.setConnectionRequestTimeout(100).setSocketTimeout(1000).build();
.setConnectionRequestTimeout(100).setSocketTimeout(1000)
.setCookieSpec(CookieSpecs.STANDARD).build();
HttpClient c = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build();
return get(artifactCode, serverUrl, c);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
Expand Down Expand Up @@ -57,7 +58,8 @@ public static List<String> loadList(String url) throws IOException {
try {
if (httpClient == null) {
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(2000)
.setConnectionRequestTimeout(100).setSocketTimeout(2000).build();
.setConnectionRequestTimeout(100).setSocketTimeout(2000)
.setCookieSpec(CookieSpecs.STANDARD).build();
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
connManager.setDefaultMaxPerRoute(10);
connManager.setMaxTotal(1000);
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/nanopub/extra/server/PublishNanopub.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
Expand Down Expand Up @@ -185,7 +187,8 @@ public void publishNanopub(Nanopub nanopub) throws IOException {
String nanopubString = NanopubUtils.writeToString(nanopub, RDFFormat.TRIG);
post.setEntity(new StringEntity(nanopubString, "UTF-8"));
post.setHeader("Content-Type", RDFFormat.TRIG.getDefaultMIMEType());
HttpResponse response = HttpClientBuilder.create().build().execute(post);
RequestConfig requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build();
HttpResponse response = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build().execute(post);
int code = response.getStatusLine().getStatusCode();
if (code >= 200 && code < 300) {
if (usedServers.containsKey(serverUrl)) {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/nanopub/extra/server/ServerInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.nio.charset.Charset;

import org.apache.http.client.HttpClient;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
Expand Down Expand Up @@ -38,7 +39,8 @@ protected static ServerInfo load(String serverUrl, Class<? extends ServerInfo> s
InputStream in = null;
try {
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(1000)
.setConnectionRequestTimeout(100).setSocketTimeout(1000).build();
.setConnectionRequestTimeout(100).setSocketTimeout(1000)
.setCookieSpec(CookieSpecs.STANDARD).build();
HttpClient c = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build();
in = c.execute(get).getEntity().getContent();
si = new Gson().fromJson(new InputStreamReader(in, Charset.forName("UTF-8")), serverInfoClass);
Expand Down

0 comments on commit ef10be6

Please sign in to comment.