Search in sources :

Example 1 with SyndCategory

use of com.rometools.rome.feed.synd.SyndCategory in project rssriver by dadoonet.

the class RssToJson method toJson.

public static XContentBuilder toJson(SyndEntry message, String riverName, String feedname, boolean raw) throws IOException {
    XContentBuilder out = jsonBuilder().startObject().field(Rss.FEEDNAME, feedname).field(Rss.TITLE, message.getTitle()).field(Rss.AUTHOR, message.getAuthor()).field(Rss.DESCRIPTION, message.getDescription() != null ? message.getDescription().getValue() : null).field(Rss.LINK, message.getLink()).field(Rss.PUBLISHED_DATE, message.getPublishedDate()).field(Rss.SOURCE, message.getSource());
    if (raw) {
        if (message.getContents() != null) {
            out.startObject(Rss.RAW);
            for (Object o : message.getContents()) {
                if (o instanceof SyndContentImpl) {
                    SyndContentImpl content = (SyndContentImpl) o;
                    out.field(content.getType(), content.getValue());
                }
            }
            out.endObject();
        }
    }
    GeoRSSModule geoRSSModule = GeoRSSUtils.getGeoRSS(message);
    if (geoRSSModule != null) {
        final Position position = geoRSSModule.getPosition();
        if (position != null) {
            out.startObject(Rss.LOCATION);
            out.field(Rss.Location.LAT, position.getLatitude());
            out.field(Rss.Location.LON, position.getLongitude());
            out.endObject();
        }
    }
    if (message.getCategories() != null && message.getCategories().size() > 0) {
        out.startArray(Rss.CATEGORIES);
        for (Object oCategory : message.getCategories()) {
            if (oCategory instanceof SyndCategory) {
                SyndCategory category = (SyndCategory) oCategory;
                out.value(category.getName());
            }
        }
        out.endArray();
    }
    if (message.getEnclosures() != null && message.getEnclosures().size() > 0) {
        out.startArray(Rss.ENCLOSURES);
        for (Object oEnclosure : message.getEnclosures()) {
            if (oEnclosure instanceof SyndEnclosure) {
                out.startObject();
                SyndEnclosure enclosure = (SyndEnclosure) oEnclosure;
                out.field(Rss.Enclosures.URL, enclosure.getUrl());
                out.field(Rss.Enclosures.TYPE, enclosure.getType());
                out.field(Rss.Enclosures.LENGTH, enclosure.getLength());
                out.endObject();
            }
        }
        out.endArray();
    }
    MediaEntryModule mediaEntryModule = (MediaEntryModule) message.getModule(MediaEntryModule.URI);
    if (mediaEntryModule != null) {
        out.startArray(Rss.MEDIAS);
        for (MediaContent mediaContent : mediaEntryModule.getMediaContents()) {
            out.startObject();
            addFieldIfNotNull(out, Rss.Medias.TYPE, mediaContent.getType());
            if (mediaContent.getReference() != null) {
                URI url = null;
                if (mediaContent.getReference() instanceof PlayerReference) {
                    url = ((PlayerReference) mediaContent.getReference()).getUrl();
                }
                if (mediaContent.getReference() instanceof UrlReference) {
                    url = ((UrlReference) mediaContent.getReference()).getUrl();
                }
                if (url != null) {
                    addFieldIfNotNull(out, Rss.Medias.REFERENCE, url.toString());
                }
            }
            addFieldIfNotNull(out, Rss.Medias.LANGUAGE, mediaContent.getLanguage());
            addFieldIfNotNull(out, Rss.Medias.TITLE, mediaContent.getMetadata().getTitle());
            addFieldIfNotNull(out, Rss.Medias.DESCRIPTION, mediaContent.getMetadata().getDescription());
            addFieldIfNotNull(out, Rss.Medias.DURATION, mediaContent.getDuration());
            addFieldIfNotNull(out, Rss.Medias.WIDTH, mediaContent.getWidth());
            addFieldIfNotNull(out, Rss.Medias.HEIGHT, mediaContent.getHeight());
            out.endObject();
        }
        out.endArray();
    }
    if (riverName != null) {
        out.field("river", riverName);
    }
    return out.endObject();
}
Also used : UrlReference(com.rometools.modules.mediarss.types.UrlReference) PlayerReference(com.rometools.modules.mediarss.types.PlayerReference) SyndCategory(com.rometools.rome.feed.synd.SyndCategory) MediaEntryModule(com.rometools.modules.mediarss.MediaEntryModule) Position(com.rometools.modules.georss.geometries.Position) SyndContentImpl(com.rometools.rome.feed.synd.SyndContentImpl) GeoRSSModule(com.rometools.modules.georss.GeoRSSModule) SyndEnclosure(com.rometools.rome.feed.synd.SyndEnclosure) MediaContent(com.rometools.modules.mediarss.types.MediaContent) URI(java.net.URI) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 2 with SyndCategory

