Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions jena-fuseki2/jena-fuseki-ui/src/services/fuseki.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,41 @@ class FusekiService {
throw new Error(error.response.data)
})
}

/**
* Fetch all prefix mappings of a dataset from its prefixes service.
* @param {string} datasetName - The name of the dataset with the prefix mappings
* @param {string} endpointName - name of the dataset's prefixes-r or prefixes-rw endpoint
* @returns {Promise<AxiosResponse<{prefix: string, uri: string}[]>>} all prefix mappings of the dataset
*/
async getPrefixes (datasetName, endpointName) {
return axios.get(this.getFusekiUrl(`/${datasetName}/${endpointName}`))
}

/**
* Add a prefix mapping to a dataset or replace an existing mapping.
* @param {string} datasetName - The name of the dataset with the prefix mappings
* @param {string} endpointName - name of the dataset's prefixes-rw endpoint
* @param {string} prefix - The prefix name to add or update
* @param {string} uri - The namespace URI the prefix expands to
* @returns {Promise<AxiosResponse<any>>}
*/
async updatePrefix (datasetName, endpointName, prefix, uri) {
const params = new URLSearchParams({ prefix, uri })
return axios.post(this.getFusekiUrl(`/${datasetName}/${endpointName}`), params)
}

/**
* Deletes a saved prefix from a dataset.
* @param {string} datasetName - The name of the dataset with the prefix mappings
* @param {string} endpointName - The name of the dataset's prefixes-rw endpoint
* @param {string} prefix - The prefix to remove
* @returns {Promise<AxiosResponse<any>>}
*/
async removePrefix (datasetName, endpointName, prefix) {
return axios.delete(this.getFusekiUrl(`/${datasetName}/${endpointName}`),
{ params: { prefix } })
}
}

export default FusekiService
57 changes: 57 additions & 0 deletions jena-fuseki2/jena-fuseki-ui/src/services/mock/json-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

import jsonServer from 'json-server'
import { DEFAULT_PREFIXES } from '../../utils/prefixes.js'

const PORT = process.env.FUSEKI_PORT || 3030

Expand Down Expand Up @@ -96,6 +97,16 @@ server.post('/\\$/datasets', (req, res) => {
'srv.type': 'upload',
'srv.description': 'File Upload',
'srv.endpoints': ['upload']
},
{

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New mock elements for the prefix store

'srv.type': 'prefixes-r',
'srv.description': 'Read prefixes',
'srv.endpoints': ['prefixes']
},
{
'srv.type': 'prefixes-rw',
'srv.description': 'Read-write prefixes',
'srv.endpoints': ['updatePrefixes']
}
]
}
Expand Down Expand Up @@ -267,6 +278,49 @@ server.post('/:datasetName/data', (req, res) => {
.send()
})

// PREFIXES
// In-memory prefix store per dataset, mirroring the Fuseki prefixes
// service semantics.
const PREFIXES = {}
const PREFIX_PATTERN = /^[A-Za-z]([\w.-]*\w)?$/

const prefixesFor = (datasetName) => {
if (!PREFIXES[datasetName]) {
PREFIXES[datasetName] = Object.fromEntries(
DEFAULT_PREFIXES.map(p => [p.prefix, p.uri]))
}
return PREFIXES[datasetName]
}

const listPrefixes = (req, res) => {
res.jsonp(
Object.entries(prefixesFor(req.params.datasetName))
.map(([prefix, uri]) => ({ prefix, uri }))
)
}
server.get('/:datasetName/prefixes', listPrefixes)
// The UI reads via the rw endpoint when the dataset declares one.
server.get('/:datasetName/updatePrefixes', listPrefixes)

server.post('/:datasetName/updatePrefixes', (req, res) => {
const { prefix, uri } = req.body
if (!prefix || !PREFIX_PATTERN.test(prefix)) {
res.status(400).send(`Invalid prefix name: '${prefix}'`)
return
}
if (!uri || /\s/.test(uri)) {
res.status(400).send(`Invalid prefix URI: '${uri}'`)
return
}
prefixesFor(req.params.datasetName)[prefix] = uri
res.sendStatus(200)
})

server.delete('/:datasetName/updatePrefixes', (req, res) => {
delete prefixesFor(req.params.datasetName)[req.query.prefix]
res.sendStatus(200)
})

// PING
// GET PING STATUS
server.get('/\\$/ping', (req, res) => {
Expand All @@ -286,6 +340,9 @@ server.get('/tests/reset', (req, res) => {
for (const dataset in DATASETS) {
delete DATASETS[dataset]
}
for (const dataset in PREFIXES) {
delete PREFIXES[dataset]
}
} catch (e) {
console.log(e)
}
Expand Down
28 changes: 28 additions & 0 deletions jena-fuseki2/jena-fuseki-ui/src/utils/prefixes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* Common prefixes offered in the query editor when a dataset has no
* prefixes service, or its prefix store is empty. Uses the same
* {prefix, uri} shape as the Fuseki prefixes service responses.
*/
export const DEFAULT_PREFIXES = [
{ prefix: 'rdf', uri: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#' },
{ prefix: 'rdfs', uri: 'http://www.w3.org/2000/01/rdf-schema#' },
{ prefix: 'owl', uri: 'http://www.w3.org/2002/07/owl#' },
{ prefix: 'xsd', uri: 'http://www.w3.org/2001/XMLSchema#' }
]
23 changes: 23 additions & 0 deletions jena-fuseki2/jena-fuseki-ui/src/utils/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,26 @@ export function validateGraphName (graphName) {
// If it reached this part, then it's a valid graph name.
return true
}

const PREFIX_NAME_PATTERN = /^[A-Za-z]([\w.-]*\w)?$/

/**
* Validates a prefix name for the Fuseki prefixes service.
*
* @param {string} prefix - The prefix name, e.g. "foaf".
* @return {boolean} - true iff the prefix name is valid.
*/
export function validatePrefixName (prefix) {
return prefix !== '' && PREFIX_NAME_PATTERN.test(prefix)
}

/**
* Validates a prefix namespace URI for the Fuseki prefixes service.
*
* @param {string} uri - The namespace URI the prefix expands to.
* @return {boolean} - true iff the URI looks valid.
*/
export function validatePrefixUri (uri) {
// Same rules as graph names: a non-empty, space-free, parseable URI.
return validateGraphName(uri)
}
Loading