Project

General

Profile

« Previous | Next » 

Revision 53932

Upgrade to commons.net:commons.net 3.6 (from 3.3) and commons-httpclient 4.5.6 (from 3.1).
using HttpConnector where needed.
httpConnector can now return an InputStream and not only a string.

View differences:

modules/dnet-modular-collector-service/trunk/src/test/java/eu/dnetlib/data/collector/plugins/oaisets/OaiSetsCollectorPluginTest.java
1 1
package eu.dnetlib.data.collector.plugins.oaisets;
2 2

  
3 3
import java.util.HashMap;
4

  
5 4
import javax.annotation.Resource;
6 5

  
6
import com.google.common.base.Joiner;
7
import eu.dnetlib.data.collector.rmi.CollectorServiceException;
8
import eu.dnetlib.data.collector.rmi.InterfaceDescriptor;
7 9
import org.junit.Before;
8 10
import org.junit.Ignore;
9 11
import org.junit.Test;
......
11 13
import org.springframework.test.context.ContextConfiguration;
12 14
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
13 15

  
14
import com.google.common.base.Joiner;
15

  
16
import eu.dnetlib.data.collector.rmi.CollectorServiceException;
17
import eu.dnetlib.data.collector.rmi.InterfaceDescriptor;
18

  
19 16
@RunWith(SpringJUnit4ClassRunner.class)
20 17
@ContextConfiguration(value = "applicationContext-OaiSetsCollectorPluginTest.xml")
21 18
public class OaiSetsCollectorPluginTest {
modules/dnet-modular-collector-service/trunk/src/test/java/eu/dnetlib/data/collector/plugins/HttpConnectorTest.java
3 3
import java.io.IOException;
4 4
import java.net.HttpURLConnection;
5 5
import java.net.URL;
6
import java.util.concurrent.TimeUnit;
7
import javax.net.ssl.HttpsURLConnection;
8
import javax.net.ssl.SSLProtocolException;
9 6

  
10 7
import eu.dnetlib.data.collector.rmi.CollectorServiceException;
11 8
import org.apache.commons.logging.Log;
12 9
import org.apache.commons.logging.LogFactory;
13
import org.apache.http.HttpResponse;
14
import org.apache.http.client.methods.HttpGet;
15
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
16
import org.apache.http.impl.client.HttpClientBuilder;
17
import org.apache.http.ssl.SSLContextBuilder;
18 10
import org.junit.Before;
19 11
import org.junit.Ignore;
20 12
import org.junit.Test;
21
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
13

  
22 14
@Ignore
23 15
public class HttpConnectorTest {
24 16

  
......
29 21
	private static final String URL_MISCONFIGURED_SERVER = "https://www.alexandria.unisg.ch/cgi/oai2?verb=Identify";
30 22
	private static final String URL_GOODSNI_SERVER = "https://air.unimi.it/oai/openaire?verb=Identify";
31 23

  
32
	private static final SSLContextBuilder sslContextBuilder = new SSLContextBuilder();
33
	private static SSLConnectionSocketFactory sslSocketFactory;
34

  
35
	private static final HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory(HttpClientBuilder
36
			.create()
37
			.setConnectionTimeToLive(0, TimeUnit.MILLISECONDS)
38
			.setMaxConnPerRoute(1)
39
			.setMaxConnTotal(1)
40
			.disableAutomaticRetries()
41
			.disableConnectionState()
42
			.setSSLSocketFactory(sslSocketFactory)
43
			.build());
44

  
45
//	static {
46
//		System.setProperty("javax.net.debug", "ssl,handshake");
47
//		System.setProperty("jsse.enableSNIExtension", "true");
48
//		try {
49
//			sslContextBuilder.loadTrustMaterial(null, (chain, authType) -> true);
50
//			SSLParameters sslParameters = new SSLParameters();
51
//			List sniHostNames = new ArrayList(1);
52
//			//sniHostNames.add(new SNIHostName(url.getHost()));
53
//			sslParameters.setServerNames(sniHostNames);
54
//			sslSocketFactory = new SSLConnectionSocketFactory(sslContextBuilder.build().se, sslParameters);
55
//
56
//		} catch (final NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) {
57
//			log.error(e);;
58
//		}
59
//	}
60

  
61 24
	@Before
62 25
	public void setUp() {
63 26
		connector = new HttpConnector();
......
75 38
		System.out.println(connector.getInputSource(URL_MISCONFIGURED_SERVER));
76 39
	}
77 40

  
78
	@Test
79
	@Ignore
41
	@Test(expected = javax.net.ssl.SSLProtocolException.class)
80 42
	public void testMisconfiguredServers2() throws IOException {
81 43
		HttpURLConnection urlConn = (HttpURLConnection) new URL(URL_MISCONFIGURED_SERVER).openConnection();
82 44
		urlConn.getResponseMessage();
83 45
	}
84 46

  
85 47
	@Test
86
	public void testDisablingSNI() throws IOException {
87
		HttpURLConnection urlConn = null;
88
		try {
89
			urlConn = (HttpURLConnection) new URL(URL_MISCONFIGURED_SERVER).openConnection();
90
			urlConn.getResponseMessage();
91
		 } catch(SSLProtocolException sslExce) {
92
			if (sslExce.getMessage() != null && sslExce.getMessage().equals("handshake alert:  unrecognized_name")) {
93
				System.out.println("Server has misconfigured SSL SNI (handshake alert:  unrecognized_name). Trying to disable SNI");
94
				if (urlConn instanceof HttpsURLConnection) {
95
					HttpResponse res = httpRequestFactory.getHttpClient().execute(new HttpGet(URL_MISCONFIGURED_SERVER));
96
					System.out.println(res.getStatusLine());
97
//					HttpsURLConnection httpsUrlConnection = (HttpsURLConnection) urlConn;
98
//					httpsUrlConnection.setSSLSocketFactory(sslSocketFactory);
99
//					httpsUrlConnection.setHostnameVerifier(new HostnameVerifier() {
100
//						public boolean verify( String s, SSLSession sess ) {
101
//							return true;
102
//						}});
103
//					httpsUrlConnection.getResponseMessage();
104
				}
105

  
106
			}
107
		}
108
	}
109

  
110

  
111

  
112

  
113
	@Test
114 48
	public void testGoodServers() throws CollectorServiceException {
115 49
		System.out.println(connector.getInputSource(URL_GOODSNI_SERVER));
116 50
	}
modules/dnet-modular-collector-service/trunk/src/test/java/eu/dnetlib/data/collector/plugins/httplist/HttpListIteratorTest.java
5 5
import java.io.IOException;
6 6
import java.util.Iterator;
7 7

  
8
import eu.dnetlib.data.collector.plugins.HttpConnector;
8 9
import org.apache.commons.io.FileUtils;
9 10
import org.junit.Before;
10 11
import org.junit.Ignore;
......
12 13

  
13 14
public class HttpListIteratorTest {
14 15

  
16
	private HttpConnector httpConnector;
15 17
	// Under test
16 18
	private Iterator<String> iter;
17 19

  
18 20
	@Before
19 21
	public void setUp() throws Exception {
20
		iter = new HttpListIterator("http://www.dlib.org/", "http://www.dlib.org/metadata/dlib_meta_files.txt");
22
		httpConnector = new HttpConnector();
23
		iter = new HttpListIterator("http://www.dlib.org/", "http://www.dlib.org/metadata/dlib_meta_files.txt", httpConnector);
21 24
	}
22 25

  
23 26
	@Test
modules/dnet-modular-collector-service/trunk/src/main/java/eu/dnetlib/data/collector/plugins/HttpCollectorPlugin.java
2 2

  
3 3
import java.io.BufferedInputStream;
4 4

  
5
import org.apache.commons.httpclient.HttpClient;
6
import org.apache.commons.httpclient.HttpMethod;
7
import org.apache.commons.httpclient.HttpStatus;
8
import org.apache.commons.httpclient.methods.GetMethod;
9

  
10 5
import eu.dnetlib.data.collector.rmi.CollectorServiceException;
6
import org.apache.commons.io.IOUtils;
7
import org.springframework.beans.factory.annotation.Autowired;
11 8

  
12 9
public class HttpCollectorPlugin extends AbstractSplittedRecordPlugin {
13 10

  
11
	@Autowired
12
	private HttpConnector httpConnector;
13

  
14 14
	@Override
15 15
	protected BufferedInputStream getBufferedInputStream(final String baseUrl) throws CollectorServiceException {
16
		try {
17
			final HttpClient client = new HttpClient();
18
			final HttpMethod method = new GetMethod(baseUrl);
19
			final int responseCode = client.executeMethod(method);
20 16

  
21
			if (HttpStatus.SC_OK != responseCode) { throw new CollectorServiceException("Error " + responseCode + " dowloading url: " + baseUrl); }
22
			return new BufferedInputStream(method.getResponseBodyAsStream());
23
		} catch (Exception e) {
24
			throw new CollectorServiceException("Error dowloading url: " + baseUrl);
25
		}
17
		String res = httpConnector.getInputSource(baseUrl);
18
		return new BufferedInputStream(IOUtils.toInputStream(res));
26 19
	}
27 20

  
28 21
}
modules/dnet-modular-collector-service/trunk/src/main/java/eu/dnetlib/data/collector/plugins/HttpConnector.java
4 4
import java.io.InputStream;
5 5
import java.net.*;
6 6
import java.security.GeneralSecurityException;
7
import java.security.cert.CertificateException;
8 7
import java.security.cert.X509Certificate;
9 8
import java.util.List;
10 9
import java.util.Map;
......
22 21

  
23 22
/**
24 23
 * @author jochen, michele, andrea
25
 *
26 24
 */
27 25
public class HttpConnector {
28 26

  
......
32 30
	private int defaultDelay = 120; // seconds
33 31
	private int readTimeOut = 120; // seconds
34 32

  
35
	private String responseType=null;
33
	private String responseType = null;
36 34

  
37 35
	private String userAgent = "Mozilla/5.0 (compatible; OAI; +http://www.openaire.eu)";
38 36

  
39
    public HttpConnector(){
40
    	CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL)); 
41
    }
42
    
37
	public HttpConnector() {
38
		CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
39
	}
40

  
43 41
	/**
44 42
	 * Given the URL returns the content via HTTP GET
45 43
	 *
......
48 46
	 * @throws CollectorServiceException when retrying more than maxNumberOfRetry times
49 47
	 */
50 48
	public String getInputSource(final String requestUrl) throws CollectorServiceException {
49
		return attemptDownlaodAsString(requestUrl, 1, new CollectorPluginErrorLogList());
50
	}
51

  
52
	/**
53
	 * Given the URL returns the content as a stream via HTTP GET
54
	 *
55
	 * @param requestUrl the URL
56
	 * @return the content of the downloaded resource as InputStream
57
	 * @throws CollectorServiceException when retrying more than maxNumberOfRetry times
58
	 */
59
	public InputStream getInputSourceAsStream(final String requestUrl) throws CollectorServiceException {
51 60
		return attemptDownload(requestUrl, 1, new CollectorPluginErrorLogList());
52 61
	}
53 62

  
63
	private String attemptDownlaodAsString(final String requestUrl, final int retryNumber, final CollectorPluginErrorLogList errorList)
64
			throws CollectorServiceException {
65
		try {
66
			InputStream s = attemptDownload(requestUrl, 1, new CollectorPluginErrorLogList());
67
			try {
68
				return IOUtils.toString(s);
69
			} catch (IOException e) {
70
				log.error("error while retrieving from http-connection occured: " + requestUrl, e);
71
				Thread.sleep(defaultDelay * 1000);
72
				errorList.add(e.getMessage());
73
				return attemptDownlaodAsString(requestUrl, retryNumber + 1, errorList);
74
			}
75
			finally{
76
				IOUtils.closeQuietly(s);
77
			}
78
		} catch (InterruptedException e) {
79
			throw new CollectorServiceException(e);
80
		}
81
	}
54 82

  
55

  
56
	private String attemptDownload(final String requestUrl, final int retryNumber, final CollectorPluginErrorLogList errorList)
83
	private InputStream attemptDownload(final String requestUrl, final int retryNumber, final CollectorPluginErrorLogList errorList)
57 84
			throws CollectorServiceException {
58 85

  
59 86
		if (retryNumber > maxNumberOfRetry) { throw new CollectorServiceException("Max number of retries exceeded. Cause: \n " + errorList); }
......
79 106
					errorList.add("503 Service Unavailable");
80 107
					urlConn.disconnect();
81 108
					return attemptDownload(requestUrl, retryNumber + 1, errorList);
82
				} else if ((urlConn.getResponseCode() == HttpURLConnection.HTTP_MOVED_PERM) || (urlConn.getResponseCode() == HttpURLConnection.HTTP_MOVED_TEMP)) {
109
				} else if ((urlConn.getResponseCode() == HttpURLConnection.HTTP_MOVED_PERM) || (urlConn.getResponseCode()
110
						== HttpURLConnection.HTTP_MOVED_TEMP)) {
83 111
					final String newUrl = obtainNewLocation(urlConn.getHeaderFields());
84 112
					log.debug("The requested url has been moved to " + newUrl);
85 113
					errorList.add(String.format("%s %s. Moved to: %s", urlConn.getResponseCode(), urlConn.getResponseMessage(), newUrl));
......
94 122
				} else {
95 123
					input = urlConn.getInputStream();
96 124
					responseType = urlConn.getContentType();
97

  
98
					return IOUtils.toString(input);
125
					return input;
99 126
				}
100 127
			} catch (IOException e) {
101 128
				log.error("error while retrieving from http-connection occured: " + requestUrl, e);
102 129
				Thread.sleep(defaultDelay * 1000);
103 130
				errorList.add(e.getMessage());
104 131
				return attemptDownload(requestUrl, retryNumber + 1, errorList);
105
			} finally {
106
				IOUtils.closeQuietly(input);
107 132
			}
108 133
		} catch (InterruptedException e) {
109 134
			throw new CollectorServiceException(e);
......
124 149

  
125 150
	private int obtainRetryAfter(final Map<String, List<String>> headerMap) {
126 151
		for (String key : headerMap.keySet()) {
127
			if ((key != null) && key.toLowerCase().equals("retry-after") && (headerMap.get(key).size() > 0) && NumberUtils.isNumber(headerMap.get(key).get(0))) { return Integer
128
					.parseInt(headerMap.get(key).get(0)) + 10; }
152
			if ((key != null) && key.toLowerCase().equals("retry-after") && (headerMap.get(key).size() > 0) && NumberUtils.isCreatable(headerMap.get(key).get(0))) {
153
				return Integer
154
						.parseInt(headerMap.get(key).get(0)) + 10;
155
			}
129 156
		}
130 157
		return -1;
131 158
	}
......
144 171
		final X509TrustManager tm = new X509TrustManager() {
145 172

  
146 173
			@Override
147
			public void checkClientTrusted(final X509Certificate[] xcs, final String string) throws CertificateException {}
174
			public void checkClientTrusted(final X509Certificate[] xcs, final String string) {
175
			}
148 176

  
149 177
			@Override
150
			public void checkServerTrusted(final X509Certificate[] xcs, final String string) throws CertificateException {}
178
			public void checkServerTrusted(final X509Certificate[] xcs, final String string) {
179
			}
151 180

  
152 181
			@Override
153 182
			public X509Certificate[] getAcceptedIssuers() {
......
164 193
		}
165 194
	}
166 195

  
167

  
168 196
	public int getMaxNumberOfRetry() {
169 197
		return maxNumberOfRetry;
170 198
	}
......
189 217
		this.readTimeOut = readTimeOut;
190 218
	}
