{
ServletFileUpload upload = new ServletFileUpload();
// Locking only needed over the insert into the dataset
String graphName = null ;
String name = null ;
ContentType ct = null ;
Lang lang = null ;
int tripleCount = 0 ;
try {
FileItemIterator iter = upload.getItemIterator(action.request);
while (iter.hasNext()) {
FileItemStream item = iter.next();
String fieldName = item.getFieldName();
InputStream stream = item.openStream();
if (item.isFormField())
{
// Graph name.
String value = Streams.asString(stream, "UTF-8") ;
if ( fieldName.equals(HttpNames.paramGraph) )
{
graphName = value ;
if ( graphName != null && ! graphName.equals(HttpNames.valueDefault) )
{
IRI iri = IRIResolver.parseIRI(value) ;
if ( iri.hasViolation(false) )
errorBadRequest("Bad IRI: "+graphName) ;
if ( iri.getScheme() == null )
errorBadRequest("Bad IRI: no IRI scheme name: "+graphName) ;
if ( iri.getScheme().equalsIgnoreCase("http") || iri.getScheme().equalsIgnoreCase("https"))
{
// Redundant??
if ( iri.getRawHost() == null )
errorBadRequest("Bad IRI: no host name: "+graphName) ;
if ( iri.getRawPath() == null || iri.getRawPath().length() == 0 )
errorBadRequest("Bad IRI: no path: "+graphName) ;
if ( iri.getRawPath().charAt(0) != '/' )
errorBadRequest("Bad IRI: Path does not start '/': "+graphName) ;
}
}
}
else if ( fieldName.equals(HttpNames.paramDefaultGraphURI) )
graphName = null ;
else
// Add file type?
log.info(format("[%d] Upload: Field=%s ignored", action.id, fieldName)) ;
} else {
// Process the input stream
name = item.getName() ;
if ( name == null || name.equals("") || name.equals("UNSET FILE NAME") )
errorBadRequest("No name for content - can't determine RDF syntax") ;
String contentTypeHeader = item.getContentType() ;
ct = ContentType.parse(contentTypeHeader) ;
lang = WebContent.contentTypeToLang(ct.getContentType()) ;
if ( lang == null )
lang = RDFLanguages.filenameToLang(name) ;
if ( lang == null )
// Desperate.
lang = RDFLanguages.RDFXML ;
// We read into a in-memory graph, then (if successful) update the dataset.
// This isolates errors.
StreamRDF dest = StreamRDFLib.graph(graphDst) ;
LangRIOT parser = RiotReader.createParser(stream, lang, base, dest) ;
parser.getProfile().setHandler(errorHandler) ;
log.info(format("[%d] Upload: Filename: %s, Content-Type=%s, Charset=%s => %s",
action.id, name, ct.getContentType(), ct.getCharset(), lang.getName())) ;
try { parser.parse() ; }
catch (RiotException ex) { errorBadRequest("Parse error: "+ex.getMessage()) ; }
}
}