Skip to content

Commit 0bd8adb

Browse files
committed
/sparql and /sparql2 : according to W3C recommendation return HTTP 500 Internal Server Error for any error
TESTED with wget -O- --content-on-error 'http://localhost:9000/sparql?query=CONSTRUCT WHERE {?s ?p ?o' wget -O- --content-on-error 'http://localhost:9000/sparql?query=SELECT * WHERE {?s ?p ?o' wget -O- --content-on-error --method=POST --body-data="query=CONTRUCT WHERE {" http://localhost:9000/sparql wget -O- --content-on-error --method=POST --body-data="query=SELECT * WHERE {" http://localhost:9000/sparql2
1 parent 3876512 commit 0bd8adb

File tree

5 files changed

+60
-41
lines changed

5 files changed

+60
-41
lines changed

scala/forms/src/main/scala/deductions/runtime/services/ApplicationFacadeImpl.scala

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -347,19 +347,19 @@ trait ApplicationFacadeImpl[Rdf <: RDF, DATASET]
347347
*/
348348
def sparqlConstructResult(query: String, lang: String,
349349
format: String = "turtle",
350-
context: Map[String,String] = Map()): String = {
350+
context: Map[String,String] = Map()): Try[String] = {
351351
logger.info("Global.sparql query " + query)
352352
if (query != "")
353353
sparqlConstructQueryTR(query, format,
354354
context + ("lang" -> lang))
355-
// TODO this code is in several places !!!!!! make function printTry( tr: Try[_] , mime:String)
356-
match {
357-
case Success(s) => s
358-
case Failure(f) =>
359-
if( format === "turtle")
360-
s"# $f" else "{error: \"" + f + "\" }"
361-
}
362-
else "# Empty query result !!!"
355+
// match {
356+
// case Success(s) => Success(s)
357+
// case Failure(f) => Failure(f)
358+
//// Failure( new Exception(
359+
//// if( format === "turtle")
360+
//// s"# $f" else "{error: \"" + f + "\" }" ))
361+
// }
362+
else Success("# Empty query result !!!")
363363
}
364364

365365
/** Display result of a SPARQL select, plus a form to edit the SPARQL text */

scala/forms/src/main/scala/deductions/runtime/sparql_cache/apps/SPARQLHelperApp.scala

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ package deductions.runtime.sparql_cache.apps
33
import deductions.runtime.jena.ImplementationSettings
44
import deductions.runtime.sparql_cache.SPARQLHelpers
55
import deductions.runtime.utils.DefaultConfiguration
6+
import scala.util.Success
7+
import scala.util.Failure
8+
import scala.util.Try
9+
610
/**
711
* Created by LaFaucheuse on 06/07/2017.
812
*/
@@ -15,14 +19,16 @@ object SPARQLHelperApp extends ImplementationSettings.RDFModule
1519
}
1620

1721
def selectJSON(queryString: String): String = {
18-
sparqlSelectJSON(queryString)
22+
prinTry( sparqlSelectJSON(queryString) )
1923
}
2024

2125
def selectXML(queryString: String): String = {
22-
sparqlSelectXML(queryString)
26+
prinTry( sparqlSelectXML(queryString) )
2327
}
24-
// def select(queryString: String): String = {
25-
// sparqlSelectQuery(queryString).toString()
26-
// }
2728

29+
private def prinTry(ts: Try[String]) =
30+
ts match {
31+
case Success(s) => s
32+
case Failure(f) => f.getLocalizedMessage
33+
}
2834
}

scala/forms_play/app/controllers/SparqlServices.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,9 @@ trait SparqlServices extends ApplicationTrait
193193
)
194194
}
195195
logInfo(s"result 10 first lines: $result".split("\n").take(10).mkString("\n"))
196-
Ok(result)
196+
result match {
197+
case Success(result) =>
198+
Ok(result)
197199
.as(s"${simpleString2mimeMap.getOrElse(resultFormat, defaultMIMEaPriori).mimeType }")
198200
.withHeaders(ACCESS_CONTROL_ALLOW_ORIGIN -> "*")
199201
.withHeaders(ACCESS_CONTROL_ALLOW_HEADERS -> "*")
@@ -202,6 +204,8 @@ trait SparqlServices extends ApplicationTrait
202204
* DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,
203205
* If-Modified-Since,Cache-Control,Content-Type,Accept-Encoding" */
204206
// charset=utf-8" ?
207+
case Failure(f) => InternalServerError(f.getLocalizedMessage)
208+
}
205209
}
206210

207211
private def update(update: String) =

scala/sparql_cache/src/main/scala/deductions/runtime/sparql_cache/BrowsableGraph.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import org.w3.banana.RDF
55
import org.w3.banana.io.{RDFWriter, Turtle}
66

77
import scala.util.Try
8+
import scala.util.Success
9+
import scala.util.Failure
810

