use of org.apache.jena.atlas.web.ContentType in project jena by apache.
the class TestSyntaxDetermination method test.
static void test(String url, String ct, Lang hint, Lang expected) {
ContentType x = WebContent.determineCT(ct, hint, url);
Lang lang = RDFDataMgr.determineLang(url, ct, hint);
assertEquals(expected, lang);
}
use of org.apache.jena.atlas.web.ContentType in project jena by apache.
the class ActionDatasets method bodyAsGraph.
// XXX Merge with Upload.incomingData
private static void bodyAsGraph(HttpAction action, StreamRDF dest) {
HttpServletRequest request = action.request;
String base = ActionLib.wholeRequestURL(request);
ContentType ct = FusekiLib.getContentType(request);
Lang lang = RDFLanguages.contentTypeToLang(ct.getContentType());
if (lang == null) {
ServletOps.errorBadRequest("Unknown content type for triples: " + ct);
return;
}
InputStream input = null;
try {
input = request.getInputStream();
} catch (IOException ex) {
IO.exception(ex);
}
// int len = request.getContentLength() ;
// if ( verbose ) {
// if ( len >= 0 )
// alog.info(format("[%d] Body: Content-Length=%d, Content-Type=%s, Charset=%s => %s", action.id, len,
// ct.getContentType(), ct.getCharset(), lang.getName())) ;
// else
// alog.info(format("[%d] Body: Content-Type=%s, Charset=%s => %s", action.id, ct.getContentType(),
// ct.getCharset(), lang.getName())) ;
// }
dest.prefix("root", base + "#");
ActionSPARQL.parse(action, dest, input, lang, base);
}
use of org.apache.jena.atlas.web.ContentType in project jena by apache.
the class ActionDatasets method execPostContainer.
// ---- POST
@Override
protected JsonValue execPostContainer(HttpAction action) {
JenaUUID uuid = JenaUUID.generate();
DatasetDescriptionRegistry registry = FusekiServer.registryForBuild();
ContentType ct = FusekiLib.getContentType(action);
boolean committed = false;
// Also acts as a concurrency lock
system.begin(ReadWrite.WRITE);
String systemFileCopy = null;
String configFile = null;
try {
// Where to build the templated service/database.
Model model = ModelFactory.createDefaultModel();
StreamRDF dest = StreamRDFLib.graph(model.getGraph());
if (WebContent.isHtmlForm(ct))
assemblerFromForm(action, dest);
else if (WebContent.isMultiPartForm(ct))
assemblerFromUpload(action, dest);
else
assemblerFromBody(action, dest);
// ----
// Keep a persistent copy immediately. This is not used for
// anything other than being "for the record".
systemFileCopy = FusekiServer.dirFileArea.resolve(uuid.asString()).toString();
try (OutputStream outCopy = IO.openOutputFile(systemFileCopy)) {
RDFDataMgr.write(outCopy, model, Lang.TURTLE);
}
// ----
// Process configuration.
Statement stmt = getOne(model, null, pServiceName, null);
if (stmt == null) {
StmtIterator sIter = model.listStatements(null, pServiceName, (RDFNode) null);
if (!sIter.hasNext())
ServletOps.errorBadRequest("No name given in description of Fuseki service");
sIter.next();
if (sIter.hasNext())
ServletOps.errorBadRequest("Multiple names given in description of Fuseki service");
throw new InternalErrorException("Inconsistent: getOne didn't fail the second time");
}
if (!stmt.getObject().isLiteral())
ServletOps.errorBadRequest("Found " + FmtUtils.stringForRDFNode(stmt.getObject()) + " : Service names are strings, then used to build the external URI");
Resource subject = stmt.getSubject();
Literal object = stmt.getObject().asLiteral();
if (object.getDatatype() != null && !object.getDatatype().equals(XSDDatatype.XSDstring))
action.log.warn(format("[%d] Service name '%s' is not a string", action.id, FmtUtils.stringForRDFNode(object)));
String datasetPath;
{
// Check the name provided.
String datasetName = object.getLexicalForm();
// ---- Check and canonicalize name.
if (datasetName.isEmpty())
ServletOps.error(HttpSC.BAD_REQUEST_400, "Empty dataset name");
if (StringUtils.isBlank(datasetName))
ServletOps.error(HttpSC.BAD_REQUEST_400, format("Whitespace dataset name: '%s'", datasetName));
if (datasetName.contains(" "))
ServletOps.error(HttpSC.BAD_REQUEST_400, format("Bad dataset name (contains spaces) '%s'", datasetName));
if (datasetName.equals("/"))
ServletOps.error(HttpSC.BAD_REQUEST_400, format("Bad dataset name '%s'", datasetName));
datasetPath = DataAccessPoint.canonical(datasetName);
}
action.log.info(format("[%d] Create database : name = %s", action.id, datasetPath));
// ---- Check whether it already exists
if (action.getDataAccessPointRegistry().isRegistered(datasetPath))
// And abort.
ServletOps.error(HttpSC.CONFLICT_409, "Name already registered " + datasetPath);
configFile = FusekiEnv.generateConfigurationFilename(datasetPath);
List<String> existing = FusekiEnv.existingConfigurationFile(datasetPath);
if (!existing.isEmpty())
ServletOps.error(HttpSC.CONFLICT_409, "Configuration file for '" + datasetPath + "' already exists");
// Write to configuration directory.
try (OutputStream outCopy = IO.openOutputFile(configFile)) {
RDFDataMgr.write(outCopy, model, Lang.TURTLE);
}
// Currently do nothing with the system database.
// In the future ... maybe ...
// Model modelSys = system.getNamedModel(gn.getURI()) ;
// modelSys.removeAll(null, pStatus, null) ;
// modelSys.add(subject, pStatus, FusekiVocab.stateActive) ;
// Need to be in Resource space at this point.
DataAccessPoint ref = FusekiBuilder.buildDataAccessPoint(subject, registry);
action.getDataAccessPointRegistry().register(datasetPath, ref);
action.getResponse().setContentType(WebContent.contentTypeTextPlain);
ServletOps.success(action);
system.commit();
committed = true;
} catch (IOException ex) {
IO.exception(ex);
} finally {
if (!committed) {
if (systemFileCopy != null)
FileOps.deleteSilent(systemFileCopy);
if (configFile != null)
FileOps.deleteSilent(configFile);
system.abort();
}
system.end();
}
return null;
}
use of org.apache.jena.atlas.web.ContentType in project jena by apache.
the class SPARQL_Upload method uploadWorker.
/** Process an HTTP file upload of RDF with additiona name field for the graph name.
* We can't stream straight into a dataset because the graph name can be after the data.
* @return graph name and count
*/
// ?? Combine with Upload.fileUploadWorker
// Difference is the handling of names for graphs.
private static UploadDetails uploadWorker(HttpAction action, String base) {
DatasetGraph dsgTmp = DatasetGraphFactory.create();
ServletFileUpload upload = new ServletFileUpload();
String graphName = null;
boolean isQuads = false;
long count = -1;
String name = null;
ContentType ct = null;
Lang lang = null;
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("") && !graphName.equals(HttpNames.valueDefault)) {
IRI iri = IRIResolver.parseIRI(value);
if (iri.hasViolation(false))
ServletOps.errorBadRequest("Bad IRI: " + graphName);
if (iri.getScheme() == null)
ServletOps.errorBadRequest("Bad IRI: no IRI scheme name: " + graphName);
if (iri.getScheme().equalsIgnoreCase("http") || iri.getScheme().equalsIgnoreCase("https")) {
// Redundant??
if (iri.getRawHost() == null)
ServletOps.errorBadRequest("Bad IRI: no host name: " + graphName);
if (iri.getRawPath() == null || iri.getRawPath().length() == 0)
ServletOps.errorBadRequest("Bad IRI: no path: " + graphName);
if (iri.getRawPath().charAt(0) != '/')
ServletOps.errorBadRequest("Bad IRI: Path does not start '/': " + graphName);
}
}
} else if (fieldName.equals(HttpNames.paramDefaultGraphURI))
graphName = null;
else
// Add file type?
action.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"))
ServletOps.errorBadRequest("No name for content - can't determine RDF syntax");
String contentTypeHeader = item.getContentType();
ct = ContentType.create(contentTypeHeader);
lang = RDFLanguages.contentTypeToLang(ct.getContentType());
if (lang == null) {
lang = RDFLanguages.filenameToLang(name);
// present we wrap the stream accordingly
if (name.endsWith(".gz"))
stream = new GZIPInputStream(stream);
}
if (lang == null)
// Desperate.
lang = RDFLanguages.RDFXML;
isQuads = RDFLanguages.isQuads(lang);
action.log.info(format("[%d] Upload: Filename: %s, Content-Type=%s, Charset=%s => %s", action.id, name, ct.getContentType(), ct.getCharset(), lang.getName()));
StreamRDF x = StreamRDFLib.dataset(dsgTmp);
StreamRDFCounting dest = StreamRDFLib.count(x);
ActionSPARQL.parse(action, dest, stream, lang, base);
count = dest.count();
}
}
if (graphName == null || graphName.equals(""))
graphName = HttpNames.valueDefault;
if (isQuads)
graphName = null;
return new UploadDetails(graphName, dsgTmp, count);
} catch (ActionErrorException ex) {
throw ex;
} catch (Exception ex) {
ServletOps.errorOccurred(ex);
return null;
}
}
use of org.apache.jena.atlas.web.ContentType in project jena by apache.
the class SPARQL_Update method validate.
@Override
protected void validate(HttpAction action) {
HttpServletRequest request = action.request;
if (HttpNames.METHOD_OPTIONS.equals(request.getMethod()))
return;
if (!HttpNames.METHOD_POST.equalsIgnoreCase(request.getMethod()))
ServletOps.errorMethodNotAllowed("SPARQL Update : use POST");
ContentType ct = FusekiLib.getContentType(action);
if (ct == null)
ct = ctSPARQLUpdate;
if (matchContentType(ctSPARQLUpdate, ct)) {
String charset = request.getCharacterEncoding();
if (charset != null && !charset.equalsIgnoreCase(charsetUTF8))
ServletOps.errorBadRequest("Bad charset: " + charset);
validate(action, paramsPOST);
return;
}
if (isHtmlForm(ct)) {
int x = countParamOccurences(request, paramUpdate) + countParamOccurences(request, paramRequest);
if (x == 0)
ServletOps.errorBadRequest("SPARQL Update: No 'update=' parameter");
if (x != 1)
ServletOps.errorBadRequest("SPARQL Update: Multiple 'update=' parameters");
String requestStr = request.getParameter(paramUpdate);
if (requestStr == null)
requestStr = request.getParameter(paramRequest);
if (requestStr == null)
ServletOps.errorBadRequest("SPARQL Update: No update= in HTML form");
validate(action, paramsForm);
return;
}
ServletOps.error(HttpSC.UNSUPPORTED_MEDIA_TYPE_415, "Must be " + contentTypeSPARQLUpdate + " or " + contentTypeHTMLForm + " (got " + ct.getContentType() + ")");
}
Aggregations