Project

General

Profile

« Previous | Next » 

Revision 9152

Added by rnahf over 9 years ago

bullet-proofed exception handling in ReplicationService.getURLStream, to make sure client communication exceptions are handled and logged.

View differences:

src/edu/ucsb/nceas/metacat/replication/ReplicationService.java
60 60
import javax.servlet.http.HttpServletResponse;
61 61

  
62 62
import org.apache.commons.io.IOUtils;
63
import org.apache.commons.lang.exception.ExceptionUtils;
64
import org.apache.http.HttpEntity;
63 65
import org.apache.http.HttpResponse;
66
import org.apache.http.StatusLine;
67
import org.apache.http.client.ClientProtocolException;
64 68
import org.apache.http.client.HttpClient;
69
import org.apache.http.client.HttpResponseException;
65 70
import org.apache.http.impl.client.HttpClientBuilder;
66 71
import org.apache.http.impl.client.HttpClients;
67 72
import org.apache.http.client.config.RequestConfig;
......
2290 2295
	 */
2291 2296
	public static InputStream getURLStream(URL u) throws Exception {
2292 2297
	    logReplication.info("Getting url stream from " + u.toString());
2293
		logReplication.info("ReplicationService.getURLStream - Before sending request to: " + u.toString());
2294
		// use httpclient to set up SSL
2295
		RestClient client = getSSLClient();
2296
        HttpResponse response = client.doGetRequest(u.toString(),null);
2297
        // get the response content
2298
        InputStream input = response.getEntity().getContent();
2299
		logReplication.info("ReplicationService.getURLStream - After getting response from: " + u.toString());
2300
		
2298
	    logReplication.info("ReplicationService.getURLStream - Before sending request to: " + u.toString());
2299
	    // use httpclient to set up SSL
2300

  
2301
	    InputStream input = null;
2302
	    try {
2303
	        RestClient client = getSSLClient();
2304
	        HttpResponse response = client.doGetRequest(u.toString(),null);
2305
	        // get the response content
2306
	        StatusLine statusLine = response.getStatusLine();
2307
	        HttpEntity entity = response.getEntity();
2308
	        logReplication.info("ReplicationService.getURLStream - After getting response from: " + u.toString());
2309
	        if (statusLine.getStatusCode() >= 300) {
2310
	            throw new HttpResponseException(
2311
	                    statusLine.getStatusCode(),
2312
	                    "ReplicationService.getURLStream - " + statusLine.getReasonPhrase());
2313
	        }
2314
	        if (entity == null) {
2315
	            throw new ClientProtocolException("ReplicationService.getURLStream - Response contains no content");
2316
	        }
2317
	        input = entity.getContent();
2318
	    } 
2319
	    catch (Throwable t) {
2320
	        logReplication.error("Unexpected Throwable encountered.  Logging and moving on: " +
2321
	                t.getClass().getCanonicalName() + ": " + t.getMessage());
2322
	        logReplication.error(ExceptionUtils.getStackTrace(t));
2323
	        throw new Exception(t);
2324
	    }
2301 2325
		return input;
2302 2326
	}
2303 2327
	

Also available in: Unified diff