Anonymous avatar Anonymous committed b614abf

Fix add model and select bnode

Comments (0)

Files changed (2)

src/main/java/net/chempound/ext/fourstore/FourStoreTripleStore.java

 import net.chempound.datastore.RemoteTripleStore;
 import net.chempound.datastore.TripleStore;
 import net.chempound.rdf.RdfIO;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpStatus;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpPut;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.util.EntityUtils;
 import org.xml.sax.SAXException;
 import org.xml.sax.helpers.DefaultHandler;
 import uk.co.magus.fourstore.client.Store;
 @Singleton
 public class FourStoreTripleStore extends RemoteTripleStore implements TripleStore {
 
+    private final SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
+    private final HttpClient httpClient = new DefaultHttpClient();
+
     private final Store store;
-    private SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
+    private final String dataUri;
 
     @Inject
     public FourStoreTripleStore(final URI url) {
         super(createSparqlUri(url));
+        this.dataUri = url.toString() + "/data/";
         try {
             this.store = new Store(url.toURL());
         } catch (final MalformedURLException e) {
     @Override
     public void saveModel(final URI uri, final Model model) {
         try {
-            store.add(uri.toString(), RdfIO.toString(model, "N3"), Store.InputFormat.N3);
+            final String depositUri = dataUri + uri.toString();
+            final HttpPut request = new HttpPut(depositUri);
+            final String rdf = RdfIO.toString(model, "RDF/XML");
+            final StringEntity entity = new StringEntity(rdf);
+            entity.setContentType(Store.InputFormat.XML.getMimeType());
+            request.setEntity(entity);
+            final HttpResponse response = httpClient.execute(request);
+            try {
+                if (response.getStatusLine().getStatusCode() != HttpStatus.SC_CREATED) {
+                    throw new IOException("Bad response: " + response.getStatusLine().toString());
+                }
+            } finally {
+                EntityUtils.consume(response.getEntity());
+            }
         } catch (IOException e) {
             throw new RuntimeException(e);
         }

src/main/java/net/chempound/ext/fourstore/ModelBuildingHandler.java

     }
 
     private Resource getBNode(final String uri) {
+        final Resource resource = bnodes.get(uri);
+        if (resource == null) {
+            return createBNode(uri);
+        }
+        return resource;
+    }
+
+    private Resource createBNode(final String uri) {
         final Resource resource = ResourceFactory.createResource();
         bnodes.put(uri, resource);
         return resource;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.