Skip to content

Commit

Permalink
Use temp file when not streaming batch requests
Browse files Browse the repository at this point in the history
  • Loading branch information
hylkevds committed Aug 30, 2024
1 parent 4a97bd8 commit d3cf107
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public String getContentString() {
return IOUtils.toString(contentBinary, StandardCharsets.UTF_8);
} catch (IOException ex) {
LOGGER.debug("Failed to convert input to a string", ex);
LOGGER.error("Failed to convert input to a string: " + ex.getMessage());
LOGGER.error("Failed to convert input to a string: {}", ex.getMessage());
throw new IllegalStateException("Failed to read input.");
}
}
Expand Down
4 changes: 4 additions & 0 deletions Plugins/BatchProcessing/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
<artifactId>FROST-Server.Util</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ public ServiceResponse executeBatchOperation(final Service service, final Servic
return sendResponse(resultContent, response);

case CONTENT_TYPE_APPLICATION_JSON:
JsonBatchResponse batchResponse = new JsonBatchProcessor(service, request, response)
.processRequest(streaming);
final JsonBatchProcessor jsonBatchProcessor = new JsonBatchProcessor(service, request, response);
final JsonBatchResponse batchResponse = jsonBatchProcessor.processRequest(streaming);
try {
new ResultFormatterDefault()
.format(null, null, batchResponse, false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,17 @@
import de.fraunhofer.iosb.ilt.frostserver.service.UpdateMode;
import de.fraunhofer.iosb.ilt.frostserver.settings.CoreSettings;
import de.fraunhofer.iosb.ilt.frostserver.util.StringHelper;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -67,6 +71,7 @@ public class JsonBatchProcessor implements Iterator<JsonBatchResultItem> {
private final ServiceRequest request;
private final ServiceResponse response;

private Path tempFile;
private JsonParser parser;
private JsonBatchResultItem next;

Expand All @@ -86,7 +91,12 @@ public JsonBatchResponse processRequest(boolean stream) {
if (stream) {
parser = mapper.createParser(request.getContentReader());
} else {
parser = mapper.createParser(request.getContentString());
tempFile = Files.createTempFile(null, null);
try (BufferedWriter tempWriter = Files.newBufferedWriter(tempFile)) {
IOUtils.copy(request.getContentReader(), tempWriter);
tempWriter.flush();
}
parser = mapper.createParser(tempFile.toFile());
}
JsonToken currentToken = parser.nextToken();
while (currentToken != null) {
Expand All @@ -112,12 +122,21 @@ public JsonBatchResponse processRequest(boolean stream) {
throw new IllegalArgumentException("No requests found in input");
}

private void close() {
public void close() {
if (parser != null) {
try {
parser.close();
} catch (IOException ex) {
LOGGER.warn("Failed to close parser!", ex);
LOGGER.warn("Failed to close parser: {}", ex.getMessage());
LOGGER.debug("Failed to close parser.", ex);
}
}
if (tempFile != null) {
try {
Files.deleteIfExists(tempFile);
} catch (IOException ex) {
LOGGER.warn("Failed to delete temp file: {}", ex.getMessage());
LOGGER.debug("Failed to delete temp file.", ex);
}
}
}
Expand Down

0 comments on commit d3cf107

Please sign in to comment.