Search in sources :

Example 21 with ContentType

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;
}
Also used : ContentType(org.apache.jena.atlas.web.ContentType)

Example 22 with ContentType

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;
}
Also used : ContentType(org.apache.jena.atlas.web.ContentType)

Example 23 with ContentType

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;
}
Also used : MalformedURLException(java.net.MalformedURLException) ContentType(org.apache.jena.atlas.web.ContentType) RiotException(org.apache.jena.riot.RiotException) TypedInputStream(org.apache.jena.atlas.web.TypedInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) TypedInputStream(org.apache.jena.atlas.web.TypedInputStream) URL(java.net.URL)

Example 24 with ContentType

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;
    }
}
Also used : ContentType(org.apache.jena.atlas.web.ContentType) TypedInputStream(org.apache.jena.atlas.web.TypedInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) TypedInputStream(org.apache.jena.atlas.web.TypedInputStream)

Example 25 with ContentType

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());
}
Also used : WebContent.matchContentType(org.apache.jena.riot.WebContent.matchContentType) ContentType(org.apache.jena.atlas.web.ContentType)

Aggregations

ContentType (org.apache.jena.atlas.web.ContentType)28 InputStream (java.io.InputStream)9 IOException (java.io.IOException)8 WebContent.matchContentType (org.apache.jena.riot.WebContent.matchContentType)7 TypedInputStream (org.apache.jena.atlas.web.TypedInputStream)6 Lang (org.apache.jena.riot.Lang)4 OutputStream (java.io.OutputStream)3 GZIPInputStream (java.util.zip.GZIPInputStream)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 StreamRDFCounting (org.apache.jena.riot.lang.StreamRDFCounting)3 FileItemIterator (org.apache.commons.fileupload.FileItemIterator)2 FileItemStream (org.apache.commons.fileupload.FileItemStream)2 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)2 ContentProducer (org.apache.http.entity.ContentProducer)2 EntityTemplate (org.apache.http.entity.EntityTemplate)2 RDFFormat (org.apache.jena.riot.RDFFormat)2 RiotParseException (org.apache.jena.riot.RiotParseException)2 StreamRDF (org.apache.jena.riot.system.StreamRDF)2 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1