Skip to content

Commit

Permalink
Typo
Browse files Browse the repository at this point in the history
  • Loading branch information
afs committed Jul 19, 2023
1 parent 958e3e4 commit a0fd476
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 78 deletions.
28 changes: 27 additions & 1 deletion jena-arq/src/main/java/org/apache/jena/http/HttpOp.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,27 @@ public static String httpGetString(String url) {
return httpGetString(HttpEnv.getDftHttpClient(), url, null);
}

/**
* Perform an HTTP and return the body as a string. Throws {@link HttpException}
* on all non-success HTTP status codes including 404,
* unlike {@link #httpGetString(String)}.
*/
public static String httpGetStringEx(String url) throws HttpException {
return httpGetString(HttpEnv.getDftHttpClient(), url, null, false);
}

/**
* Perform an HTTP and discard the body.
* Only useful to use GET as a ping-like operation on a URL
* Throws {@link HttpException} on all non-success HTTP status codes.
*/
public static void httpGetDiscard(String url) throws HttpException {
HttpRequest httpRequest = newGetRequest(url, setAcceptHeader("*/*"));
HttpClient httpClient = HttpEnv.getDftHttpClient();
HttpResponse<InputStream> response = execute(httpClient, httpRequest);
HttpLib.handleResponseNoBody(response);
}

