From fdd5b43d3c139cf2cbd1655d2efbfaf9032a5b5e Mon Sep 17 00:00:00 2001 From: lresende Date: Wed, 11 Nov 2009 23:14:18 +0000 Subject: Moving 1.x branches git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@835145 13f79535-47bb-0310-9956-ffa450edef68 --- .../sca-java-1.5.1/samples/feed-aggregator/README | 110 +++++++ .../samples/feed-aggregator/build.xml | 72 ++++ .../samples/feed-aggregator/feed-aggregator.png | Bin 0 -> 17429 bytes .../samples/feed-aggregator/feed-aggregator.svg | 364 +++++++++++++++++++++ .../sca-java-1.5.1/samples/feed-aggregator/pom.xml | 84 +++++ .../src/main/java/feed/AggregatorImpl.java | 199 +++++++++++ .../feed-aggregator/src/main/java/feed/Sort.java | 38 +++ .../src/main/java/feed/SortImpl.java | 57 ++++ .../src/main/java/launch/LaunchFeedServer.java | 47 +++ .../src/main/resources/FeedAggregator.composite | 61 ++++ .../src/test/java/feed/FeedAggregatorTestCase.java | 358 ++++++++++++++++++++ 11 files changed, 1390 insertions(+) create mode 100644 sca-java-1.x/branches/sca-java-1.5.1/samples/feed-aggregator/README create mode 100644 sca-java-1.x/branches/sca-java-1.5.1/samples/feed-aggregator/build.xml create mode 100644 sca-java-1.x/branches/sca-java-1.5.1/samples/feed-aggregator/feed-aggregator.png create mode 100644 sca-java-1.x/branches/sca-java-1.5.1/samples/feed-aggregator/feed-aggregator.svg create mode 100644 sca-java-1.x/branches/sca-java-1.5.1/samples/feed-aggregator/pom.xml create mode 100644 sca-java-1.x/branches/sca-java-1.5.1/samples/feed-aggregator/src/main/java/feed/AggregatorImpl.java create mode 100644 sca-java-1.x/branches/sca-java-1.5.1/samples/feed-aggregator/src/main/java/feed/Sort.java create mode 100644 sca-java-1.x/branches/sca-java-1.5.1/samples/feed-aggregator/src/main/java/feed/SortImpl.java create mode 100644 sca-java-1.x/branches/sca-java-1.5.1/samples/feed-aggregator/src/main/java/launch/LaunchFeedServer.java create mode 100644 sca-java-1.x/branches/sca-java-1.5.1/samples/feed-aggregator/src/main/resources/FeedAggregator.composite create mode 100644 sca-java-1.x/branches/sca-java-1.5.1/samples/feed-aggregator/src/test/java/feed/FeedAggregatorTestCase.java (limited to 'sca-java-1.x/branches/sca-java-1.5.1/samples/feed-aggregator') diff --git a/sca-java-1.x/branches/sca-java-1.5.1/samples/feed-aggregator/README b/sca-java-1.x/branches/sca-java-1.5.1/samples/feed-aggregator/README new file mode 100644 index 0000000000..8094bbf066 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.5.1/samples/feed-aggregator/README @@ -0,0 +1,110 @@ +Feed Aggregator Sample +====================================== +This sample demonstrates using the Feed binding to aggregate ATOM and RSS +feeds and publish a new aggregated feed. + +The README in the samples directory (the directory above this) provides +general instructions about building and running samples. Take a look there +first. + +If you run the sample using ant, navigate to this sample directory and do: + +ant run + +OR if you don't have ant, on Windows do + +java -cp ..\..\lib\tuscany-sca-manifest.jar;target\sample-feed-aggregator.jar launch.LaunchFeedServer + +and on *nix do + +java -cp ../../lib/tuscany-sca-manifest.jar:target/sample-feed-aggregator.jar launch.LaunchFeedServer + +You should see the following output on the screen. + +run: + [java] Added Servlet mapping: http://localhost:8083/rssAggregator + [java] Added Servlet mapping: http://localhost:8083/atomAggregator/* + [java] Sample Feed server started (press enter to shutdown) + + [java] To read the aggregated feeds, point your Web browser to the following addresses: + [java] http://localhost:8083/atomAggregator + [java] http://localhost:8083/atomAggregator/atomsvc (for the Atom service document) + [java] http://localhost:8083/rssAggregator + +As this point the aggregated Feeds are exposed by a web server started +automatically by the SCA runtime. You can later stop the server by pressing enter. + +Now that the server is started you can point your Web browser to each URL of the +aggregated feeds to see the information in your browser. These URLs are: + +http://localhost:8083/atomAggregator +http://localhost:8083/atomAggregator/atomsvc +http://localhost:8083/rssAggregator + +** Please note that if your browser is not configured correctly to receive +feed information, you will be prompted to open each file that contains the feed +information in XML. + +Sample Overview +--------------- +The sample provides a single component exposing a Web resource. + +web-resource/ + src/ + main/ + java/ + feed/ + AggregatorImpl.java - implementation of the Feed + aggregator component + Sort.java - utility interface + SortImpl.java - implementation of the Sort component + launch + LaunchFeedServer.java - starts the SCA Runtime and + publishes the aggregated feeds + resources/ + FeedAggregator.composite - the SCA assembly for this sample + feed-aggregator.png - a pictorial representation of the + sample .composite file + build.xml - the Ant build file + pom.xml - the Maven build file + + +Details of how this sample works +-------------------------------- +1. AggregatorImpl is used to implement the two main components here (RSSAggregator & AtomAggregator). +2. AggregatorImpl directly implements the Tuscany Atom Collection interface. It is a component + that provides an Atom feed to respond (see tuscany/modules/binding-atom-abdera) +3. The AggregatorImpl component is not explicitly configured using nested element nor its + implementation class is annotated with @Service annotation. Instead, the Tuscany Atom Collection interface + defines its services using @Remotable annotation. + +Building And Running The Sample Using Ant +----------------------------------------- +With the binary distribution the sample can be built and run using Ant using the +following commands + +cd feed-aggregator +ant compile +ant run + +You should see the following output from the run target. + +run: + [java] Added Servlet mapping: http://localhost:8083/rssAggregator + [java] Added Servlet mapping: http://localhost:8083/atomAggregator/* + [java] Sample Feed server started (press enter to shutdown) + + [java] To read the aggregated feeds, point your Web browser to the following addresses: + [java] http://localhost:8083/atomAggregator + [java] http://localhost:8083/atomAggregator/atomsvc (for the Atom service document) + [java] http://localhost:8083/rssAggregator + + +Building The Sample Using Maven +------------------------------------------- +With either the binary or source distributions the sample can be built using +Maven as follows. + +cd feed-aggregator +mvn + diff --git a/sca-java-1.x/branches/sca-java-1.5.1/samples/feed-aggregator/build.xml b/sca-java-1.x/branches/sca-java-1.5.1/samples/feed-aggregator/build.xml new file mode 100644 index 0000000000..0ab6fed7fc --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.5.1/samples/feed-aggregator/build.xml @@ -0,0 +1,72 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sca-java-1.x/branches/sca-java-1.5.1/samples/feed-aggregator/feed-aggregator.png b/sca-java-1.x/branches/sca-java-1.5.1/samples/feed-aggregator/feed-aggregator.png new file mode 100644 index 0000000000..c8fea8a7b8 Binary files /dev/null and b/sca-java-1.x/branches/sca-java-1.5.1/samples/feed-aggregator/feed-aggregator.png differ diff --git a/sca-java-1.x/branches/sca-java-1.5.1/samples/feed-aggregator/feed-aggregator.svg b/sca-java-1.x/branches/sca-java-1.5.1/samples/feed-aggregator/feed-aggregator.svg new file mode 100644 index 0000000000..72fe6a00ac --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.5.1/samples/feed-aggregator/feed-aggregator.svg @@ -0,0 +1,364 @@ + + + + + + + + + + + + + image/svg+xml + + + + + + + + FeedAggregator + Sort + + newFirst = true + + RssAggregator + + + + + AtomAggregator + + + Rss Sample + Atom Sample + + + + + + + + + + Engadget + BBC + Oreilly + Apache + + + RSS Atom + diff --git a/sca-java-1.x/branches/sca-java-1.5.1/samples/feed-aggregator/pom.xml b/sca-java-1.x/branches/sca-java-1.5.1/samples/feed-aggregator/pom.xml new file mode 100644 index 0000000000..9c4b2bd3e6 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.5.1/samples/feed-aggregator/pom.xml @@ -0,0 +1,84 @@ + + + + 4.0.0 + + org.apache.tuscany.sca + tuscany-sca + 1.5.1-SNAPSHOT + ../pom.xml + + sample-feed-aggregator + Apache Tuscany SCA Sample Atom+RSS Feed Aggregator + + + + apache.incubator + http://people.apache.org/repo/m2-incubating-repository + + + + + + org.apache.tuscany.sca + tuscany-host-embedded + 1.5.1-SNAPSHOT + + + + org.apache.tuscany.sca + tuscany-binding-rss-rome + 1.5.1-SNAPSHOT + + + + org.apache.tuscany.sca + tuscany-binding-atom-abdera + 1.5.1-SNAPSHOT + + + + org.apache.tuscany.sca + tuscany-implementation-java-runtime + 1.5.1-SNAPSHOT + runtime + + + + org.apache.tuscany.sca + tuscany-host-tomcat + 1.5.1-SNAPSHOT + runtime + + + + junit + junit + 4.5 + test + + + + + + ${artifactId} + + + diff --git a/sca-java-1.x/branches/sca-java-1.5.1/samples/feed-aggregator/src/main/java/feed/AggregatorImpl.java b/sca-java-1.x/branches/sca-java-1.5.1/samples/feed-aggregator/src/main/java/feed/AggregatorImpl.java new file mode 100644 index 0000000000..7b2edb2e6a --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.5.1/samples/feed-aggregator/src/main/java/feed/AggregatorImpl.java @@ -0,0 +1,199 @@ +/* + * 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 feed; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.OutputStreamWriter; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import org.apache.abdera.Abdera; +import org.apache.abdera.factory.Factory; +import org.apache.abdera.model.Document; +import org.apache.abdera.model.Entry; +import org.apache.abdera.model.Feed; +import org.apache.abdera.model.Person; +import org.apache.abdera.parser.Parser; +import org.apache.tuscany.sca.binding.atom.collection.Collection; +import org.apache.tuscany.sca.binding.atom.collection.NotFoundException; +import org.osoa.sca.annotations.Property; +import org.osoa.sca.annotations.Reference; + +import com.sun.syndication.feed.synd.SyndFeed; +import com.sun.syndication.io.SyndFeedOutput; + +/** + * Implementation of an SCA component that aggregates several + * Atom and RSS feeds. + * + * @version $Rev$ $Date$ + */ +public class AggregatorImpl implements org.apache.tuscany.sca.binding.atom.collection.Collection { + + @Reference(required = false) + public Collection atomFeed1; + @Reference(required = false) + public Collection atomFeed2; + + @Reference(required = false) + public org.apache.tuscany.sca.binding.rss.collection.Collection rssFeed1; + @Reference(required = false) + public org.apache.tuscany.sca.binding.rss.collection.Collection rssFeed2; + + @Reference(required = false) + public Sort sort; + + @Property + public String feedTitle = "Aggregated Feed"; + @Property + public String feedDescription = "Anonymous Aggregated Feed"; + @Property + public String feedAuthor = "anonymous"; + + public Feed getFeed() { + + // Create a new Feed + Factory factory = Abdera.getNewFactory(); + Feed feed = factory.newFeed(); + feed.setTitle(feedTitle); + feed.setSubtitle(feedDescription); + Person author = factory.newAuthor(); + author.setName(feedAuthor); + feed.addAuthor(author); + feed.addLink("http://tuscany.apache.org/", "alternate"); + + // Aggregate entries from atomFeed1, atomFeed2, rssFeed1 and rssFeed2 + List entries = new ArrayList(); + if (atomFeed1 != null) { + try { + entries.addAll(atomFeed1.getFeed().getEntries()); + } catch (Exception e) { + int x = 0; + } + } + if (atomFeed2 != null) { + try { + entries.addAll(atomFeed2.getFeed().getEntries()); + } catch (Exception e) { + int x = 0; + + } + } + if (rssFeed1 != null) { + try { + entries.addAll(atomFeed(rssFeed1.getFeed()).getEntries()); + } catch (Exception e) { + int x = 0; + + } + } + if (rssFeed2 != null) { + try { + entries.addAll(atomFeed(rssFeed2.getFeed()).getEntries()); + } catch (Exception e) { + int x = 0; + + } + } + + // Sort entries by updated date + if (sort != null) { + entries = sort.sort(entries); + } + + // Add the entries to the new feed + // Also synthesize a feed id and updated field base on entries + Date feedUpdated = new Date( 0 ); + for (Entry entry: entries) { + Date entryUpdated = entry.getUpdated(); + if (( entryUpdated != null ) && ( entryUpdated.compareTo( feedUpdated ) > 0 )) { + feedUpdated = entryUpdated; + } + feed.addEntry(entry); + } + feed.setUpdated( feedUpdated ); + // Note that feed id should be permanent, immutable, and unique + // in order to support proper ETag creation. + // Tough to do when the feed is regenerated with each get. + feed.setId( "http://tuscany.apache.org/feed", true ); + + return feed; + } + + public Feed query(String queryString) { + Factory factory = Abdera.getNewFactory(); + Feed feed = factory.newFeed(); + feed.setTitle(feedTitle); + feed.setSubtitle(feedDescription); + Person author = factory.newAuthor(); + author.setName(feedAuthor); + feed.addAuthor(author); + feed.addLink("http://tuscany.apache.org", "alternate"); + + Feed allFeed = getFeed(); + if (queryString.startsWith("title=")) { + String title = queryString.substring(6); + + for (Entry entry: allFeed.getEntries()) { + if (entry.getTitle().contains(title)) { + feed.addEntry(entry); + } + } + } + return feed; + } + + public void delete(String id) throws NotFoundException { + } + + public Entry get(String id) throws NotFoundException { + return null; + } + + public Entry post(Entry entry) { + return null; + } + + public void put(String id, Entry entry) throws NotFoundException { + } + + /** + * Convert a ROME feed to an Abdera feed. + * + * @param romeFeed + * @return + */ + private static Feed atomFeed(SyndFeed syndFeed) { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + + syndFeed.setFeedType("atom_1.0"); + SyndFeedOutput syndOutput = new SyndFeedOutput(); + try { + syndOutput.output(syndFeed, new OutputStreamWriter(bos)); + } catch (Exception e) { + throw new RuntimeException(e); + } + Parser parser = Abdera.getNewParser(); + Document document = parser.parse(new ByteArrayInputStream(bos.toByteArray())); + + return document.getRoot(); + } +} diff --git a/sca-java-1.x/branches/sca-java-1.5.1/samples/feed-aggregator/src/main/java/feed/Sort.java b/sca-java-1.x/branches/sca-java-1.5.1/samples/feed-aggregator/src/main/java/feed/Sort.java new file mode 100644 index 0000000000..dec073c36b --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.5.1/samples/feed-aggregator/src/main/java/feed/Sort.java @@ -0,0 +1,38 @@ +/* + * 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 feed; + +import java.util.List; + +import org.apache.abdera.model.Entry; + +/** + * The Sort service business interface. + * + * @version $Rev$ $Date$ + */ +public interface Sort { + + /** + * Sort feed entries by published date. + * @param entries + * @return + */ + List sort(List entries); +} diff --git a/sca-java-1.x/branches/sca-java-1.5.1/samples/feed-aggregator/src/main/java/feed/SortImpl.java b/sca-java-1.x/branches/sca-java-1.5.1/samples/feed-aggregator/src/main/java/feed/SortImpl.java new file mode 100644 index 0000000000..f7ead126a7 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.5.1/samples/feed-aggregator/src/main/java/feed/SortImpl.java @@ -0,0 +1,57 @@ +/* + * 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 feed; + +import java.util.Arrays; +import java.util.Comparator; +import java.util.Date; +import java.util.List; + +import org.apache.abdera.model.Entry; +import org.osoa.sca.annotations.Property; + +/** + * Implementation of a Feed Sort service component. + * + * @version $Rev$ $Date$ + */ +public class SortImpl implements Sort { + + @Property + public boolean newFirst = true; + + @SuppressWarnings("unchecked") + public List sort(List entries) { + Entry[] entriesArray = new Entry[entries.size()]; + entriesArray = (Entry[])entries.toArray(entriesArray); + Arrays.sort(entriesArray, new Comparator() { + public int compare(final Object xObj, final Object yObj) { + Date xDate = ((Entry)xObj).getUpdated(); + Date yDate = ((Entry)yObj).getUpdated(); + if (xDate == null) + return -1; + if (newFirst) + return yDate.compareTo(xDate); + else + return xDate.compareTo(yDate); + } + }); + return Arrays.asList(entriesArray); + } +} diff --git a/sca-java-1.x/branches/sca-java-1.5.1/samples/feed-aggregator/src/main/java/launch/LaunchFeedServer.java b/sca-java-1.x/branches/sca-java-1.5.1/samples/feed-aggregator/src/main/java/launch/LaunchFeedServer.java new file mode 100644 index 0000000000..1c549f7cd2 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.5.1/samples/feed-aggregator/src/main/java/launch/LaunchFeedServer.java @@ -0,0 +1,47 @@ +/* + * 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 launch; + +import java.io.IOException; + +import org.apache.tuscany.sca.host.embedded.SCADomain; + +public class LaunchFeedServer { + public static void main(String[] args) throws Exception { + SCADomain scaDomain = SCADomain.newInstance("FeedAggregator.composite"); + + try { + System.out.println("Sample Feed server started (press enter to shutdown)"); + System.out.println(); + System.out.println("To read the aggregated feeds, point your Web browser to the following addresses:"); + System.out.println("http://localhost:8083/atomAggregator"); + System.out.println("http://localhost:8083/atomAggregator/atomsvc (for the Atom service document)"); + System.out.println("http://localhost:8083/rssAggregator"); + System.out.println(); + System.in.read(); + } catch (IOException e) { + e.printStackTrace(); + } + + + scaDomain.close(); + System.out.println("Sample Feed server stopped"); + } +} diff --git a/sca-java-1.x/branches/sca-java-1.5.1/samples/feed-aggregator/src/main/resources/FeedAggregator.composite b/sca-java-1.x/branches/sca-java-1.5.1/samples/feed-aggregator/src/main/resources/FeedAggregator.composite new file mode 100644 index 0000000000..018effc298 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.5.1/samples/feed-aggregator/src/main/resources/FeedAggregator.composite @@ -0,0 +1,61 @@ + + + + + + + + + + + + + + + + + + + + + RSS Aggregator Sample + + + + + true + + + + + + + + + + + + Atom Aggregator Sample + + + diff --git a/sca-java-1.x/branches/sca-java-1.5.1/samples/feed-aggregator/src/test/java/feed/FeedAggregatorTestCase.java b/sca-java-1.x/branches/sca-java-1.5.1/samples/feed-aggregator/src/test/java/feed/FeedAggregatorTestCase.java new file mode 100644 index 0000000000..7eb52d95f1 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.5.1/samples/feed-aggregator/src/test/java/feed/FeedAggregatorTestCase.java @@ -0,0 +1,358 @@ +/* + * 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 feed; + +import java.io.IOException; +import java.io.Reader; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Date; +import java.util.List; + +import junit.framework.Assert; + +import org.apache.abdera.Abdera; +import org.apache.abdera.model.Base; +import org.apache.abdera.model.Collection; +import org.apache.abdera.model.Document; +import org.apache.abdera.model.Entry; +import org.apache.abdera.model.Feed; +import org.apache.abdera.parser.Parser; +import org.apache.abdera.protocol.Response.ResponseType; +import org.apache.abdera.protocol.client.AbderaClient; +import org.apache.abdera.protocol.client.ClientResponse; +import org.apache.abdera.protocol.client.RequestOptions; +import org.apache.abdera.writer.Writer; +import org.apache.abdera.writer.WriterFactory; +import org.apache.tuscany.sca.host.embedded.SCADomain; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * Tests use of server provided feed entity tags for Atom binding in Tuscany. + * Tests conditional gets (e.g. get if-none-match) or conditional posts (post + * if-match) using entity tags and last modified entries in headers. Uses the + * SCA provided Provider composite to act as a server. Uses the Abdera provided + * Client to act as a client. + */ +public class FeedAggregatorTestCase { + public final static String providerURI = "http://localhost:8083/atomAggregator"; + protected static SCADomain scaProviderDomain; + protected static Abdera abdera; + protected static AbderaClient client; + protected static Parser abderaParser; + protected static String eTag; + protected static Date lastModified; + protected static long contentLength; + protected static int numberEntries; + protected static final SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z"); // RFC822 date time + + @BeforeClass + public static void init() throws Exception { + try { + System.out.println(">>>FeedAggregatorTest.init"); + scaProviderDomain = SCADomain.newInstance("FeedAggregator.composite"); + abdera = new Abdera(); + client = new AbderaClient(abdera); + abderaParser = Abdera.getNewParser(); + } catch (Throwable e) { + System.out.println(e); + e.printStackTrace(); + } + } + + @AfterClass + public static void destroy() throws Exception { + System.out.println(">>>FeedAggregatorTest.destroy"); + scaProviderDomain.close(); + } + + @Test + public void testPrelim() throws Exception { + Assert.assertNotNull(scaProviderDomain); + Assert.assertNotNull(client); + } + + @Test + public void testFeedBasics() throws Exception { + System.out.println(">>>FeedAggregatorTest.testFeedBasics"); + RequestOptions opts = new RequestOptions(); + // Normal feed request + ClientResponse res = client.get(providerURI, opts); + Assert.assertNotNull(res); + try { + // Assert feed provided since no predicates + Assert.assertEquals(200, res.getStatus()); + Assert.assertEquals(ResponseType.SUCCESS, res.getType()); + // AtomTestCaseUtils.printResponseHeaders( "Feed response headers:", + // " ", res ); + + // Perform other tests on feed. + contentLength = getContentLength(res); + System.out.println("FeedAggregatorTest.testFeedBasics full contentLength=" + contentLength); + + Document doc = res.getDocument(); + Assert.assertNotNull(doc); + Feed feed = doc.getRoot(); + Assert.assertNotNull(feed); + + // printFeed( "Feed values", " ", feed ); + // RFC 4287 requires non-null id, title, updated elements + Assert.assertNotNull(feed.getId()); + Assert.assertNotNull(feed.getTitle()); + Assert.assertNotNull(feed.getUpdated()); + + eTag = res.getHeader("ETag"); + Assert.assertNotNull(eTag); + lastModified = res.getLastModified(); + Assert.assertNotNull(lastModified); + + numberEntries = getEntryCount(feed); + System.out.println("FeedAggregatorTest.testFeedBasics number entries=" + numberEntries); + + // printFeed( "Aggregated Feed Contents:", " ", feed ); + // System.out.println( "FeedAggregatorTest.testFeedBasics feed=" + + // feed ); + // printResponseHeaders( "Aggregated Feed response headers:", " ", + // res ); + // System.out.println("Aggregated Feed response body:"); + // prettyPrint(abdera, res.getDocument()); + // printEntryUpdates( "Aggregated Feed feed updates", " ", feed ); + } finally { + res.release(); + } + } + + @Test + public void testUnmodifiedGetIfModified() throws Exception { + System.out.println(">>>FeedAggregatorTest.testFeedUnmodifiedGetIfModified"); + // Feed request with predicates + RequestOptions opts = new RequestOptions(); + final String contentType = "application/atom+xml"; + opts.setContentType(contentType); + opts.setHeader("If-Modified-Since", dateFormat.format(new Date(0))); + + ClientResponse res = client.get(providerURI, opts); + Assert.assertNotNull(res); + try { + // Should return 200 - Feed provided since feed is changed. + Assert.assertEquals(200, res.getStatus()); + Assert.assertEquals(ResponseType.SUCCESS, res.getType()); + + String thisETag = res.getHeader("ETag"); + Assert.assertNotNull(thisETag); + Date thisLastModified = res.getLastModified(); + Assert.assertNotNull(thisLastModified); + + // Entry count and body size should be equal to basic request + long thisContentLength = getContentLength(res); + + Document doc = res.getDocument(); + Assert.assertNotNull(doc); + Feed feed = doc.getRoot(); + Assert.assertNotNull(feed); + int thisNumberEntries = getEntryCount(feed); + // System.out.println( + // "FeedAggregatorTest.UnmodifiedGetIfModified number entries=" + + // numberEntries + ", this number entries=" + thisNumberEntries ) ; + } finally { + res.release(); + } + } + + @Test + public void testUnmodifiedGetIfUnModified() throws Exception { + System.out.println(">>>FeedAggregatorTest.testFeedUnmodifiedGetIfUnModified"); + // Feed request with predicates + RequestOptions opts = new RequestOptions(); + final String contentType = "application/atom+xml"; + opts.setContentType(contentType); + opts.setHeader("If-Unmodified-Since", dateFormat.format(new Date(0))); + + ClientResponse res = client.get(providerURI, opts); + Assert.assertNotNull(res); + try { + // Should return 304 - Feed not provided since feed is modified + // since. + Assert.assertEquals(304, res.getStatus()); + + // Entry count and body size should be equal to basic request + long thisContentLength = getContentLength(res); + System.out + .println("FeedAggregatorTest.UnModifiedGetIfUnModified saved " + (contentLength - thisContentLength) + + " bytes of network traffic due to caching."); + } finally { + res.release(); + } + } + + /** Print feed vital fields. */ + public static void printFeed(String title, String indent, Feed feed) { + if (feed == null) { + System.out.println(title + " feed is null"); + return; + } + + System.out.println(title); + System.out.println(indent + "id=" + feed.getId()); + System.out.println(indent + "title=" + feed.getTitle()); + System.out.println(indent + "updated=" + feed.getUpdated()); + System.out.println(indent + "author=" + feed.getAuthor()); + System.out.println(indent + "self link=" + feed.getSelfLink()); + Collection collection = feed.getCollection(); + if (collection == null) { + System.out.println(indent + "collection=null"); + } else { + System.out.println(indent + "collection=" + collection); + } + } + + /* Print headers of request. */ + public static void printRequestHeaders(String title, String indent, RequestOptions request) { + System.out.println(title); + if (request == null) { + System.out.println(indent + " request is null"); + return; + } + String[] headerNames = request.getHeaderNames(); + for (String headerName : headerNames) { + String header = request.getHeader(headerName); + System.out.println(indent + " header name,value=" + headerName + "," + header); + } + } + + /* Print headers of response. */ + public static void printResponseHeaders(String title, String indent, ClientResponse response) { + System.out.println(title); + if (response == null) { + System.out.println(indent + " response is null"); + return; + } + String[] headerNames = response.getHeaderNames(); + for (String headerName : headerNames) { + String header = response.getHeader(headerName); + System.out.println(indent + " header name,value=" + headerName + "," + header); + } + + } + + /** Pretty print the document body. */ + public static void prettyPrint(Abdera abdera, Base doc) throws IOException { + WriterFactory factory = abdera.getWriterFactory(); + Writer writer = factory.getWriter("prettyxml"); + writer.writeTo(doc, System.out); + System.out.println(); + } + + /** Print the updated elements of the feed entries. */ + public static void printEntryUpdates(String title, String indent, Feed feed) { + if (feed == null) { + System.out.println(title + " feed is null"); + return; + } + + System.out.println(title); + List entries = feed.getEntries(); + if (entries == null) { + System.out.println(indent + " null entries"); + } + System.out.println(indent + "entries size=" + entries.size()); + + int i = 0; + for (Entry entry : entries) { + String entryTitle = entry.getTitle(); + if ((entryTitle != null) && (entryTitle.length() > 20)) + entryTitle = entryTitle.substring(0, 20); + // System.out.println( indent + i++ + ": title=\"" + entryTitle + + // "\", updated=" + entry.getUpdated() + ", published=" + + // entry.getPublished() ); + System.out.println(indent + i++ + ": title=\"" + entryTitle + "\", updated=" + entry.getUpdated()); + } + } + + /** Get the length of the response body content. */ + public static long getContentLength(ClientResponse response) { + // getContentLenght returns -1 + // contentLength = response.getContentLength(); + try { + Reader reader = response.getReader(); + long actualSkip = reader.skip(Long.MAX_VALUE); + return actualSkip; + } catch (IOException e) { + } + return -1L; + } + + /** Get a count of entries in the feed. */ + public static int getEntryCount(Feed feed) { + if (feed == null) { + return 0; + } + + List entries = feed.getEntries(); + if (entries == null) { + return 0; + } + return entries.size(); + } + + /** + * Given a feed, determine the median point of the entries. Use the updated + * field of the entries to determine median. + * + * @param feed + * @return + */ + public static Date getUpdatedMedian(Feed feed) { + Date sentinal = null; + if (feed == null) { + return sentinal; + } + + List entries = feed.getEntries(); + if (entries == null) { + return sentinal; + } + int size = entries.size(); + if (size == 0) { + return sentinal; + } + // System.out.println( "getUpdatedMedian entries size=" + + // entries.size()); + ArrayList updates = new ArrayList(size); + + for (Entry entry : entries) { + Date entryUpdated = entry.getUpdated(); + if (entryUpdated == null) { + entryUpdated = new Date(0); + } + updates.add(entryUpdated); + } + Collections.sort(updates); + // System.out.println( "getUpdatedMedian entry min update=" + + // updates.get( 0 )); + // System.out.println( "getUpdatedMedian entry max update=" + + // updates.get( size - 1 )); + Date median = updates.get(size / 2); + // System.out.println( "getUpdatedMedian entry max median=" + median ); + return median; + } +} -- cgit v1.2.3