Migration from Apache Abdera to Apache Wink
Apache Wink is the perfect solution for consuming and producing Atom, APP and RSS documents. The following section describes how to migrate from Apache Abdera to Apache Wink by providing a set of examples that cover most use cases.
Advantages of Apache Wink over Apache Abdera
- Standardized APIs (using JAX-RS and JAXB)
- Support for handling XML and JSON more easily
- Support for handling RSS and ATOM more easily
This section contains the following topics:
...
1) Consuming Atom Documents
The following code example demonstrates the consumption of Atom documents using Apache Abdera.
Apache Abdera - Click on link to Download - ConsumeAtomUsingAbdera.java |
---|
Code Block |
---|
Abdera abdera = new Abdera();
Parser parser = abdera.getParser();
URL url = new URL |
|
1) Consuming Atom Documents:
Apache Abdera
| Apache Wink
|
---|
Code Block |
---|
Abdera abdera = new Abdera();
Parser parser = abdera.getParser();
URL url = new URL("http://alexharden.org/blog/atom.xml");
Document<Feed> doc = parser.parse(url.openStream());
Feed feed = doc.getRoot();
System.out.println(feed.getTitle());
for (Entry entry : feed.getEntries()) {
System.out.println("\t" + entry.getTitle());
}
|
| Code Block |
---|
RestClient client = new RestClient();
Resource resource = client.resource("http://alexharden.org/blog/atom.xml");
AtomFeedDocument<Feed> feeddoc = resourceparser.acceptparse(MediaType.APPLICATION_ATOM_XML).get(AtomFeed.class);
System.outurl.openStream());
Feed feed = doc.getRoot();
System.out.println(feed.getTitle().getValue());
for (AtomEntryEntry entry : feed.getEntries()) {
System.out.println("\t" + entry.getTitle().getValue());
}
|
|
The following code example demonstrates the consumption of Atom documents using Apache Wink.
2) a) Producing Atom Documents:
Apache Abdera | Apache Wink |
---|
Code Block |
---|
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Abdera abdera RestClient client = new AbderaRestClient();
Feed feedResource resource = abderaclient.newFeed();
feed.setId("tag:example.org,2007:/fooresource("http://alexharden.org/blog/atom.xml");
AtomFeed feed = feedresource.setTitle("Test Feed");
feed.setSubtitle("Feed subtitle");
feed.setUpdated(new Dateaccept(MediaType.APPLICATION_ATOM_XML).get(AtomFeed.class);
System.out.println(feed.getTitle().getValue());
for (AtomEntry entry : feed.addAuthorgetEntries("Shiva HR");)) {
feedSystem.out.addLinkprintln("\t" + entry.getTitle().getValue());
}
|
|
...
2) a) Producing Atom Documents
The following code example demonstrates the production of Atom documents using Apache Abdera.
Apache Abdera - Click on links to Download - ProduceAtomUsingAbdera.java ProduceAtomUsingAbdera_web.xml |
---|
Code Block |
---|
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Abdera abdera = new Abdera();
Feed feed = abdera.newFeed();
feed.setId("tag:example.org,2007:/foo");http://example.com");
feed.addLink("http://example.com/foo", "self");
Entry entry = feed.addEntry();
entry.setId("tag:example.org,2007:/foo/entries/1");
entry.setTitle("Entry title");
entry.setSummaryAsHtml("<p>This is the entry title</p>");
entry.setUpdated(new Date());
entry.setPublished(new Date());
entry.addLink("http://example.com/foo/entries/1");
feed.getDocumentsetTitle("Test Feed").writeTo(response.getWriter());
} | Code Block |
---|
protected void doGet(HttpServletRequest request, HttpServletResponse response);
feed.setSubtitle("Feed subtitle");
feed.setUpdated(new Date());
throws ServletException, IOException {
feed.addAuthor("Shiva HR");
AtomFeed feed = new AtomFeed(.addLink("http://example.com");
feed.setIdaddLink("taghttp://example.org,2007:com/foo", "self");
Entry entry = feed.setTitleaddEntry(new AtomText("Test Feed")));
entry.setId("tag:example.org,2007:/foo/entries/1");
feedentry.setSubtitle(new AtomText("Feed subtitle"))setTitle("Entry title");
entry.setSummaryAsHtml("<p>This is the entry title</p>");
feedentry.setUpdated(new Date());
AtomPerson person = entry.setPublished(new AtomPersonDate());
personentry.setNameaddLink("Shiva HRhttp://example.com/foo/entries/1");
feed.getAuthorsgetDocument().addwriteTo(response.getWriter(person);
AtomLink link1 = new AtomLink();
} |
|
The following code example demonstrates the production of Atom documents using Apache Wink.
Apache Wink - Click on links to Download - ProduceAtomUsingWink.java ProduceAtomUsingWink_web.xml |
---|
Code Block |
---|
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { link1.setHref("http://example.com");
feed.getLinks().add(link1);
AtomLinkAtomFeed link2feed = new AtomLinkAtomFeed();
link2feed.setHrefsetId("httptag://example.comorg,2007:/foo");
link2.setRelfeed.setTitle(new AtomText("selfTest Feed"));
feed.getLinks().add(link2setSubtitle(new AtomText("Feed subtitle"));
feed.setUpdated(new Date());
AtomEntryAtomPerson entryperson = new AtomEntryAtomPerson();
entryperson.setId("tag:example.org,2007:/foo/entries/1setName("Shiva HR");
entryfeed.setTitle(new AtomText("Entry title")getAuthors().add(person);
AtomTextAtomLink summarylink1 = new AtomTextAtomLink();
summarylink1.setType(AtomTextType.htmlsetHref("http://example.com");
summary.setValue("<p>This is the entry title</p>");
entry.setSummary(summary);
feed.getLinks().add(link1);
AtomLink link2 = new AtomLink();
entrylink2.setUpdated(new Date())setHref("http://example.com/foo");
link2.setRel("self");
entryfeed.setPublished(new Date()getLinks().add(link2);
AtomLinkAtomEntry link3entry = new AtomLinkAtomEntry();
link3entry.setHrefsetId("httptag://example.comorg,2007:/foo/entries/1");
entry.getLinks().add(link3setTitle(new AtomText("Entry title"));
feed.getEntries().add(entry);
AtomText summary = new AtomText();
AtomFeedsummary.marshal(feed, response.getOutputStream()setType(AtomTextType.html);
}
|
|
ProduceAtomUsingAbdera.java ProduceAtomUsingAbdera_web.xml | ProduceAtomUsingWink.java ProduceAtomUsingWink_web.xml |
2) b) A more elegant way of producing Atom documents using Apache Wink (the JAX-RS way):
- Create a Dynamic Web Project
- Add Wink & its dependent JARs into Java Build Path and Java EE Module Dependencies.
- Create a POJO class and a method that creates Atom feed document. Annotate the class & its methods with required JAX-RS annotations as below:
ProduceAtom.java - Add org.apache.wink.server.internal.servlet.RestServlet into web.xml and specify the path of above Resource class in it's init-param.
See ProduceAtomWinkElegant_web.xml and application - Deploy the web-application and access it using the url http://localhost:8080/ProduceAtom_Wink_Elegant/rest/getAtom
Final WAR -> ProduceAtom_Wink_Elegant.zip (add Wink & its dependent JARs under ProduceAtom_Wink_Elegant\WEB-INF\lib and re-zip it as WAR).
summary.setValue("<p>This is the entry title</p>");
entry.setSummary(summary);
entry.setUpdated(new Date());
entry.setPublished(new Date());
AtomLink link3 = new AtomLink();
link3.setHref("http://example.com/foo/entries/1");
entry.getLinks().add(link3);
feed.getEntries().add(entry);
AtomFeed.marshal(feed, response.getOutputStream());
}
|
|
2) b) Producing Atom Documents - the JAX-RS way
A more elegant way of producing Atom documents using Apache Wink is the JAX-RS way as described below:
- Open the Eclipse development environment and create a "Dynamic Web Project".
- Add Apache Wink & its dependent JARs under Java EE Module Dependencies.
- Create a POJO class and a method that creates Atom feed document. Annotate the class & its methods with the required JAX-RS annotations as below:
ProduceAtom.java - Add org.apache.wink.server.internal.servlet.RestServlet into web.xml and specify the path of above Resource class in it's init-param.
See ProduceAtomWinkElegant_web.xml and application - Deploy the web-application and access it using the url http://localhost:8080/ProduceAtom_Wink_Elegant/rest/getAtom
- Final WAR -> ProduceAtom_Wink_Elegant.zip (add Wink & its dependent JARs under ProduceAtom_Wink_Elegant\WEB-INF\lib and re-zip it as WAR).
...
The following code example demonstrates the consuming of RSS documents using Apache Abdera.
Apache Abdera - Click on link to Download - ConsumeRssUsingAbdera.java |
---|
Code Block |
---|
public static void main(String[] args) throws ParseException, IOException {
System.out.println("Consuming RSS Documents using Abdera...\n");
Abdera |
|
Apache Abdera
| Apache Wink
|
---|
Code Block |
---|
public static void main(String[] args) throws ParseException, IOException {
System.out.println("Consuming RSS Documents using Abdera...\n");
Abdera abdera = new Abdera();
Parser parser = abdera.getParser();
URL url = new URL("http://www.rssboard.org/files/sample-rss-2.xml");
Document<RssFeed> doc = parser.parse(url.openStream());
RssFeed rssFeed = doc.getRoot();
System.out.println("Title: " + rssFeed.getTitle());
System.out.println("Description: " + rssFeed.getSubtitle() + "\n");
int itemCount = 0;
for (Entry entry : rssFeed.getEntries()) {
System.out.println("Item " + ++itemCount + ":");
System.out.println("\tTitle: " + entry.getTitle());
new URL("http://www.rssboard.org/files/sample-rss-2.xml");
Document<RssFeed> doc = parser.parse(url.openStream());
RssFeed rssFeed = doc.getRoot();
System.out.println("Title: " + rssFeed.getTitle());
System.out.println("Description: " + rssFeed.getSubtitle() + "\n");
int itemCount = 0;
for (Entry entry : rssFeed.getEntries()) {
System.out.println("Item " + ++itemCount + ":");
System.out.println("\tTitle: " + entry.getTitle());
System.out.println("\tPublish Date: " + entry.getPublished());
System.out.println("\tDescription: " + entry.getContent());
}
} |
|
The following code example demonstrates the consuming of RSS documents using Apache Wink.
Apache Wink - Click on link to Download - ConsumeRssUsingWink.java |
---|
Code Block |
---|
public static void main(String[] args) {
System.out.println("Consuming RSS Documents using Apache Wink...\n");
RestClient client = new RestClient();
String url = "http://www.rssboard.org/files/sample-rss-2.xml";
Resource resource = client.resource(url);
RssFeed rss = resource.accept(MediaType.APPLICATION_XML).get(RssFeed.class);
RssChannel channel = rss.getChannel();
System.out.println("Title: " + channel.getTitle());
System.out.println("Description: " + channel.getDescription() + "\n");
int itemCount = 0;
for (RssItem item : channel.getItems()) {
System.out.println("Item " + ++itemCount + ":");
System.out.println("\tTitle: " + item.getTitle());
System.out.println("\tPublish Date: " + item.getPubDate());
System.out.println("\tDescription: " + item.getDescription());
}
}
|
|
...
Apache Abdera
Apache Abdera version 0.4 does not support RSS write.
Apache Wink
Same as in 2) b) Producing Atom Documents - the JAX-RS way. However the resource method now returns an RssFeed object instead of AtomFeed object.
Apache Wink - Click on link to Download - ProduceRss_Wink_Elegant.zip |
---|
Code Block |
---|
@Path("/getRss")
public class ProduceRss {
@GET
@Produces(MediaType.APPLICATION_XML)
public Rss getRss() {
RssFeed rss = new RssFeed();
RssChannel channel = new RssChannel();
channel.setTitle("Liftoff News");
channel.setLink("http://liftoff.msfc.nasa.gov");
channel.setDescription("Liftoff to Space Exploration.");
channel.setPubDate(new Date().toString());
RssItem item = new RssItem();
item.setTitle("Star City");
item.setLink("http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp");
item.setDescription("How do Americans get ready to work with Russians aboard the International Space Station?");
channel.getItem().add(item);
rss.setChannel(channel);
return rss;
}
}
|
|
...
5) Writing Atom Publishing Protocol (APP) Server
The following steps explain how to implement an APP server as described in the following beautiful article by James Snell: http://www.ibm.com/developerworks/library/x-atompp1/
Apache Abdera
- Open the Eclipse development environment and create a "Dynamic Web Project".
- Add Apache Abdera & its dependent JARs under Java EE Module Dependencies.
- Add the following CollectionAdapter and Provider classes under src/myPackage directory: APP_CollectionAdapter.java APP_ContentProvider.java
- Add org.apache.abdera.protocol.server.servlet.AbderaServlet into web.xml and point the following init paramters to the classes added above.
org.apache.abdera.protocol.server.Provider
org.apache.abdera.protocol.server.CollectionAdapter
APP_Server_Abdera_web.xml - Add the following index.jsp which has help on how to perform the APP operations: APP_Server_Abdera_index.jsp
- Deploy and run the application.
Final WAR -> APP_Server_Abdera.zip (add Apache Abdera & its dependent JARs under APP_Server_Abdera\WEB-INF\lib and re-zip it as WAR).
Apache Wink
- Open the Eclipse development environment and create a "Dynamic Web Project".
- Add Apache Wink & its dependent JARs under Java EE Module Dependencies.
- Add the following Resource class under src/myPackage directory: EntriesCollection.java
- Add org.apache.wink.server.internal.servlet.RestServlet into web.xml and specify the path of above Resource class in it's init-param. APP_Server_Wink_web.xml APP_Server_Wink_application
- Add the following index.jsp which has help on how to perform the APP operations: APP_Server_Wink_index.jsp
- Deploy and run the application.
Final WAR -> APP_Server_Wink.zip (add Apache Wink & its dependent JARs under APP_Server_Wink\WEB-INF\lib and re-zip it as WAR)
References
...
6) Writing Atom Publishing Protocol (APP) Client
In order to write an Atom Publishing Protocol client refer to the following examples.
Info |
---|
|
Make sure that the APP_Server_Abdera.war and the APP_Server_Wink.war provided in the previous example are deployed before running these examples. |
Apache Abdera - Click on link to Download - APP_Client_Abdera.java
|
---|
1. Acessing Service Document:
Code Block |
---|
Document<Service> introspection = abderaClient.get(SERVICE_URL).getDocument();
Service service = introspection.getRoot();
List<Workspace> workspaces = service.getWorkspaces();
for (Workspace workspace : workspaces) {
System.out.println("\tPublish Date: t" + entryworkspace.getPublishedgetTitle());
List<Collection> System.out.println("\tDescription: " + entry.getContent()collections = workspace.getCollections();
}
} | Code Block |
---|
public static void main(String[] argsfor (Collection collection : collections) {
System.out.println("\t"Consuming RSS Documents using Apache Wink...\n");
RestClient client = new RestClient();
String url = "http://www.rssboard.org/files/sample-rss-2.xml";
Resource resource = client.resource(url);
RssFeed rss = resource.accept(MediaType.APPLICATION_XML).get(RssFeed.class);
RssChannel channel = rss.getChannel();
System.out.println("Title: " + channel.getTitle());
System.out.println("Description: " + channel.getDescription() + "\n");
int itemCount = 0;
for (RssItem item : channel.getItems()) {
System.out.println("Item " + ++itemCount + ":");
System.out.println("\tTitle: " + item.getTitle());
System.out.println("\tPublish Date: " + item.getPubDate());
System.out.println("\tDescription: " + item.getDescription());
}
}
|
|
ConsumeRssUsingAbdera.java | ConsumeRssUsingWink.java |
+ collection.getTitle() + "\t:\t" + collection.getHref());
}
System.out.print("\n");
}
|
|
2. Getting a Feed Code Block |
---|
RequestOptions opts = new RequestOptions();
opts.setContentType("application/atom+xml;type=feed");
ClientResponse response = abderaClient.get(FEED_URL, opts);
Feed feed = (Feed)response.getDocument().getRoot();
|
|
3. Posting an entry to a Feed
Code Block |
---|
RequestOptions opts = new RequestOptions();
opts.setContentType("application/atom+xml;type=entry");
ClientResponse response = abderaClient.post(FEED_URL, newEntry, opts);
|
|
4. Putting a change to an Entry
Code Block |
---|
RequestOptions opts = new RequestOptions();
opts.setContentType("application/atom+xml;type=entry");
ClientResponse response = abderaClient.put(ENTRY_URL, changedEntry.getDocument(), opts);
|
|
5. Getting an Entry
Code Block |
---|
RequestOptions opts = new RequestOptions();
opts.setContentType("application/atom+xml;type=entry");
ClientResponse response = abderaClient.get(ENTRY_URL, opts);
Entry entry = (Entry)response.getDocument().getRoot();
|
|
6. Deleting an Entry
Code Block |
---|
ClientResponse response = abderaClient.delete(ENTRY_URL);
|
|
Apache Wink - Click on link to Download - APP_Client_Wink.java
|
---|
1. Acessing Service Document:
Code Block |
---|
Resource resource = restClient.resource(SERVICE_URL);
AppService service = resource.accept(MediaTypeUtils.ATOM_SERVICE_DOCUMENT).get(AppService.class);
List<AppWorkspace> workspaces = service.getWorkspace();
for (AppWorkspace workspace : workspaces) {
System.out.println("\t" + workspace.getTitle().getValue());
List<AppCollection> collections = workspace.getCollection |
|
Apache Abdera
| Apache Wink
|
---|
As of v0.4 Abdera has no support for RSS write.
| Same as in 2)a) or 2)b). The resource method now returns an RssFeed object instead of AtomFeed object.
Code Block |
---|
@Path("/getRss")
public class ProduceRss {
@GET
@Produces(MediaType.APPLICATION_XML)
public Rss getRss() {
RssFeed rss = new RssFeed();
RssChannel channel = new RssChannel();
for (AppCollection channel.setTitle("Liftoff News");collection : collections) {
channelSystem.out.setLinkprintln("http://liftoff.msfc.nasa.gov");
\t" + collection.getTitle().getValue()
channel.setDescription("Liftoff to Space Exploration.");
+ "\t:\t"
channel.setPubDate(new Date().toString+ collection.getHref());
}
RssItem item = new RssItem();
item.setTitle("Star City");
item.setLink("http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp");
item.setDescription("How do Americans get ready to work with Russians aboard the International Space Station?");
channel.getItem().add(item);
rss.setChannel(channel);
return rss;
}
} |
|
| ProduceRss_Wink_Elegant.zip |
5) Writing Atom Publishing Protocol (APP) Server:
...
System.out.print("\n");
}
|
|
2. Getting a Feed
Code Block |
---|
Resource feedResource = restClient.resource(FEED_URL);
AtomFeed feed = feedResource.accept(MediaType.APPLICATION_ATOM_XML).get(AtomFeed.class);
|
|
3. Posting an entry to a Feed
Code Block |
---|
Resource feedResource = restClient.resource(FEED_URL);
ClientResponse response =
feedResource.contentType(MediaType.APPLICATION_ATOM_XML).post(newEntry);
|
|
4. Putting a change to an Entry
Code Block |
---|
Resource feedResource = restClient.resource(ENTRY_URL);
ClientResponse response =
feedResource.contentType(MediaType.APPLICATION_ATOM_XML).put(changedEntry);
|
|
5. Getting an Entry
Code Block |
---|
Resource feedResource = restClient.resource(ENTRY_URL);
AtomEntry atomEntry = feedResource.accept(MediaType.APPLICATION_ATOM_XML).get(AtomEntry.class);
|
|
6. Deleting an Entry
Code Block |
---|
Resource feedResource = restClient.resource(ENTRY_URL);
ClientResponse response = feedResource.delete();
|
|
...