com.rometools.rome.feed.atom.Feed Java Examples

The following examples show how to use com.rometools.rome.feed.atom.Feed. 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: FileBasedCollection.java    From rome with Apache License 2.0 6 votes vote down vote up
/**
 * Get feed document representing collection.
 *
 * @throws com.rometools.rome.propono.atom.server.AtomException On error retrieving feed file.
 * @return Atom Feed representing collection.
 */
public Feed getFeedDocument() throws AtomException {
    InputStream in = null;
    synchronized (FileStore.getFileStore()) {
        in = FileStore.getFileStore().getFileInputStream(getFeedPath());
        if (in == null) {
            in = createDefaultFeedDocument(contextURI + servletPath + "/" + handle + "/" + collection);
        }
    }
    try {
        final WireFeedInput input = new WireFeedInput();
        final WireFeed wireFeed = input.build(new InputStreamReader(in, "UTF-8"));
        return (Feed) wireFeed;
    } catch (final Exception ex) {
        throw new AtomException(ex);
    }
}
 
Example #2
Source File: AtomFeedHttpMessageConverterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void read() throws IOException {
	InputStream is = getClass().getResourceAsStream("atom.xml");
	MockHttpInputMessage inputMessage = new MockHttpInputMessage(is);
	inputMessage.getHeaders().setContentType(new MediaType("application", "atom+xml", StandardCharsets.UTF_8));
	Feed result = converter.read(Feed.class, inputMessage);
	assertEquals("title", result.getTitle());
	assertEquals("subtitle", result.getSubtitle().getValue());
	List<?> entries = result.getEntries();
	assertEquals(2, entries.size());

	Entry entry1 = (Entry) entries.get(0);
	assertEquals("id1", entry1.getId());
	assertEquals("title1", entry1.getTitle());

	Entry entry2 = (Entry) entries.get(1);
	assertEquals("id2", entry2.getId());
	assertEquals("title2", entry2.getTitle());
}
 
Example #3
Source File: FileBasedCollection.java    From rome with Apache License 2.0 6 votes vote down vote up
/**
 * Delete an entry and any associated media file.
 *
 * @param fsid Internal ID of entry
 * @throws java.lang.Exception On error
 */
public void deleteEntry(final String fsid) throws Exception {
    synchronized (FileStore.getFileStore()) {

        // Remove entry from Feed
        final Feed feed = getFeedDocument();
        updateFeedDocumentRemovingEntry(feed, fsid);

        final String entryFilePath = getEntryPath(fsid);
        FileStore.getFileStore().deleteFile(entryFilePath);

        final String entryMediaPath = getEntryMediaPath(fsid);
        if (entryMediaPath != null) {
            FileStore.getFileStore().deleteFile(entryMediaPath);
        }

        final String entryDirPath = getEntryDirPath(fsid);
        FileStore.getFileStore().deleteDirectory(entryDirPath);

        try {
            Thread.sleep(500L);
        } catch (final Exception ignored) {
        }
    }
}
 
Example #4
Source File: AtomFeedHttpMessageConverterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void read() throws IOException {
	InputStream is = getClass().getResourceAsStream("atom.xml");
	MockHttpInputMessage inputMessage = new MockHttpInputMessage(is);
	inputMessage.getHeaders().setContentType(new MediaType("application", "atom+xml", StandardCharsets.UTF_8));
	Feed result = converter.read(Feed.class, inputMessage);
	assertEquals("title", result.getTitle());
	assertEquals("subtitle", result.getSubtitle().getValue());
	List<?> entries = result.getEntries();
	assertEquals(2, entries.size());

	Entry entry1 = (Entry) entries.get(0);
	assertEquals("id1", entry1.getId());
	assertEquals("title1", entry1.getTitle());

	Entry entry2 = (Entry) entries.get(1);
	assertEquals("id2", entry2.getId());
	assertEquals("title2", entry2.getTitle());
}
 
Example #5
Source File: CustomerAtomFeedView.java    From event-driven-spring-boot with Apache License 2.0 6 votes vote down vote up
@Override
protected void buildFeedMetadata(Map<String, Object> model, Feed feed, HttpServletRequest request) {
	feed.setId("https://github.com/mploed/event-driven-spring-boot/customer");
	feed.setTitle("Customer");
	List<Link> alternateLinks = new ArrayList<>();
	Link link = new Link();
	link.setRel("self");
	link.setHref(baseUrl(request) + "feed");
	alternateLinks.add(link);
	List<SyndPerson> authors = new ArrayList<SyndPerson>();
	Person person = new Person();
	person.setName("Big Pug Bank");
	authors.add(person);
	feed.setAuthors(authors);

	feed.setAlternateLinks(alternateLinks);
	feed.setUpdated(customerRepository.lastUpdate());
	Content subtitle = new Content();
	subtitle.setValue("List of all customers");
	feed.setSubtitle(subtitle);
}
 
