summaryrefslogtreecommitdiffstats
path: root/sandbox
diff options
context:
space:
mode:
authormcombellack <mcombellack@13f79535-47bb-0310-9956-ffa450edef68>2009-06-21 11:48:02 +0000
committermcombellack <mcombellack@13f79535-47bb-0310-9956-ffa450edef68>2009-06-21 11:48:02 +0000
commit2321a44e501d413b4b4c830060ff95ffaa18dcc6 (patch)
treedadd956bdb7b48ae618d97804a9cc1eb6184de0e /sandbox
parent4a925ec55ed4737e0a65b9eaa77de280723b043d (diff)
Updated the application so that it uses the Tuscany Data API for creating the blog feeds so that we can add both an Atom and RSS feed onto the same Java component implementation.
For completeness, added implementations that extend the Collections interfaces for the Atom and RSS bindings to show it can be done that way too but the implementation code cannot support both bindings at the same time git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@786996 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sandbox')
-rw-r--r--sandbox/travelsample/contributions/blog-feed-contribution/src/main/java/scatours/blog/BlogPost.java68
-rw-r--r--sandbox/travelsample/contributions/blog-feed-contribution/src/main/java/scatours/blog/feed/AtomBlogFeedImpl.java (renamed from sandbox/travelsample/contributions/blog-feed-contribution/src/main/java/scatours/blog/feed/BlogFeedImpl.java)76
-rw-r--r--sandbox/travelsample/contributions/blog-feed-contribution/src/main/java/scatours/blog/feed/BaseBlogFeedImpl.java89
-rw-r--r--sandbox/travelsample/contributions/blog-feed-contribution/src/main/java/scatours/blog/feed/GenericBlogFeedImpl.java75
-rw-r--r--sandbox/travelsample/contributions/blog-feed-contribution/src/main/java/scatours/blog/feed/RSSBlogFeedImpl.java94
-rw-r--r--sandbox/travelsample/contributions/blog-feed-contribution/src/main/resources/blog-feed.composite34
6 files changed, 378 insertions, 58 deletions
diff --git a/sandbox/travelsample/contributions/blog-feed-contribution/src/main/java/scatours/blog/BlogPost.java b/sandbox/travelsample/contributions/blog-feed-contribution/src/main/java/scatours/blog/BlogPost.java
new file mode 100644
index 0000000000..931a9f18c8
--- /dev/null
+++ b/sandbox/travelsample/contributions/blog-feed-contribution/src/main/java/scatours/blog/BlogPost.java
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package scatours.blog;
+
+import java.util.Date;
+
+/**
+ * Bean for blog posts.
+ */
+public class BlogPost {
+
+ private final String author;
+ private final String title;
+ private final String content;
+ private final Date updated;
+ private final String link;
+ private final String related;
+
+ public BlogPost(String author, String title, String content, Date updated, String link, String related) {
+ this.author = author;
+ this.title = title;
+ this.content = content;
+ this.updated = updated;
+ this.link = link;
+ this.related = related;
+ }
+
+ public String getAuthor() {
+ return author;
+ }
+
+ public String getTitle() {
+ return title;
+ }
+
+ public String getContent() {
+ return content;
+ }
+
+ public Date getUpdated() {
+ return updated;
+ }
+
+ public String getLink() {
+ return link;
+ }
+
+ public String getRelated() {
+ return related;
+ }
+}
diff --git a/sandbox/travelsample/contributions/blog-feed-contribution/src/main/java/scatours/blog/feed/BlogFeedImpl.java b/sandbox/travelsample/contributions/blog-feed-contribution/src/main/java/scatours/blog/feed/AtomBlogFeedImpl.java
index 016ae77ab6..e98c4dd433 100644
--- a/sandbox/travelsample/contributions/blog-feed-contribution/src/main/java/scatours/blog/feed/BlogFeedImpl.java
+++ b/sandbox/travelsample/contributions/blog-feed-contribution/src/main/java/scatours/blog/feed/AtomBlogFeedImpl.java
@@ -19,80 +19,52 @@
package scatours.blog.feed;
-import java.util.Date;
-import java.util.concurrent.atomic.AtomicInteger;
+import java.util.List;
import org.apache.abdera.Abdera;
import org.apache.abdera.factory.Factory;
import org.apache.abdera.model.Entry;
import org.apache.abdera.model.Feed;
-import org.apache.abdera.model.Person;
import org.apache.tuscany.sca.binding.atom.collection.NotFoundException;
-public class BlogFeedImpl implements org.apache.tuscany.sca.binding.atom.collection.Collection {
+import scatours.blog.BlogPost;
- /**
- * Title of the blog.feed.
- */
- private static final String FEED_TITLE = "Tuscany SCA Tours Blog Feed";
-
- /**
- * Description of the blog feed.
- */
- private static final String FEED_DESCRIPTION = "Feed contianing the latest blog posts from Tuscany SCA Tours";
-
- /**
- * Author of the blog feed.
- */
- private static final String FEED_AUTHOR = "SCA Tours CEO";
-
- /**
- * Used to generate unique IDs for the blog entries.
- */
- private static final AtomicInteger ID_GEN = new AtomicInteger();
+/**
+ * An Atom feed that implements the org.apache.tuscany.sca.binding.atom.collection.Collection
+ * interface and uses the Atom APIs to construct the Atom feed.
+ */
+public class AtomBlogFeedImpl extends BaseBlogFeedImpl implements org.apache.tuscany.sca.binding.atom.collection.Collection {
/**
- * Gets a feed containing all the blog posts.
+ * Gets an Atom feed containing all the blog posts.
*
- * @return A feed containing all the blog posts.
+ * @return An Atom feed containing all the blog posts.
*/
public Feed getFeed() {
+ // Create SCA Tours blog Atom feed
final Factory factory = Abdera.getNewFactory();
-
- // Create SCA Tours blog feed
final Feed feed = factory.newFeed();
feed.setTitle(FEED_TITLE);
feed.setSubtitle(FEED_DESCRIPTION);
-
- // Create blog author
- final Person author = factory.newAuthor();
- author.setName(FEED_AUTHOR);
- feed.addAuthor(author);
-
- // Create a sample entry
- final Entry entry = factory.newEntry();
- entry.setId(nextBlogID());
- entry.addAuthor(FEED_AUTHOR);
- entry.setTitle("Apache Tuscany in Action book features SCA Tours");
- entry.setContentAsHtml("We are famous as SCA Tours has been featured in the Apache Tuscany in Action book published by Manning");
- entry.setUpdated(new Date());
- entry.addLink("http://www.manning.com/laws/");
- feed.addEntry(entry);
+ feed.addAuthor(FEED_AUTHOR);
+
+ // Get all blog posts and convert to Atom entries
+ final List<BlogPost> blogEntries = getAllBlogPosts();
+ for (BlogPost blogEntry : blogEntries) {
+ final Entry entry = factory.newEntry();
+ entry.setId(nextBlogID());
+ entry.addAuthor(blogEntry.getAuthor());
+ entry.setTitle(blogEntry.getTitle());
+ entry.setContentAsHtml(blogEntry.getContent());
+ entry.setUpdated(blogEntry.getUpdated());
+ entry.addLink(blogEntry.getLink());
+ feed.addEntry(entry);
+ }
return feed;
}
/**
- * Generates the next blog entry ID.
- *
- * @return Next blog entry ID
- */
- private String nextBlogID()
- {
- return Integer.toString(ID_GEN.incrementAndGet());
- }
-
- /**
* Query the feed.
*
* @param query The query
diff --git a/sandbox/travelsample/contributions/blog-feed-contribution/src/main/java/scatours/blog/feed/BaseBlogFeedImpl.java b/sandbox/travelsample/contributions/blog-feed-contribution/src/main/java/scatours/blog/feed/BaseBlogFeedImpl.java
new file mode 100644
index 0000000000..ca898fe2ad
--- /dev/null
+++ b/sandbox/travelsample/contributions/blog-feed-contribution/src/main/java/scatours/blog/feed/BaseBlogFeedImpl.java
@@ -0,0 +1,89 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package scatours.blog.feed;
+
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import scatours.blog.BlogPost;
+
+/**
+ * Base class for all blog feeds that provides the common methods
+ * that are shared by all the blog feed implementations.
+ */
+public abstract class BaseBlogFeedImpl {
+
+ /**
+ * Title of the blog.feed.
+ */
+ protected static final String FEED_TITLE = "Tuscany SCA Tours Blog Feed";
+
+ /**
+ * Description of the blog feed.
+ */
+ protected static final String FEED_DESCRIPTION = "Feed contianing the latest blog posts from Tuscany SCA Tours";
+
+ /**
+ * Author of the blog feed.
+ */
+ protected static final String FEED_AUTHOR = "SCA Tours CEO";
+
+ /**
+ * Used to generate unique IDs for the blog entries.
+ */
+ protected static final AtomicInteger ID_GEN = new AtomicInteger();
+
+ /**
+ * Generates the next blog entry ID.
+ *
+ * @return Next blog entry ID
+ */
+ protected String nextBlogID() {
+ return Integer.toString(ID_GEN.incrementAndGet());
+ }
+
+ /**
+ * Retrieves a list of all blog posts.
+ *
+ * @return A list of all blog posts.
+ */
+ public List<BlogPost> getAllBlogPosts() {
+ // Note: To keep things simple, we will just hard code a sample post.
+ // A proper implementation would load all blog posts from some resource
+ // such as files or a database.
+ List<BlogPost> blogEntries = new ArrayList<BlogPost>();
+
+ // Create a sample entry
+ final BlogPost samplePost = new BlogPost(
+ FEED_AUTHOR,
+ "Apache Tuscany in Action book features SCA Tours",
+ "We are famous as SCA Tours has been featured in the Apache Tuscany in Action book published by Manning",
+ new Date(),
+ "http://www.manning.com/laws/",
+ null);
+
+ // Add sample post to the list of posts
+ blogEntries.add(samplePost);
+
+ return blogEntries;
+ }
+}
diff --git a/sandbox/travelsample/contributions/blog-feed-contribution/src/main/java/scatours/blog/feed/GenericBlogFeedImpl.java b/sandbox/travelsample/contributions/blog-feed-contribution/src/main/java/scatours/blog/feed/GenericBlogFeedImpl.java
new file mode 100644
index 0000000000..dbc0de429d
--- /dev/null
+++ b/sandbox/travelsample/contributions/blog-feed-contribution/src/main/java/scatours/blog/feed/GenericBlogFeedImpl.java
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package scatours.blog.feed;
+
+import java.util.List;
+
+import org.apache.tuscany.sca.data.collection.Entry;
+import org.apache.tuscany.sca.data.collection.Item;
+
+import scatours.blog.BlogPost;
+
+/**
+ * Implementation of the blog feed that uses the Tuscany Data API so
+ * that it is independent of any Feed APIs such as Atom and RSS.
+ */
+public class GenericBlogFeedImpl extends BaseBlogFeedImpl {
+
+ /**
+ * Implementation of the getAll() method from the Tuscany API
+ * that will return all of the blog posts as generic Tuscany
+ * feed items.
+ *
+ * @return All blog entries
+ */
+ public Entry<Object, Object>[] getAll() {
+ final List<BlogPost> posts = getAllBlogPosts();
+
+ final Entry<Object, Object>[] entries = new Entry[posts.size()];
+ int i = 0;
+ for (BlogPost post : posts) {
+ entries[i++] = convertBlogPostToFeedItem(post);
+ }
+
+ return entries;
+ }
+
+ /**
+ * Converts a blog post to a Tuscany API feed item.
+ *
+ * @param post The blog post to convert
+ * @return The blog post as a Tuscany API feed item
+ */
+ private Entry<Object, Object> convertBlogPostToFeedItem(BlogPost post) {
+ // Convert Blog entry into an Item
+ final Item item = new Item(
+ post.getTitle(),
+ post.getContent(),
+ post.getLink(),
+ post.getRelated(),
+ post.getUpdated());
+
+ // Add item to entry
+ final Entry<Object, Object> entry = new Entry<Object, Object>(
+ nextBlogID(), item);
+
+ return entry;
+ }
+}
diff --git a/sandbox/travelsample/contributions/blog-feed-contribution/src/main/java/scatours/blog/feed/RSSBlogFeedImpl.java b/sandbox/travelsample/contributions/blog-feed-contribution/src/main/java/scatours/blog/feed/RSSBlogFeedImpl.java
new file mode 100644
index 0000000000..e728bf7617
--- /dev/null
+++ b/sandbox/travelsample/contributions/blog-feed-contribution/src/main/java/scatours/blog/feed/RSSBlogFeedImpl.java
@@ -0,0 +1,94 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package scatours.blog.feed;
+
+import java.util.List;
+
+import org.apache.tuscany.sca.binding.rss.collection.NotFoundException;
+
+import scatours.blog.BlogPost;
+
+import com.sun.syndication.feed.synd.SyndContent;
+import com.sun.syndication.feed.synd.SyndContentImpl;
+import com.sun.syndication.feed.synd.SyndEntry;
+import com.sun.syndication.feed.synd.SyndEntryImpl;
+import com.sun.syndication.feed.synd.SyndFeed;
+import com.sun.syndication.feed.synd.SyndFeedImpl;
+
+/**
+ * An RSS feed that implements the org.apache.tuscany.sca.binding.rss.collection.Collection
+ * interface and uses the RSS APIs to construct the RSS feed.
+ */
+public class RSSBlogFeedImpl extends BaseBlogFeedImpl implements org.apache.tuscany.sca.binding.rss.collection.Collection {
+
+ /**
+ * Gets an RSS feed containing all the blog posts.
+ *
+ * @return An RSS feed containing all the blog posts.
+ */
+ public SyndFeed getFeed() {
+ // Create SCA Tours blog RSS feed
+ SyndFeed feed = new SyndFeedImpl();
+ feed.setTitle(FEED_TITLE);
+ feed.setDescription(FEED_DESCRIPTION);
+ feed.setAuthor(FEED_AUTHOR);
+
+ // Get all blog posts and convert to RSS entries
+ final List<BlogPost> blogEntries = getAllBlogPosts();
+ for (BlogPost blogEntry : blogEntries) {
+ SyndEntry entry = new SyndEntryImpl();
+ entry.setUri(nextBlogID());
+ entry.setAuthor(blogEntry.getAuthor());
+ entry.setTitle(blogEntry.getTitle());
+
+ SyndContent content = new SyndContentImpl();
+ content.setType("text");
+ content.setValue(blogEntry.getContent());
+
+ entry.setPublishedDate(blogEntry.getUpdated());
+ entry.setLink(blogEntry.getLink());
+
+ feed.getEntries().add(entry);
+ }
+
+ return feed;
+ }
+
+ /**
+ * Query the feed.
+ *
+ * @param query The query
+ * @return Always returns null as method not implemented
+ */
+ public SyndFeed query(String query) {
+ // Not implemented
+ return null;
+ }
+
+ public SyndEntry get(String id) throws NotFoundException {
+ // Not implemented
+ return null;
+ }
+
+ public List<SyndEntry> getAll() throws NotFoundException {
+ // Not implemented
+ return null;
+ }
+}
diff --git a/sandbox/travelsample/contributions/blog-feed-contribution/src/main/resources/blog-feed.composite b/sandbox/travelsample/contributions/blog-feed-contribution/src/main/resources/blog-feed.composite
index b236c5feb0..1c9cd044b3 100644
--- a/sandbox/travelsample/contributions/blog-feed-contribution/src/main/resources/blog-feed.composite
+++ b/sandbox/travelsample/contributions/blog-feed-contribution/src/main/resources/blog-feed.composite
@@ -22,16 +22,38 @@
targetNamespace="http://goodvaluetrips.com/"
name="blog-feed">
- <service name="BlogRSS" promote="Blog">
- <tuscany:binding.atom uri="http://localhost:8090/BlogRSS"/>
+
+ <!-- Example that shows using the Tuscany Data APIs to create a feed that -->
+ <!-- is independent of the Feed API -->
+ <service name="BlogAtom" promote="BlogFeed">
+ <tuscany:binding.atom uri="http://localhost:8090/BlogAtom"/>
+ </service>
+
+ <service name="BlogRSS" promote="BlogFeed">
+ <tuscany:binding.rss uri="http://localhost:8090/BlogRSS"/>
</service>
- <service name="BlogAtom" promote="Blog">
- <tuscany:binding.atom uri="http://localhost:8090/BlogAtom"/>
+ <component name="BlogFeed">
+ <implementation.java class="scatours.blog.feed.GenericBlogFeedImpl"/>
+ </component>
+
+ <!-- Example that shows using the Atom Collections API to create a feed that -->
+ <!-- uses the Atom APIs -->
+ <service name="BlogAtomAPIs" promote="BlogAtom">
+ <tuscany:binding.atom uri="http://localhost:8090/BlogAtomAPIs"/>
</service>
- <component name="Blog">
- <implementation.java class="scatours.blog.feed.BlogFeedImpl"/>
+ <component name="BlogAtom">
+ <implementation.java class="scatours.blog.feed.AtomBlogFeedImpl"/>
</component>
+ <!-- Example that shows using the RSS Collections API to create a feed that -->
+ <!-- uses the RSS APIs -->
+ <service name="BlogRSSAPIs" promote="BlogRSS">
+ <tuscany:binding.rss uri="http://localhost:8090/BlogRSSAPIs"/>
+ </service>
+
+ <component name="BlogRSS">
+ <implementation.java class="scatours.blog.feed.RSSBlogFeedImpl"/>
+ </component>
</composite>