Java Code Examples for org.openrdf.rio.RDFFormat#forMIMEType()

The following examples show how to use org.openrdf.rio.RDFFormat#forMIMEType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OntologyLoader.java    From anno4j with Apache License 2.0 5 votes vote down vote up
private RDFFormat forMIMEType(String contentType, RDFFormat fallback) {
	RDFFormat format = RDFFormat.forMIMEType(contentType);
	RDFParserRegistry registry = RDFParserRegistry.getInstance();
	if (format != null && registry.has(format))
		return format;
	return fallback;
}
 
Example 2
Source File: UpdateServlet.java    From database with GNU General Public License v2.0 5 votes vote down vote up
private void processData(final BigdataSailConnection conn, 
        final String contentType, 
        final InputStream is, 
        final RDFHandler handler,
        final String baseURI) 
            throws Exception {

    /**
     * Note: The request was already validated.
     * 
     * <a href="https://sourceforge.net/apps/trac/bigdata/ticket/620">
     * UpdateServlet fails to parse MIMEType when doing conneg. </a>
     */

    final RDFFormat format = RDFFormat
            .forMIMEType(new MiniMime(contentType).getMimeType());

    final RDFParserFactory rdfParserFactory = RDFParserRegistry
            .getInstance().get(format);

    final RDFParser rdfParser = rdfParserFactory.getParser();

    rdfParser.setValueFactory(conn.getTripleStore()
            .getValueFactory());

    rdfParser.setVerifyData(true);

    rdfParser.setStopAtFirstError(true);

    rdfParser
            .setDatatypeHandling(RDFParser.DatatypeHandling.IGNORE);

    rdfParser.setRDFHandler(handler);

    /*
     * Run the parser, which will cause statements to be deleted.
     */
    rdfParser.parse(is, baseURI);

}
 
Example 3
Source File: RepositoryModel.java    From semweb4j with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Resolves an RDF2Go {@link Syntax} to an OpenRDF {@link RDFFormat}.
 * 
 * @param syntax The RDF2Go Syntax to resolve.
 * @return A RDFFormat for the specified syntax.
 * @throws SyntaxNotSupportedException When the Syntax could not be resolved to a
 *             RDFFormat.
 */
public static RDFFormat getRDFFormat(Syntax syntax) throws SyntaxNotSupportedException {
    for (String mimeType : syntax.getMimeTypes()) {
        RDFFormat format = RDFFormat.forMIMEType(mimeType);
        if (format != null) {
            return format;
        }
    }
	throw new SyntaxNotSupportedException("This version of Sesame seems to have no "
	        + "support for " + syntax);
}
 
Example 4
Source File: RDFDataSetModule.java    From mustard with MIT License 5 votes vote down vote up
/**
 * RDFDataSet based on a single file
 * 
 * @param filename
 * @param mimetype
 */
public RDFDataSetModule(
		@In(name="filename") String filename, 
		@In(name="mimetype") String mimetype,
		@In(name="isUrl") Boolean isUrl) {
	super();
	this.filename = filename;
	this.format = RDFFormat.forMIMEType(mimetype);
	this.isUrl = isUrl;
}
 
Example 5
Source File: InsertServlet.java    From database with GNU General Public License v2.0 4 votes vote down vote up
/**
 * POST with request body containing statements to be inserted.
 * 
 * @param req
 *            The request.
 * 
 * @return The response.
 * 
 * @throws Exception
 */
private void doPostWithBody(final HttpServletRequest req,
		final HttpServletResponse resp) throws IOException {
    
       final String baseURI = req.getRequestURL().toString();
       
       final String contentType = req.getContentType();

       if (contentType == null)
           buildAndCommitResponse(resp, HTTP_BADREQUEST, MIME_TEXT_PLAIN,
                   "Content-Type not specified.");
       
       if (log.isInfoEnabled())
           log.info("Request body: " + contentType);

       /**
        * <a href="https://sourceforge.net/apps/trac/bigdata/ticket/620">
        * UpdateServlet fails to parse MIMEType when doing conneg. </a>
        */

	final String mimeTypeStr = new MiniMime(contentType).getMimeType();

	final RDFFormat format = RDFFormat.forMIMEType(mimeTypeStr);

       if (format == null) {

           buildAndCommitResponse(resp, HTTP_BADREQUEST, MIME_TEXT_PLAIN,
                   "Content-Type not recognized as RDF: " + contentType);

           return;

       }

       if (log.isInfoEnabled())
           log.info("RDFFormat=" + format);
       
       final RDFParserFactory rdfParserFactory = RDFParserRegistry
               .getInstance().get(format);

       if (rdfParserFactory == null) {

           buildAndCommitResponse(resp, HTTP_INTERNALERROR, MIME_TEXT_PLAIN,
                   "Parser factory not found: Content-Type=" + contentType
                           + ", format=" + format);

           return;

       }

       /*
        * Allow the caller to specify the default contexts.
        */
       final Resource[] defaultContext;
       {
           final String[] s = req.getParameterValues(BigdataRDFContext.CONTEXT_URI);
           if (s != null && s.length > 0) {
               try {
               	defaultContext = toURIs(s);
               } catch (IllegalArgumentException ex) {
                   buildAndCommitResponse(resp, HTTP_INTERNALERROR, MIME_TEXT_PLAIN,
                           ex.getLocalizedMessage());
                   return;
               }
           } else {
               defaultContext = new Resource[0];
           }
       }

       try {
           
           submitApiTask(
                   new InsertWithBodyTask(req, resp, getNamespace(req),
                           ITx.UNISOLATED, baseURI, defaultContext,
                           rdfParserFactory)).get();
           
       } catch (Throwable t) {

        BigdataRDFServlet.launderThrowable(t, resp,
              "INSERT-WITH-BODY: baseURI=" + baseURI + ", Content-Type="
                    + contentType + ", " + BigdataRDFContext.CONTEXT_URI + "="
                    + Arrays.toString(defaultContext));

       }

   }
 
