Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
Expand Down Expand Up @@ -275,7 +276,7 @@ public String getHttpClientResponseEntity(String url, String method, String body
}
}

public CloseableHttpResponse getHttpClientResponse(String url, String method, String body) throws IOException {
public InputStream getHttpClientResponse(String url, String method, String body) throws IOException {
int timeout = getMDMConnectionTimeout(method);
LOGGER.debug("MDM HTTP CALL method : {} with timeout {}", method, timeout);
RequestConfig requestConfig = RequestConfig.custom()
Expand All @@ -284,12 +285,36 @@ public CloseableHttpResponse getHttpClientResponse(String url, String method, St
.setConnectionRequestTimeout(timeout)
.build();
CloseableHttpClient client = HttpClients.createDefault();
StringEntity requestEntity = new StringEntity(body, ContentType.create("Content-Type", Consts.UTF_8));
HttpUriRequest httpUriRequest = RequestBuilder.create(method)
.setConfig(requestConfig)
.setUri(url)
.setEntity(requestEntity)
.build();
return client.execute(httpUriRequest);
try {
StringEntity requestEntity = new StringEntity(body, ContentType.create("Content-Type", Consts.UTF_8));
HttpUriRequest httpUriRequest = RequestBuilder.create(method)
.setConfig(requestConfig)
.setUri(url)
.setEntity(requestEntity)
.build();
CloseableHttpResponse response = client.execute(httpUriRequest);
if (response == null || response.getEntity() == null) {
response.close();
client.close();
return null;
}
return new FilterInputStream(response.getEntity().getContent()) {
@Override
public void close() throws IOException {
try {
super.close();
} finally {
try {
response.close();
} finally {
client.close();
}
}
}
};
} catch (IOException e) {
client.close();
throw e;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.util.LinkedList;
import java.util.List;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -138,14 +137,9 @@ public InputStream stream(MdmBioDevice bioDevice, String modality) throws RegBas
timeout);

String request = objectMapper.writeValueAsString(streamRequestDTO);
CloseableHttpResponse response = mosipDeviceSpecificationHelper.getHttpClientResponse(
InputStream urlStream = mosipDeviceSpecificationHelper.getHttpClientResponse(
bioDevice.getCallbackId() + MosipBioDeviceConstants.STREAM_ENDPOINT, "STREAM", request);

InputStream urlStream = null;
if (response != null && response.getEntity() != null) {
urlStream = response.getEntity().getContent();
}

try {
byte[] byteArray = mosipDeviceSpecificationHelper.getJPEGByteArray(urlStream,
System.currentTimeMillis() + Long.parseLong(timeout));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,28 @@ public void buildUrlTest() {
Assert.assertEquals("http://127.0.0.1:4501/info", url);
}

@Test
public void getHttpClientResponseReturnsStreamAndClosesTest() throws IOException {
MockResponse mockResponse = new MockResponse().setBody("stream-data");
mockWebServer.enqueue(mockResponse);

String url = mosipDeviceSpecificationHelper.buildUrl(4501, "stream");
InputStream stream = mosipDeviceSpecificationHelper.getHttpClientResponse(url, "GET", "");
Assert.assertNotNull(stream);
stream.close(); // must not throw; closes response and client
}

@Test
public void getHttpClientResponseNullOnNoEntityTest() throws IOException {
// Server returns 204 No Content — entity will be null
MockResponse mockResponse = new MockResponse().setResponseCode(204);
mockWebServer.enqueue(mockResponse);

String url = mosipDeviceSpecificationHelper.buildUrl(4501, "stream");
InputStream stream = mosipDeviceSpecificationHelper.getHttpClientResponse(url, "GET", "");
Assert.assertNull(stream);
}

@Test
public void checkServiceAvailabilityTest() throws IOException {
mockWebServer.enqueue(new MockResponse());
Expand Down