Java Code Examples for org.openrdf.repository.Repository#getValueFactory()

The following examples show how to use org.openrdf.repository.Repository#getValueFactory() . 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: GraphHandler.java    From cumulusrdf with Apache License 2.0 6 votes vote down vote up
/**
 * Get all statements and export them as RDF.
 * 
 * @param repository the Repository object
 * @param request the HttpServletRequest object
 * @param response the HttpServletResponse object
 * @return a model and view for exporting the statements.
 * @throws ClientHTTPException throws when errors in parameters 
 */
private ModelAndView getExportStatementsResult(final Repository repository, final HttpServletRequest request,
		final HttpServletResponse response)
		throws ClientHTTPException {
	ProtocolUtil.logRequestParameters(request);

	ValueFactory vf = repository.getValueFactory();

	URI graph = getGraphName(request, vf);

	RDFWriterFactory rdfWriterFactory = ProtocolUtil.getAcceptableService(request, response,
			RDFWriterRegistry.getInstance());

	Map<String, Object> model = new HashMap<String, Object>();

	model.put(ExportStatementsView.CONTEXTS_KEY, new Resource[] { graph });
	model.put(ExportStatementsView.FACTORY_KEY, rdfWriterFactory);
	model.put(ExportStatementsView.USE_INFERENCING_KEY, true);
	model.put(ExportStatementsView.HEADERS_ONLY, METHOD_HEAD.equals(request.getMethod()));
	return new ModelAndView(ExportStatementsView.getInstance(), model);
}
 
Example 2
Source File: GraphHandler.java    From cumulusrdf with Apache License 2.0 6 votes vote down vote up
/**
 * Delete data from the graph.
 * 
 * @param repository the Repository object
 * @param request the HttpServletRequest object
 * @param response the HttpServletResponse object
 * @return the EmptySuccessView if successes
 * @throws ClientHTTPException throws when there are errors in getting the name of the Graph
 * @throws ServerHTTPException throws when errors happens update the data
 */
private ModelAndView getDeleteDataResult(final Repository repository,
		final HttpServletRequest request, final HttpServletResponse response)
		throws ClientHTTPException, ServerHTTPException {
	ProtocolUtil.logRequestParameters(request);

	ValueFactory vf = repository.getValueFactory();

	URI graph = getGraphName(request, vf);

	try {
		RepositoryConnection repositoryCon = repository.getConnection();
		synchronized (repositoryCon) {
			repositoryCon.clear(graph);
		}
		repositoryCon.close();
		return new ModelAndView(EmptySuccessView.getInstance());
	} catch (RepositoryException e) {
		throw new ServerHTTPException("Repository update error: " + e.getMessage(), e);
	}
}
 
Example 3
Source File: SizeHandler.java    From cumulusrdf with Apache License 2.0 5 votes vote down vote up
@Override
public ModelAndView serve(final Repository repository, final HttpServletRequest request, final HttpServletResponse response)
		throws Exception {
	ProtocolUtil.logRequestParameters(request);

	Map<String, Object> model = new HashMap<String, Object>();
	final boolean headersOnly = METHOD_HEAD.equals(request.getMethod());

	if (!headersOnly) {

		ValueFactory vf = repository.getValueFactory();
		Resource[] contexts = ProtocolUtil.parseContextParam(request, Protocol.CONTEXT_PARAM_NAME, vf);

		long size = -1;

		try {
			RepositoryConnection repositoryCon = repository.getConnection();
			synchronized (repositoryCon) {
				size = repositoryCon.size(contexts);
			}
			repositoryCon.close();
		} catch (RepositoryException e) {
			throw new ServerHTTPException("Repository error: " + e.getMessage(), e);
		}
		model.put(SimpleResponseView.CONTENT_KEY, String.valueOf(size));
	}

	return new ModelAndView(SimpleResponseView.getInstance(), model);
}
 
