Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for receiving JSON-LD files compressed with Gzip #63

Merged
merged 3 commits into from
Apr 20, 2020
Merged
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
76 changes: 59 additions & 17 deletions src/main/java/org/phyloref/jphyloref/commands/WebserverCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@
import fi.iki.elonen.NanoHTTPD;
import fi.iki.elonen.NanoHTTPD.Response.IStatus;
import fi.iki.elonen.NanoHTTPD.Response.Status;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.zip.GZIPInputStream;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;
import org.eclipse.rdf4j.rio.RDFParseException;
Expand Down Expand Up @@ -279,32 +284,69 @@ public Response serve(IHTTPSession session) {
return preflightResponse;
}

// We accept two kinds of inputs:
// We accept three kinds of inputs:
// 1. A form containing 'jsonld' as a JSON-LD string to process.
// 2. A form containing 'jsonldFile' as a JSON-LD file to read.
String filename;
File jsonldFile;

// 2. A form containing 'jsonldGzipped' as a Base64-encoded Gzipped string
// containing a JSON-LD string to process.
// 3. A form containing 'jsonldFile' as a JSON-LD file to read.
// For simplicity's sake, we convert all the possible forms into a
// File for processing.
try {
File jsonldFile = null;

if (params.containsKey("jsonldFile")) {
filename = String.join("; ", params.get("jsonldFile"));
// It's already a file!
jsonldFile = new File(files.get("jsonldFile"));
} else if (params.containsKey("jsonld")) {
jsonldFile = File.createTempFile("jphyloref", null);

FileWriter writer = new FileWriter(jsonldFile);
writer.write(String.join(";", params.get("jsonld")));
writer.close();

} else {
response.put("status", "error");
response.put(
"error",
"Expected a form with a file upload in the 'jsonldFile' field or a JSON-LD string in the 'jsonld' field, but no such field was found");
return createResponse(Status.BAD_REQUEST, response);
// It's a string, which we need to write to a temporary file.
String jsonld = null;

if (params.containsKey("jsonld")) {
// Read JSON-LD as string.
jsonld = String.join("", params.get("jsonld"));

} else if (params.containsKey("jsonldGzipped")) {
// Read JSON-LD as Gzipped string.
String jsonldgzBase64 = String.join("", params.get("jsonldGzipped"));

// Convert to a byte stream using Base64.
byte[] jsonldGzipped = Base64.getDecoder().decode(jsonldgzBase64);

// Convert unzip byte stream.
GZIPInputStream gzis = new GZIPInputStream(new ByteArrayInputStream(jsonldGzipped));

// Read unzipped byte stream as a UTF-8 string, which has to be done line by line.
BufferedReader reader = new BufferedReader(new InputStreamReader(gzis, "UTF-8"));
StringBuffer jsonldStringBuffer = new StringBuffer();

String buffer;
while ((buffer = reader.readLine()) != null) {
jsonldStringBuffer.append(buffer);
}

// Store UTF-8 string for further processing.
jsonld = jsonldStringBuffer.toString();
} else {
response.put("status", "error");
response.put(
"error",
"Expected a form with a file upload in the 'jsonldFile' field or a JSON-LD string in the 'jsonld' or 'jsonldGzipped' field, but no such field was found");
return createResponse(Status.BAD_REQUEST, response);
}

// Store the JSON-LD into a file for further processing.
if (jsonld != null) {
jsonldFile = File.createTempFile("jphyloref", null);
FileWriter writer = new FileWriter(jsonldFile);
writer.write(String.join(";", jsonld));
writer.close();
}
}

// Process JSON-LD file and return response.
return createResponse(Status.OK, serveReason(jsonldFile));

} catch (OWLOntologyCreationException | RDFParseException | IOException ex) {
response.put("status", "error");
response.put("error", "Exception thrown: " + ex.getMessage());
Expand Down