summaryrefslogtreecommitdiffstats
path: root/sca-java-1.x/branches/sca-java-1.6.2/demos/alert-aggregator-webapp/src/main/java/org/apache/tuscany/sca/demos/aggregator/RSSCheckerServiceImpl.java
blob: 549abbf602681c44376da130eb0e15699123a82f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
 * 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 org.apache.tuscany.sca.demos.aggregator;

import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.tuscany.sca.demos.aggregator.types.AlertType;
import org.apache.tuscany.sca.demos.aggregator.types.AlertsType;
import org.apache.tuscany.sca.demos.aggregator.types.TypesFactory;

import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;

/**
 * The interface for the rss checker service
 */
public class RSSCheckerServiceImpl implements RSSCheckerService {

    public AlertsType getNewAlerts(String rssaddress, String lastchecktimestamp){
        // Create the list of alerts to return
        TypesFactory factory    = TypesFactory.INSTANCE;
        AlertsType returnAlerts = factory.createAlertsType();
        List returnAlertList    = returnAlerts.getAlert();
        
        try {
        	// lastchecktimestamp comes from sources.xml configuration.
        	// That origin requires ISO 8601 date input (yyyy-MM-dd hh:mm:ss).
        	DateFormat configDateFormatter = new SimpleDateFormat( "yyyy-MM-dd hh:mm:ss"); 
            Date timestamp = configDateFormatter.parse(lastchecktimestamp);
            // Turn feed dates into something we can process. 
            DateFormat feedDateFormatter = DateFormat.getDateTimeInstance();
            
            // get the feed data from the supplied address            
            SyndFeedInput input = new SyndFeedInput();
            SyndFeed feed = input.build(new XmlReader(new URL(rssaddress)));
            //System.out.println(feed);
            
            // check all the items to see if we have seen them before
            List entries = feed.getEntries();
            for(Object entry: entries){
                SyndEntry syndEntry = (SyndEntry)entry;             
                
                // System.err.println( "Entry pubdate=" + syndEntry.getPublishedDate() );
                if (syndEntry.getPublishedDate().after(timestamp)){
                    AlertType newAlert = factory.createAlertType();
                    
                    newAlert.setTitle(syndEntry.getTitle());
                 //   newAlert.setSummary("<![CDATA[" + 
                //                        syndEntry.getDescription().getValue() +
                //                        "]]>");
                    newAlert.setSummary("");                    
                    newAlert.setAddress(syndEntry.getLink());
                    newAlert.setDate(feedDateFormatter.format(syndEntry.getPublishedDate()));
                    newAlert.setId(rssaddress);
                    newAlert.setUnread(true);
                    
                    returnAlertList.add(newAlert);
                }
            }
            
        } catch(Exception ex) {
        	ex.printStackTrace( System.err );
            System.err.println("Exception " + ex.toString());
        }

        return returnAlerts;
    }

}