makes our HTTPClient closable

pull/436/head
sgaebel 3 years ago
parent fc4275f901
commit 69adaa9f55

@ -25,6 +25,7 @@
package net.yacy.cora.protocol.http;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@ -119,7 +120,7 @@ import net.yacy.kelondro.util.NamePrefixThreadFactory;
* @author sixcooler
*
*/
public class HTTPClient {
public class HTTPClient implements Closeable {
private static final int default_timeout = 6000;
@ -468,7 +469,7 @@ public class HTTPClient {
}
}
} finally {
finish();
close();
}
}
return null;
@ -477,7 +478,7 @@ public class HTTPClient {
/**
* This method GETs a page from the server.
* to be used for streaming out
* Please take care to call finish()!
* Please take care to call close()!
*
* @param uri the url to get
* @throws IOException
@ -489,7 +490,7 @@ public class HTTPClient {
/**
* This method GETs a page from the server.
* to be used for streaming out
* Please take care to call finish()!
* Please take care to call close()!
*
* @param url the url to get
* @throws IOException
@ -536,7 +537,7 @@ public class HTTPClient {
/**
* This method POSTs a page from the server.
* to be used for streaming out
* Please take care to call finish()!
* Please take care to call close()!
*
* @param uri the url to post
* @param instream the input to post
@ -552,7 +553,7 @@ public class HTTPClient {
/**
* This method POSTs a page from the server.
* to be used for streaming out
* Please take care to call finish()!
* Please take care to call close()!
*
* @param url the url to post
* @param instream the input to post
@ -652,7 +653,7 @@ public class HTTPClient {
}
}
} finally {
finish();
close();
}
}
return null;
@ -770,7 +771,7 @@ public class HTTPClient {
/**
* This method gets direct access to the content-stream
* Since this way is uncontrolled by the Client think of using 'writeTo' instead!
* Please take care to call finish()!
* Please take care to call close()!
*
* @return the content as InputStream
* @throws IOException
@ -781,7 +782,7 @@ public class HTTPClient {
if (httpEntity != null) try {
return httpEntity.getContent();
} catch (final IOException e) {
finish();
close();
throw e;
}
}
@ -790,7 +791,7 @@ public class HTTPClient {
/**
* This method streams the content to the outputStream
* Please take care to call finish()!
* Please take care to call close()!
*
* @param outputStream
* @throws IOException
@ -802,18 +803,19 @@ public class HTTPClient {
httpEntity.writeTo(outputStream);
outputStream.flush();
} finally {
finish();
close();
}
}
}
/**
* This method ensures correct finish of client-connections
* This method ensures correct close of client-connections
* This method should be used after every use of GET or POST and writeTo or getContentstream!
*
* @throws IOException
*/
public void finish() throws IOException {
@Override
public void close() throws IOException {
try {
if (this.httpResponse != null) {
// Ensures that the entity content Stream is closed.
@ -850,7 +852,7 @@ public class HTTPClient {
}
}
} finally {
finish();
close();
}
return null;
}
@ -904,7 +906,7 @@ public class HTTPClient {
}
this.httpResponse.setHeader(HeaderFramework.RESPONSE_TIME_MILLIS, Long.toString(System.currentTimeMillis() - time));
} catch (final Throwable e) {
finish();
close();
throw new IOException("Client can't execute: "
+ (e.getCause() == null ? e.getMessage() : e.getCause().getMessage())
+ " duration=" + Long.toString(System.currentTimeMillis() - time) + " for url " + uri);

@ -60,7 +60,7 @@ public class HTTPInputStream extends InputStream {
*/
@Override
public void close() throws IOException {
httpClient.finish();
httpClient.close();
}

@ -156,7 +156,7 @@ public final class HTTPLoader {
// check redirection
if (statusCode > 299 && statusCode < 310) {
client.finish();
client.close();
final DigestURL redirectionUrl = extractRedirectURL(request, profile, url, statusline, responseHeader, requestURLString);
@ -225,7 +225,7 @@ public final class HTTPLoader {
} catch (final IOException e) {
this.log.warn("cannot write " + url + " to Cache (3): " + e.getMessage(), e);
} finally {
client.finish();
client.close();
}
contentStream = new ByteArrayInputStream(content);
@ -251,7 +251,7 @@ public final class HTTPLoader {
return new StreamResponse(new Response(request, requestHeader, responseHeader, profile, false, null), contentStream);
} else {
client.finish();
client.close();
// if the response has not the right response type then reject file
this.sb.crawlQueues.errorURL.push(request.url(), request.depth(), profile,
FailCategory.TEMPORARY_NETWORK_FAILURE, "wrong http status code", statusCode);

@ -156,7 +156,7 @@ public class opensearchdescriptionReader extends DefaultHandler {
ConcurrentLog.logException(e);
} finally {
try {
www.finish();
www.close();
} catch (final IOException e) {
ConcurrentLog.logException(e);
}
@ -187,7 +187,7 @@ public class opensearchdescriptionReader extends DefaultHandler {
return false;
} finally {
try {
www.finish();
www.close();
} catch (final IOException e) {
ConcurrentLog.logException(e);
}

@ -220,7 +220,7 @@ public class ProxyHandler extends AbstractRemoteHandler implements Handler {
} catch (final SocketException se) {
throw new ServletException("Socket Exception: " + se.getMessage());
} finally {
client.finish();
client.close();
}
// we handled this request, break out of handler chain

@ -601,7 +601,7 @@ public final class HTTPDProxyHandler {
// // client cut proxy connection, abort download
// res.abort();
// }
client.finish();
client.close();
handleProxyException(se,conProp,respond,url);
} finally {
// if opened ...
@ -609,7 +609,7 @@ public final class HTTPDProxyHandler {
// // ... close connection
// res.closeStream();
// }
client.finish();
client.close();
}
} catch (final Exception e) {
handleProxyException(e,conProp,respond,url);

Loading…
Cancel
Save