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:

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
}

Also available in: Unified diff