use of org.apache.jena.atlas.web.ContentType in project jena by apache.
the class WebContent method determineCT.
/**
* <p>
* Determine the content type to be used, given the target URL, the content-type from
* Content Negotiation and a hint language. This is a pragmatic balance.
* A content-type of "text/plain" is ignored - it is too often wrong.
* </p><p>
* The decision is
* <blockquote>
* <i>Content type</i> (but not text/plain) > <i>hint</i> > <i>file extension</i>.
* </blockquote>
* We make content type (via content negotiation) strongest because a server
* may return something unexpected because that is all it can do. We are
* assuming servers don't lie. The "hint" is really a hint just for file extenion override.
* </p><p>
* In the case of no file extension, this reduces to the hint being
* the default choice if conneg does not produce anything useful.
* </p>
*
* @param contentTypeStr Content-Type string
* @param hintLang Default language
* @param target The URL of the target (file extension may be used)
*
* @return ContentType or null
*/
public static ContentType determineCT(String contentTypeStr, Lang hintLang, String target) {
boolean isTextPlain = contentTypeTextPlain.equals(contentTypeStr);
if (contentTypeStr != null)
contentTypeStr = contentTypeCanonical(contentTypeStr);
// If it's text plain, we ignore it because a lot of naive
// server setups return text/plain for any file type.
// (It was never registered as being N-triples;
// that was only for RDF 2004 testing.)
ContentType ct = null;
if (!isTextPlain)
// Not guaranteed to be registered as a language here.
ct = (contentTypeStr == null) ? null : ContentType.create(contentTypeStr);
if (ct == null && hintLang != null)
ct = hintLang.getContentType();
if (ct == null)
ct = RDFLanguages.guessContentType(target);
return ct;
}
use of org.apache.jena.atlas.web.ContentType in project jena by apache.
the class RDFDataMgr method determineLang.
/** Determine the Lang, given the URI target, any content type header string and a hint */
public static Lang determineLang(String target, String ctStr, Lang hintLang) {
ContentType ct = WebContent.determineCT(ctStr, hintLang, target);
if (ct == null)
return hintLang;
Lang lang = RDFLanguages.contentTypeToLang(ct);
if (lang == null)
return hintLang;
return lang;
}
use of org.apache.jena.atlas.web.ContentType in project jena by apache.
the class LocatorFTP method performOpen.
@Override
public TypedInputStream performOpen(String uri) {
if (uri.startsWith("ftp://")) {
try {
URL url = new URL(uri);
InputStream in = url.openStream();
ContentType ct = RDFLanguages.guessContentType(uri);
return new TypedInputStream(in, ct);
} catch (MalformedURLException ex) {
throw new RiotException("Bad FTP URL: " + uri, ex);
} catch (IOException ex) {
// This includes variations on "not found"
IO.exception(ex);
}
}
return null;
}
use of org.apache.jena.atlas.web.ContentType in project jena by apache.
the class LocatorZip method open.
@Override
public TypedInputStream open(String filenameOrURI) {
ZipEntry entry = zipFile.getEntry(filenameOrURI);
if (entry == null) {
if (StreamManager.logAllLookups && log.isDebugEnabled())
log.debug("Not found: " + zipFileName + " : " + filenameOrURI);
return null;
}
try {
InputStream in = zipFile.getInputStream(entry);
if (in == null) {
if (StreamManager.logAllLookups && log.isTraceEnabled())
log.trace("Not found: " + filenameOrURI);
return null;
}
if (StreamManager.logAllLookups && log.isTraceEnabled())
log.trace("Found: " + filenameOrURI);
ContentType ct = RDFLanguages.guessContentType(filenameOrURI);
return new TypedInputStream(in, ct, filenameOrURI);
} catch (IOException ex) {
log.warn("IO Exception opening zip entry: " + filenameOrURI);
return null;
}
}
use of org.apache.jena.atlas.web.ContentType in project jena by apache.
the class SPARQL_Query method perform.
@Override
protected final void perform(HttpAction action) {
// OPTIONS
if (action.request.getMethod().equals(HttpNames.METHOD_OPTIONS)) {
// Share with update via SPARQL_Protocol.
doOptions(action);
return;
}
// GET
if (action.request.getMethod().equals(HttpNames.METHOD_GET)) {
executeWithParameter(action);
return;
}
ContentType ct = FusekiLib.getContentType(action);
// POST ?query= and no Content-Type
if (ct == null || isHtmlForm(ct)) {
// validation checked that if no Content-type, then its a POST with ?query=
executeWithParameter(action);
return;
}
// POST application/sparql-query
if (matchContentType(ct, ctSPARQLQuery)) {
executeBody(action);
return;
}
ServletOps.error(HttpSC.UNSUPPORTED_MEDIA_TYPE_415, "Bad content type: " + ct.getContentType());
}
Aggregations