Example 6
Source File: DeleteServlet.java    From database with GNU General Public License v2.0 4 votes vote down vote up
/**
 * DELETE request with a request body containing the statements to be
 * removed.
 */
private void doDeleteWithBody(final HttpServletRequest req,
        final HttpServletResponse resp) throws IOException {

    final String baseURI = req.getRequestURL().toString();

    final String contentType = req.getContentType();
    
    final boolean suppressTruthMaintenance = getBooleanValue(req, QueryServlet.ATTR_TRUTH_MAINTENANCE, false);

    if (contentType == null)
        throw new UnsupportedOperationException();

    if (log.isInfoEnabled())
        log.info("Request body: " + contentType);

    /**
     * There is a request body, so let's try and parse it.
     * 
     * <a href="https://sourceforge.net/apps/trac/bigdata/ticket/620">
     * UpdateServlet fails to parse MIMEType when doing conneg. </a>
     */

    final RDFFormat format = RDFFormat.forMIMEType(new MiniMime(
            contentType).getMimeType());

    if (format == null) {

        buildAndCommitResponse(resp, HTTP_BADREQUEST, MIME_TEXT_PLAIN,
                "Content-Type not recognized as RDF: " + contentType);

        return;

    }

    final RDFParserFactory rdfParserFactory = RDFParserRegistry
            .getInstance().get(format);

    if (rdfParserFactory == null) {

        buildAndCommitResponse(resp, HTTP_INTERNALERROR, MIME_TEXT_PLAIN,
                "Parser factory not found: Content-Type=" + contentType
                        + ", format=" + format);

        return;

    }

    /*
     * Allow the caller to specify the default contexts.
     */
    final Resource[] defaultContext;
    {
        final String[] s = req.getParameterValues("context-uri");
        if (s != null && s.length > 0) {
            try {
                defaultContext = toURIs(s);
            } catch (IllegalArgumentException ex) {
                buildAndCommitResponse(resp, HTTP_INTERNALERROR, MIME_TEXT_PLAIN,
                        ex.getLocalizedMessage());
                return;
            }
        } else {
            defaultContext = new Resource[0];
        }
    }

    try {

        submitApiTask(
                new DeleteWithBodyTask(req, resp, getNamespace(req),
                        ITx.UNISOLATED, baseURI, suppressTruthMaintenance, defaultContext,
                        rdfParserFactory)).get();

    } catch (Throwable t) {

        BigdataRDFServlet.launderThrowable(t, resp,
                "DELETE-WITH-BODY: baseURI=" + baseURI + ", context-uri="
                        + Arrays.toString(defaultContext));

    }

}
 
Example 7
Source File: HttpProtocol.java    From cumulusrdf with Apache License 2.0 4 votes vote down vote up
/**
 * <p>Retrieves the MIME type for the content-type parameter.
 * The content-type parameter could be specified by the client as a parameter or 
 * a header in the HTTP request.
 * HTTP header has a higher priority.</p>
 * <p> Default (if no content-type parameter is given): RDF/XML.</p>
 * 
 * @param request - the HTTP request.
 * @return MIME type of the content-type parameter.
 */