Example #6
Source File: OrderAtomFeedView.java    From microservice-atom with Apache License 2.0 6 votes vote down vote up
@Override
protected void buildFeedMetadata(Map<String, Object> model, Feed feed, HttpServletRequest request) {
	feed.setId("tag:ewolff.com/microservice-atom/order");
	feed.setTitle("Order");
	List<Link> alternateLinks = new ArrayList<>();
	Link link = new Link();
	link.setRel("self");
	link.setHref(baseUrl(request) + "feed");
	alternateLinks.add(link);
	List<SyndPerson> authors = new ArrayList<SyndPerson>();
	Person person = new Person();
	person.setName("Big Money Online Commerce Inc.");
	authors.add(person);
	feed.setAuthors(authors);

	feed.setAlternateLinks(alternateLinks);
	feed.setUpdated(orderRepository.lastUpdate());
	Content subtitle = new Content();
	subtitle.setValue("List of all orders");
	feed.setSubtitle(subtitle);
}
 
Example #7
Source File: AtomClientTest.java    From microservice-atom with Apache License 2.0 6 votes vote down vote up
@Test
public void requestWithLastModifiedReturns304() {
	Order order = new Order();
	order.setCustomer(customerRepository.findAll().iterator().next());
	orderRepository.save(order);
	ResponseEntity<Feed> response = restTemplate.exchange(feedUrl(), HttpMethod.GET, new HttpEntity(null),
			Feed.class);

	Date lastModified = DateUtils.parseDate(response.getHeaders().getFirst("Last-Modified"));

	HttpHeaders requestHeaders = new HttpHeaders();
	requestHeaders.set("If-Modified-Since", DateUtils.formatDate(lastModified));
	HttpEntity requestEntity = new HttpEntity(requestHeaders);

	response = restTemplate.exchange(feedUrl(), HttpMethod.GET, requestEntity, Feed.class);

	assertEquals(HttpStatus.NOT_MODIFIED, response.getStatusCode());
}
 
Example #8
Source File: AtomClientTest.java    From microservice-atom with Apache License 2.0 6 votes vote down vote up
@Test
public void feedReturnsNewlyCreatedOrder() {
	Order order = new Order();
	order.setCustomer(customerRepository.findAll().iterator().next());
	orderRepository.save(order);
	Feed feed = retrieveFeed();
	boolean foundLinkToCreatedOrder = false;
	List<Entry> entries = feed.getEntries();
	for (Entry entry : entries) {
		for (Content content : entry.getContents()) {
			if (content.getSrc().contains(Long.toString(order.getId()))) {
				foundLinkToCreatedOrder = true;
			}
		}
	}
	assertTrue(foundLinkToCreatedOrder);
}
 
Example #9
Source File: FileBasedCollection.java    From rome with Apache License 2.0 6 votes vote down vote up
private void updateFeedDocument(final Feed f) throws AtomException {
    try {
        synchronized (FileStore.getFileStore()) {
            final WireFeedOutput wireFeedOutput = new WireFeedOutput();
            final Document feedDoc = wireFeedOutput.outputJDom(f);
            final XMLOutputter outputter = new XMLOutputter();
            // outputter.setFormat(Format.getPrettyFormat());
            final OutputStream fos = FileStore.getFileStore().getFileOutputStream(getFeedPath());
            outputter.output(feedDoc, new OutputStreamWriter(fos, "UTF-8"));
        }
    } catch (final FeedException fex) {
        throw new AtomException(fex);
    } catch (final IOException ex) {
        throw new AtomException(ex);
    }
}
 
Example #10
Source File: AtomFeedHttpMessageConverterTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void read() throws IOException {
	InputStream is = getClass().getResourceAsStream("atom.xml");
	MockHttpInputMessage inputMessage = new MockHttpInputMessage(is);
	inputMessage.getHeaders().setContentType(new MediaType("application", "atom+xml", utf8));
	Feed result = converter.read(Feed.class, inputMessage);
	assertEquals("title", result.getTitle());
	assertEquals("subtitle", result.getSubtitle().getValue());
	List<?> entries = result.getEntries();
	assertEquals(2, entries.size());

	Entry entry1 = (Entry) entries.get(0);
	assertEquals("id1", entry1.getId());
	assertEquals("title1", entry1.getTitle());

	Entry entry2 = (Entry) entries.get(1);
	assertEquals("id2", entry2.getId());
	assertEquals("title2", entry2.getTitle());
}
 
