Skip to content

Commit

Permalink
Detect Fuseki by request id. Remove dev logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
afs committed Mar 29, 2018
1 parent 930d4d3 commit 2a5381f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import javax.servlet.http.HttpServletRequest ;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpOptions;
Expand Down Expand Up @@ -275,17 +276,24 @@ public static int choosePort() {
}
}

/** Test whether a URL identifies a Fuseki server */
/**
* Test whether a URL identifies a Fuseki server. This operation can not guaranttee to
* detech a Fuseki server - for example, it may be behind a reverse proxy that masks
* the signature.
*/
public static boolean isFuseki(String datasetURL) {
System.err.println("isFuseki: "+datasetURL);
HttpOptions request = new HttpOptions(datasetURL);
HttpClient httpClient = HttpOp.getDefaultHttpClient();
if ( httpClient == null )
httpClient = HttpClients.createSystem();
return isFuseki(request, httpClient, null);
}

/** Test whether a {@link RDFConnectionRemote} connects to a Fuseki server */
/**
* Test whether a {@link RDFConnectionRemote} connects to a Fuseki server. This
* operation can not guaranttee to detech a Fuseki server - for example, it may be
* behind a reverse proxy that masks the signature.
*/
public static boolean isFuseki(RDFConnectionRemote connection) {
HttpOptions request = new HttpOptions(connection.getDestination());
HttpClient httpClient = connection.getHttpClient();
Expand All @@ -296,28 +304,34 @@ public static boolean isFuseki(RDFConnectionRemote connection) {
}

private static boolean isFuseki(HttpOptions request, HttpClient httpClient, HttpContext httpContext) {
System.err.println("isFuseki(worker)");
if ( httpClient == null ) {
System.err.println("httpClient is null");
}
try {
HttpResponse response = httpClient.execute(request);
// Fuseki-Request-ID:
//String reqId = response.getFirstHeader("Fuseki-Request-ID").getValue();
// Server:
System.err.println("isFuseki(worker)-1");
String serverIdent = response.getFirstHeader("Server").getValue();
System.err.println("isFuseki(worker)-2");
Log.debug(ARQ.getHttpRequestLogger(), "Server: "+serverIdent);
System.err.println("isFuseki(worker)-3");
boolean isFuseki = serverIdent.startsWith("Apache Jena Fuseki");
System.err.println("isFuseki(worker)-4");
if ( !isFuseki )
isFuseki = serverIdent.toLowerCase().contains("fuseki");
System.err.println("isFuseki(worker)-return");
return isFuseki; // Maybe
// Fuseki does not send "Server" in release mode.
// (best practice).
// All we can do is try for the "Fuseki-Request-ID"
String reqId = safeGetHeader(response, "Fuseki-Request-ID");
if ( reqId != null )
return true;

// If returning "Server"
String serverIdent = safeGetHeader(response, "Server");
if ( serverIdent != null ) {
Log.debug(ARQ.getHttpRequestLogger(), "Server: "+serverIdent);
boolean isFuseki = serverIdent.startsWith("Apache Jena Fuseki");
if ( !isFuseki )
isFuseki = serverIdent.toLowerCase().contains("fuseki");
return isFuseki;
}
return false;
} catch (IOException ex) {
throw new HttpException("Failed to check for a Fuseki server", ex);
}
}

private static String safeGetHeader(HttpResponse response, String header) {
Header h = response.getFirstHeader(header);
if ( h == null )
return null;
return h.getValue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,15 @@ public class TestRDFConnectionFusekiBinary {
model.getGraph().add(triple);

int PORT = FusekiLib.choosePort();
FusekiServer server = createFusekiServer(PORT).build().start();
FusekiServer server = createFusekiServer(PORT).build().start();
try {
String dsURL = "http://localhost:"+PORT+"/ds" ;
{
System.err.println("dsURL="+dsURL);
boolean b1 = FusekiLib.isFuseki(dsURL);
assertTrue("isFuseki(1)", b1) ;
}
System.err.println("builder");
assertTrue(FusekiLib.isFuseki(dsURL));

RDFConnectionRemoteBuilder builder = RDFConnectionFuseki.create().destination(dsURL);

try (RDFConnectionFuseki conn = (RDFConnectionFuseki)builder.build()) {
{
boolean b2 = FusekiLib.isFuseki(conn);
assertTrue("isFuseki(2)", b2);
}
assertTrue(FusekiLib.isFuseki(conn));
// GSP
conn.put(model);
checkModel(conn, "b3456");
Expand Down

0 comments on commit 2a5381f

Please sign in to comment.