Example 4
Source File: StatementHandler.java    From cumulusrdf with Apache License 2.0 5 votes vote down vote up
/**
 * Get all statements and export them as RDF.
 * 
 * @param repository the Repository object
 * @param request the HttpServletRequest object
 * @param response the HttpServletResponse object
 * @return a model and view for exporting the statements.
 * @throws ClientHTTPException throws when there errors in parsing the request
 */
private ModelAndView getExportStatementsResult(final Repository repository, final HttpServletRequest request,
		final HttpServletResponse response)
		throws ClientHTTPException {
	ProtocolUtil.logRequestParameters(request);

	ValueFactory vf = repository.getValueFactory();

	Resource subj = ProtocolUtil.parseResourceParam(request, SUBJECT_PARAM_NAME, vf);
	URI pred = ProtocolUtil.parseURIParam(request, PREDICATE_PARAM_NAME, vf);
	Value obj = ProtocolUtil.parseValueParam(request, OBJECT_PARAM_NAME, vf);
	Resource[] contexts = ProtocolUtil.parseContextParam(request, CONTEXT_PARAM_NAME, vf);
	boolean useInferencing = ProtocolUtil.parseBooleanParam(request, INCLUDE_INFERRED_PARAM_NAME, true);

	RDFWriterFactory rdfWriterFactory = ProtocolUtil.getAcceptableService(request, response,
			RDFWriterRegistry.getInstance());

	Map<String, Object> model = new HashMap<String, Object>();
	model.put(ExportStatementsView.SUBJECT_KEY, subj);
	model.put(ExportStatementsView.PREDICATE_KEY, pred);
	model.put(ExportStatementsView.OBJECT_KEY, obj);
	model.put(ExportStatementsView.CONTEXTS_KEY, contexts);
	model.put(ExportStatementsView.USE_INFERENCING_KEY, Boolean.valueOf(useInferencing));
	model.put(ExportStatementsView.FACTORY_KEY, rdfWriterFactory);
	model.put(ExportStatementsView.HEADERS_ONLY, METHOD_HEAD.equals(request.getMethod()));
	model.put(ExportStatementsView.REPO_KEY, repository);
	return new ModelAndView(ExportStatementsView.getInstance(), model);
}
 
Example 5
Source File: StatementHandler.java    From cumulusrdf with Apache License 2.0 5 votes vote down vote up
/**
 * Delete data from the repository.
 * 
 * @param repository the Repository object
 * @param request the HttpServletRequest object
 * @param response the HttpServletResponse object
 * @return EmptySuccessView if success
 * @throws HTTPException throws when there are repository update errors
 */
private ModelAndView getDeleteDataResult(final Repository repository, final HttpServletRequest request,
		final HttpServletResponse response)
		throws HTTPException {
	ProtocolUtil.logRequestParameters(request);

	ValueFactory vf = repository.getValueFactory();

	Resource subj = ProtocolUtil.parseResourceParam(request, SUBJECT_PARAM_NAME, vf);
	URI pred = ProtocolUtil.parseURIParam(request, PREDICATE_PARAM_NAME, vf);
	Value obj = ProtocolUtil.parseValueParam(request, OBJECT_PARAM_NAME, vf);
	Resource[] contexts = ProtocolUtil.parseContextParam(request, CONTEXT_PARAM_NAME, vf);

	try {
		RepositoryConnection repositoryCon = repository.getConnection();
		synchronized (repositoryCon) {
			repositoryCon.remove(subj, pred, obj, contexts);
		}
		repositoryCon.close();
		return new ModelAndView(EmptySuccessView.getInstance());
	} catch (RepositoryException e) {
		if (e.getCause() != null && e.getCause() instanceof HTTPException) {
			// custom signal from the backend, throw as HTTPException directly
			// (see SES-1016).
			throw (HTTPException) e.getCause();
		} else {
			throw new ServerHTTPException("Repository update error: " + e.getMessage(), e);
		}
	}
}
 