Example #11
Source File: AtomFeedView.java    From wallride with Apache License 2.0 6 votes vote down vote up
protected void buildFeedMetadata(
			Map<String, Object> model,
			Feed feed,
			HttpServletRequest request) {
		Blog blog = blogService.getBlogById(Blog.DEFAULT_ID);
		String language = LocaleContextHolder.getLocale().getLanguage();

		feed.setTitle(blog.getTitle(language));
		Content info = new Content();
		info.setValue(blog.getTitle(language));
		feed.setInfo(info);

		ArrayList<Link> links = new ArrayList<>();
		Link link = new Link();
		UriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentContextPath();
		link.setHref(builder.buildAndExpand().toUriString());
		links.add(link);
		feed.setOtherLinks(links);
//		feed.setIcon("http://" + settings.getAsString(Setting.Key.SITE_URL) + "resources/default/img/favicon.ico");
	}
 
Example #12
Source File: FileBasedCollection.java    From rome with Apache License 2.0 6 votes vote down vote up
private void updateFeedDocumentWithExistingEntry(final Feed f, final Entry e) throws AtomException {
    final Entry old = findEntry(e.getId(), f);
    f.getEntries().remove(old);

    boolean inserted = false;
    for (int i = 0; i < f.getEntries().size(); i++) {
        final Entry entry = f.getEntries().get(i);
        final AppModule entryAppModule = (AppModule) entry.getModule(AppModule.URI);
        final AppModule eAppModule = (AppModule) entry.getModule(AppModule.URI);
        if (eAppModule.getEdited().before(entryAppModule.getEdited())) {
            f.getEntries().add(i, e);
            inserted = true;
            break;
        }
    }
    if (!inserted) {
        f.getEntries().add(0, e);
    }
    updateFeedDocument(f);
}
 
Example #13
Source File: FileBasedCollection.java    From rome with Apache License 2.0 6 votes vote down vote up
/**
 * Update an entry in the collection.
 *
 * @param entry Updated entry to be stored
 * @param fsid Internal ID of entry
 * @throws java.lang.Exception On error
 */
public void updateEntry(final Entry entry, String fsid) throws Exception {
    synchronized (FileStore.getFileStore()) {

        final Feed f = getFeedDocument();

        if (fsid.endsWith(".media-link")) {
            fsid = fsid.substring(0, fsid.length() - ".media-link".length());
        }

        updateTimestamps(entry);

        updateEntryAppLinks(entry, fsid, false);
        updateFeedDocumentWithExistingEntry(f, entry);

        final String entryPath = getEntryPath(fsid);
        final OutputStream os = FileStore.getFileStore().getFileOutputStream(entryPath);
        updateEntryAppLinks(entry, fsid, true);
        Atom10Generator.serializeEntry(entry, new OutputStreamWriter(os, "UTF-8"));
        os.flush();
        os.close();
    }
}
 
Example #14
Source File: Atom10Parser.java    From rome with Apache License 2.0 6 votes vote down vote up
/**
 * Parse entry from reader.
 */
public static Entry parseEntry(final Reader rd, final String baseURI, final Locale locale) throws JDOMException, IOException, IllegalArgumentException,
        FeedException {

    // Parse entry into JDOM tree
    final SAXBuilder builder = new SAXBuilder();
    final Document entryDoc = builder.build(rd);
    final Element fetchedEntryElement = entryDoc.getRootElement();
    fetchedEntryElement.detach();

    // Put entry into a JDOM document with 'feed' root so that Rome can
    // handle it
    final Feed feed = new Feed();
    feed.setFeedType("atom_1.0");
    final WireFeedOutput wireFeedOutput = new WireFeedOutput();
    final Document feedDoc = wireFeedOutput.outputJDom(feed);
    feedDoc.getRootElement().addContent(fetchedEntryElement);

    if (baseURI != null) {
        feedDoc.getRootElement().setAttribute("base", baseURI, Namespace.XML_NAMESPACE);
    }

    final WireFeedInput input = new WireFeedInput(false, locale);
    final Feed parsedFeed = (Feed) input.build(feedDoc);
    return parsedFeed.getEntries().get(0);
}
 
Example #15
Source File: Atom10Generator.java    From rome with Apache License 2.0 6 votes vote down vote up
/**
 * Utility method to serialize an entry to writer.
 */
