Project

General

Profile

1
package eu.dnetlib.validator.service.impls.providers;
2

    
3
import java.io.IOException;
4
import java.io.InputStream;
5
import java.net.HttpURLConnection;
6
import java.net.URL;
7
import java.net.URLConnection;
8

    
9
import org.apache.commons.io.IOUtils;
10
import org.apache.log4j.Logger;
11

    
12
public class URLStreamer {
13
	
14
	private transient Logger logger = Logger.getLogger(URLStreamer.class);
15

    
16
	public String getResponse(URL url, int timeout, int delay, int retryDelay, int retryEfforts) throws IOException {
17
    	try {
18
			Thread.sleep(delay);
19
		} catch (InterruptedException e1) {
20
			logger.error("", e1);
21
			throw new IOException();
22
		}
23
    	
24
		InputStream inStream = null;
25
		String ret = null;
26
		for(int i=1; i<=retryEfforts; i++) {
27
			try {
28
				URLConnection urlConn = url.openConnection();
29
		    	urlConn.setConnectTimeout(timeout);
30
		    	urlConn.setReadTimeout(timeout);
31
		    	urlConn.setAllowUserInteraction(false);         
32
		    	urlConn.setDoOutput(true);
33
		    	
34
		    	if(urlConn instanceof HttpURLConnection) {
35
		    		HttpURLConnection huc = (HttpURLConnection) urlConn;
36
		    		int code = huc.getResponseCode();
37
		    		if(code < 200 || code >=300) {
38
		    			logger.error("Status code: "+code+". Retry effort: "+i);
39
						try {
40
							logger.debug("Sleeping for "+retryDelay+" millis before retrying");
41
							Thread.sleep(retryDelay);
42
						} catch (InterruptedException ex) {
43
							logger.error("", ex);
44
							throw new IOException();
45
						}
46
		    			continue;
47
		    		}
48
		    	}
49
		    	
50
		    	inStream = urlConn.getInputStream();
51
		    	
52
		    	ret = IOUtils.toString(inStream, "UTF-8");
53
			} catch(Exception e) {
54
				logger.error("Exception raised. Retry effort: "+i, e);
55
				try {
56
					logger.debug("Sleeping for "+retryDelay+" millis before retrying");
57
					Thread.sleep(retryDelay);
58
				} catch (InterruptedException ex) {
59
					logger.error("", ex);
60
					throw new IOException();
61
				}
62
				continue;
63
			} finally {
64
				if(inStream != null)
65
					inStream.close();
66
			}
67
			return ret;
68
		}
69
		return null;
70
	}
71
}
(11-11/11)