/** Perform an HTTP and return the body as a string, Return null for a "404 Not Found". */
public static String httpGetString(String url, String acceptHeader) {
return httpGetString(HttpEnv.getDftHttpClient(), url, acceptHeader);
Expand All @@ -92,12 +113,17 @@ public static String httpGetString(HttpClient httpClient, String url) {

/** Perform an HTTP and return the body as a string. Return null for a "404 Not Found". */
public static String httpGetString(HttpClient httpClient, String url, String acceptHeader) {
return httpGetString(httpClient, url, acceptHeader, true);
}

/** Perform an HTTP and return the body as a string. Return null for a "404 Not Found". */
private static String httpGetString(HttpClient httpClient, String url, String acceptHeader, boolean notFoundAsNull) {
HttpRequest request = newGetRequest(url, setAcceptHeader(acceptHeader));
HttpResponse<InputStream> response = execute(httpClient, request);
try {
return handleResponseRtnString(response);
} catch (HttpException ex) {
if ( ex.getStatusCode() == HttpSC.NOT_FOUND_404 )
if ( notFoundAsNull && ex.getStatusCode() == HttpSC.NOT_FOUND_404 )
return null;
throw ex;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* DatasetGraph wrapper controls entry and exit of transactions.
* <ul>
* <li>Exclusive access (no transactions active)
* <li>Read only database - No possible writers (write and promotable transactions)
* <li>Read only database - No possible writers (write and promote-able transactions)
* </ul>
*/

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -452,8 +452,8 @@ protected void processModulesAndArgs() {
// Static files.
String servletContextPath = getValue(argPathBase);

if ( servletContextPath.endsWith("/") )
throw new CmdException("Path base must not end with \"/\": "+argPathBase);
if ( ! servletContextPath.equals("/") && servletContextPath.endsWith("/") )
throw new CmdException("Path base must not end with \"/\": '"+servletContextPath+"'");
serverConfig.servletContextPath = servletContextPath;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@

package org.apache.jena.fuseki.main;

import static org.apache.jena.fuseki.main.FusekiTestLib.expect400;
import static org.apache.jena.fuseki.main.FusekiTestLib.expect404;
import static org.apache.jena.fuseki.main.FusekiTestLib.expectQuery400;
import static org.apache.jena.fuseki.main.FusekiTestLib.expectQuery404;
import static org.junit.Assert.*;

import java.io.IOException;
import java.util.function.Consumer;

import org.apache.jena.atlas.iterator.Iter;
import org.apache.jena.atlas.logging.LogCtl;
import org.apache.jena.atlas.web.HttpException;
import org.apache.jena.atlas.web.TypedInputStream;
import org.apache.jena.fuseki.Fuseki;
import org.apache.jena.fuseki.server.DataAccessPointRegistry;
Expand All @@ -42,11 +45,11 @@
import org.apache.jena.sparql.core.Quad;
import org.apache.jena.sparql.exec.QueryExec;
import org.apache.jena.sparql.exec.RowSet;
import org.apache.jena.sparql.exec.http.GSP;
import org.apache.jena.sparql.exec.http.QueryExecHTTP;
import org.apache.jena.sparql.sse.SSE;
import org.apache.jena.system.Txn;
import org.apache.jena.update.UpdateExecution;
import org.apache.jena.web.HttpSC;
import org.junit.Test;
import org.slf4j.Logger;

Expand Down Expand Up @@ -208,9 +211,10 @@ public class TestFusekiServerBuild {
server.start();
int port = server.getPort();
try {
query("http://localhost:"+port+"/dsrv1/q","ASK{}",x->{});
String x1 = HttpOp.httpGetString("http://localhost:"+port+"/dsrv1/gsp?default");
assertNotNull(x1);
boolean b = queryASK("http://localhost:"+port+"/dsrv1/q","ASK{}");
assertTrue(b);

HttpOp.httpGetDiscard("http://localhost:"+port+"/dsrv1/gsp?default");
} finally { server.stop(); }
}

Expand All @@ -229,12 +233,11 @@ public class TestFusekiServerBuild {
int port = server.getPort();

try {
query("http://localhost:"+port+"/dsrv1/q","ASK{}",x->{});
String x1 = HttpOp.httpGetString("http://localhost:"+port+"/dsrv1/gsp?default");
assertNotNull(x1);
// Static
String x2 = HttpOp.httpGetString("http://localhost:"+port+"/test.txt");
assertNotNull(x2);
boolean b = queryASK("http://localhost:"+port+"/dsrv1/q","ASK{}");
assertTrue(b);

HttpOp.httpGetDiscard("http://localhost:"+port+"/dsrv1/gsp?default");
HttpOp.httpGetDiscard("http://localhost:"+port+"/test.txt");
} finally { server.stop(); }
}

Expand All @@ -253,13 +256,8 @@ public class TestFusekiServerBuild {
server.start();
int port = server.getPort();
try {
String x2 = HttpOp.httpGetString("http://localhost:"+port+"/dsrv1/x");
fail("httpGetString should not return");
} catch (HttpException ex) {
// Unregistered operation.
assertEquals(400, ex.getStatusCode());
}
finally { server.stop(); }
expect400(()-> HttpOp.httpGetDiscard("http://localhost:"+port+"/dsrv1/x"));
} finally { server.stop(); }
}

@Test public void fuseki_build_no_stats() {
Expand All @@ -272,18 +270,14 @@ public class TestFusekiServerBuild {
int port = server.getPort();
try {
// No server services
String x1 = HttpOp.httpGetString("http://localhost:"+port+"/$/ping");
assertNull(x1);
expect404(()-> HttpOp.httpGetDiscard("http://localhost:"+port+"/$/ping") );

String x2 = HttpOp.httpGetString("http://localhost:"+port+"/$/stats");
assertNull(x2);
expect404(()-> HttpOp.httpGetDiscard("http://localhost:"+port+"/$/stats") );

String x3 = HttpOp.httpGetString("http://localhost:"+port+"/$/metrics");
assertNull(x3);
expect404(()-> HttpOp.httpGetDiscard("http://localhost:"+port+"/$/metrics") );

HttpException ex = assertThrows(HttpException.class,
() -> HttpOp.httpPostStream("http://localhost:"+port+"/$/compact/ds", "application/json"));
assertEquals(404, ex.getStatusCode());
expect404(()->
HttpOp.httpPostStream("http://localhost:"+port+"/$/compact/ds", "application/json").close() );
} finally { server.stop(); }
}

Expand All @@ -297,9 +291,9 @@ public class TestFusekiServerBuild {
server.start();
int port = server.getPort();

String x = HttpOp.httpGetString("http://localhost:"+port+"/$/ping");
assertNotNull(x);
server.stop();
try {
HttpOp.httpGetDiscard("http://localhost:"+port+"/$/ping");
} finally { server.stop(); }
}

@Test public void fuseki_build_stats() {
Expand All @@ -311,9 +305,9 @@ public class TestFusekiServerBuild {
.build();
server.start();
int port = server.getPort();
String x = HttpOp.httpGetString("http://localhost:"+port+"/$/stats");
assertNotNull(x);
server.stop();
try {
HttpOp.httpGetDiscard("http://localhost:"+port+"/$/stats");
} finally { server.stop(); }
}

@Test public void fuseki_build_metrics() {
Expand All @@ -325,9 +319,9 @@ public class TestFusekiServerBuild {
.build();
server.start();
int port = server.getPort();
String x = HttpOp.httpGetString("http://localhost:"+port+"/$/metrics");
assertNotNull(x);
server.stop();
try {
HttpOp.httpGetDiscard("http://localhost:"+port+"/$/metrics");
} finally { server.stop(); }
}

@Test public void fuseki_build_compact() throws IOException {
Expand All @@ -342,11 +336,8 @@ public class TestFusekiServerBuild {
assertNotNull(x0);
assertNotEquals(0, x0.readAllBytes().length);

String x1 = HttpOp.httpGetString("http://localhost:"+port+"/$/tasks");
assertNotNull(x1);
} finally {
server.stop();
}
HttpOp.httpGetDiscard("http://localhost:"+port+"/$/tasks");
} finally { server.stop(); }
}

@Test public void fuseki_build_tasks() {
Expand All @@ -358,13 +349,12 @@ public class TestFusekiServerBuild {
.build();
server.start();
int port = server.getPort();
String x = HttpOp.httpGetString("http://localhost:"+port+"/$/tasks");
assertNotNull(x);
server.stop();
try {
HttpOp.httpGetDiscard("http://localhost:"+port+"/$/tasks");
} finally { server.stop(); }
}

// Context path.
@Test public void fuseki_build_08() {
@Test public void fuseki_build_cxtpath_01() {
// Context path
DatasetGraph dsg = dataset();
FusekiServer server = FusekiServer.create()
Expand All @@ -375,47 +365,80 @@ public class TestFusekiServerBuild {
server.start();
int port = server.getPort();
try {
String x1 = HttpOp.httpGetString("http://localhost:"+port+"/ds");
assertNull(x1);
String x2 = HttpOp.httpGetString("http://localhost:"+port+"/ABC/ds");
assertNotNull(x2);
expect404(()-> HttpOp.httpGetDiscard("http://localhost:"+port+"/ds")) ;
HttpOp.httpGetDiscard("http://localhost:"+port+"/ABC/ds");
} finally { server.stop(); }
}

@Test public void fuseki_build_cxtpath_02() {
DatasetGraph dsg = dataset();
DataService dataService = DataService.newBuilder(dsg)
.addEndpoint(Operation.GSP_R, "get")
.build();
FusekiServer server = FusekiServer.create()
.port(0)
// If the default is explicitly set.
.contextPath("/")
.add("/ds", dataService)
.build();
server.start();
int port = server.getPort();

try {
expect400(()-> HttpOp.httpGetDiscard("http://localhost:"+port+"/ds") );
expect400(()-> HttpOp.httpGetDiscard("http://localhost:"+port+"/ds/") );
// The only endpoint.
HttpOp.httpGetDiscard("http://localhost:"+port+"/ds/get?default");
// Again, but as a higher level request.
Graph g = GSP.service("http://localhost:"+port+"/ds/get").defaultGraph().GET();
assertNotNull(g);

} finally { server.stop(); }
}

@Test public void fuseki_build_09() {
@Test public void fuseki_build_cxtpath_03() {
// Config file
FusekiServer server = FusekiServer.create()
.port(0)
.parseConfigFile(DIR+"config.ttl")
.parseConfigFile(DIR+"config-plain.ttl")
.build();
server.start();
int port = server.getPort();
try {
query("http://localhost:"+port+"/FuTest", "SELECT * {}", x->{});
expectQuery400(()->{
// Nothing on /FuTest
query("http://localhost:"+port+"/FuTest", "SELECT * {}", x->x.select());
});
query("http://localhost:"+port+"/FuTest/sparql", "SELECT * {}", x->x.select());
} finally { server.stop(); }
}

@Test public void fuseki_build_10() {
@Test public void fuseki_build_cxtpath_04() {
// Context path and config file
FusekiServer server = FusekiServer.create()
.port(0)
.contextPath("/ABC")
.parseConfigFile(DIR+"config.ttl")
.parseConfigFile(DIR+"config-plain.ttl")
.build();
server.start();
int port = server.getPort();
try {
try {
query("http://localhost:"+port+"/FuTest", "ASK{}", x->{});
} catch (HttpException ex) {
assertEquals(HttpSC.METHOD_NOT_ALLOWED_405, ex.getStatusCode());
}

query("http://localhost:"+port+"/ABC/FuTest","ASK{}",x->{});
expect404(()->{
// Low level!
String url = "http://localhost:"+port+"/FuTest/sparql?query=ASK%7B%7D";
HttpOp.httpPost(url);
});
expectQuery404(()->{
// No such endpoint. No context path given.
boolean b = queryASK("http://localhost:"+port+"/FuTest/sparql","ASK{}");
assertTrue(b);
});
boolean b = queryASK("http://localhost:"+port+"/ABC/FuTest/sparql","ASK{}");
assertTrue(b);
} finally { server.stop(); }
}

@Test public void fuseki_build_11() {
@Test public void fuseki_build_cxtpath_05() {
// Config file with context path
FusekiServer server = FusekiServer.create()
.port(0)
Expand All @@ -424,19 +447,19 @@ public class TestFusekiServerBuild {
server.start();
int port = server.getPort();
try {
try {
query("http://localhost:"+port+"/FuTest", "ASK{}", x->{});
} catch (HttpException ex) {
assertEquals(HttpSC.METHOD_NOT_ALLOWED_405, ex.getStatusCode());
}

query("http://localhost:"+port+"/ABC/FuTest","ASK{}",x->{});
// Context setting is /ABC
expectQuery404(()->{
queryASK("http://localhost:"+port+"/FuTest/sparql", "ASK{}");
});
expectQuery400(()->{
// Dataset exists - no operations -> Bad request
queryASK("http://localhost:"+port+"/ABC/FuTest", "ASK{}");
});
queryASK("http://localhost:"+port+"/ABC/FuTest/sparql","ASK{}");
} finally { server.stop(); }
}


// Errors in the configuration file.

@Test public void fuseki_configFile_01() {
silent(()->{
FusekiServer server = FusekiServer.create()
Expand All @@ -454,8 +477,8 @@ public class TestFusekiServerBuild {

// IF a dataset, then no endpoints.
server.getDataAccessPointRegistry().forEach((n,dap)->{
System.err.println(dap.getName());
System.err.println(dap.getDataService().getEndpoints());
// System.err.println(dap.getName());
// System.err.println(dap.getDataService().getEndpoints());
assertEquals(0, dap.getDataService().getEndpoints().size());
});

Expand All @@ -477,6 +500,12 @@ private static void silent(Runnable action) {
return DatasetGraphFactory.createTxnMem();
}

/*package*/ static boolean queryASK(String URL, String query) {
try (QueryExec qExec = QueryExecHTTP.newBuilder().endpoint(URL).queryString(query).build() ) {
return qExec.ask();
}
}

/*package*/ static void query(String URL, String query, Consumer<QueryExec> body) {
try (QueryExec qExec = QueryExecHTTP.newBuilder().endpoint(URL).queryString(query).build() ) {
body.accept(qExec);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
## Licensed under the terms of http://www.apache.org/licenses/LICENSE-2.0

PREFIX fuseki: <http://jena.apache.org/fuseki#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
Expand Down
Loading

0 comments on commit a0fd476

Please sign in to comment.