public static void serializeEntry(final Entry entry, final Writer writer) throws IllegalArgumentException, FeedException, IOException {

    // Build a feed containing only the entry
    final List<Entry> entries = new ArrayList<Entry>();
    entries.add(entry);
    final Feed feed1 = new Feed();
    feed1.setFeedType("atom_1.0");
    feed1.setEntries(entries);

    // Get Rome to output feed as a JDOM document
    final WireFeedOutput wireFeedOutput = new WireFeedOutput();
    final Document feedDoc = wireFeedOutput.outputJDom(feed1);

    // Grab entry element from feed and get JDOM to serialize it
    final Element entryElement = feedDoc.getRootElement().getChildren().get(0);

    final XMLOutputter outputter = new XMLOutputter();
    outputter.output(entryElement, writer);
}
 
Example #16
Source File: FileBasedCollection.java    From rome with Apache License 2.0 6 votes vote down vote up
/**
 * Add entry to collection.
 *
 * @param entry Entry to be added to collection. Entry will be saved to disk in a directory
 *            under the collection's directory and the path will follow the pattern
 *            [collection-plural]/[entryid]/entry.xml. The entry will be added to the
 *            collection's feed in [collection-plural]/feed.xml.
 * @throws java.lang.Exception On error.
 * @return Entry as it exists on the server.
 */
public Entry addEntry(final Entry entry) throws Exception {
    synchronized (FileStore.getFileStore()) {
        final Feed f = getFeedDocument();

        final String fsid = FileStore.getFileStore().getNextId();
        updateTimestamps(entry);

        // Save entry to file
        final String entryPath = getEntryPath(fsid);

        final OutputStream os = FileStore.getFileStore().getFileOutputStream(entryPath);
        updateEntryAppLinks(entry, fsid, true);
        Atom10Generator.serializeEntry(entry, new OutputStreamWriter(os, "UTF-8"));
        os.flush();
        os.close();

        // Update feed file
        updateEntryAppLinks(entry, fsid, false);
        updateFeedDocumentWithNewEntry(f, entry);

        return entry;
    }
}
 
Example #17
Source File: TestAtomContent.java    From rome with Apache License 2.0 5 votes vote down vote up
private Feed createFeed() {
    final Feed feed = new Feed();
    final Content content = new Content();
    content.setType("application/xml");
    content.setValue("<test>Hello Hello</test>");
    feed.setTitleEx(content);
    feed.setFeedType("atom_1.0");
    return feed;
}
 
Example #18
Source File: TestSyndFeedAtom10prefix.java    From rome with Apache License 2.0 5 votes vote down vote up
public void testTitle() throws Exception {
    final Feed feed = (Feed) getWireFeed();
    assertEquals("1", feed.getId());
    assertEquals("xxx1", feed.getOtherLinks().get(0).getRel());
    assertEquals("xxx2", feed.getOtherLinks().get(1).getRel());
    assertEquals("xxx11", feed.getOtherLinks().get(0).getType());
    assertEquals("xxx22", feed.getOtherLinks().get(1).getType());
    assertEquals("http://foo.com/1", feed.getOtherLinks().get(0).getHref());
    assertEquals("http://foo.com/2", feed.getOtherLinks().get(1).getHref());
}
 
Example #19
Source File: Atom03Generator.java    From rome with Apache License 2.0 5 votes vote down vote up
protected void addFeed(final Feed feed, final Element parent) throws FeedException {
    final Element eFeed = parent;
    populateFeedHeader(feed, eFeed);
    checkFeedHeaderConstraints(eFeed);
    generateFeedModules(feed.getModules(), eFeed);
    generateForeignMarkup(eFeed, feed.getForeignMarkup());
}
 
Example #20
Source File: Atom03Generator.java    From rome with Apache License 2.0 5 votes vote down vote up
protected Element createRootElement(final Feed feed) {
    final Element root = new Element("feed", getFeedNamespace());
    root.addNamespaceDeclaration(getFeedNamespace());
    final Attribute version = new Attribute("version", getVersion());
    root.setAttribute(version);
    generateModuleNamespaceDefs(root);
    return root;
}
 
Example #21
Source File: Atom03Generator.java    From rome with Apache License 2.0 5 votes vote down vote up
@Override
public Document generate(final WireFeed wFeed) throws FeedException {
    final Feed feed = (Feed) wFeed;
    final Element root = createRootElement(feed);
    populateFeed(feed, root);
    purgeUnusedNamespaceDeclarations(root);
    return createDocument(root);
}
 
Example #22
Source File: TestAtomContent.java    From rome with Apache License 2.0 5 votes vote down vote up
public void testXML() throws Exception {
    final Feed feed = createFeed();
    final StringWriter sw = new StringWriter();
    final WireFeedOutput output = new WireFeedOutput();
    output.output(feed, sw);
    sw.close();
    assertTrue(sw.toString().contains("<test xmlns=\"\">Hello Hello</test>"));
}
 