public static String parseContentTypeHeader(final HttpServletRequest request) {

	String content_type = request.getHeader(Headers.CONTENT_TYPE);

	if (isNullOrEmptyString(content_type)) {
		content_type = getParameterValue(request, Parameters.CONTENT_TYPE);
	}

	if (isNotNullOrEmptyString(content_type)) {
		content_type = content_type.trim().toLowerCase();
	}

	if (RDFFormat.forMIMEType(content_type) == null) {

		/*
		 * default: MimeTypes.RDF_XML
		 */
		if ((content_type == null) || content_type.isEmpty()) {
			content_type = MimeTypes.RDF_XML;
		}
		/*
		 * guess MIME type
		 */
		else if (content_type.contains("htm")) {
			content_type = MimeTypes.TEXT_HTML;
		} else if (content_type.contains("xml")) {
			content_type = MimeTypes.RDF_XML;
		} else if (content_type.contains("rdf+json")) {
			content_type = MimeTypes.RDF_JSON;
		} else if (content_type.contains("json")) {
			content_type = MimeTypes.JSON_LD;
		} else if (content_type.contains("trix")) {
			content_type = MimeTypes.TRIX;
		} else if (content_type.contains("trig")) {
			content_type = MimeTypes.TRIG;
		} else if (content_type.contains("n3")) {
			content_type = MimeTypes.N3;
		} else if (content_type.contains("turtle") || content_type.contains("ttl")) {
			content_type = MimeTypes.TURTLE;
		} else if (content_type.contains("triple")) {
			content_type = MimeTypes.TEXT_PLAIN;
		} else if (content_type.contains("quad")) {
			content_type = MimeTypes.N_QUADS;
		} else if (content_type.contains("plain")) {
			content_type = MimeTypes.TEXT_PLAIN;
		} else if (content_type.contains("binary")) {
			content_type = MimeTypes.BINARY;
		}
		/*
		 * default: RDF/XML				
		 */
		else {
			content_type = MimeTypes.RDF_XML;
		}
	}

	return content_type;
}
 
Example 8
Source File: UpdateServlet.java    From database with GNU General Public License v2.0 2 votes vote down vote up
private boolean validateItem(
		final HttpServletResponse resp, final FileItem item) 
			throws IOException {
	
	final String contentType = item.getContentType();
	
    if (contentType == null) {
    	
        buildAndCommitResponse(resp, HTTP_BADREQUEST, MIME_TEXT_PLAIN,
                "Content-Type not specified");

        return false;
        
    }

       final RDFFormat format = RDFFormat
               .forMIMEType(new MiniMime(contentType).getMimeType());

    if (format == null) {

        buildAndCommitResponse(resp, HTTP_BADREQUEST, MIME_TEXT_PLAIN,
                "Content-Type not recognized as RDF: " + contentType);

        return false;

    }
    
       final RDFParserFactory rdfParserFactory = RDFParserRegistry
	        .getInstance().get(format);
	
	if (rdfParserFactory == null) {
	
	    buildAndCommitResponse(resp, HTTP_INTERNALERROR, MIME_TEXT_PLAIN,
	            "Parser factory not found: Content-Type=" + contentType
	                    + ", format=" + format);
	
	    return false;
	
	}

    if (item.getInputStream() == null) {
    	
        buildAndCommitResponse(resp, HTTP_BADREQUEST, MIME_TEXT_PLAIN,
                "No content");

        return false;
    	
    }
    
    return true;
	
}
 
Example 9
Source File: WorkbenchServlet.java    From database with GNU General Public License v2.0 2 votes vote down vote up
/**
     * Convert RDF data from one format to another.
     */
    private void doConvert(final HttpServletRequest req,
            final HttpServletResponse resp) throws IOException {
        
    	final String baseURI = req.getRequestURL().toString();
    	
    	// The content type of the request.
        final String contentType = req.getContentType();

        if (log.isInfoEnabled())
            log.info("Request body: " + contentType);

        /**
         * <a href="https://sourceforge.net/apps/trac/bigdata/ticket/620">
         * UpdateServlet fails to parse MIMEType when doing conneg. </a>
         */

        final RDFFormat requestBodyFormat = RDFFormat.forMIMEType(new MiniMime(
                contentType).getMimeType());

        if (requestBodyFormat == null) {

            buildAndCommitResponse(resp, HTTP_BADREQUEST, MIME_TEXT_PLAIN,
                    "Content-Type not recognized as RDF: " + contentType);

            return;

        }

        final RDFParserFactory rdfParserFactory = RDFParserRegistry
                .getInstance().get(requestBodyFormat);

        if (rdfParserFactory == null) {

            buildAndCommitResponse(resp, HTTP_INTERNALERROR, MIME_TEXT_PLAIN,
                    "Parser factory not found: Content-Type="
                            + contentType + ", format=" + requestBodyFormat);
            
            return;

        }

//        final String s= IOUtil.readString(req.getInputStream());
//        System.err.println(s);
        
        final Graph g = new LinkedHashModel();
        
        try {
        
	        /*
	         * There is a request body, so let's try and parse it.
	         */
	
	        final RDFParser rdfParser = rdfParserFactory
	                .getParser();
	
	        rdfParser.setValueFactory(new ValueFactoryImpl());
	
	        rdfParser.setVerifyData(true);
	
	        rdfParser.setStopAtFirstError(true);
	
	        rdfParser
	                .setDatatypeHandling(RDFParser.DatatypeHandling.IGNORE);
	
	        rdfParser.setRDFHandler(new StatementCollector(g));
	
	        /*
	         * Run the parser, which will cause statements to be
	         * inserted.
	         */
	        rdfParser.parse(req.getInputStream(), baseURI);
	
	        /*
			 * Send back the graph using CONNEG to decide the MIME Type of the
			 * response.
			 */
	        sendGraph(req, resp, g);
	        
        } catch (Throwable t) {

            BigdataRDFServlet.launderThrowable(t, resp, null);

        }

    }