diff --git a/src/main/java/fr/inria/corese/command/utils/http/SparqlHttpClient.java b/src/main/java/fr/inria/corese/command/utils/http/SparqlHttpClient.java index 1129d2f83..4915affce 100644 --- a/src/main/java/fr/inria/corese/command/utils/http/SparqlHttpClient.java +++ b/src/main/java/fr/inria/corese/command/utils/http/SparqlHttpClient.java @@ -1,18 +1,20 @@ package fr.inria.corese.command.utils.http; +import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; +import java.util.Map; import org.apache.commons.lang3.tuple.Pair; import fr.inria.corese.command.VersionProvider; import fr.inria.corese.command.utils.TestType; import fr.inria.corese.core.Graph; -import fr.inria.corese.core.query.QueryProcess; import fr.inria.corese.core.kgram.core.Query; +import fr.inria.corese.core.query.QueryProcess; import fr.inria.corese.core.sparql.triple.parser.Constant; import fr.inria.corese.core.sparql.triple.update.ASTUpdate; import fr.inria.corese.core.sparql.triple.update.Composite; @@ -153,6 +155,9 @@ public String sendRequest(String query, List defaultGraphUris, List(); @@ -173,30 +178,26 @@ public String sendRequest(String query, List defaultGraphUris, List defaultGraphUris, List> headers = response.getHeaders(); + if (!headers.isEmpty()) { + err.println("\n► HEADERS"); + for (Map.Entry> header : headers.entrySet()) { + for (Object value : header.getValue()) { + err.println(" " + header.getKey() + ": " + value); + } + } } + + // Do not display the response body here + + err.println("\n──────────────────────────────────"); } /** @@ -233,38 +265,60 @@ private void printResponse(Response response) { * * @param webTarget the web target of the request * @param bodyContent the body content of the request + * @param contentType the content type of the request */ - private void printRequest(WebTarget webTarget, String bodyContent) { - this.spec.commandLine().getErr().println("Request Details:"); + private void printRequest(WebTarget webTarget, String bodyContent, String contentType) { + PrintWriter err = this.spec.commandLine().getErr(); - // Print URL + err.println("╔════════════════════════════════╗"); + err.println("║ REQUEST DETAILS ║"); + err.println("╚════════════════════════════════╝\n"); + + // URL if (webTarget != null && webTarget.getUri() != null) { - this.spec.commandLine().getErr().println("\tURL: " + webTarget.getUri()); + err.println("► URL"); + err.println(" " + webTarget.getUri()); } - // Print request method + // Method if (this.requestMethod != null) { - this.spec.commandLine().getErr().println("\tmethod: " + this.requestMethod); + err.println("\n► METHOD"); + err.println(" " + this.requestMethod); } - // Print query string parameter + // Query parameters if (webTarget != null && webTarget.getUri() != null && webTarget.getUri().getQuery() != null && !webTarget.getUri().getQuery().isEmpty()) { - this.spec.commandLine().getErr().println("\tQuery string parameter: " + webTarget.getUri().getQuery()); + err.println("\n► QUERY PARAMETERS"); + String[] queryLines = webTarget.getUri().getQuery().split("\n"); + for (String line : queryLines) { + err.println(" " + line); + } } - // Print headers - if (this.headers != null && !this.headers.isEmpty()) { - this.spec.commandLine().getErr().println("\tHeaders:"); + // Headers + if ((this.headers != null && !this.headers.isEmpty()) || (contentType != null && !contentType.isEmpty())) { + err.println("\n► HEADERS"); + for (Pair header : this.headers) { + err.println(" " + header.getKey() + ": " + header.getValue()); + } - this.spec.commandLine().getErr().println(" " + header.getKey() + ": " + header.getValue()); + if (contentType != null && !contentType.isEmpty()) { + err.println(" Content-Type: " + contentType); } } + // Body content if (bodyContent != null && !bodyContent.isEmpty()) { - this.spec.commandLine().getErr().println("\tRequest body: " + bodyContent); + err.println("\n► REQUEST BODY"); + String[] bodyLines = bodyContent.split("\n"); + for (String line : bodyLines) { + err.println(" " + line); + } } + + err.println("\n──────────────────────────────────"); } /** @@ -451,20 +505,32 @@ private Response executeRequest(WebTarget webTarget, String bodyContent) { Response response = null; // Add headers - Builder builder = webTarget.request() - .header("User-Agent", this.USERAGENT); + Builder builder = webTarget.request(); for (Pair header : this.headers) { builder = builder.header(header.getKey(), header.getValue()); } + // Add request content types + String contentType = null; + if (this.requestMethod == EnumRequestMethod.POST_URLENCODED) { + contentType = MediaType.APPLICATION_FORM_URLENCODED; + } else if (this.requestMethod == EnumRequestMethod.POST_DIRECT) { + contentType = "application/sparql-query"; + } + + // Print query and body content if verbose mode is enabled + if (this.verbose) { + this.printRequest(webTarget, bodyContent, contentType); + } + // Send the request if (this.requestMethod == EnumRequestMethod.GET) { response = builder.get(); } else if (this.requestMethod == EnumRequestMethod.POST_URLENCODED) { - response = builder.post(Entity.entity(bodyContent, MediaType.APPLICATION_FORM_URLENCODED)); + response = builder.post(Entity.entity(bodyContent, contentType)); } else if (this.requestMethod == EnumRequestMethod.POST_DIRECT) { - response = builder.post(Entity.entity(bodyContent, "application/sparql-query")); + response = builder.post(Entity.entity(bodyContent, contentType)); } return response;