首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

为站点提供 RSS 和 Atom 提要-1

为站点提供 RSS 和 Atom 提要-1

在如今的 Web 2.0 热潮中,您很可能希望在自己的应用程序中使用来自 RSS 或 Atom                提要的数据。通过使用 GWT 和 Asynchronous JavaScript + XML                (Ajax),已经有几种方法可以通过客户端代码获得和处理这类提要。不过,绕过 SOP 这样的浏览器限制无论如何都需要一些临时的解决方法。本文给出了一个简单的 GWT 应用程序,它可以解决提要的获得和处理。本文无意构建一个功能完善的提要阅读器,相反,本文旨在为您指明方向以便您能开始用 RSS 和 Atom 为自己的 Web 应用程序提供提要。
获得提要首先,考虑如何获得一个提要。通常,答案是应用 Ajax。不过,SOP(参见 )不允许连接 GWT 页面与提要源 — 当然,除非,您的提要源本身就处于您自己的服务器上,但是,那并不怎么有趣!
SOP 问题同源原则是一种浏览器安全限制,为的是防止从一个源头(即原始 URL 的协议/主机/端口这三者)加载的页面访问来自任何其他源头的数据。( Windows® Internet Explorer® 对于 SOP 比较随意并且会忽视端口更改,但这并不符合标准。)比如,如果某个 Web 页面是从                http://www.yourownsite.com:80/some/page 加载的,那么 SOP 就不会允许它从任何其他的 URL(包括如下所列)获得数据:
  • https://www.yourownsite(更改了协议)
  • http://othersite(更改了主机)
  • http://www.yourownsite.com:8080(更改了端口)
因 SOP 不允许来自一个源头的页面访问、处理和显示来自另一个源头的数据,所以它对安全性很有意义。消除 SOP 对于网页假冒者无疑是个好消息,因为您虽然浏览的是一个来自可信站点的有效页面,但是第三方却有可能监视您的行动以及任何您所发送或接收的数据。SOP 能够确保您接收的任何数据都是由原始的 Web 站点实际发送的;您不能够从任何其他(有可能是可疑的)源头接收数据。
考虑到这个限制,可以有如下两个选项:
  • 使用 Ajax 连接到自己服务器上的一个代理来获得提要(如果代码没有在浏览器上运行,就没有这些 SOP 限制)并将其发送回页面。
  • 利用一个有趣的与 JavaScript 有关的技术 — 由 Google 的 API  协助 — 允许绕过 SOP 限制。
本文首先探讨代理技术,然后转而讨论 Google                AJAX Feed API 方法,并会进行 Java™ 和                JavaScript 的混合编程。
Web 页面设计我们从最为基础的页面设计开始。我的页面设计中包括了一个文本框,用于所需提要的 URL,一个列表框,用来选择以何种方法获得字段(在实际中,您不需要给用户这样的选项),一个命令按钮,用来获得数据。在  中,可以看到一个基础的空白窗口,其中只有一个已经填充好的默认 URL。顺便说一下,这种浏览器,您可能并不熟悉,它是 GWT 自托管模式的基于 Mozilla 的浏览器。当然,在编译并部署了应用程序后,可以使用任何一种浏览器。
图 1. 空白页面当提要回来时,我不做任何特殊的处理,而只是简单地显示主要的新闻标题、一个简短的描述和一个链接。您可以按您所想,获得尽量多的提要,在新数据显示之前,先前的数据会被删除。 给出了一个实际运行的例子。
图 2. 获取提要所得到的结果总之,这个程序的总体结构如  所示。出于简明的原因,我对每个方法都进行了简短的说明,并借此缩短了需要显示的代码。我稍后会详细介绍每个方法内的代码。参见  获得完整的原始代码。
清单 1. 代码的总体结构
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
package com.fkereki.rssread.client;

//... "import" lines ...

public class Rssreader implements EntryPoint {
  // ... variable definitions...

  public void onModuleLoad() {
    // set up the form and its fields

    // call getFeedViaProxy(...) or getFeedViaGoogle(...)
    // depending on the listbox value
  }

  void getFeedViaProxy(final String feedUrl) {
    // connect to the remote server via RPC
    // when data arrives, call processAndShowFeed(...)
  }

  native void getFeedViaGoogle(final String feedUrl) /*-{
    // call Google Feed API (using native JavaScript, not Java)
    // when data arrives, call processAndShowFeed(...)
  }-*/;

  void processAndShowFeed(final String xmlDocument) {
    // clear results from a previous run, if any

    // decide whether it's RSS or Atom, and call
    // processRssFeed() or processAtomFeed() as required
  }

  void processRssFeed(final Element root) {
    // navigate a RSS feed, extraction titles, descriptions,
    // and links, and using showFeedItem(...) to show them
  }

  void processAtomFeed(final Element root) {
    // navigate an Atom feed, extraction titles, descriptions,
    // and links (by using getValueIfPresent(...) and
    // getLinkIfPresent(...), and showFeedItem(...) to show them
  }

  private String getValueIfPresent(final Element el, final String tn) {
    // get an XML node, and return the value that corresponds to a certain tag
  }

  private String getLinkIfPresent(final Element el) {
    // given a XML "link" node, return the corresponding address
  }

  private void showFeedItem(final String title, final String description,
      final String link) {

    // add some lines to the screen, with the data for the latest news
  }
}

返回列表