/** Utility method for debugging model problems. **/ @SuppressWarnings("unused") public static void describeModel(Model model) { model.listSubjects().forEachRemaining( r -> { logger.info(r.toString()); StmtIterator props = r.listProperties(); props.forEachRemaining(p -> logger.info("\t" + p.getPredicate() + " " + p.getObject())); } ); } }
private static List<Resource> getContainedResource(Model model, String url) { ImmutableList.Builder<Resource> results = new ImmutableList.Builder<>(); Resource self = model.getResource(url); self.listProperties(model.createProperty("http://www.w3.org/ns/ldp#contains")) .forEachRemaining(s-> results.add(s.getResource())); /*List<Statement> containedStatements = getProperties( self, "http://www.w3.org/ns/ldp#contains"); for (Statement s : containedStatements) { results.add(s.getResource()); }*/ return results.build(); }
private String createIndex(String url, String slug, SolidUtilities utilities) throws Exception { Model model = ModelFactory.createDefaultModel(); Resource containerResource = model.createResource("#this"); containerResource.addProperty(RDF.type, model.getResource(VCARD4.NS + "AddressBook")); containerResource.addProperty( model.createProperty(VCARD4.NS + "nameEmailIndex"), model.createResource("people.ttl")); containerResource.addProperty( model.createProperty(VCARD4.NS + "groupIndex"), model.createResource("groups.ttl")); containerResource.addProperty(DC_11.title, slug); return utilities.postContent( url, "index", BASIC_RESOURCE_TYPE, model); }
if (r.hasProperty(VCARD4.fn)) { vcard.setFormattedName(r.getProperty(VCARD4.fn).getString()); if (r.hasProperty(VCARD4.hasName)) { StmtIterator nameIterator = r.listProperties(VCARD4.hasName); while (nameIterator.hasNext()) { Statement nameStatement = nameIterator.nextStatement(); Resource structuredNameResource = nameStatement.getResource(); StructuredName structuredName = new StructuredName(); if (structuredNameResource.hasProperty(VCARD4.family_name)) { structuredName.setFamily(structuredNameResource.getProperty(VCARD4.family_name).getString()); if (structuredNameResource.hasProperty(VCARD4.given_name)) { structuredName.setGiven(structuredNameResource.getProperty(VCARD4.given_name).getString()); structuredNameResource.listProperties(VCARD4.hasHonorificPrefix).forEachRemaining( prefix -> structuredName.getPrefixes().add(prefix.getString()) ); structuredNameResource.listProperties(VCARD4.hasHonorificSuffix).forEachRemaining( suffix -> structuredName.getSuffixes().add(suffix.getString()) ); structuredNameResource.listProperties(VCARD4.hasAdditionalName).forEachRemaining( additional -> structuredName.getAdditionalNames().add(additional.getString()) ); vcard.getStructuredNames().add(structuredName); if (r.hasProperty(VCARD4.organization_name)) { vcard.setOrganization(r.getProperty(VCARD4.organization_name).getString());
private String createContainer(String url, String slug, SolidUtilities utilities) throws Exception { Model containerModel = ModelFactory.createDefaultModel(); Resource containerResource = containerModel.createResource(""); containerResource.addProperty(DCTerms.title, slug); return utilities.postContent(url, slug, BASIC_CONTAINER_TYPE, containerModel); }
private List<VCard> parseAddressBook(Resource selfResource, SolidUtilities utilities) throws IOException { String peopleUri = selfResource.getProperty(NAME_EMAIL_INDEX_PROPERTY).getResource().getURI(); Model peopleModel = utilities.getModel(peopleUri); List<VCard> vcards = new ArrayList<>(); ResIterator subjects = peopleModel.listSubjects(); while (subjects.hasNext()) { Resource subject = subjects.nextResource(); Model personModel = utilities.getModel(subject.getURI()); Resource personResource = SolidUtilities.getResource(subject.getURI(), personModel); if (personResource == null) { throw new IllegalStateException(subject.getURI() + " not found in " + subject.toString()); } vcards.add(parsePerson(personResource)); } return vcards; }
private String createPersonDirectory(String url, SolidUtilities utilities) throws IOException { Model personDirectoryModel = ModelFactory.createDefaultModel(); personDirectoryModel.createResource(""); return utilities.postContent( url, "Person", BASIC_CONTAINER_TYPE, personDirectoryModel); }
/** Checks if a {@link Resource} is a given type. **/ public static boolean isType(Resource resource, String type) { return resource.listProperties(RDF.type) .toList() .stream() .anyMatch(s -> s.getResource().getURI().equalsIgnoreCase(type)); }
private Resource getPersonResource(String data) { String example = "https://example.com/resource1"; Model defaultModel = ModelFactory.createDefaultModel(); Model model = defaultModel.read( new StringReader(data), example, "TURTLE"); return model.getResource(example + "#this"); }
/** * Does a depth first traversal of a RDF graph, passing each {@link Resource} into the * provided {@link Consumer} */ public void explore(String url, Consumer<Resource> resourceConsumer) throws IOException { logger.debug("Exploring: {}", url); Model model = getModel(url); Resource selfResource = getResource(url, model); if (selfResource == null) { resourceConsumer.accept(model.createResource(url)); return; } if (isType(selfResource, "http://www.w3.org/ns/ldp#Container")) { for (Resource r : getContainedResource(model, url)) { explore(r.getURI(), resourceConsumer); } } resourceConsumer.accept(selfResource); }
/** * Parses the contents of a URL to produce an RDF model. */ public Model getModel(String url) throws IOException { HttpRequestFactory factory = TRANSPORT.createRequestFactory(); HttpRequest rootGetRequest = factory.buildGetRequest( new GenericUrl(url)); HttpHeaders headers = new HttpHeaders(); headers.setCookie(authCookie); headers.setAccept("text/turtle"); rootGetRequest.setHeaders(headers); HttpResponse response = rootGetRequest.execute(); if (response.getStatusCode() != 200) { throw new IOException("Unexpected return code: " + response.getStatusCode() + "\nMessage:\n" + response.getStatusMessage()); } StringWriter writer = new StringWriter(); IOUtils.copy(response.getContent(), writer, "UTF-8"); String fixedString = fixProblematicPeriods(writer.toString()); Model defaultModel = ModelFactory.createDefaultModel(); return defaultModel.read( new StringReader(fixedString), url, "TURTLE"); }
/** Recursively deletes all sub resources starting at the given url. **/ public void recursiveDelete(String url) throws IOException{ explore(url, r-> delete(r.getURI())); }
/** Looks up the {@link Resource}s for a given string, that might be comma delimited. **/ private static ImmutableList<Resource> getPhoneOrMailTypes( String type, Map<String, Resource> map) { return ImmutableList.copyOf( Arrays.stream(type.split(",")).map(t -> { Resource r = map.get(t.toLowerCase()); if (r == null) { logger.warn("%s didn't contain '%s' from %s", map, t.toLowerCase(), type); r = ModelFactory.createDefaultModel().getResource(VCARD4.NS + t); } return r; }) .collect(Collectors.toList())); } }
/** Gets a given resource (including the #this reference) from a model. **/ public static Resource getResource(String url, Model model) { List<Resource> matchingSubjects = model.listSubjects() .filterKeep(s -> s.getURI() != null) .filterKeep(s -> s.getURI().equalsIgnoreCase(url) || s.getURI().equalsIgnoreCase(url + "#this")).toList(); if (matchingSubjects.isEmpty()) { return null; } checkState(matchingSubjects.size() == 1, "Model %s didn't contain %s", model, url); return matchingSubjects.get(0); }
@Test public void testFromVcard() { for (VCard vcardInput : Ezvcard.parse(TestData.VCARD_TEXT).all()) { Model personModel = SolidContactsImport.getPersonModel(vcardInput); StringWriter stringWriter = new StringWriter(); personModel.write(stringWriter, "TURTLE"); String rdf = stringWriter.toString(); VCard vcardOutput = SolidContactsExport.parsePerson(getPersonResource(rdf)); checkEquality(vcardInput, vcardOutput); } }
private static Statement getValueStatement(Resource r) { Statement valueStatement = r.getProperty(VCARD4.hasValue); if (valueStatement == null) { valueStatement = r.getProperty(VCARD4.value); } if (valueStatement == null) { throw new IllegalStateException("Couldn't find value property in: " + r); } return valueStatement; } }
private String createPeopleFile( String baseUrl, String containerUrl, Map<String, VCard> importedPeople, SolidUtilities utilities) throws Exception { Model peopleModel = ModelFactory.createDefaultModel(); Resource indexResource = peopleModel.createResource("index.ttl#this"); for (String insertedId : importedPeople.keySet()) { VCard insertedPerson = importedPeople.get(insertedId); String relativePath = insertedId.replace(containerUrl, ""); Resource personResource = peopleModel.createResource(relativePath + "#this"); if (insertedPerson.getFormattedName() != null) { personResource.addProperty(VCARD4.fn, insertedPerson.getFormattedName().getValue()); } personResource.addProperty( peopleModel.createProperty(VCARD4.NS, "inAddressBook"), indexResource); } return utilities.postContent( baseUrl + containerUrl, "people", BASIC_RESOURCE_TYPE, peopleModel); }
private String insertPerson(String baseUrl, String container, VCard person, SolidUtilities utilities) { Model personContainerModel = ModelFactory.createDefaultModel(); personContainerModel.createResource(""); try { String directory = utilities.postContent( baseUrl + container, null, BASIC_CONTAINER_TYPE, personContainerModel); return utilities.postContent( baseUrl + directory, "index", BASIC_RESOURCE_TYPE, getPersonModel(person)); } catch (IOException e) { throw new IllegalStateException("Couldn't insert: " + person.getFormattedName() + " into: " + baseUrl + container, e); } }
private List<VCard> parseAddressBookIfApplicable(Resource resource, SolidUtilities utilities) throws IOException { if (SolidUtilities.isType(resource, "http://www.w3.org/2006/vcard/ns#AddressBook")) { logger.debug("Got Address book at {}", resource.getURI()); return parseAddressBook(resource, utilities); } return null; }
private List<List<VCard>> explore(String url, SolidUtilities utilities) throws IOException { logger.debug("Exploring: {}", url); List<List<VCard>> results = new ArrayList<>(); utilities.explore(url, r -> { try { List<VCard> cards = parseAddressBookIfApplicable(r, utilities); if (cards != null) { results.add(cards); } } catch (IOException e) { throw new IllegalStateException("Problem parsing " + r.getURI(), e); } }); return results; }