Search in sources :

Example 6 with DefaultAtomModelWrapper

use of won.protocol.util.DefaultAtomModelWrapper in project webofneeds by researchstudio-sat.

the class MailFileAtomProducer method readAtomFromFile.

@Override
public synchronized Dataset readAtomFromFile(final File file) throws IOException {
    logger.debug("processing as mail file: {} ", file);
    try (FileInputStream fis = new FileInputStream(file)) {
        DefaultAtomModelWrapper atomModelWrapper = new DefaultAtomModelWrapper("no:uri");
        MimeMessage emailMessage = new MimeMessage(null, fis);
        MimeMessageParser parser = new MimeMessageParser(emailMessage);
        parser.parse();
        atomModelWrapper.setTitle(parser.getSubject());
        String content = null;
        if (parser.hasPlainContent()) {
            content = parser.getPlainContent();
        } else if (parser.hasHtmlContent()) {
            Document doc = Jsoup.parse(parser.getHtmlContent());
            content = doc.text();
        }
        if (content != null) {
            atomModelWrapper.setDescription(content);
        }
        logger.debug("mail subject          : {}", parser.getSubject());
        logger.debug("mail has plain content: {}", parser.hasPlainContent());
        logger.debug("mail has html content : {}", parser.hasHtmlContent());
        logger.debug("mail has attachments  : {}", parser.hasAttachments());
        logger.debug("mail plain content    : {}", StringUtils.abbreviate(parser.getPlainContent(), 200));
        logger.debug("mail html content     : {}", StringUtils.abbreviate(parser.getHtmlContent(), 200));
        return atomModelWrapper.copyDatasetWithoutSysinfo();
    } catch (Exception e) {
        logger.error("could not parse email from file {} ", file, e);
    }
    return null;
}
Also used : DefaultAtomModelWrapper(won.protocol.util.DefaultAtomModelWrapper) MimeMessage(javax.mail.internet.MimeMessage) MimeMessageParser(org.apache.commons.mail.util.MimeMessageParser) Document(org.jsoup.nodes.Document) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException)

Example 7 with DefaultAtomModelWrapper

use of won.protocol.util.DefaultAtomModelWrapper in project webofneeds by researchstudio-sat.

the class AtomIndexer method indexAtomModel.

public void indexAtomModel(Model atomModel, String id, boolean useTestCore) throws IOException, JsonLdError {
    // create the json from rdf model
    StringWriter sw = new StringWriter();
    RDFDataMgr.write(sw, atomModel, Lang.JSONLD);
    String jsonld = sw.toString();
    Object jsonObject = JsonUtils.fromString(jsonld);
    Object frame = JsonUtils.fromString(" {\"@type\": \"" + WON.Atom + "\"} ");
    JsonLdOptions options = new JsonLdOptions();
    Map<String, Object> framed = JsonLdProcessor.frame(jsonObject, frame, options);
    // add the uri of the atom as id field to avoid multiple adding of atoms but
    // instead allow updates
    framed.put("id", id);
    // add latitude and longitude values in one field for Solr spatial queries
    DefaultAtomModelWrapper atomModelWrapper = new DefaultAtomModelWrapper(atomModel, null);
    Resource atomContentNode = atomModelWrapper.getAtomContentNode();
    Coordinate atomCoordinate = atomModelWrapper.getLocationCoordinate(atomContentNode);
    if (atomCoordinate != null) {
        framed.put(SOLR_IS_LOCATION_COORDINATES_FIELD, String.valueOf(atomCoordinate.getLatitude()) + "," + String.valueOf(atomCoordinate.getLongitude()));
    }
    for (Resource contentNode : atomModelWrapper.getSeeksNodes()) {
        Coordinate coordinate = atomModelWrapper.getLocationCoordinate(contentNode);
        if (coordinate != null) {
            framed.put(SOLR_SEEKS_LOCATION_COORDINATES_FIELD, String.valueOf(coordinate.getLatitude()) + "," + String.valueOf(coordinate.getLongitude()));
        }
    }
    for (Resource contentNode : atomModelWrapper.getSeeksSeeksNodes()) {
        Coordinate coordinate = atomModelWrapper.getLocationCoordinate(contentNode);
        if (coordinate != null) {
            framed.put(SOLR_SEEKS_SEEKS_LOCATION_COORDINATES_FIELD, String.valueOf(coordinate.getLatitude()) + "," + String.valueOf(coordinate.getLongitude()));
        }
    }
    // write the final json string
    sw = new StringWriter();
    JsonUtils.writePrettyPrint(sw, framed);
    String atomJson = sw.toString();
    // post the atom to the solr index
    String indexUri = config.getSolrEndpointUri(useTestCore);
    indexUri += "update/json/docs";
    if (config.isCommitIndexedAtomImmediately()) {
        indexUri += "?commit=" + config.isCommitIndexedAtomImmediately();
    }
    logger.debug("Post atom to solr index. \n Solr URI: {} \n Atom (JSON): {}", indexUri, atomJson);
    try {
        httpService.postJsonRequest(indexUri, atomJson);
    } catch (HttpClientErrorException e) {
        logger.info("Error indexing atom with solr. \n Solr URI: {} \n Atom (JSON): {}", indexUri, atomJson);
    }
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) StringWriter(java.io.StringWriter) JsonLdOptions(com.github.jsonldjava.core.JsonLdOptions) DefaultAtomModelWrapper(won.protocol.util.DefaultAtomModelWrapper) Coordinate(won.protocol.model.Coordinate) Resource(org.apache.jena.rdf.model.Resource)

