Commit 8f32c913 authored by Matija Obreza's avatar Matija Obreza
Browse files

HttpClientBuilder is not thread-safe

parent 5c8e44e5
......@@ -24,6 +24,9 @@ import java.util.List;
import javax.net.ssl.SSLPeerUnverifiedException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
......@@ -37,22 +40,15 @@ import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.genesys2.server.service.EasySMTA;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
@Profile("aws")
@Component
public class EasySMTAConnector implements EasySMTA {
private static final Log LOG = LogFactory.getLog(EasySMTAConnector.class);
@Autowired
private HttpClientBuilder httpClientBuilder;
@Value("${itpgrfa.easysmta.url}")
private String serverUrl;
......@@ -83,7 +79,7 @@ public class EasySMTAConnector implements EasySMTA {
final BufferedReader br = null;
final CloseableHttpClient httpclient = httpClientBuilder.build();
final CloseableHttpClient httpclient = HttpClientBuilder.create().build();
try {
LOG.info("Using " + serverUsername + " as Easy-SMTA username.");
response = httpclient.execute(httpPost);
......
......@@ -26,6 +26,9 @@ import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.StopWatch;
......@@ -40,15 +43,11 @@ import org.apache.http.impl.client.HttpClientBuilder;
import org.genesys2.server.model.impl.Country;
import org.genesys2.server.service.CountryNamesUpdater;
import org.genesys2.server.service.GeoService;
import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.impl.CSVReader2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
/*
The table 'alternate names' :
-----------------------------
......@@ -68,12 +67,6 @@ public class CountryAlternateNamesUpdater implements CountryNamesUpdater {
public static final Log LOG = LogFactory.getLog(CountryAlternateNamesUpdater.class);
@Autowired
private HttpClientBuilder httpClientBuilder;
@Autowired
private InstituteService instituteService;
@Autowired
private GeoService geoService;
......@@ -91,7 +84,7 @@ public class CountryAlternateNamesUpdater implements CountryNamesUpdater {
public void updateAlternateNames() throws IOException {
InputStream alternateNamesStream = null;
final CloseableHttpClient httpclient = httpClientBuilder.build();
final CloseableHttpClient httpclient = HttpClientBuilder.create().build();
final HttpGet httpget = new HttpGet(ALTERNATE_NAMES_URL);
HttpResponse response = null;
try {
......
......@@ -32,7 +32,6 @@ import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
......@@ -47,9 +46,6 @@ public class DavrosCountrySource {
public static final Log LOG = LogFactory.getLog(DavrosCountrySource.class);
@Autowired
private HttpClientBuilder httpClientBuilder;
/**
* Retrieve data from davros.org
*
......@@ -57,7 +53,7 @@ public class DavrosCountrySource {
*/
public List<CountryInfo> fetchCountryData() throws IOException {
final CloseableHttpClient httpclient = httpClientBuilder.build();
final CloseableHttpClient httpclient = HttpClientBuilder.create().build();
final HttpGet httpget = new HttpGet(DAVROS_ISO3166_URL);
HttpResponse response = null;
......
......@@ -23,6 +23,8 @@ import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import com.opencsv.CSVReader;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
......@@ -34,11 +36,8 @@ import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.opencsv.CSVReader;
/**
* Fetch and parse country information from
* http://download.geonames.org/export/dump/countryInfo.txt
......@@ -51,9 +50,6 @@ public class GeoNamesCountrySource {
public static final Log LOG = LogFactory.getLog(GeoNamesCountrySource.class);
@Autowired
private HttpClientBuilder httpClientBuilder;
/**
* Retrieve data from davros.org
*
......@@ -66,7 +62,7 @@ public class GeoNamesCountrySource {
HttpResponse response = null;
InputStream instream = null;
final CloseableHttpClient httpclient = httpClientBuilder.build();
final CloseableHttpClient httpclient = HttpClientBuilder.create().build();
try {
response = httpclient.execute(httpget);
......
......@@ -24,6 +24,8 @@ import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import com.opencsv.CSVReader;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
......@@ -43,8 +45,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Component;
import com.opencsv.CSVReader;
/**
* Update country ITPGRFA status by fetching data from
* {@link ITPGRFAStatusUpdater#ITPGRFA_STATUS_URL}.
......@@ -64,9 +64,6 @@ public class ITPGRFAStatusUpdater {
@Autowired
private TaskExecutor taskExecutor;
@Autowired
private HttpClientBuilder httpClientBuilder;
private static final int BATCH_SIZE = 20;
private static final String[] CSV_HEADERS = { "Country ISO CODE", "Country", "FAO Region 1", "FAO Region 2", "CP", "Name of NFP", "Membership", "by",
......@@ -88,7 +85,7 @@ public class ITPGRFAStatusUpdater {
final HttpGet httpget = new HttpGet(ITPGRFA_STATUS_URL);
HttpResponse response = null;
final CloseableHttpClient httpclient = httpClientBuilder.build();
final CloseableHttpClient httpclient = HttpClientBuilder.create().build();
try {
response = httpclient.execute(httpget);
......
......@@ -16,7 +16,7 @@
package org.genesys2.server.service.worker;
import static org.genesys2.util.NumberUtils.parseDoubleIgnore0;
import static org.genesys2.util.NumberUtils.*;
import java.io.IOException;
import java.io.InputStreamReader;
......@@ -29,6 +29,8 @@ import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import com.opencsv.CSVReader;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
......@@ -46,8 +48,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Component;
import com.opencsv.CSVReader;
@Component
public class InstituteUpdater {
public static final String WIEWS_EXPORT_URL = "http://www.fao.org/wiews-archive/export_c.zip";
......@@ -89,9 +89,6 @@ public class InstituteUpdater {
@Autowired
private TaskExecutor taskExecutor;
@Autowired
private HttpClientBuilder httpClientBuilder;
private static final int BATCH_SIZE = 50;
/**
......@@ -105,7 +102,7 @@ public class InstituteUpdater {
HttpResponse response = null;
final CloseableHttpClient httpclient = httpClientBuilder.build();
final CloseableHttpClient httpclient = HttpClientBuilder.create().build();
try {
response = httpclient.execute(httpget);
......
......@@ -33,6 +33,10 @@ import java.util.List;
import java.util.Set;
import java.util.UUID;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
......@@ -64,10 +68,6 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* Download data from WorldClim.org and update local cache.
* {@link WorldClimUpdaterImpl#WORLDCLIM_ORG}.
......@@ -90,9 +90,6 @@ public class WorldClimUpdater implements InitializingBean {
// http://biogeo.ucdavis.edu/data/climate/worldclim/1_4/grid/cur/bio_2-5m_bil.zip
// http://biogeo.ucdavis.edu/data/climate/worldclim/1_4/grid/cur/alt_2-5m_bil.zip
@Autowired
private HttpClientBuilder httpClientBuilder;
@Value("${download.files.dir}")
private String downloadDir;
......@@ -316,7 +313,7 @@ public class WorldClimUpdater implements InitializingBean {
File tempFile = File.createTempFile("worldclim", ".zip");
final CloseableHttpClient httpclient = httpClientBuilder.build();
final CloseableHttpClient httpclient = HttpClientBuilder.create().build();
final HttpGet httpget = new HttpGet(WORLDCLIM_ORG_FILEBASE + fileName);
HttpResponse response = null;
try {
......
......@@ -68,8 +68,4 @@ public class SpringCommonConfig {
return handler;
}
@Bean
public HttpClientBuilder httpClientBuilder() {
return HttpClientBuilder.create();
}
}
......@@ -56,11 +56,6 @@ public class WorldClimUpdaterDummyTest {
return new WorldClimUpdater();
}
@Bean
public HttpClientBuilder httpClientBuilder() {
return HttpClientBuilder.create();
}
@Bean
public GenesysLowlevelRepository genesysLowlevel() {
return new GenesysLowlevelRepositoryCustomImpl();
......
......@@ -7,7 +7,9 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.impl.client.HttpClientBuilder;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.spring.cache.HazelcastCacheManager;
import org.genesys2.server.model.dataset.DS;
import org.genesys2.server.model.dataset.DSDescriptor;
import org.genesys2.server.persistence.domain.GenesysLowlevelRepository;
......@@ -28,9 +30,6 @@ import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.spring.cache.HazelcastCacheManager;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = WorldClimUpdaterTest.Config.class, initializers = PropertyPlacholderInitializer.class)
@ActiveProfiles("dev")
......@@ -51,11 +50,6 @@ public class WorldClimUpdaterTest {
return new WorldClimUpdater();
}
@Bean
public HttpClientBuilder httpClientBuilder() {
return HttpClientBuilder.create();
}
@Bean
public DSService dsService() {
return new DSServiceImpl();
......
......@@ -159,11 +159,6 @@ public class BatchRESTServiceTest {
return new InstituteServiceImpl();
}
@Bean
public HttpClientBuilder httpClientBuilder() {
return HttpClientBuilder.create();
}
@Bean
public TaskExecutor taskExecutor() {
return new ThreadPoolTaskExecutor();
......
......@@ -404,11 +404,6 @@ public abstract class AbstractRestTest extends SpringTest {
return new EasySMTAMockConnector();
}
@Bean
public HttpClientBuilder httpClientBuilder() {
return HttpClientBuilder.create();
}
@Bean
public TokenController tokenController() {
return new TokenController();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment