try {
String graphName = null ;
Graph graphTmp = GraphFactory.createGraphMem() ;
Node gn = null ;
String name = null ;
ContentType ct = null ;
Lang lang = null ;
int tripleCount = 0 ;
FileItemIterator iter = upload.getItemIterator(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) ;
}
gn = Node.createURI(graphName) ;
}
}
else if ( fieldName.equals(HttpNames.paramDefaultGraphURI) )
graphName = null ;
else
// Add file type?
log.info(format("[%d] Upload: Field="+fieldName+" - ignored")) ;
} 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 = FusekiLib.langFromContentType(ct.getContentType()) ;
if ( lang == null )
lang = Lang.guess(name) ;
if ( lang == null )
// Desperate.
lang = Lang.RDFXML ;
String base = "http://example/upload-base/" ;
// We read into a in-memory graph, then (if successful) update the dataset.
// This isolates errors.
Sink<Triple> sink = new SinkTriplesToGraph(graphTmp) ;
LangRIOT parser = RiotReader.createParserTriples(stream, lang, base, sink) ;
parser.getProfile().setHandler(errorHandler) ;
try {
parser.parse() ;
}
catch (RiotException ex) { errorBadRequest("Parse error: "+ex.getMessage()) ; }
finally { sink.close() ; }
tripleCount = graphTmp.size() ;
//DatasetGraph dsgTmp = DatasetGraphFactory.create(graphTmp) ;
}
}
if ( graphName == null )
graphName = "default" ;
log.info(format("[%d] Upload: Filename: %s, Content-Type=%s, Charset=%s => (%s,%s,%d triple(s))",
action.id, name, ct.getContentType(), ct.getCharset(), graphName, lang.getName(), tripleCount)) ;
// Delay updating until all form fields processed to get the graph name
action.beginWrite() ;
try {
if ( graphName.equals(HttpNames.valueDefault) )