use of org.apache.jena.riot.Lang in project jena by apache.
the class Upload method fileUploadWorker.
/** Process an HTTP upload of RDF files (triples or quads)
* Stream straight into a graph or dataset -- unlike SPARQL_Upload the destination
* is known at the start of the multipart file body
*/
public static UploadDetails fileUploadWorker(HttpAction action, StreamRDF dest) {
String base = ActionLib.wholeRequestURL(action.request);
ServletFileUpload upload = new ServletFileUpload();
//log.info(format("[%d] Upload: Field=%s ignored", action.id, fieldName)) ;
// Overall counting.
StreamRDFCounting countingDest = StreamRDFLib.count(dest);
try {
FileItemIterator iter = upload.getItemIterator(action.request);
while (iter.hasNext()) {
FileItemStream fileStream = iter.next();
if (fileStream.isFormField()) {
// Ignore?
String fieldName = fileStream.getFieldName();
InputStream stream = fileStream.openStream();
String value = Streams.asString(stream, "UTF-8");
ServletOps.errorBadRequest(format("Only files accepted in multipart file upload (got %s=%s)", fieldName, value));
}
//Ignore the field name.
//String fieldName = fileStream.getFieldName();
InputStream stream = fileStream.openStream();
// Process the input stream
String contentTypeHeader = fileStream.getContentType();
ContentType ct = ContentType.create(contentTypeHeader);
Lang lang = null;
if (!matchContentType(ctTextPlain, ct))
lang = RDFLanguages.contentTypeToLang(ct.getContentType());
if (lang == null) {
String name = fileStream.getName();
if (name == null || name.equals(""))
ServletOps.errorBadRequest("No name for content - can't determine RDF syntax");
lang = RDFLanguages.filenameToLang(name);
if (name.endsWith(".gz"))
stream = new GZIPInputStream(stream);
}
if (lang == null)
// Desperate.
lang = RDFLanguages.RDFXML;
String printfilename = fileStream.getName();
if (printfilename == null || printfilename.equals(""))
printfilename = "<none>";
// Before
// action.log.info(format("[%d] Filename: %s, Content-Type=%s, Charset=%s => %s",
// action.id, printfilename, ct.getContentType(), ct.getCharset(), lang.getName())) ;
// count just this step
StreamRDFCounting countingDest2 = StreamRDFLib.count(countingDest);
try {
ActionSPARQL.parse(action, countingDest2, stream, lang, base);
UploadDetails details1 = new UploadDetails(countingDest2.count(), countingDest2.countTriples(), countingDest2.countQuads());
action.log.info(format("[%d] Filename: %s, Content-Type=%s, Charset=%s => %s : %s", action.id, printfilename, ct.getContentType(), ct.getCharset(), lang.getName(), details1.detailsStr()));
} catch (RiotParseException ex) {
action.log.info(format("[%d] Filename: %s, Content-Type=%s, Charset=%s => %s : %s", action.id, printfilename, ct.getContentType(), ct.getCharset(), lang.getName(), ex.getMessage()));
throw ex;
}
}
} catch (ActionErrorException ex) {
throw ex;
} catch (Exception ex) {
ServletOps.errorOccurred(ex.getMessage());
}
// Overall results.
UploadDetails details = new UploadDetails(countingDest.count(), countingDest.countTriples(), countingDest.countQuads());
return details;
}
use of org.apache.jena.riot.Lang in project jena by apache.
the class DataValidatorHTML method executeHTML.
//static final String paramSyntaxExtended = "extendedSyntax" ;
public static void executeHTML(HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
try {
// if ( log.isInfoEnabled() )
// log.info("data validation request") ;
String syntax = FusekiLib.safeParameter(httpRequest, paramSyntax);
if (syntax == null || syntax.equals(""))
syntax = RDFLanguages.NQUADS.getName();
Lang language = RDFLanguages.shortnameToLang(syntax);
if (language == null) {
httpResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, "Unknown syntax: " + syntax);
return;
}
Reader input = createInput(httpRequest, httpResponse);
ServletOutputStream outStream = httpResponse.getOutputStream();
ErrorHandlerMsg errorHandler = new ErrorHandlerMsg(outStream);
// Capture logging errors.
PrintStream stderr = System.err;
System.setErr(new PrintStream(outStream));
// Headers
setHeaders(httpResponse);
outStream.println("<html>");
printHead(outStream, "Jena Data Validator Report");
outStream.println("<body>");
outStream.println("<h1>RIOT Parser Report</h1>");
outStream.println("<p>Line and column numbers refer to original input</p>");
outStream.println("<p> </p>");
// Need to escape HTML.
OutputStream output1 = new OutputStreamNoHTML(new BufferedOutputStream(outStream));
StreamRDF output = StreamRDFWriter.getWriterStream(output1, Lang.NQUADS);
try {
startFixed(outStream);
RDFParser parser = RDFParser.create().lang(language).errorHandler(errorHandler).resolveURIs(false).build();
RiotException exception = null;
startFixed(outStream);
try {
output.start();
parser.parse(output);
output.finish();
output1.flush();
outStream.flush();
System.err.flush();
} catch (RiotException ex) {
ex.printStackTrace(stderr);
exception = ex;
}
} finally {
finishFixed(outStream);
System.err.flush();
System.setErr(stderr);
}
outStream.println("</body>");
outStream.println("</html>");
} catch (Exception ex) {
serviceLog.warn("Exception in validationRequest", ex);
}
}
use of org.apache.jena.riot.Lang in project jena by apache.
the class REST_Quads method doPost.
@Override
protected void doPost(HttpAction action) {
if (!action.getDatasetRef().allowDatasetUpdate)
errorMethodNotAllowed("POST");
// Graph Store Protocol mode - POST triples to dataset causes
// a new graph to be created and the new URI returned via Location.
// Normally off.
// When off, POST of triples goes to default graph.
boolean gspMode = Fuseki.graphStoreProtocolPostCreate;
// Code to pass the GSP test suite.
// Not necessarily good code.
String x = action.request.getContentType();
if (x == null)
errorBadRequest("Content-type required for data format");
MediaType mediaType = MediaType.create(x);
Lang lang = RDFLanguages.contentTypeToLang(mediaType.getContentType());
if (lang == null)
lang = RDFLanguages.TRIG;
if (action.verbose)
log.info(format("[%d] Post: Content-Type=%s, Charset=%s => %s", action.id, mediaType.getContentType(), mediaType.getCharset(), lang.getName()));
if (RDFLanguages.isQuads(lang))
doPostQuads(action, lang);
else if (gspMode && RDFLanguages.isTriples(lang))
doPostTriplesGSP(action, lang);
else if (RDFLanguages.isTriples(lang))
doPostTriples(action, lang);
else
errorBadRequest("Not a triples or quads format: " + mediaType);
}
use of org.apache.jena.riot.Lang in project jena by apache.
the class REST_Quads method doGet.
@Override
protected void doGet(HttpAction action) {
MediaType mediaType = HttpAction.contentNegotationQuads(action);
ServletOutputStream output;
try {
output = action.response.getOutputStream();
} catch (IOException ex) {
errorOccurred(ex);
output = null;
}
TypedOutputStream out = new TypedOutputStream(output, mediaType);
Lang lang = RDFLanguages.contentTypeToLang(mediaType.getContentType());
if (lang == null)
lang = RDFLanguages.TRIG;
if (action.verbose)
log.info(format("[%d] Get: Content-Type=%s, Charset=%s => %s", action.id, mediaType.getContentType(), mediaType.getCharset(), lang.getName()));
if (!RDFLanguages.isQuads(lang))
errorBadRequest("Not a quads format: " + mediaType);
action.beginRead();
try {
DatasetGraph dsg = action.getActiveDSG();
RDFDataMgr.write(out, dsg, lang);
success(action);
} finally {
action.endRead();
}
}
use of org.apache.jena.riot.Lang in project jena by apache.
the class ModLangOutput method printRegistered.
private static void printRegistered(PrintStream out) {
out.println("Streaming languages:");
Set<Lang> seen = new HashSet<>();
for (RDFFormat fmt : StreamRDFWriter.registered()) {
Lang lang = fmt.getLang();
if (hiddenLanguages.contains(lang))
continue;
if (seen.contains(lang))
continue;
seen.add(lang);
out.println(" " + lang.getLabel());
}
System.err.println("Non-streaming languages:");
for (RDFFormat fmt : RDFWriterRegistry.registered()) {
Lang lang = fmt.getLang();
if (hiddenLanguages.contains(lang))
continue;
if (seen.contains(lang))
continue;
seen.add(lang);
out.println(" " + lang.getLabel());
}
}
Aggregations