use of com.rometools.rome.feed.synd.SyndCategory in project ddf by codice.

the class OpenSearchSource method createResponseFromEntry.

/**
     * Creates a single response from input parameters. Performs XPath operations on the document to
     * retrieve data not passed in.
     *
     * @param entry a single Atom entry
     * @return single response
     * @throws ddf.catalog.source.UnsupportedQueryException
     */
private List<Result> createResponseFromEntry(SyndEntry entry) throws UnsupportedQueryException {
    String id = entry.getUri();
    if (id != null && !id.isEmpty()) {
        id = id.substring(id.lastIndexOf(':') + 1);
    }
    List<SyndContent> contents = entry.getContents();
    List<SyndCategory> categories = entry.getCategories();
    List<Metacard> metacards = new ArrayList<>();
    List<Element> foreignMarkup = entry.getForeignMarkup();
    String relevance = "";
    String source = "";
    for (Element element : foreignMarkup) {
        if (element.getName().equals("score")) {
            relevance = element.getContent(0).getValue();
        }
    }
    //we currently do not support downloading content via an RSS enclosure, this support can be added at a later date if we decide to include it
    for (SyndContent content : contents) {
        MetacardImpl metacard = getMetacardImpl(parseContent(content.getValue(), id));
        metacard.setSourceId(this.shortname);
        String title = metacard.getTitle();
        if (StringUtils.isEmpty(title)) {
            metacard.setTitle(entry.getTitle());
        }
        if (!source.isEmpty()) {
            metacard.setSourceId(source);
        }
        metacards.add(metacard);
    }
    for (int i = 0; i < categories.size() && i < metacards.size(); i++) {
        SyndCategory category = categories.get(i);
        Metacard metacard = metacards.get(i);
        if (StringUtils.isBlank(metacard.getContentTypeName())) {
            ((MetacardImpl) metacard).setContentTypeName(category.getName());
        }
    }
    List<Result> results = new ArrayList<>();
    for (Metacard metacard : metacards) {
        ResultImpl result = new ResultImpl(metacard);
        if (relevance == null || relevance.isEmpty()) {
            LOGGER.debug("couldn't find valid relevance. Setting relevance to 0");
            relevance = "0";
        }
        result.setRelevanceScore(new Double(relevance));
        results.add(result);
    }
    return results;
}
Also used : SyndCategory(com.rometools.rome.feed.synd.SyndCategory) Element(org.jdom2.Element) ArrayList(java.util.ArrayList) ResultImpl(ddf.catalog.data.impl.ResultImpl) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Result(ddf.catalog.data.Result) Metacard(ddf.catalog.data.Metacard) SyndContent(com.rometools.rome.feed.synd.SyndContent)

Aggregations

SyndCategory (com.rometools.rome.feed.synd.SyndCategory)2 GeoRSSModule (com.rometools.modules.georss.GeoRSSModule)1 Position (com.rometools.modules.georss.geometries.Position)1 MediaEntryModule (com.rometools.modules.mediarss.MediaEntryModule)1 MediaContent (com.rometools.modules.mediarss.types.MediaContent)1 PlayerReference (com.rometools.modules.mediarss.types.PlayerReference)1 UrlReference (com.rometools.modules.mediarss.types.UrlReference)1 SyndContent (com.rometools.rome.feed.synd.SyndContent)1 SyndContentImpl (com.rometools.rome.feed.synd.SyndContentImpl)1 SyndEnclosure (com.rometools.rome.feed.synd.SyndEnclosure)1 Metacard (ddf.catalog.data.Metacard)1 Result (ddf.catalog.data.Result)1 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)1 ResultImpl (ddf.catalog.data.impl.ResultImpl)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)1 Element (org.jdom2.Element)1