Search in sources :

Example 1 with ContentType

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

Example 2 with ContentType

use of org.apache.jena.atlas.web.ContentType in project jena by apache.

the class LocatorClassLoader method open.

@Override
public TypedInputStream open(String resourceName) {
    if (classLoader == null)
        return null;
    InputStream in = classLoader.getResourceAsStream(resourceName);
    if (in == null) {
        if (StreamManager.logAllLookups && log.isTraceEnabled())
            log.trace("Failed to open: " + resourceName);
        return null;
    }
    if (StreamManager.logAllLookups && log.isTraceEnabled())
        log.trace("Found: " + resourceName);
    ContentType ct = RDFLanguages.guessContentType(resourceName);
    // No sensible base URI.
    return new TypedInputStream(in, ct, null);
}
Also used : ContentType(org.apache.jena.atlas.web.ContentType) TypedInputStream(org.apache.jena.atlas.web.TypedInputStream) InputStream(java.io.InputStream) TypedInputStream(org.apache.jena.atlas.web.TypedInputStream)

Example 3 with ContentType

use of org.apache.jena.atlas.web.ContentType in project jena by apache.

the class LocatorFile method open.

/** Open anything that looks a bit like a file name */
@Override
public TypedInputStream open(String filenameIRI) {
    String fn = toFileName(filenameIRI);
    if (fn == null)
        return null;
    try {
        if (!exists$(fn)) {
            if (StreamManager.logAllLookups && log.isTraceEnabled())
                log.trace("Not found: " + filenameIRI + thisDirLogStr);
            return null;
        }
    } catch (AccessControlException e) {
        log.warn("Security problem testing for file", e);
        return null;
    }
    try {
        InputStream in = IO.openFileEx(fn);
        if (StreamManager.logAllLookups && log.isTraceEnabled())
            log.trace("Found: " + filenameIRI + thisDirLogStr);
        ContentType ct = RDFLanguages.guessContentType(filenameIRI);
        return new TypedInputStream(in, ct, filenameIRI);
    } catch (IOException ioEx) {
        // Includes FileNotFoundException
        // We already tested whether the file exists or not.
        log.warn("File unreadable (but exists): " + fn + " Exception: " + ioEx.getMessage());
        return null;
    }
}
Also used : ContentType(org.apache.jena.atlas.web.ContentType) TypedInputStream(org.apache.jena.atlas.web.TypedInputStream) InputStream(java.io.InputStream) AccessControlException(java.security.AccessControlException) IOException(java.io.IOException) TypedInputStream(org.apache.jena.atlas.web.TypedInputStream)

Example 4 with ContentType

use of org.apache.jena.atlas.web.ContentType in project jena by apache.

the class SPARQL_REST_RW method incomingData.

private static void incomingData(HttpAction action, StreamRDF dest) {
    String base = wholeRequestURL(action.request);
    ContentType ct = FusekiLib.getContentType(action);
    Lang lang = RDFLanguages.contentTypeToLang(ct.getContentType());
    if (lang == null) {
        errorBadRequest("Unknown content type for triples: " + ct);
        return;
    }
    InputStream input = null;
    try {
        input = action.request.getInputStream();
    } catch (IOException ex) {
        IO.exception(ex);
    }
    int len = action.request.getContentLength();
    if (action.verbose) {
        if (len >= 0)
            log.info(format("[%d]   Body: Content-Length=%d, Content-Type=%s, Charset=%s => %s", action.id, len, ct.getContentType(), ct.getCharset(), lang.getName()));
        else
            log.info(format("[%d]   Body: Content-Type=%s, Charset=%s => %s", action.id, ct.getContentType(), ct.getCharset(), lang.getName()));
    }
    parse(action, dest, input, lang, base);
}
Also used : ContentType(org.apache.jena.atlas.web.ContentType) InputStream(java.io.InputStream) Lang(org.apache.jena.riot.Lang) IOException(java.io.IOException)

Example 5 with ContentType

use of org.apache.jena.atlas.web.ContentType in project jena by apache.

the class SPARQL_Query method validateParams.

/**
     * Helper method for validating request.
     * @param request HTTP request
     * @param params parameters in a collection of Strings
     */
protected void validateParams(HttpServletRequest request, Collection<String> params) {
    ContentType ct = FusekiLib.getContentType(request);
    boolean mustHaveQueryParam = true;
    if (ct != null) {
        String incoming = ct.getContentType();
        if (WebContent.contentTypeSPARQLQuery.equals(incoming)) {
            mustHaveQueryParam = false;
        //error(HttpSC.UNSUPPORTED_MEDIA_TYPE_415, "Unofficial "+WebContent.contentTypeSPARQLQuery+" not supported") ;
        } else if (WebContent.contentTypeHTMLForm.equals(incoming)) {
        } else
            error(HttpSC.UNSUPPORTED_MEDIA_TYPE_415, "Unsupported: " + incoming);
    }
    if (mustHaveQueryParam) {
        int N = countParamOccurences(request, paramQuery);
        if (N == 0)
            errorBadRequest("SPARQL Query: No 'query=' parameter");
        if (N > 1)
            errorBadRequest("SPARQL Query: Multiple 'query=' parameters");
        // application/sparql-query does not use a query param.
        String queryStr = request.getParameter(HttpNames.paramQuery);
        if (queryStr == null)
            errorBadRequest("SPARQL Query: No query specified (no 'query=' found)");
        if (queryStr.isEmpty())
            errorBadRequest("SPARQL Query: Empty query string");
    }
    if (params != null) {
        Enumeration<String> en = request.getParameterNames();
        for (; en.hasMoreElements(); ) {
            String name = en.nextElement();
            if (!params.contains(name))
                warning("SPARQL Query: Unrecognize request parameter (ignored): " + name);
        }
    }
}
Also used : ContentType(org.apache.jena.atlas.web.ContentType)

Aggregations

ContentType (org.apache.jena.atlas.web.ContentType)29 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