Skip to content

Commit cdd6718

Browse files
committed
Saving app settings to RDF file works
1 parent ba7080e commit cdd6718

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

src/main/java/com/atomgraph/linkeddatahub/Application.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,6 @@
176176
import java.io.FileOutputStream;
177177
import java.io.UnsupportedEncodingException;
178178
import java.nio.charset.StandardCharsets;
179-
import java.nio.file.Path;
180-
import java.nio.file.Paths;
181179
import java.security.MessageDigest;
182180
import java.util.Arrays;
183181
import java.util.List;
@@ -2018,7 +2016,7 @@ public Model getContextModel()
20182016
public Model getDataspaceModel(com.atomgraph.linkeddatahub.apps.model.Application application)
20192017
{
20202018
if (application == null) throw new IllegalArgumentException("Application cannot be null");
2021-
return getContextDataset().getNamedModel(application.getURI());
2019+
return ModelFactory.createModelForGraph(new GraphReadOnly(getContextDataset().getNamedModel(application.getURI()).getGraph()));
20222020
}
20232021

20242022
/**
@@ -2032,7 +2030,7 @@ public Model getDataspaceModel(com.atomgraph.linkeddatahub.apps.model.Applicatio
20322030
* @param newModel the new RDF model to replace the existing named graph
20332031
* @throws IOException if an I/O error occurs
20342032
*/
2035-
public void updateDataspace(com.atomgraph.linkeddatahub.apps.model.Application application, Model newModel) throws IOException
2033+
public void updateApp(com.atomgraph.linkeddatahub.apps.model.Application application, Model newModel) throws IOException
20362034
{
20372035
if (application == null) throw new IllegalArgumentException("Application cannot be null");
20382036
if (newModel == null) throw new IllegalArgumentException("Model cannot be null");
@@ -2041,22 +2039,22 @@ public void updateDataspace(com.atomgraph.linkeddatahub.apps.model.Application a
20412039
{
20422040
String dataspaceURI = application.getURI();
20432041

2044-
// Only support file-based URIs for the default implementation
2045-
if (!getContextDatasetURI().isAbsolute() || !"file".equals(getContextDatasetURI().getScheme()))
2046-
{
2047-
throw new UnsupportedOperationException("Only file-based context dataset URIs are supported for updates in default implementation");
2048-
}
2049-
2050-
Path configFilePath = Paths.get(getContextDatasetURI());
2051-
20522042
// Update the named graph in the dataset
20532043
getContextDataset().removeNamedModel(dataspaceURI).
20542044
addNamedModel(dataspaceURI, newModel);
20552045

2056-
// Write the updated dataset back to file
2057-
com.atomgraph.linkeddatahub.server.util.SystemConfigFileManager.writeDataset(getContextDataset(), configFilePath);
2046+
// Write the updated dataset back to file using RDFDataMgr
2047+
// Support both absolute file:// URIs and relative webapp paths (like getDataset does)
2048+
try (java.io.OutputStream out = (getContextDatasetURI().isAbsolute() ?
2049+
new FileOutputStream(new java.io.File(getContextDatasetURI())) :
2050+
new FileOutputStream(getServletConfig().getServletContext().getRealPath(getContextDatasetURI().toString()))))
2051+
{
2052+
Lang lang = RDFDataMgr.determineLang(getContextDatasetURI().toString(), null, null);
2053+
if (lang == null) throw new IOException("Could not determine RDF format from dataset URI: " + getContextDatasetURI().toString());
20582054

2059-
if (log.isInfoEnabled()) log.info("Updated dataspace <{}> in file: {}", dataspaceURI, configFilePath);
2055+
RDFDataMgr.write(out, getContextDataset(), lang);
2056+
if (log.isInfoEnabled()) log.info("Updated dataspace <{}> in context dataset: {}", dataspaceURI, getContextDatasetURI());
2057+
}
20602058
}
20612059
}
20622060

src/main/java/com/atomgraph/linkeddatahub/resource/Settings.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.apache.jena.query.Dataset;
3636
import org.apache.jena.query.DatasetFactory;
3737
import org.apache.jena.rdf.model.Model;
38+
import org.apache.jena.rdf.model.ModelFactory;
3839
import org.apache.jena.update.UpdateAction;
3940
import org.apache.jena.update.UpdateRequest;
4041
import org.slf4j.Logger;
@@ -119,22 +120,25 @@ public Response patch(UpdateRequest updateRequest) throws IOException
119120
if (dataspaceModel == null || dataspaceModel.isEmpty())
120121
throw new NotFoundException("No settings found for dataspace <" + getApplication().getURI() + "> in context dataset");
121122

123+
// Create a mutable copy since getDataspaceModel() returns a read-only view
124+
Model mutableModel = ModelFactory.createDefaultModel().add(dataspaceModel);
125+
122126
// Execute the SPARQL UPDATE on the dataspace model in memory
123-
Dataset dataset = DatasetFactory.wrap(dataspaceModel);
127+
Dataset dataset = DatasetFactory.wrap(mutableModel);
124128
UpdateAction.execute(updateRequest, dataset);
125129

126130
// if PATCH results in an empty model, reject it as Bad Request
127-
if (dataspaceModel.isEmpty())
131+
if (mutableModel.isEmpty())
128132
{
129133
if (log.isWarnEnabled()) log.warn("PATCH resulted in empty dataspace model for <{}>", getApplication().getURI());
130134
throw new BadRequestException("PATCH cannot result in empty dataspace model");
131135
}
132136

133137
// validate the updated model
134-
validate(dataspaceModel);
138+
validate(mutableModel);
135139

136140
// Write the updated model back to the context dataset file
137-
getSystem().updateDataspace(getApplication(), dataspaceModel);
141+
getSystem().updateApp(getApplication(), mutableModel);
138142

139143
if (log.isInfoEnabled()) log.info("Updated settings for dataspace <{}> via PATCH", getApplication().getURI());
140144

0 commit comments

Comments
 (0)