Skip to content

Commit

Permalink
input stream must be closed only after reading results
Browse files Browse the repository at this point in the history
  • Loading branch information
GordeaS authored and GordeaS committed Jul 19, 2024
1 parent 605c8d4 commit b6495c9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import javax.xml.transform.stream.StreamSource;
import org.apache.commons.httpclient.HttpURL;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.stanbol.commons.jsonld.JsonSerializer;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -41,6 +43,9 @@
value = {"classpath:annotation.properties", "classpath:config/annotation.user.properties"},
ignoreResourceNotFound = true)
public class MetisDereferenciationClient implements InitializingBean {

private static final Logger LOG = LogManager.getLogger(MetisDereferenciationClient.class);

@Value("${metis.baseUrl}")
private String baseUrl;
@Value("${metis.connection.retries:3}")
Expand All @@ -53,7 +58,7 @@ public class MetisDereferenciationClient implements InitializingBean {
static final String PARAM_LANGS = "langs";
Transformer transformer;
private HttpConnection httpConnection;

public void afterPropertiesSet() throws Exception {
synchronized(this) {
if(StringUtils.isBlank(baseUrl)) {
Expand Down Expand Up @@ -112,7 +117,19 @@ public Map<String, String> dereferenceOne(String uri, String language) {
throw new UpstreamServerErrorRuntimeException("MetisDereferenciationClient I/O (transport) problem while obtaining the response body.", ex);
} catch (RuntimeException ex) {
throw new AnnotationDereferenciationException(ex);
} finally {
if (streamResponse != null) {
try {
streamResponse.close();
} catch (IOException e) {
if (LOG.isWarnEnabled()) {
LOG.warn("Unexpected exception occured when trying to close metis input stream", e);
}
}
}
}


return res;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public String getURLContentAsString(String url, String headerName, String header
/**
* retrieve the response as stream to be used for parsing to specific type. The Accept header is
* set to application/xml
* NOTE: the InputStrem needs to be closed by the calling methods
*
* @param url - url of the web resource
* @return - the Stream for accessing the content of the body
Expand All @@ -99,7 +100,6 @@ public InputStream getURLContentAsStream(String url) throws IOException {
GetMethod get = new GetMethod(url);
get.setRequestHeader("Accept", "application/xml");

try {
client.executeMethod(get);
if (get.getStatusCode() >= STATUS_OK_START && get.getStatusCode() <= STATUS_OK_END) {
return get.getResponseBodyAsStream();
Expand All @@ -110,9 +110,6 @@ public InputStream getURLContentAsStream(String url) throws IOException {
}
return null;
}
} finally {
get.releaseConnection();
}
}

/**
Expand Down

0 comments on commit b6495c9

Please sign in to comment.