@Override public boolean toRdf(final Repository myRepository, final int modelVersion, final URI... keyToUse) throws OpenRDFException { super.toRdf(myRepository, modelVersion, keyToUse); final RepositoryConnection con = myRepository.getConnection(); try { if (SpinInferencingRuleImpl.DEBUG) { SpinInferencingRuleImpl.log.debug( "SparqlNormalisationRuleImpl.toRdf: keyToUse=" + keyToUse); } final URI keyUri = this.getKey(); con.setAutoCommit(false); con.add( keyUri, RDF.TYPE, SpinInferencingRuleSchema.getSpinInferencingRuleTypeUri(), keyToUse); // If everything went as planned, we can commit the result con.commit(); return true; } catch (final RepositoryException re) { // Something went wrong during the transaction, so we roll it back con.rollback(); SpinInferencingRuleImpl.log.error("RepositoryException: " + re.getMessage()); } finally { con.close(); } return false; }
private boolean isManaged( RepositoryConnection conn, Resource subject, URI predicate, Value object, String operation) { try { if (conn.hasStatement(subject, predicate, object, true, managedContext)) { // Ignore/Strip any triple that is already present in the mgmt-context (i.e. "unchanged" // props). if (log.isTraceEnabled()) { log.trace( "[{}] filtering out statement that is already present in the managed context: {}", operation, new StatementImpl(subject, predicate, object)); } return true; } else if (this.subject.equals(subject) && managedProperties.contains(predicate)) { // We do NOT allow changing server-managed properties. if (log.isTraceEnabled()) { log.trace( "[{}] filtering out statement with managed propterty {}: {}", operation, predicate, new StatementImpl(subject, predicate, object)); } deniedProperties.add(predicate); return true; } } catch (RepositoryException e) { log.error("Error while filtering server managed properties: {}", e.getMessage()); } return false; }
public ValueLoader(String host, String port, String user, String pwd) { String connectionString = "jdbc:virtuoso://" + host + ':' + port; repository = new VirtuosoRepository(connectionString, user, pwd, true); try { RepositoryConnection con = repository.getConnection(); } catch (RepositoryException e) { throw new IllegalArgumentException(e.getMessage(), e); } }
@Override public void transform(Repository repository, URI graph, TransformContext context) throws TransformException { String catalogUrl = odsUrl + "id/catalog/" + catalogIdentifier + '/'; context.getCustomData().put("dcatTransformerGraph", catalogUrl); Collection<String> warnings = context.getWarnings(); URI catalogUri = valueFactory.createURI(catalogUrl); Value rawGraph = getRawGraph(repository, graph); if (rawGraph == null) { warnings.add("no catalog found"); throw new TransformException("no catalog found in raw data"); } try { RepositoryConnection connection = repository.getConnection(); try { connection.add( valueFactory.createStatement(catalogUri, rawCatalogPredicate, rawGraph), graph); connection.add( valueFactory.createStatement( catalogUri, LODMSPredicates.RDFTYPE, LODMSPredicates.DCAT_CATALOG), graph); copyCatalogAttributes(graph, catalogUri, rawGraph, connection); extractDatasetInfo(graph, catalogUri, rawGraph, connection); } catch (RepositoryException e) { warnings.add(e.getMessage()); throw new TransformException(e.getMessage(), e); } catch (MalformedQueryException e) { warnings.add(e.getMessage()); throw new TransformException(e.getMessage(), e); } catch (UpdateExecutionException e) { warnings.add(e.getMessage()); throw new TransformException(e.getMessage(), e); } finally { connection.close(); } } catch (Exception e) { throw new TransformException(e.getMessage(), e); } }
/** * Return a list of versions that affect the resource whose uri is passed as argument. For each * version, the result will contain the id, the creator, and the date when the version was * recorded. Further details for a version can be requested by calling the * /versioning/versions/{id} webservice. * * <p>Note that resource_uri is an optional parameter. In case no resource uri is given, all * versions recorded by the LMF are returned, which can take a considerable amount of time. @HTTP * 200 in case the versions were retrieved successfully @HTTP 404 in case the resource passed as * argument resource_uri could not be found * * @param resource_uri the URI of the resource for which to return the versions (optional, see * warning above) * @return a JSON list of versions, each a map with the properties "id" (long), "creator" (uri), * "date" (ISO 8601 String) */ @GET @Produces("application/json") @Path("/versions/list") public Response getVersions( @QueryParam("resource") String resource_uri, @QueryParam("from") String dateFrom, @QueryParam("to") String dateTo) { try { RepositoryConnection conn = sesameService.getConnection(); try { if (resource_uri != null) { URI resource = ResourceUtils.getUriResource(conn, resource_uri); if (resource != null && resource instanceof KiWiUriResource) { if (dateFrom == null && dateTo == null) { return Response.ok() .entity(formatVersions(versioningService.listVersions(resource))) .build(); } else { Date dateFromD = DateUtils.parseDate(dateFrom); Date dateToD = DateUtils.parseDate(dateTo); return Response.ok() .entity( formatVersions(versioningService.listVersions(resource, dateFromD, dateToD))) .build(); } } else { return Response.status(Response.Status.NOT_FOUND) .entity("resource with URI " + resource_uri + " was not found in the system") .build(); } } else { if (dateFrom == null && dateTo == null) { return Response.ok().entity(formatVersions(versioningService.listVersions())).build(); } else { Date dateFromD = DateUtils.parseDate(dateFrom); Date dateToD = DateUtils.parseDate(dateTo); return Response.ok() .entity(formatVersions(versioningService.listVersions(dateFromD, dateToD))) .build(); } } } finally { conn.commit(); conn.close(); } } catch (RepositoryException ex) { return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(ex.getMessage()).build(); } catch (SailException ex) { return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(ex.getMessage()).build(); } }
private ModelAndView getRemoveNamespaceResult(HttpServletRequest request, String prefix) throws ServerHTTPException { try { RepositoryConnection repositoryCon = RepositoryInterceptor.getRepositoryConnection(request); synchronized (repositoryCon) { repositoryCon.removeNamespace(prefix); } } catch (RepositoryException e) { throw new ServerHTTPException("Repository error: " + e.getMessage(), e); } return new ModelAndView(EmptySuccessView.getInstance()); }
@Override public boolean schemaToRdf( final Repository myRepository, final int modelVersion, final URI... contextUri) throws OpenRDFException { final RepositoryConnection con = myRepository.getConnection(); final ValueFactory f = Constants.VALUE_FACTORY; try { con.setAutoCommit(false); con.add(OwlNormalisationRuleSchema.getOwlRuleTypeUri(), RDF.TYPE, OWL.CLASS, contextUri); con.add( OwlNormalisationRuleSchema.getOwlRuleTypeUri(), RDFS.SUBCLASSOF, ValidatingRuleSchema.getValidatingRuleTypeUri(), contextUri); con.add( OwlNormalisationRuleSchema.getOwlRuleTypeUri(), RDFS.LABEL, f.createLiteral( "An OWL normalisation rule intended to validate triples based on an OWL ontology."), contextUri); // If everything went as planned, we can commit the result con.commit(); return true; } catch (final RepositoryException re) { // Something went wrong during the transaction, so we roll it back if (con != null) { con.rollback(); } OwlNormalisationRuleSchema.LOG.error("RepositoryException: " + re.getMessage()); } finally { if (con != null) { con.close(); } } return false; }
@Override public Endpoint loadEndpoint(RepositoryInformation repoInfo) throws FedXException { File store = FileUtil.getFileLocation(repoInfo.getLocation()); if (!store.exists()) { throw new FedXRuntimeException( "Store does not exist at '" + repoInfo.getLocation() + ": " + store.getAbsolutePath() + "'."); } try { NativeStore ns = new NativeStoreExt(store); SailRepository repo = new SailRepository(ns); repo.initialize(); ProviderUtil.checkConnectionIfConfigured(repo); Endpoint res = new Endpoint( repoInfo.getId(), repoInfo.getName(), repoInfo.getLocation(), repoInfo.getType(), EndpointClassification.Local); res.setEndpointConfiguration(repoInfo.getEndpointConfiguration()); res.setRepo(repo); // register a federated service manager to deal with this endpoint SAILFederatedService federatedService = new SAILFederatedService(res); federatedService.initialize(); FederatedServiceManager.getInstance().registerService(repoInfo.getName(), federatedService); return res; } catch (RepositoryException e) { throw new FedXException( "Repository " + repoInfo.getId() + " could not be initialized: " + e.getMessage(), e); } }
private Value getRawGraph(Repository repository, URI graph) throws TransformException { RepositoryResult<Statement> s = null; try { RepositoryConnection connection = repository.getConnection(); try { List<Statement> catalogStatement = connection .getStatements( null, LODMSPredicates.RDFTYPE, LODMSPredicates.DCAT_CATALOG, false, graph) .asList(); if (catalogStatement.isEmpty()) return null; return catalogStatement.get(0).getSubject(); } catch (RepositoryException e) { connection.close(); return null; } } catch (RepositoryException e) { throw new TransformException(e.getMessage(), e); } }
private ModelAndView getUpdateNamespaceResult(HttpServletRequest request, String prefix) throws IOException, ClientHTTPException, ServerHTTPException { String namespace = IOUtil.readString(request.getReader()); namespace = namespace.trim(); if (namespace.length() == 0) { throw new ClientHTTPException(SC_BAD_REQUEST, "No namespace name found in request body"); } // FIXME: perform some sanity checks on the namespace string try { RepositoryConnection repositoryCon = RepositoryInterceptor.getRepositoryConnection(request); synchronized (repositoryCon) { repositoryCon.setNamespace(prefix, namespace); } } catch (RepositoryException e) { throw new ServerHTTPException("Repository error: " + e.getMessage(), e); } return new ModelAndView(EmptySuccessView.getInstance()); }
private ModelAndView getExportNamespaceResult(HttpServletRequest request, String prefix) throws ServerHTTPException, ClientHTTPException { try { String namespace = null; RepositoryConnection repositoryCon = RepositoryInterceptor.getRepositoryConnection(request); synchronized (repositoryCon) { namespace = repositoryCon.getNamespace(prefix); } if (namespace == null) { throw new ClientHTTPException(SC_NOT_FOUND, "Undefined prefix: " + prefix); } Map<String, Object> model = new HashMap<String, Object>(); model.put(SimpleResponseView.CONTENT_KEY, namespace); return new ModelAndView(SimpleResponseView.getInstance(), model); } catch (RepositoryException e) { throw new ServerHTTPException("Repository error: " + e.getMessage(), e); } }
/** * Resolve/Redirect access to /user/* uris. * * @param login the login of the user to redirect to * @param types header param of accepted mime-types * @return a redirect to the user-resource in the resource service. @HTTP 404 if no such user * exists. @HTTP 303 on success @HTTP 400 if no valid resource uri could be built with the * login @HTTP 500 on other exceptions */ @GET @Path("/{login:[^#?]+}") public Response getUser(@PathParam("login") String login, @HeaderParam("Accept") String types) { if (login.equals("me")) { return get(); } else { try { RepositoryConnection conn = sesameService.getConnection(); try { final URI user = userService.getUser(login); if (user == null) return Response.status(Status.NOT_FOUND) .entity(String.format("User %s not found", login)) .build(); java.net.URI u = new java.net.URI( configurationService.getServerUri() + "resource?uri=" + URLEncoder.encode(user.stringValue(), "utf-8")); return Response.seeOther(u).header("Accept", types).build(); } finally { conn.commit(); conn.commit(); } } catch (URISyntaxException e) { return Response.status(Status.BAD_REQUEST) .entity(String.format("Invalid URI: %s", e.getMessage())) .build(); } catch (UnsupportedEncodingException e) { return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build(); } catch (RepositoryException e) { return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build(); } } }