Example 6
Source File: RepositoryListHandler.java    From cumulusrdf with Apache License 2.0 4 votes vote down vote up
@Override
public ModelAndView serve(final Repository repository, final HttpServletRequest request, final HttpServletResponse response)
		throws Exception {
	Map<String, Object> model = new HashMap<String, Object>();

	if (METHOD_GET.equals(request.getMethod())) {
		Repository systemRepository = _repositoryManager.getSystemRepository();
		ValueFactory vf = systemRepository.getValueFactory();

		try {
			RepositoryConnection con = systemRepository.getConnection();
			try {
				// connection before returning. Would be much better to stream the query result directly to the client.

				List<String> bindingNames = new ArrayList<String>();
				List<BindingSet> bindingSets = new ArrayList<BindingSet>();

				TupleQueryResult queryResult = con.prepareTupleQuery(QueryLanguage.SERQL,
						REPOSITORY_LIST_QUERY).evaluate();
				try {
					// Determine the repository's URI
					StringBuffer requestURL = request.getRequestURL();
					if (requestURL.charAt(requestURL.length() - 1) != '/') {
						requestURL.append('/');
					}
					String namespace = requestURL.toString();

					while (queryResult.hasNext()) {
						QueryBindingSet bindings = new QueryBindingSet(queryResult.next());

						String id = bindings.getValue("id").stringValue();
						bindings.addBinding("uri", vf.createURI(namespace, id));

						bindingSets.add(bindings);
					}

					bindingNames.add("uri");
					bindingNames.addAll(queryResult.getBindingNames());
				} finally {
					queryResult.close();
				}
				model.put(QueryResultView.QUERY_RESULT_KEY,
						new TupleQueryResultImpl(bindingNames, bindingSets));

			} finally {
				con.close();
			}
		} catch (RepositoryException e) {
			throw new ServerHTTPException(e.getMessage(), e);
		}
	}

	TupleQueryResultWriterFactory factory = ProtocolUtil.getAcceptableService(request, response,
			TupleQueryResultWriterRegistry.getInstance());

	model.put(QueryResultView.FILENAME_HINT_KEY, "repositories");
	model.put(QueryResultView.FACTORY_KEY, factory);
	model.put(QueryResultView.HEADERS_ONLY, METHOD_HEAD.equals(request.getMethod()));

	return new ModelAndView(TupleQueryResultView.getInstance(), model);
}
 
Example 7
Source File: RepositoryModelSet.java    From semweb4j with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private void init(Repository repository) {
	this.repository = repository;
	this.valueFactory = repository.getValueFactory();
}
 
Example 8
Source File: RepositoryModelTest.java    From semweb4j with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Test
public void testDirectRepositoryAccess() throws Exception {
	Model model = getModelFactory().createModel();
	model.open();

	// fetch the Repository, a Connection and a ValueFactory
	Repository repository = (Repository) model
			.getUnderlyingModelImplementation();
	RepositoryConnection connection = repository.getConnection();
	ValueFactory factory = repository.getValueFactory();

	// add a statement
	model.addStatement(subject, predicate, object);

	// convert the statement parts to OpenRDF data types
	Resource openRdfSubject = ConversionUtil.toOpenRDF(subject, factory);
	org.openrdf.model.URI openRdfPredicate = ConversionUtil.toOpenRDF(
			predicate, factory);
	Value openRdfObject = ConversionUtil.toOpenRDF(object, factory);
	org.openrdf.model.URI context = RepositoryModel.DEFAULT_OPENRDF_CONTEXT;

	// make sure this statement is contained in this model
	assertTrue(connection.hasStatement(openRdfSubject, openRdfPredicate,
			openRdfObject, false, context));

	// make sure this statement can also be found through the Model API
	ClosableIterator<? extends Statement> sit = model.findStatements(
			subject, predicate, object);
	assertNotNull(sit);
	assertTrue(sit.hasNext());

	Statement s2 = sit.next();
	URI s2s = (URI) s2.getSubject();
	URI s2p = s2.getPredicate();
	URI s2o = (URI) s2.getObject();

	assertEquals(subject, s2s);
	assertEquals(predicate, s2p);
	assertEquals(object, s2o);

	// make sure that this statement is only returned once
	assertFalse(sit.hasNext());

	// clean-up
	sit.close();
	connection.close();
	model.close();
}