com.sun.syndication.feed.synd.SyndContentImpl Java Examples

The following examples show how to use com.sun.syndication.feed.synd.SyndContentImpl. 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: RSSGenServlet.java    From jivejdon with Apache License 2.0 6 votes vote down vote up
private void addSitemapUrl(String url, List<SyndEntrySorted> entries, Url urlsm, HttpServletRequest request) {
	try {
		SyndEntrySorted entry = new SyndEntrySorted();
		entry.setTitle(urlsm.getName());
		entry.setLink(urlsm.getIoc());

		Date publishedDate = Constants.parseDateTime(urlsm.getCreationDate());
		entry.setPublishedDate(publishedDate);
		entry.setUpdatedDate(publishedDate);

		SyndContent description = new SyndContentImpl();
		description.setType("text/html");
		description.setValue("");
		entry.setDescription(description);

		entries.add(entry);
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example #2
Source File: RSSGenServlet.java    From jivejdon with Apache License 2.0 5 votes vote down vote up
private void addMessage(String url, List<SyndEntrySorted> entries, ForumMessage message, HttpServletRequest request) {
	try {
		SyndEntrySorted entry = new SyndEntrySorted();
		entry.setTitle(message.getMessageVO().getSubject());
		entry.setLink(getItemLink(url, message, request));

		Date publishedDate = Constants.parseDateTime(message.getCreationDate());
		entry.setPublishedDate(publishedDate);
		entry.setAuthor(message.getAccount().getUsername());
		Date updateDate = Constants.parseDateTime(message.getModifiedDate());
		entry.setUpdatedDate(updateDate);

		SyndContent description = new SyndContentImpl();
		description.setType("text/html");
		description.setValue(message.getMessageVO().getShortBody(300));
		entry.setDescription(description);

		if (message.isRoot()) {
			List<SyndCategory> cats = new ArrayList<SyndCategory>();
			for (Object o : message.getForumThread().getTags()) {
				ThreadTag tag = (ThreadTag) o;
				SyndCategory cat = new SyndCategoryImpl();

				cat.setTaxonomyUri(url + "/tags/" + tag.getTagID());
				cat.setName(tag.getTitle());
				cats.add(cat);
			}
			entry.setCategories(cats);
		}
		entries.add(entry);
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example #3
Source File: RSSScraper.java    From Babler with Apache License 2.0 5 votes vote down vote up
public static List getAllPostsFromFeed(String urlToGet, String source) throws IOException, FeedException {

        ArrayList<BlogPost> posts = new ArrayList<BlogPost>();

        URL url = new URL(urlToGet);
        SyndFeedInput input = new SyndFeedInput();
        try {
            SyndFeed feed = input.build(new XmlReader(url));

            int items = feed.getEntries().size();

            if (items > 0) {
                log.info("Attempting to parse rss feed: " + urlToGet);
                log.info("This Feed has " + items + " items");
                List<SyndEntry> entries = feed.getEntries();

                for (SyndEntry item : entries) {
                    if (item.getContents().size() > 0) {
                        SyndContentImpl contentHolder = (SyndContentImpl) item.getContents().get(0);
                        String content = contentHolder.getValue();
                        if (content != null && !content.isEmpty()) {
                            BlogPost post = new BlogPost(content, null, null, source, item.getLink(), item.getUri(), null);
                            posts.add(post);
                        }
                    }
                }
            }
            return posts;
        }
        catch(Exception ex){
            log.error(ex);
            return posts;
        }

    }
 
Example #4
Source File: StubbornJavaRss.java    From StubbornJava with MIT License 5 votes vote down vote up
private static String getFeed(HttpUrl host) {
    SyndFeed feed = new SyndFeedImpl();
    feed.setFeedType("rss_2.0");
    feed.setTitle("StubbornJava");
    feed.setLink(host.toString());
    feed.setDescription("Unconventional guides, examples, and blog utilizing modern Java");

    List<PostRaw> posts = Posts.getAllRawPosts();
    List<SyndEntry> entries = Seq.seq(posts)
        .map(p -> {
            SyndEntry entry = new SyndEntryImpl();
            entry.setTitle(p.getTitle());
            entry.setLink(host.newBuilder().encodedPath(p.getUrl()).build().toString());
            entry.setPublishedDate(Date.from(p.getDateCreated()
                                              .toLocalDate()
                                              .atStartOfDay(ZoneId.systemDefault())
                                              .toInstant()));
            entry.setUpdatedDate(Date.from(p.getDateUpdated()
                                            .toLocalDate()
                                            .atStartOfDay(ZoneId.systemDefault())
                                            .toInstant()));
            SyndContentImpl description = new SyndContentImpl();
            description.setType("text/plain");
            description.setValue(p.getMetaDesc());
            entry.setDescription(description);
            return entry;
        }).toList();
    feed.setEntries(entries);

    StringWriter writer = new StringWriter();
    SyndFeedOutput output = new SyndFeedOutput();

    return Unchecked.supplier(() -> {
        output.output(feed, writer);
        return writer.toString();
    }).get();
}
 
Example #5
Source File: StubbornJavaRss.java    From StubbornJava with MIT License 5 votes vote down vote up
private static String getFeed(HttpUrl host) {
    SyndFeed feed = new SyndFeedImpl();
    feed.setFeedType("rss_2.0");
    feed.setTitle("StubbornJava");
    feed.setLink(host.toString());
    feed.setDescription("Unconventional guides, examples, and blog utilizing modern Java");

    List<PostRaw> posts = Posts.getAllRawPosts();
    List<SyndEntry> entries = Seq.seq(posts)
        .map(p -> {
            SyndEntry entry = new SyndEntryImpl();
            entry.setTitle(p.getTitle());
            entry.setLink(host.newBuilder().encodedPath(p.getUrl()).build().toString());
            entry.setPublishedDate(Date.from(p.getDateCreated()
                                              .toLocalDate()
                                              .atStartOfDay(ZoneId.systemDefault())
                                              .toInstant()));
            entry.setUpdatedDate(Date.from(p.getDateUpdated()
                                            .toLocalDate()
                                            .atStartOfDay(ZoneId.systemDefault())
                                            .toInstant()));
            SyndContentImpl description = new SyndContentImpl();
            description.setType("text/plain");
            description.setValue(p.getMetaDesc());
            entry.setDescription(description);
            return entry;
        }).toList();
    feed.setEntries(entries);

    StringWriter writer = new StringWriter();
    SyndFeedOutput output = new SyndFeedOutput();

    return Unchecked.supplier(() -> {
        output.output(feed, writer);
        return writer.toString();
    }).get();
}
 
Example #6
Source File: RSSScraper.java    From Babler with Apache License 2.0 4 votes vote down vote up
public AbstractMap.SimpleEntry<Integer, Integer> fetchAndSave() throws Exception {

        URL url = new URL(this.url);

        SyndFeedInput input = new SyndFeedInput();
        SyndFeed feed = input.build(new XmlReader(url));


        int items = feed.getEntries().size();

        if(items > 0){
            log.info("Attempting to parse rss feed: "+ this.url );
            log.info("This Feed has "+items +" items");
        }

        List <SyndEntry> entries = feed.getEntries();

        for (SyndEntry item : entries){
            log.info("Title: " + item.getTitle());
            log.info("Link: " + item.getLink());
            SyndContentImpl contentHolder = (SyndContentImpl) item.getContents().get(0);
            String content = contentHolder.getValue();

            //content might contain html data, let's clean it up
            Document doc = Jsoup.parse(content);
            content = doc.text();
            try {
                    Result result = ld.detectLanguage(content, language);
                    if (result.languageCode.equals(language) && result.isReliable) {

                        FileSaver file = new FileSaver(content, this.language, "bs", item.getLink(), item.getUri(), String.valueOf(content.hashCode()));
                        String fileName = file.getFileName();
                        BlogPost post = new BlogPost(content,this.language,null,"bs",item.getLink(),item.getUri(),fileName);
                        if(DAO.saveEntry(post)) {
                            file.save(this.logDb);
                            numOfFiles++;
                            wrongCount = 0;
                        }

                    }

                    else{
                        log.info("Item " + item.getTitle() + "is in a diff languageCode, skipping this post  "+ result.languageCode);
                        wrongCount ++;
                        if(wrongCount > 3){
                            log.info("Already found 3 posts in the wrong languageCode, skipping this blog");
                        }
                        break;
                    }

            }
            catch(Exception e){
                log.error(e);
                break;
            }


        }
        return new AbstractMap.SimpleEntry<>(numOfFiles,wrongCount);
    }
 
Example #7
Source File: RSSFeed.java    From olat with Apache License 2.0 4 votes vote down vote up
/**
    * Constructor. The identityKey is needed to generate personal URLs for the corresponding user.
    */
protected RSSFeed(final Feed feed, final Identity identity, final Long courseId, final String nodeId,final Translator translator, RepositoryService repositoryService) {
       super();

       // This helper object is required for generating the appropriate URLs for
       // the given user (identity)
	final FeedViewHelper helper = new FeedViewHelper(feed, identity, courseId, nodeId, translator, repositoryService);

       setFeedType("rss_2.0");
       setEncoding(RSSServlet.DEFAULT_ENCODING);
       setTitle(feed.getTitle());
       // According to the rss specification, the feed channel description is not
       // (explicitly) allowed to contain html tags.
       String strippedDescription = FilterFactory.getHtmlTagsFilter().filter(feed.getDescription());
       strippedDescription = strippedDescription.replaceAll("&nbsp;", " "); // TODO: remove when filter
       // does it
       setDescription(strippedDescription);
       setLink(helper.getJumpInLink());

       setPublishedDate(feed.getLastModified());
       // The image
       if (feed.getImageName() != null) {
           final SyndImage image = new SyndImageImpl();
           image.setDescription(feed.getDescription());
           image.setTitle(feed.getTitle());
           image.setLink(getLink());
           image.setUrl(helper.getImageUrl());
           setImage(image);
       }

       final List<SyndEntry> episodes = new ArrayList<SyndEntry>();
       for (final Item item : feed.getPublishedItems()) {
           final SyndEntry entry = new SyndEntryImpl();
           entry.setTitle(item.getTitle());

           final SyndContent itemDescription = new SyndContentImpl();
           itemDescription.setType("text/plain");
           itemDescription.setValue(helper.getItemDescriptionForBrowser(item));
           entry.setDescription(itemDescription);

           // Link will also be converted to the rss guid tag. Except if there's an
           // enclosure, then the enclosure url is used.
           // Use jump-in link far all entries. This will be overriden if the item
           // has an enclosure.
           entry.setLink(helper.getJumpInLink() + "#" + item.getGuid());
           entry.setPublishedDate(item.getPublishDate());
           entry.setUpdatedDate(item.getLastModified());

           // The enclosure is the media (audio or video) file of the episode
           final Enclosure media = item.getEnclosure();
           if (media != null) {
               final SyndEnclosure enclosure = new SyndEnclosureImpl();
               enclosure.setUrl(helper.getMediaUrl(item));
               enclosure.setType(media.getType());
               enclosure.setLength(media.getLength());
               // Also set the item link to point to the enclosure
               entry.setLink(helper.getMediaUrl(item));
               final List<SyndEnclosure> enclosures = new ArrayList<SyndEnclosure>();
               enclosures.add(enclosure);
               entry.setEnclosures(enclosures);
           }

           episodes.add(entry);
       }
       setEntries(episodes);
   }
 
Example #8
Source File: RSSFeed.java    From olat with Apache License 2.0 4 votes vote down vote up
/**
 * Constructor. The identityKey is needed to generate personal URLs for the corresponding user.
 */
protected RSSFeed(final Feed feed, final Identity identity, final Long courseId, final String nodeId, final Translator translator, RepositoryService repositoryService) {
    super();

    // This helper object is required for generating the appropriate URLs for
    // the given user (identity)
    final FeedViewHelper helper = new FeedViewHelper(feed, identity, courseId, nodeId, translator, repositoryService);

    setFeedType("rss_2.0");
    setEncoding(DEFAULT_ENCODING);
    setTitle(feed.getTitle());
    // According to the rss specification, the feed channel description is not
    // (explicitly) allowed to contain html tags.
    String strippedDescription = FilterFactory.getHtmlTagsFilter().filter(feed.getDescription());
    strippedDescription = strippedDescription.replaceAll("&nbsp;", " "); // TODO: remove when filter
    // does it
    setDescription(strippedDescription);
    setLink(helper.getJumpInLink());

    setPublishedDate(feed.getLastModified());
    // The image
    if (feed.getImageName() != null) {
        final SyndImage image = new SyndImageImpl();
        image.setDescription(feed.getDescription());
        image.setTitle(feed.getTitle());
        image.setLink(getLink());
        image.setUrl(helper.getImageUrl());
        setImage(image);
    }

    final List<SyndEntry> episodes = new ArrayList<SyndEntry>();
    for (final Item item : feed.getPublishedItems()) {
        final SyndEntry entry = new SyndEntryImpl();
        entry.setTitle(item.getTitle());

        final SyndContent itemDescription = new SyndContentImpl();
        itemDescription.setType("text/plain");
        itemDescription.setValue(helper.getItemDescriptionForBrowser(item));
        entry.setDescription(itemDescription);

        // Link will also be converted to the rss guid tag. Except if there's an
        // enclosure, then the enclosure url is used.
        // Use jump-in link far all entries. This will be overriden if the item
        // has an enclosure.
        entry.setLink(helper.getJumpInLink() + "#" + item.getGuid());
        entry.setPublishedDate(item.getPublishDate());
        entry.setUpdatedDate(item.getLastModified());

        // The enclosure is the media (audio or video) file of the episode
        final Enclosure media = item.getEnclosure();
        if (media != null) {
            final SyndEnclosure enclosure = new SyndEnclosureImpl();
            enclosure.setUrl(helper.getMediaUrl(item));
            enclosure.setType(media.getType());
            enclosure.setLength(media.getLength());
            // Also set the item link to point to the enclosure
            entry.setLink(helper.getMediaUrl(item));
            final List<SyndEnclosure> enclosures = new ArrayList<SyndEnclosure>();
            enclosures.add(enclosure);
            entry.setEnclosures(enclosures);
        }

        episodes.add(entry);
    }
    setEntries(episodes);
}