Example 8 with DefaultAtomModelWrapper

use of won.protocol.util.DefaultAtomModelWrapper in project webofneeds by researchstudio-sat.

the class LinkedDataServiceImpl method getFilteredAtomURIListDataset.

private Dataset getFilteredAtomURIListDataset(Model model, Collection<URI> uris, URI filterSocketTypeUri, URI filterAtomTypeUri) {
    Resource atomListPageResource = model.createResource(this.atomResourceURIPrefix + "/");
    Instant start = logger.isDebugEnabled() ? Instant.now() : null;
    if (filterSocketTypeUri == null && filterAtomTypeUri == null) {
        uris.forEach(atomURI -> model.add(model.createStatement(atomListPageResource, RDFS.member, model.createResource(atomURI.toString()))));
    } else {
        uris.forEach(atomURI -> {
            Dataset atomDataset = getAtomDatasetForFilter(atomURI);
            DefaultAtomModelWrapper atomModelWrapper = new DefaultAtomModelWrapper(atomDataset);
            if ((filterSocketTypeUri == null || atomModelWrapper.getSocketTypeUriMap().containsValue(filterSocketTypeUri)) && (filterAtomTypeUri == null || atomModelWrapper.getContentTypes().contains(filterAtomTypeUri))) {
                model.add(model.createStatement(atomListPageResource, RDFS.member, model.createResource(atomURI.toString())));
            }
        });
    // Parallel Approach 1:
    // The parallel Approach leads to LazyInitializationException due to the lazy
    // fetch of the Atom, and thus cant be used
    // for now, TODO: FIGURE OUT A DIFFERENT APPROACH TO MAKE PARALLEL PROCESSING
    // POSSIBLE OR TO ENHANCE THE PERFORMANCE
    // 
    // uris.parallelStream().filter(atomURI -> {
    // Dataset atomDataset = getAtomDatasetForFilter(atomURI);
    // DefaultAtomModelWrapper atomModelWrapper = new
    // DefaultAtomModelWrapper(atomDataset);
    // return (filterSocketTypeUri == null
    // || atomModelWrapper.getSocketTypeUriMap().containsValue(filterSocketTypeUri))
    // && (filterAtomTypeUri == null
    // || atomModelWrapper.getContentTypes().contains(filterAtomTypeUri));
    // }).collect(Collectors.toList()).forEach(atomURI ->
    // model.add(model.createStatement(atomListPageResource,
    // RDFS.member, model.createResource(atomURI.toString()))));
    }
    Dataset ret = newDatasetWithNamedModel(createDataGraphUriFromResource(atomListPageResource), model);
    addBaseUriAndDefaultPrefixes(ret);
    if (logger.isDebugEnabled() && start != null) {
        Instant finish = Instant.now();
        logger.debug("getFilteredAtomURIListDataset for {} Uris took {}ms", uris.size(), Duration.between(start, finish).toMillis());
    }
    return ret;
}
Also used : DefaultAtomModelWrapper(won.protocol.util.DefaultAtomModelWrapper) Dataset(org.apache.jena.query.Dataset) Instant(java.time.Instant) Resource(org.apache.jena.rdf.model.Resource)

Aggregations

DefaultAtomModelWrapper (won.protocol.util.DefaultAtomModelWrapper)8 Dataset (org.apache.jena.query.Dataset)5 VelocityContext (org.apache.velocity.VelocityContext)4 StringWriter (java.io.StringWriter)3 Resource (org.apache.jena.rdf.model.Resource)2 JsonLdOptions (com.github.jsonldjava.core.JsonLdOptions)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 Instant (java.time.Instant)1 HashMap (java.util.HashMap)1 MimeMessage (javax.mail.internet.MimeMessage)1 MimeMessageParser (org.apache.commons.mail.util.MimeMessageParser)1 Document (org.jsoup.nodes.Document)1 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)1 IncorrectPropertyCountException (won.protocol.exception.IncorrectPropertyCountException)1 Coordinate (won.protocol.model.Coordinate)1