Skip to content

Commit acf7372

Browse files
committed
Do not create absolute URI's from user entered strings, but use defaultInstanceURIHostPrefix from config.
DONE tested with useLocalHostPrefixForURICreation=true & the LDP GET URL of this server: the JSON returned is empty TODO: - the LDP GET service should return the same data as the web HTML /display service - content negotiation for LDP server
1 parent 5d6078d commit acf7372

File tree

10 files changed

+184
-79
lines changed

10 files changed

+184
-79
lines changed

scala/forms/src/main/scala/deductions/runtime/abstract_syntax/FormSyntaxFactory.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ trait FormSyntaxFactory[Rdf <: RDF, DATASET]
109109
fromUri(_), fromBNode(_), fromLiteral(_)._1))
110110

111111
val rdfs = RDFSPrefix[Rdf]
112-
override val rdf = RDFPrefix[Rdf]
112+
override lazy val rdf = RDFPrefix[Rdf]
113113

114114

115115
/** create Form from an instance (subject) URI;

scala/forms/src/main/scala/deductions/runtime/abstract_syntax/InstanceLabelsInference2.scala

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package deductions.runtime.abstract_syntax
22

33
import scala.collection.Seq
4-
54
import org.w3.banana.FOAFPrefix
65
import org.w3.banana.PointedGraph
76
import org.w3.banana.RDF
87
import org.w3.banana.RDFPrefix
98
import org.w3.banana.RDFSPrefix
9+
import deductions.runtime.utils.RDFHelpers
1010

1111
/**
1212
* populate Fields in form by inferring possible label from:
@@ -18,13 +18,14 @@ import org.w3.banana.RDFSPrefix
1818
* systematically trying to get the matching language form,
1919
* and as last fallback, the last segment of the URI.
2020
*/
21-
private[abstract_syntax] trait InstanceLabelsInference2[Rdf <: RDF] {
21+
private[abstract_syntax] trait InstanceLabelsInference2[Rdf <: RDF]
22+
extends RDFHelpers[Rdf] {
2223
self: PreferredLanguageLiteral[Rdf] =>
2324

2425
import ops._
2526
private lazy val foaf = FOAFPrefix[Rdf]
2627
private lazy val rdfs = RDFSPrefix[Rdf]
27-
private lazy val rdf = RDFPrefix[Rdf]
28+
// private lazy val rdf = RDFPrefix[Rdf]
2829

2930
def instanceLabels(list: Seq[Rdf#Node], lang: String = "")(implicit graph: Rdf#Graph): Seq[String] =
3031
list.map { node => instanceLabel(node, graph, lang) }
@@ -83,20 +84,21 @@ private[abstract_syntax] trait InstanceLabelsInference2[Rdf <: RDF] {
8384
case None => lsegment
8485
}
8586
}
86-
87-
def last_segment(node: Rdf#Node) =
88-
try {
89-
foldNode(node)(
90-
uri => {
91-
val ls = lastSegment(uri)
92-
ls match {
93-
case "" => fromUri(uri)
94-
case _ => ls
95-
}
96-
},
97-
bn => bn.toString(),
98-
x => x.toString())
99-
} catch {
100-
case t: Throwable => node.toString()
101-
}
87+
88+
// /** NOTE: currently lastSegment() in Banana can return null :( */
89+
// def last_segment(node: Rdf#Node) =
90+
// try {
91+
// foldNode(node)(
92+
// uri => {
93+
// val ls = lastSegment(uri)
94+
// ls match {
95+
// case "" => fromUri(uri)
96+
// case _ => ls
97+
// }
98+
// },
99+
// bn => bn.toString(),
100+
// x => x.toString())
101+
// } catch {
102+
// case t: Throwable => node.toString()
103+
// }
102104
}

scala/forms/src/main/scala/deductions/runtime/abstract_syntax/UnfilledFormFactory.scala

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,58 @@
44

55
package deductions.runtime.abstract_syntax
66

7+
import java.net.InetAddress
8+
import java.net.URLEncoder
79
import scala.language.postfixOps
810
import org.w3.banana.RDF
911
import deductions.runtime.services.Configuration
10-
import java.net.InetAddress
1112
import deductions.runtime.services.DefaultConfiguration
13+
import deductions.runtime.utils.URIHelpers
14+
import java.net.URLDecoder
1215

13-
/**
16+
/** TODO move to package services, and rename to URIManagement
1417
* @author j.m. Vanel
15-
*
1618
*/
17-
object UnfilledFormFactory extends DefaultConfiguration {
19+
object UnfilledFormFactory extends DefaultConfiguration
20+
with URIHelpers {
21+
22+
def makeId: String = {
23+
makeId(instanceURIPrefix)
24+
}
25+
26+
/** make URI From String, if not already an absolute URI */
27+
def makeURIFromString(objectStringFromUser: String): String = {
28+
if (isAbsoluteURI(objectStringFromUser))
29+
objectStringFromUser
30+
else {
31+
instanceURIPrefix +
32+
// urlencode takes care of other forbidden character in "candidate" URI
33+
URLEncoder.encode(objectStringFromUser.replaceAll(" ", "_"), "UTF-8")
34+
}
35+
}
36+
37+
/** make String From URI */
38+
def makeStringFromURI( uri: String): String = {
39+
lastSegment( URLDecoder.decode( uri, "UTF-8" ) ).replaceAll("_", " ")
40+
}
41+
1842
/** make a unique Id with given prefix, currentTimeMillis() and nanoTime() */
19-
def makeId(instanceURIPrefix: String): String = {
43+
private def makeId(instanceURIPrefix: String): String = {
2044
instanceURIPrefix + System.currentTimeMillis() + "-" + System.nanoTime() // currentId = currentId + 1
2145
}
46+
47+
/** TODO : get the actual port */
48+
val port = "9000"
49+
50+
val instanceURIPrefix: String = {
51+
val hostNameUsed =
52+
if (useLocalHostPrefixForURICreation) {
53+
"http://" + InetAddress.getLocalHost().getHostName()
54+
// TODO : get the actual port
55+
} else defaultInstanceURIHostPrefix
56+
hostNameUsed + ":" + port + "/" + relativeURIforCreatedResourcesByForm
57+
}
58+
2259
}
2360

2461
/** Factory for an Unfilled Form */
@@ -27,12 +64,7 @@ trait UnfilledFormFactory[Rdf <: RDF, DATASET]
2764
with FormConfigurationFactory[Rdf, DATASET]
2865
with Configuration {
2966

30-
val instanceURIPrefix: String = // defaultInstanceURIPrefix
31-
if( useLocalHostPrefixForURICreation ) {
32-
val localHost = InetAddress.getLocalHost().getHostName()
33-
localHost + "/" + relativeURIforCreatedResourcesByForm
34-
} else defaultInstanceURIHostPrefix
35-
67+
3668
import ops._
3769

3870
/**
@@ -51,7 +83,7 @@ trait UnfilledFormFactory[Rdf <: RDF, DATASET]
5183
} else {
5284
lookPropertieslistFormInConfiguration(classs)
5385
}
54-
val newId = makeId
86+
val newId = UnfilledFormFactory . makeId
5587
if (propsListInFormConfig.isEmpty) {
5688
val props = fieldsFromClass(classs, graph)
5789
createFormDetailed(makeUri(newId), addRDFSLabelComment(props), classs, CreationMode)
@@ -60,7 +92,4 @@ trait UnfilledFormFactory[Rdf <: RDF, DATASET]
6092
CreationMode, formConfig = formConfig)
6193
}
6294

63-
def makeId: String = {
64-
UnfilledFormFactory.makeId(instanceURIPrefix)
65-
}
6695
}

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@ package deductions.runtime.services
22

33
import deductions.runtime.html.CSS
44

5-
//trait Configuration {
65
trait DefaultConfiguration extends Configuration {
6+
/**
7+
* use Local Host from getLocalHost() as Prefix For URI Creation, otherwise use defaultInstanceURIHostPrefix below
8+
* TODO true should be the default, to adapt to normal machine settings
9+
*/
10+
override val useLocalHostPrefixForURICreation = false
11+
712
/** URI Prefix prepended to newly created resource instances */
813
override val defaultInstanceURIHostPrefix =
9-
"http://ldp.assemblee-virtuelle.org/" // ??????
10-
/** otherwise use defaultInstanceURIHostPrefix */
11-
override val useLocalHostPrefixForURICreation = false
14+
// "http://ldp.assemblee-virtuelle.org/"
15+
"http://ldp.virtual-assembly.org"
1216

1317
override val relativeURIforCreatedResourcesByForm = "ldp/"
1418
override val relativeURIforCreatedResourcesByLDP = relativeURIforCreatedResourcesByForm

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
package deductions.runtime.services
22

33
import java.net.URLDecoder
4-
54
import scala.collection.mutable.ArrayBuffer
65
import scala.concurrent.ExecutionContext.Implicits.global
76
import scala.concurrent.Future
87
import scala.util.Failure
98
import scala.util.Try
10-
119
import org.apache.log4j.Logger
1210
import org.w3.banana.FOAFPrefix
1311
import org.w3.banana.RDF
1412
import org.w3.banana.TryW
15-
1613
import deductions.runtime.dataset.RDFStoreLocalProvider
1714
import deductions.runtime.semlogs.LogAPI
1815
import deductions.runtime.utils.Timer
16+
import deductions.runtime.abstract_syntax.UnfilledFormFactory
1917

2018
trait FormSaver[Rdf <: RDF, DATASET]
2119
extends RDFStoreLocalProvider[Rdf, DATASET]
@@ -88,10 +86,12 @@ trait FormSaver[Rdf <: RDF, DATASET]
8886
val objectFromUser = foldNode(originalTriple.objectt)(
8987
_ => { if( objectStringFromUser.startsWith("_:") )
9088
BNode(objectStringFromUser.substring(2))
91-
else
92-
// TODO other forbidden character in URI
93-
URI(objectStringFromUser.replaceAll(" ", "_"))
94-
},
89+
else {
90+
if (objectStringFromUser != "")
91+
println(s"objectStringFromUser $objectStringFromUser changed: spaces removed")
92+
URI( UnfilledFormFactory.makeURIFromString(objectStringFromUser) )
93+
}
94+
},
9595
_ => BNode(objectStringFromUser.replaceAll(" ", "_")), // ?? really do this ?
9696
_ => Literal(objectStringFromUser))
9797
if (originalTriple.objectt != objectStringFromUser) {

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

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package deductions.runtime.services
22

3+
import scala.language.postfixOps
4+
import org.w3.banana.FOAFPrefix
35
import org.w3.banana.PointedGraph
46
import org.w3.banana.RDF
57
import org.w3.banana.RDFSPrefix
6-
import org.w3.banana.SparqlGraphModule
78
import deductions.runtime.dataset.RDFStoreLocalProvider
8-
import org.w3.banana.FOAFPrefix
9-
import scala.language.postfixOps
9+
import deductions.runtime.utils.URIHelpers
10+
import deductions.runtime.abstract_syntax.UnfilledFormFactory
1011

1112
/**
1213
* ensure that types inferred from ontologies are added to objects of given triples
@@ -15,7 +16,8 @@ import scala.language.postfixOps
1516
* so that the form for V will be correctly populated.
1617
*/
1718
trait TypeAddition[Rdf <: RDF, DATASET]
18-
extends RDFStoreLocalProvider[Rdf, DATASET] {
19+
extends RDFStoreLocalProvider[Rdf, DATASET]
20+
with URIHelpers {
1921

2022
import ops._
2123
import sparqlOps._
@@ -32,12 +34,16 @@ trait TypeAddition[Rdf <: RDF, DATASET]
3234
v.flatten
3335
}
3436

35-
/** NON transactional */
37+
/** input a triple ?S ?P ?O , where the type(s) ?C of ?O is inferred from rdfs:range of ?P,
38+
* and then these triples are added in given graph URI:
39+
* ?O a ?C
40+
* NON transactional */
3641
def addType(triple: Rdf#Triple, graphURI: Option[Rdf#URI],
3742
graph: Rdf#Graph = allNamedGraph): Iterable[Rdf#Triple] = {
3843
val objectt = triple.objectt
3944
val pgObjectt = PointedGraph[Rdf](objectt, graph)
4045

46+
/** these triples are added in given graph URI: ?O a ?C */
4147
def addTypeValue() = {
4248
val existingTypes = (pgObjectt / rdf.typ).nodes
4349
if (existingTypes isEmpty) {
@@ -56,15 +62,30 @@ trait TypeAddition[Rdf <: RDF, DATASET]
5662
} else Seq()
5763
}
5864

65+
/** if there is not already some rdfs.label, foaf.lastName, foaf.familyName properties set,
66+
* add a triple
67+
* ?O rdfs.label ?LAB ,
68+
* where ?LAB is computed from URI string of ?O
69+
* NOTE: related to InstanceLabelsInference2#instanceLabel(), but here we actually add a triple,
70+
* because we are in a callback for user edits */
5971
def addRDFSLabelValue() = {
6072
val existingValues = (pgObjectt / rdfs.label).nodes
6173
val existingValues2 = (pgObjectt / foaf.lastName).nodes
6274
val existingValues3 = (pgObjectt / foaf.familyName).nodes
6375
if (existingValues.isEmpty &&
6476
existingValues2.isEmpty &&
6577
existingValues3.isEmpty &&
66-
!objectt.toString().contains(":")) {
67-
val labelTriple = makeTriple(objectt, rdfs.label, Literal(objectt.toString().replace("_", " ")))
78+
( ! isAbsoluteURI(objectt.toString()) ||
79+
objectt.toString().startsWith( UnfilledFormFactory.instanceURIPrefix )
80+
) ) {
81+
// ! objectt.toString().contains(":")) {
82+
if (isAbsoluteURI(objectt.toString()))
83+
println("isAbsoluteURI " + objectt)
84+
val labelTriple = makeTriple(
85+
objectt, rdfs.label,
86+
Literal( UnfilledFormFactory.makeStringFromURI(
87+
objectt.toString() ) )
88+
)
6889
rdfStore.appendToGraph( dataset, makeGraphForSaving(), ops.makeGraph(Seq(labelTriple)))
6990
}
7091
}
@@ -77,6 +98,8 @@ trait TypeAddition[Rdf <: RDF, DATASET]
7798
lit => URI("")))
7899
}
79100

101+
////
102+
80103
val result = if (objectt.isURI) {
81104
val pgObjectt = PointedGraph[Rdf](objectt, graph)
82105
val existingTypes = (pgObjectt / rdf.typ).nodes

scala/forms/src/main/scala/deductions/runtime/sparql_cache/RDFCacheAlgo.scala

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package deductions.runtime.sparql_cache
22

33
import java.util.Date
4-
54
import scala.concurrent.ExecutionContext.Implicits
65
import scala.util.Failure
76
import scala.util.Success
87
import scala.util.Try
9-
108
import org.apache.log4j.Logger
119
import org.w3.banana.OWLPrefix
1210
import org.w3.banana.RDF
@@ -15,9 +13,10 @@ import org.w3.banana.io.RDFLoader
1513
import org.w3.banana.io.RDFReader
1614
import org.w3.banana.io.RDFXML
1715
import org.w3.banana.io.Turtle
18-
1916
import deductions.runtime.dataset.RDFStoreLocalProvider
2017
import deductions.runtime.services.SPARQLHelpers
18+
import deductions.runtime.utils.URIHelpers
19+
import deductions.runtime.utils.RDFHelpers
2120

2221
//import deductions.runtime.abstract_syntax.InstanceLabelsInferenceMemory0
2322

@@ -34,7 +33,8 @@ trait RDFCacheAlgo[Rdf <: RDF, DATASET] extends RDFStoreLocalProvider[Rdf, DATAS
3433
with RDFLoader[Rdf, Try]
3534
with SPARQLHelpers[Rdf, DATASET]
3635
with TimestampManagement[Rdf, DATASET]
37-
with MirrorManagement[Rdf, DATASET] {
36+
with MirrorManagement[Rdf, DATASET]
37+
with RDFHelpers[Rdf] {
3838

3939
import ops._
4040
import rdfStore.transactorSyntax._
@@ -218,21 +218,14 @@ trait RDFCacheAlgo[Rdf <: RDF, DATASET] extends RDFStoreLocalProvider[Rdf, DATAS
218218
throw e
219219
}
220220
}
221-
222-
def isDownloadableURI(uri: Rdf#URI) = {
223-
val u = fromUri(uri)
224-
u.startsWith("http") ||
225-
u.startsWith("ftp:") ||
226-
u.startsWith("file:")
227-
// u.startsWith("_:")
228-
}
229221

230222
/**
231223
* read from uri and store in TDB, no matter what the syntax is;
232224
* can also load an URI with the # part
233225
*/
234226
def storeURINoTransaction(uri: Rdf#URI, graphUri: Rdf#URI, dataset: DATASET): Rdf#Graph = {
235227
Logger.getRootLogger().info(s"Before load uri $uri into graphUri $graphUri")
228+
236229
if (isDownloadableURI(uri)) {
237230
System.setProperty("sun.net.client.defaultReadTimeout", "10000")
238231
System.setProperty("sun.net.client.defaultConnectTimeout", "10000")
@@ -242,10 +235,10 @@ trait RDFCacheAlgo[Rdf <: RDF, DATASET] extends RDFStoreLocalProvider[Rdf, DATAS
242235
rdfStore.appendToGraph( dataset, graphUri, graph)
243236
Logger.getRootLogger().info(s"storeURI uri $uri : stored into graphUri $graphUri")
244237
graph
238+
245239
} else {
246240
val message = s"Load uri $uri is not possible, not a downloadable URI."
247241
Logger.getRootLogger().warn(message)
248-
// throw new Exception(message)
249242
emptyGraph
250243
}
251244
}

0 commit comments

Comments
 (0)