191 219

  
192
	public String getResponseType() {return responseType;}
220
	public String getResponseType() {
221
		return responseType;
222
	}
193 223

  
194 224
}
modules/dnet-modular-collector-service/trunk/src/main/java/eu/dnetlib/data/collector/plugins/httplist/HttpListCollectorPlugin.java
1 1
package eu.dnetlib.data.collector.plugins.httplist;
2 2

  
3
import java.util.Iterator;
4

  
5 3
import eu.dnetlib.data.collector.plugin.AbstractCollectorPlugin;
4
import eu.dnetlib.data.collector.plugins.HttpConnector;
6 5
import eu.dnetlib.data.collector.rmi.CollectorServiceException;
7 6
import eu.dnetlib.data.collector.rmi.InterfaceDescriptor;
7
import org.springframework.beans.factory.annotation.Autowired;
8 8

  
9 9
public class HttpListCollectorPlugin extends AbstractCollectorPlugin {
10 10

  
11
	@Autowired
12
	private HttpConnector httpConnector;
11 13
	@Override
12 14
	public Iterable<String> collect(final InterfaceDescriptor interfaceDescriptor, final String fromDate, final String untilDate)
13 15
			throws CollectorServiceException {
14 16
		final String baseUrl = interfaceDescriptor.getBaseUrl();
15 17
		final String listAddress = interfaceDescriptor.getParams().get("listUrl");
16 18

  
17
		return new Iterable<String>() {
18

  
19
			@Override
20
			public Iterator<String> iterator() {
21
				return new HttpListIterator(baseUrl, listAddress);
22
			}
23
		};
19
		return () -> new HttpListIterator(baseUrl, listAddress, httpConnector);
24 20
	}
25 21
}
modules/dnet-modular-collector-service/trunk/src/main/java/eu/dnetlib/data/collector/plugins/httplist/HttpListIterator.java
6 6
import java.io.StringReader;
7 7
import java.util.Iterator;
8 8

  
9
import org.apache.commons.httpclient.HttpClient;
10
import org.apache.commons.httpclient.HttpMethod;
11
import org.apache.commons.httpclient.HttpStatus;
12
import org.apache.commons.httpclient.methods.GetMethod;
9
import eu.dnetlib.data.collector.plugins.HttpConnector;
10
import eu.dnetlib.data.collector.rmi.CollectorServiceException;
13 11
import org.apache.commons.io.IOUtils;
14 12
import org.apache.commons.lang3.StringUtils;
15 13

  
16 14
public class HttpListIterator implements Iterator<String> {
17 15

  
18
	final HttpClient client = new HttpClient();
16
	private HttpConnector httpConnector;
19 17

  
20 18
	private String baseUrl;
21 19
	private String currentLine;
22 20
	private BufferedReader reader;
23 21

  
24
	public HttpListIterator(final String baseUrl, final String listAddress) {
22
	public HttpListIterator(final String baseUrl, final String listAddress, final HttpConnector httpConnector) {
23
		this.httpConnector = httpConnector;
24
		this.baseUrl = baseUrl;
25 25
		try {
26
			this.baseUrl = baseUrl;
27 26
			this.reader = new BufferedReader(new StringReader(download(listAddress)));
28 27
			this.currentLine = reader.readLine();
29 28
		} catch (Exception e) {
......
55 54

  
56 55
	private String download(final String url) {
57 56
		try {
58
			final HttpMethod method = new GetMethod(url);
59
			final int responseCode = client.executeMethod(method);
60
			if (responseCode == HttpStatus.SC_OK) {
61
				return IOUtils.toString(new BufferedInputStream(method.getResponseBodyAsStream()));
62
			} else {
63
				throw new RuntimeException("Error " + responseCode + " dowloading url: " + url);
64
			}
65
		} catch (IOException e) {
66
			throw new RuntimeException("Error dowloading url: " + url);
57
			return IOUtils.toString(new BufferedInputStream(httpConnector.getInputSourceAsStream(url)));
58
		} catch(Exception e) {
59
			throw new RuntimeException(String.format("Error downloading url: %s, error: %s", url, e.getMessage()));
67 60
		}
68 61
	}
69 62

  
70 63
	@Override
71 64
	public void remove() {}
72 65

  
66
	public HttpConnector getHttpConnector() {
67
		return httpConnector;
68
	}
69

  
70
	public void setHttpConnector(final HttpConnector httpConnector) {
71
		this.httpConnector = httpConnector;
72
	}
73 73
}
modules/dnet-modular-collector-service/trunk/pom.xml
3 3
	<parent>
4 4
		<groupId>eu.dnetlib</groupId>
5 5
		<artifactId>dnet45-parent</artifactId>
6
		<version>1.0.0</version>
6
		<version>1.0.0-SNAPSHOT</version>
7 7
		<relativePath />
8 8
	</parent>
9 9
	<modelVersion>4.0.0</modelVersion>
......
50 50
		<dependency>
51 51
			<groupId>commons-net</groupId>
52 52
			<artifactId>commons-net</artifactId>
53
			<version>3.3</version>
53
			<version>[3.6, 4.0)</version>
54 54
		</dependency>
55 55
		<dependency>
56 56
			<groupId>org.apache.commons</groupId>
......
63 63
			<version>${mockito.version}</version>
64 64
			<scope>test</scope>
65 65
		</dependency>
66
		<!--<dependency>-->
67
			<!--<groupId>commons-httpclient</groupId>-->
68
			<!--<artifactId>commons-httpclient</artifactId>-->
69
			<!--<version>3.1</version>-->
70
		<!--</dependency>-->
66 71
		<dependency>
67
			<groupId>commons-httpclient</groupId>
68
			<artifactId>commons-httpclient</artifactId>
69
			<version>3.1</version>
72
			<groupId>org.apache.httpcomponents</groupId>
73
			<artifactId>httpclient</artifactId>
74
			<version>[4.5.6,5.0.0)</version>
70 75
		</dependency>
71 76
		<dependency>
72 77
			<groupId>com.google.code.gson</groupId>
......
99 104
			<artifactId>vtd-xml</artifactId>
100 105
			<version>2.13.2</version>
101 106
		</dependency>
102
	 <dependency>
103
	  <groupId>org.apache.httpcomponents</groupId>
104
	  <artifactId>httpcore</artifactId>
105
	  <version>4.4.1</version>
106
	  <scope>test</scope>
107
	  <type>jar</type>
108
	 </dependency>
109
	 <dependency>
110
	  <groupId>org.apache.httpcomponents</groupId>
111
	  <artifactId>httpclient</artifactId>
112
	  <version>4.5</version>
113
	  <scope>test</scope>
114
	  <type>jar</type>
115
	 </dependency>
107

  
116 108
	</dependencies>
117 109
</project>
modules/dnet-modular-objectstore-service/trunk/src/main/java/eu/dnetlib/data/objectstore/modular/ObjectStoreConsistency.java
1 1
package eu.dnetlib.data.objectstore.modular;
2 2

  
3
import java.util.List;
4
import java.util.Set;
5

  
3 6
import com.google.common.base.Function;
4 7
import com.google.common.collect.Lists;
5 8
import com.google.common.collect.Sets;
......
16 19
import org.springframework.web.bind.annotation.RequestMapping;
17 20
import org.springframework.web.bind.annotation.ResponseBody;
18 21

  
19
import java.util.List;
20
import java.util.Set;
21 22

  
22

  
23 23
@Controller
24 24
public class ObjectStoreConsistency {
25 25

  
modules/dnet-modular-objectstore-service/trunk/pom.xml
3 3
	<parent>
4 4
		<groupId>eu.dnetlib</groupId>
5 5
		<artifactId>dnet45-parent</artifactId>
6
		<version>1.0.0</version>
6
		<version>1.0.0-SNAPSHOT</version>
7 7
	</parent>
8 8
	<modelVersion>4.0.0</modelVersion>
9 9
	<groupId>eu.dnetlib</groupId>
......
46 46
			<scope>test</scope>
47 47
		</dependency>
48 48
		<dependency>
49
			<groupId>apache</groupId>
49
			<groupId>commons-net</groupId>
50 50
			<artifactId>commons-net</artifactId>
51
			<version>2.2</version>
51
			<version>[3.6,4.0)</version>
52 52
		</dependency>
53
		<dependency>
54
			<groupId>apache</groupId>
55
			<artifactId>commons-net-ftp</artifactId>
56
			<version>2.0</version>
57
		</dependency>
53
		<!--<dependency>-->
54
			<!--<groupId>apache</groupId>-->
55
			<!--<artifactId>commons-net-ftp</artifactId>-->
56
			<!--<version>2.0</version>-->
57
		<!--</dependency>-->
58 58

  
59 59
	</dependencies>
60 60
</project>

Also available in: Unified diff