Skip to content

Commit eb9381b

Browse files
committed
make the variable configurable and also add more tests
1 parent 2696c04 commit eb9381b

2 files changed

Lines changed: 121 additions & 4 deletions

File tree

tools/server-spring/src/main/java/org/eclipse/rdf4j/http/server/repository/statements/ExportStatementsView.java

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
*/
4646
public class ExportStatementsView implements View {
4747

48+
private static final Logger logger = LoggerFactory.getLogger(ExportStatementsView.class);
49+
4850
public static final String SUBJECT_KEY = "subject";
4951
public static final String PREDICATE_KEY = "predicate";
5052
public static final String OBJECT_KEY = "object";
@@ -56,9 +58,29 @@ public class ExportStatementsView implements View {
5658
public static final String HEADERS_ONLY = "headersOnly";
5759

5860
private static final ExportStatementsView INSTANCE = new ExportStatementsView();
59-
public static final int MAX_NUMBER_OF_STATEMENTS_WHEN_TESTING_FOR_POSSIBLE_EXCEPTIONS = 1024;
60-
61-
private static final Logger logger = LoggerFactory.getLogger(ExportStatementsView.class);
61+
public static int MAX_NUMBER_OF_STATEMENTS_WHEN_TESTING_FOR_POSSIBLE_EXCEPTIONS;
62+
63+
static {
64+
int max = 1024; // default value
65+
String maxStatements = System.getProperty(
66+
"org.eclipse.rdf4j.http.server.repository.statements.ExportStatementsView.MAX_NUMBER_OF_STATEMENTS_WHEN_TESTING_FOR_POSSIBLE_EXCEPTIONS");
67+
if (maxStatements != null) {
68+
try {
69+
int userMax = Integer.parseInt(maxStatements);
70+
if (userMax >= -1) {
71+
max = userMax;
72+
} else {
73+
logger.warn(
74+
"Invalid value for MAX_NUMBER_OF_STATEMENTS_WHEN_TESTING_FOR_POSSIBLE_EXCEPTIONS: {}, must be >= -1, using default value of {}.",
75+
maxStatements, max);
76+
}
77+
} catch (NumberFormatException e) {
78+
logger.warn("Invalid value for MAX_NUMBER_OF_STATEMENTS_WHEN_TESTING_FOR_POSSIBLE_EXCEPTIONS: "
79+
+ maxStatements, e);
80+
}
81+
}
82+
MAX_NUMBER_OF_STATEMENTS_WHEN_TESTING_FOR_POSSIBLE_EXCEPTIONS = max;
83+
}
6284

6385
public static ExportStatementsView getInstance() {
6486
return INSTANCE;
@@ -143,6 +165,10 @@ public void render(Map model, HttpServletRequest request, HttpServletResponse re
143165
private static void attemptToDetectExceptions(HttpServletRequest request, RDFWriterFactory rdfWriterFactory,
144166
boolean headersOnly, Resource subj, IRI pred, Value obj, boolean useInferencing, Resource[] contexts)
145167
throws IOException, ServerHTTPException {
168+
if (MAX_NUMBER_OF_STATEMENTS_WHEN_TESTING_FOR_POSSIBLE_EXCEPTIONS == 0) {
169+
return;
170+
}
171+
146172
try (OutputStream out = OutputStream.nullOutputStream()) {
147173
RDFHandler rdfWriter = new LimitedSizeRDFHandler(rdfWriterFactory.getWriter(out),
148174
MAX_NUMBER_OF_STATEMENTS_WHEN_TESTING_FOR_POSSIBLE_EXCEPTIONS);
@@ -200,7 +226,7 @@ public void handleComment(String comment) throws RDFHandlerException {
200226

201227
private void incrementCurrentSize() {
202228
currentSize++;
203-
if (currentSize > maxSize) {
229+
if (maxSize >= 0 && currentSize > maxSize) {
204230
endRDF();
205231
logger.trace(
206232
"Limited size reached, throwing LimitedSizeReachedException to signal that we are done testing the export of statements for exceptions.");

tools/server/src/test/java/org/eclipse/rdf4j/http/server/ProtocolIT.java

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.apache.http.message.BasicNameValuePair;
4848
import org.eclipse.rdf4j.common.io.IOUtil;
4949
import org.eclipse.rdf4j.http.protocol.Protocol;
50+
import org.eclipse.rdf4j.http.server.repository.statements.ExportStatementsView;
5051
import org.eclipse.rdf4j.model.IRI;
5152
import org.eclipse.rdf4j.model.Namespace;
5253
import org.eclipse.rdf4j.model.ValueFactory;
@@ -93,6 +94,8 @@ public static void stopServer() throws Exception {
9394
public void clearRepository() throws Exception {
9495
// Clear the repository after each test
9596
delete(Protocol.getStatementsLocation(TestServer.REPOSITORY_URL));
97+
ExportStatementsView.MAX_NUMBER_OF_STATEMENTS_WHEN_TESTING_FOR_POSSIBLE_EXCEPTIONS = 1024;
98+
9699
}
97100

98101
/**
@@ -422,6 +425,94 @@ public void testUploadAndRetrieveStatements_GET() throws Exception {
422425

423426
}
424427

428+
@Test
429+
public void testUploadAndRetrieveStatementsNoValidation_GET() throws Exception {
430+
431+
ExportStatementsView.MAX_NUMBER_OF_STATEMENTS_WHEN_TESTING_FOR_POSSIBLE_EXCEPTIONS = 0;
432+
433+
String statementsLocation = Protocol.getStatementsLocation(TestServer.REPOSITORY_URL);
434+
435+
// PUT the Turtle file into the repository
436+
final String baseLocation = Protocol.getStatementsLocation(TestServer.REPOSITORY_URL);
437+
final String file = "/testcases/default-graph-2.ttl";
438+
439+
// 1. PUT the same file multiple times so that we would trigger an OOM error when retrieving it if it were not
440+
// directly written to the http output stream
441+
for (int i = 1; i <= 20000; i++) {
442+
IRI context = vf.createIRI("http://example.org/graph" + i);
443+
putFile(baseLocation, file, context);
444+
}
445+
446+
// GET all statements back from the same endpoint
447+
URL url = new URL(statementsLocation);
448+
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
449+
conn.setRequestMethod("GET");
450+
conn.setRequestProperty("Accept", RDFFormat.NQUADS.getDefaultMIMEType()); // ask for easy-to-parse format
451+
conn.connect();
452+
453+
try {
454+
assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode(),
455+
"GET /statements should respond 200 OK");
456+
457+
// Parse the response stream and count triples
458+
try (InputStream in = conn.getInputStream()) {
459+
Scanner scanner = new Scanner(in);
460+
int count = 0;
461+
while (scanner.hasNext()) {
462+
scanner.nextLine();
463+
count++;
464+
}
465+
assertEquals(1860000, count, "Expected 1860000 triples, but got " + count + " instead.");
466+
}
467+
} finally {
468+
conn.disconnect();
469+
}
470+
471+
}
472+
473+
@Test
474+
public void testUploadAndRetrieveStatementsWithFullValidation_GET() throws Exception {
475+
476+
ExportStatementsView.MAX_NUMBER_OF_STATEMENTS_WHEN_TESTING_FOR_POSSIBLE_EXCEPTIONS = -1;
477+
478+
String statementsLocation = Protocol.getStatementsLocation(TestServer.REPOSITORY_URL);
479+
480+
String baseLocation = Protocol.getStatementsLocation(TestServer.REPOSITORY_URL);
481+
String file = "/testcases/default-graph-2.ttl";
482+
483+
// PUT the Turtle file into the repository
484+
for (int i = 1; i <= 2000; i++) {
485+
IRI context = vf.createIRI("http://example.org/graph" + i);
486+
putFile(baseLocation, file, context);
487+
}
488+
489+
// GET all statements back from the same endpoint
490+
URL url = new URL(statementsLocation);
491+
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
492+
conn.setRequestMethod("GET");
493+
conn.setRequestProperty("Accept", RDFFormat.NQUADS.getDefaultMIMEType()); // ask for easy-to-parse format
494+
conn.connect();
495+
496+
try {
497+
assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode(),
498+
"GET /statements should respond 200 OK");
499+
500+
// Parse the response stream and count triples
501+
try (InputStream in = conn.getInputStream()) {
502+
Scanner scanner = new Scanner(in);
503+
int count = 0;
504+
while (scanner.hasNext()) {
505+
scanner.nextLine();
506+
count++;
507+
}
508+
assertEquals(186000, count, "Expected 186000 triples, but got " + count + " instead.");
509+
}
510+
} finally {
511+
conn.disconnect();
512+
}
513+
514+
}
515+
425516
/**
426517
* Test for SES-1861
427518
*

0 commit comments

Comments
 (0)