Example #23
Source File: ConverterForAtom10.java    From rome with Apache License 2.0 5 votes vote down vote up
public SyndEnclosure createSyndEnclosure(final Feed feed, final Entry entry, final Link link) {
    final SyndEnclosure syndEncl = new SyndEnclosureImpl();
    syndEncl.setUrl(link.getHrefResolved());
    syndEncl.setType(link.getType());
    syndEncl.setLength(link.getLength());
    return syndEncl;
}
 
Example #24
Source File: ConverterForAtom10.java    From rome with Apache License 2.0 5 votes vote down vote up
protected List<SyndEntry> createSyndEntries(final Feed feed, final List<Entry> atomEntries, final boolean preserveWireItems) {
    final List<SyndEntry> syndEntries = new ArrayList<SyndEntry>();
    for (final Entry atomEntry : atomEntries) {
        syndEntries.add(createSyndEntry(feed, atomEntry, preserveWireItems));
    }
    return syndEntries;
}
 
Example #25
Source File: AtomFeedHttpMessageConverterTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void write() throws IOException, SAXException {
	Feed feed = new Feed("atom_1.0");
	feed.setTitle("title");

	Entry entry1 = new Entry();
	entry1.setId("id1");
	entry1.setTitle("title1");

	Entry entry2 = new Entry();
	entry2.setId("id2");
	entry2.setTitle("title2");

	List<Entry> entries = new ArrayList<>(2);
	entries.add(entry1);
	entries.add(entry2);
	feed.setEntries(entries);

	MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
	converter.write(feed, null, outputMessage);

	assertEquals("Invalid content-type", new MediaType("application", "atom+xml", StandardCharsets.UTF_8),
			outputMessage.getHeaders().getContentType());
	String expected = "<feed xmlns=\"http://www.w3.org/2005/Atom\">" + "<title>title</title>" +
			"<entry><id>id1</id><title>title1</title></entry>" +
			"<entry><id>id2</id><title>title2</title></entry></feed>";
	NodeMatcher nm = new DefaultNodeMatcher(ElementSelectors.byName);
	assertThat(outputMessage.getBodyAsString(StandardCharsets.UTF_8),
			isSimilarTo(expected).ignoreWhitespace().withNodeMatcher(nm));
}
 
Example #26
Source File: FoundEpisodesAtomFeedView.java    From podcastpedia-web with MIT License 5 votes vote down vote up
protected void buildFeedMetadata(Map model, Feed feed, HttpServletRequest request) {
    feed.setId("" + model.get("feed_id"));
    feed.setTitle("" + model.get("feed_title"));
    Content subTitle = new Content();
    subTitle.setValue("" + model.get("feed_description"));
    feed.setSubtitle(subTitle);
}
 
Example #27
Source File: GenericAtomFeedView.java    From podcastpedia-web with MIT License 5 votes vote down vote up
protected void buildFeedMetadata(Map model, Feed feed, HttpServletRequest request) {
    feed.setId("" + model.get("feed_id"));
    feed.setTitle("" + model.get("feed_title"));
    Content subTitle = new Content();
    subTitle.setValue("" + model.get("feed_description"));
    feed.setSubtitle(subTitle);
}
 
Example #28
Source File: MessageConvertersController.java    From spring-tutorial with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
@RequestMapping(value = "/atom", method = RequestMethod.GET)
public @ResponseBody
Feed writeFeed() {
	Feed feed = new Feed();
	feed.setFeedType("atom_1.0");
	feed.setTitle("My Atom feed");
	return feed;
}
 
Example #29
Source File: AtomFeedHttpMessageConverterTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void writeOtherCharset() throws IOException, SAXException {
	Feed feed = new Feed("atom_1.0");
	feed.setTitle("title");
	String encoding = "ISO-8859-1";
	feed.setEncoding(encoding);

	MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
	converter.write(feed, null, outputMessage);

	assertEquals("Invalid content-type", new MediaType("application", "atom+xml", Charset.forName(encoding)),
			outputMessage.getHeaders().getContentType());
}
 
Example #30
Source File: Atom03Generator.java    From rome with Apache License 2.0 5 votes vote down vote up
protected void addEntries(final Feed feed, final Element parent) throws FeedException {
    final List<Entry> entries = feed.getEntries();
    for (final Entry entry : entries) {
        addEntry(entry, parent);
    }
    checkEntriesConstraints(parent);
}