Package com.epimorphics.rdfutil
Class SPARQLUpdate
- java.lang.Object
-
- com.epimorphics.rdfutil.SPARQLUpdate
-
public class SPARQLUpdate extends Object
Low level support for POSTing SPARQL update requests to a server. Supports logging of the entire request to a file and serialization of models. Typical sequence:SPARQLUpdate up = new SPARQLUpdate(server); up.open(); up.sendPrefixes(model); up.send(command); up.send(model, true); up.send(command); up.close(); int status = up.getStatus(); boolean ok = up.wasSuccessful();
- Version:
- $Revision: $
- Author:
- Dave Reynolds
-
-
Field Summary
Fields Modifier and Type Field Description protected HttpURLConnectionconnprotected BufferedWriterlogFileprotected BufferedWriteroutprotected StringresponseBodyprotected StringresponseMessageprotected booleanstartedprotected intstatusprotected URLtarget
-
Constructor Summary
Constructors Constructor Description SPARQLUpdate(String server)Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Finish the update command, capturing the result status and any return messageStringgetResponseBody()Get the body of the HTTP response, in the event of an error response the server may return useful human readable information in the body.StringgetResponseMessage()Get the message part of the HTTP response, generally not interestingintgetStatus()Get the returned status code from the requestbooleanisStarted()Return true if at least one command string has been sent, whether complete or notvoidopen()Open the POST channel to the servervoidperformUpdate(String preamble, org.apache.jena.rdf.model.Model model, String postamble)Convenience packaging of common pattern of use.voidsend(InputStream in)Write a set of formatted SPARQL Update instructions to the streamvoidsend(String command)Send a string (hopefully part of a SPARQL update command!) to the servervoidsend(org.apache.jena.rdf.model.Model model)Write a model as part of the update request, typically the body of and INSERT DATA {} or DELETE DATA {} request but it is up to the caller to send the surrounding command syntax.voidsend(org.apache.jena.rdf.model.Model model, boolean turtle)Write a model as part of the update request, typically the body of and INSERT DATA {} or DELETE DATA {} request but it is up to the caller to send the surrounding command syntax.voidsendPrefixes(org.apache.jena.shared.PrefixMapping pm)Write the prefixes from the given model (or other prefix mapping) in SPARQL update syntax.voidsendUpdate(String preamble, org.apache.jena.rdf.model.Model model, String postamble)Convenience packaging of common pattern of use.voidsetLogFileBase(String filebase)Request that the entire formatted update request be recorded in a timestamped file with the name "- .log". booleanwasSuccessful()Return true if the update succeeded (response code 2xx or 3xx)
-
-
-
Field Detail
-
logFile
protected BufferedWriter logFile
-
target
protected URL target
-
status
protected int status
-
responseMessage
protected String responseMessage
-
responseBody
protected String responseBody
-
conn
protected HttpURLConnection conn
-
out
protected BufferedWriter out
-
started
protected boolean started
-
-
Constructor Detail
-
SPARQLUpdate
public SPARQLUpdate(String server)
Constructor. Throws an unchecked exception (EpiException) if the server URL is malformed.- Parameters:
server- URL of the of the server which should be send the request
-
-
Method Detail
-
setLogFileBase
public void setLogFileBase(String filebase)
Request that the entire formatted update request be recorded in a timestamped file with the name "- .log". Throws an unchecked exception (EpiException) if the file can't be created or is not writable - Parameters:
file- name of the file to log to
-
open
public void open()
Open the POST channel to the server
-
send
public void send(String command)
Send a string (hopefully part of a SPARQL update command!) to the server
-
isStarted
public boolean isStarted()
Return true if at least one command string has been sent, whether complete or not
-
sendPrefixes
public void sendPrefixes(org.apache.jena.shared.PrefixMapping pm)
Write the prefixes from the given model (or other prefix mapping) in SPARQL update syntax. This is separated from send(model) because in SPARQL update the prefix block comes before the command.
-
send
public void send(org.apache.jena.rdf.model.Model model, boolean turtle)Write a model as part of the update request, typically the body of and INSERT DATA {} or DELETE DATA {} request but it is up to the caller to send the surrounding command syntax.- Parameters:
model- the model to sendturtle- if true then the model will be send in compact turtle syntax using any prefixes declared in the model (the caller must have issued sendPrefixes at the start of the update for this to be legal), if false then N-Triple syntax will be used which is more verbose but faster to write and does not require prefixes
-
send
public void send(org.apache.jena.rdf.model.Model model)
Write a model as part of the update request, typically the body of and INSERT DATA {} or DELETE DATA {} request but it is up to the caller to send the surrounding command syntax. Uses N-triple syntax so that the model data is self-contained with no requirement to have declared prefixes.
-
send
public void send(InputStream in) throws IOException
Write a set of formatted SPARQL Update instructions to the stream- Throws:
IOException
-
performUpdate
public void performUpdate(String preamble, org.apache.jena.rdf.model.Model model, String postamble)
Convenience packaging of common pattern of use. Opens the connection if not already open, sends Model prefixes, then preamble command, then the model (using compact Turtle) then postamble command then closes.
-
sendUpdate
public void sendUpdate(String preamble, org.apache.jena.rdf.model.Model model, String postamble)
Convenience packaging of common pattern of use. Opens the connection if not already open, sends Model prefixes, then preamble command, then the model (using compact Turtle) then postamble command. Leaves udpate open
-
close
public void close()
Finish the update command, capturing the result status and any return message
-
getStatus
public int getStatus()
Get the returned status code from the request
-
getResponseMessage
public String getResponseMessage()
Get the message part of the HTTP response, generally not interesting
-
getResponseBody
public String getResponseBody()
Get the body of the HTTP response, in the event of an error response the server may return useful human readable information in the body. May be null if the there was no response body (e.g. 204 return)
-
wasSuccessful
public boolean wasSuccessful()
Return true if the update succeeded (response code 2xx or 3xx)- Returns:
-
-