1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.news.util;
24  
25  import com.liferay.portal.kernel.util.HttpUtil;
26  import com.liferay.portal.kernel.util.Time;
27  import com.liferay.portal.kernel.webcache.WebCacheException;
28  import com.liferay.portal.kernel.webcache.WebCacheItem;
29  import com.liferay.portal.kernel.xml.Document;
30  import com.liferay.portal.kernel.xml.Element;
31  import com.liferay.portal.kernel.xml.SAXReaderUtil;
32  import com.liferay.portlet.news.model.Article;
33  import com.liferay.portlet.news.model.News;
34  
35  import java.io.ByteArrayInputStream;
36  
37  import java.text.DateFormat;
38  import java.text.SimpleDateFormat;
39  
40  import java.util.ArrayList;
41  import java.util.Iterator;
42  import java.util.List;
43  
44  /**
45   * <a href="NewsWebCacheItem.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brian Wing Shun Chan
48   *
49   */
50  public class NewsWebCacheItem implements WebCacheItem {
51  
52      public Object convert(String text) throws WebCacheException {
53          try {
54              int pos = text.indexOf(";");
55  
56              String categoryName = text.substring(0, pos);
57              String feedURL = text.substring(pos + 1, text.length());
58              String xml = HttpUtil.URLtoString(
59                  "http://p.moreover.com/cgi-local/page?" + feedURL + "&o=xml");
60  
61              ByteArrayInputStream bais = new ByteArrayInputStream(
62                  xml.getBytes());
63  
64              Document doc = SAXReaderUtil.read(bais);
65  
66              List<Article> articles = new ArrayList<Article>();
67  
68              DateFormat df = new SimpleDateFormat("MMM d yyyy K:mma z");
69  
70              Element root = doc.getRootElement();
71  
72              Iterator<Element> itr = root.elements("article").iterator();
73  
74              while (itr.hasNext()) {
75                  Element articleEl = itr.next();
76  
77                  String harvestTime =
78                      articleEl.element("harvest_time").getTextTrim() + " GMT";
79  
80                  Article article = new Article(
81                      articleEl.element("headline_text").getTextTrim(),
82                      articleEl.element("url").getTextTrim(),
83                      articleEl.element("source").getTextTrim(),
84                      articleEl.element("document_url").getTextTrim(),
85                      articleEl.element("access_status").getTextTrim(),
86                      articleEl.element("access_registration").getTextTrim(),
87                      df.parse(harvestTime));
88  
89                  articles.add(article);
90              }
91  
92              return new News(feedURL, categoryName, articles);
93          }
94          catch (Exception e) {
95              throw new WebCacheException(e);
96          }
97      }
98  
99      public long getRefreshTime() {
100         return _REFRESH_TIME;
101     }
102 
103     private static final long _REFRESH_TIME = Time.MINUTE * 20;
104 
105 }