911
/**
1012
* Browsable Graph implementation, in the sense of
@@ -65,7 +67,10 @@ trait BrowsableGraph[Rdf <: RDF, DATASET] extends RDFStoreLocalProvider[Rdf, DAT
6567
else
6668
"jsonld"
6769

68-
graph2String(transaction.get, uri, format=format)
70+
graph2String(transaction.get, uri, format=format) match {
71+
case Success(s) => s
72+
case Failure(f) => f.getLocalizedMessage
73+
}
6974
} catch {
7075
case t: Throwable =>
7176
t.printStackTrace()

scala/sparql_cache/src/main/scala/deductions/runtime/sparql_cache/SPARQLHelpers.scala

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ trait SPARQLHelpers[Rdf <: RDF, DATASET]
179179
sparqlConstructQuery(queryString, context=context),
180180
"", format)
181181
})
182-
transaction // .get
182+
transaction .flatten
183183
}
184184

185185
/** transactional */
@@ -557,7 +557,7 @@ trait SPARQLHelpers[Rdf <: RDF, DATASET]
557557
def sparqlSelectConneg(queryString: String,
558558
format: String = "xml",
559559
ds: DATASET = dataset,
560-
context: Map[String,String] = Map()): String = {
560+
context: Map[String,String] = Map()): Try[String] = {
561561
logger.debug(s"format $format")
562562
format match {
563563
case "jsonld" => sparqlSelectJSON(queryString, ds, context)
@@ -574,10 +574,11 @@ trait SPARQLHelpers[Rdf <: RDF, DATASET]
574574
*/
575575
def sparqlSelectXML(queryString: String,
576576
ds: DATASET = dataset,
577-
context: Map[String,String] = Map()): String = {
577+
context: Map[String,String] = Map()): Try[String] = {
578578
val result = sparqlSelectQuery(queryString, ds, context)
579579
val output = result match {
580580
case Success(res) =>
581+
val printer = new scala.xml.PrettyPrinter(80, 2)
581582
if (!res.isEmpty) {
582583
val header = res.head.map { node => literalNodeToString(node) }
583584
logger.debug(s"sparqlSelectXML: header $header")
@@ -616,33 +617,31 @@ trait SPARQLHelpers[Rdf <: RDF, DATASET]
616617
}
617618
</results>
618619
</sparql>
619-
xml
620+
Success( printer.format(xml) )
620621
} else {
621622
val xml =
622623
<sparql xmlns="http://www.w3.org/2005/sparql-results#">
623624
<head></head>
624625
<results></results>
625626
</sparql>
626-
xml
627+
Success( printer.format(xml) )
627628
}
628-
case Failure(f) =>
629-
<sparql>
630-
{
631-
Comment(
632-
"ERROR in sparql Select:\n" +
633-
f.getLocalizedMessage)
634-
}
635-
</sparql>
629+
case Failure(f) => Failure(f)
630+
// <sparql>
631+
// {
632+
// Comment(
633+
// "ERROR in sparql Select:\n" +
634+
// f.getLocalizedMessage)
635+
// }
636+
// </sparql>
636637
}
637-
// output.toString()
638-
val printer = new scala.xml.PrettyPrinter(80, 2)
639-
printer.format(output)
638+
output
640639
}
641640

642641
/** sparql Select, JSON output, see https://www.w3.org/TR/sparql11-results-json/#example */
643642
def sparqlSelectJSON(queryString: String,
644643
ds: DATASET = dataset,
645-
context: Map[String,String] = Map()): String = {
644+
context: Map[String,String] = Map()): Try[String] = {
646645
val result = sparqlSelectQuery(queryString, ds, context)
647646
// println(s">>>> sparqlSelectJSON: queryString '$queryString', context $context, result $result")
648647
val output = result match {
@@ -681,14 +680,19 @@ trait SPARQLHelpers[Rdf <: RDF, DATASET]
681680
binding
682681
}
683682
val resultsValue = Json.obj("bindings" -> bindings)
683+
Success(
684+
Json.prettyPrint(
684685
Json.obj(
685686
"head" -> headValue,
686-
"results" -> resultsValue)
687+
"results" -> resultsValue) ) )
687688
} else
688-
Json.obj("head" -> "", "results" -> "" )
689-
case Failure(f) => Json.toJson(f.getLocalizedMessage)
689+
Success(
690+
Json.prettyPrint(
691+
Json.obj("head" -> "", "results" -> "" )))
692+
case Failure(f) => Failure(f)
693+
// Json.toJson(f.getLocalizedMessage)
690694
}
691-
Json.prettyPrint(output)
695+
output
692696
}
693697

694698
/**
@@ -765,7 +769,7 @@ trait SPARQLHelpers[Rdf <: RDF, DATASET]
765769
* @param format "turtle" or "rdfxml" or "jsonld"
766770
* TODO REFACTOR, use conneg helper
767771
*/
768-
def graph2String(triples: Try[Rdf#Graph], baseURI: String, format: String = "turtle"): String = {
772+
def graph2String(triples: Try[Rdf#Graph], baseURI: String, format: String = "turtle"): Try[String] = {
769773
logger.info(s"graph2String: base URI <$baseURI>, format $format, ${triples}")
770774
triples match {
771775
case Success(graph) =>
@@ -780,15 +784,15 @@ trait SPARQLHelpers[Rdf <: RDF, DATASET]
780784

781785
// logger.debug( s">>>> graph2String writer $writer, stats $stats, baseURI $baseURI, graph $graph" )
782786

783-
stats + {
787+
Success( stats + {
784788
val tryString = writer.asString(graph, base = "") // baseURI)
785789
// logger.debug( s">>>> graph2String tryString $tryString" )
786790
tryString match {
787791
case Success(s) => s
788792
case Failure(f) => s"graph2String: trouble in writing graph: $f"
789793
}
790-
}
791-
case Failure(f) => f.getLocalizedMessage
794+
})
795+
case Failure(f) => Failure(f)
792796
}
793797
}
794798

0 commit comments

Comments
 (0)