Constructing the RSS Aggregator


Now follow these steps to begin constructing the RSS aggregator client for the REST RSS service:

1.

Open the MainForm.cs module of the SafeReader project of the RepresentationalStateTransfer solution in the Windows Forms Designer. It should appear as shown in Figure 13.3. It has a WebBrowser control, named browser, that will be used to display the content referred to by the link field in RSS feeds retrieved from the REST RSS service that we have constructed, provided, of course, that the content rating indicates that the content is suitable to be shown to children.

Figure 13.3. The design of the Safe Reader RSS aggregator's main window.


2.

Add a copy of the module IReallySimpleSyndication.cs in the SafeFeeder project to the SafeReader project.

3.

Add an application configuration file called app.config to the SafeReader project, and modify it as shown in Listing 13.9.

Listing 13.9. RSS Aggregator Configuration

<?xml version="1.0" encoding="utf-8" ?> <configuration>         <system.serviceModel>                <client>                       <endpoint name="ReallySimpleSyndicationService"                                    address="http://localhost:8888/Service"                                    binding="customBinding"                                    bindingConfiguration                                      ="PlainXMLRepresentationalStateTransferBinding"                                    contract ="RepresentationalStateTransfer.IReallySimpleSyndication,SafeReader"/>         </client>        <bindings>              <customBinding>                      <binding name="PlainXMLRepresentationalStateTransferBinding">                             <plainXMLEncoding/>                             <httpTransport mapAddressingHeadersToHttpHeaders="true"/>              </binding>          </customBinding>       </bindings>      <extensions>             <bindingElementExtensions>                    <add name="plainXMLEncoding" type="System.ServiceModel.PlainXMLEncoderBindingElementExtensionSection,                     PlainXMLRepresentationalStateTransfer"/>             </bindingElementExtensions>         </extensions>      </system.serviceModel> </configuration>

This Windows Communication Foundation configuration provides the information that the Windows Communication Foundation requires to enable the SafeReader RSS aggregator to communicate with the SafeFeeder REST RSS service. Specifically, the configuration provides the address where the REST RSS service is located, identifies the contract implemented by the service at that address as the IReallySimpleSyndication contract, and, finally, defines a custom binding specifying how to communicate with the plain XML REST RSS service that is identical to the binding defined for the service itself.

4.

Modify the handler of the form's Load event, which is the method MainForm_Load(), in the module MainForm.cs, as shown in Listing 13.10.

Listing 13.10. Form Load Event Handler

private void MainForm_Load(object sender, EventArgs e) {     IReallySimpleSyndication client =          new ChannelFactory<IReallySimpleSyndication>(              "ReallySimpleSyndicationService").CreateChannel();     System.ServiceModel.Message message =         System.ServiceModel.Message.CreateMessage(             Constants.DefaultAction);     System.ServiceModel.Message output = client.Get(message);     XmlDocument document = new XmlDocument();     XmlDictionaryReader reader = output.GetReaderAtBodyContents();     document.LoadXml(reader.ReadOuterXml());     ContentRating rating;     string link = null;     bool validRating =         ReallySimpleRatedSyndication.ValidateSignedContentRating(             document,             out rating,             out link);         if ((validRating) && (rating == ContentRating.AllAges))         {              try              {                  this.browser.Url = new Uri(link);              }              catch (Exception)              {                  this.browser.DocumentText = string.Empty;              }           }           else           {               this.browser.DocumentText = Resources.Prohibited;            } }

This code for the handler of the form's Load event uses the Windows Communication Foundation's ChannelFactory<T> generic to create a proxy for communicating with REST RSS service. It uses that proxy to send an empty instance of the Message class to the service to retrieve the RSS feed from the service. Then it passes the RSS feed to the ReallySimpleRatedSyndication class's ValidateSignedContentRating() method to confirm that the content rating incorporated in the feed was digitally signed with the private key of a trusted content ratings organization. If the signature of the content rating is confirmed and the rating indicates that the content is suitable for children, the content referred to in the RSS feed is shown to the user. Otherwise, the user is told that the content is not suitable for viewing.

5.

Examine the ReallySimpleRatedSyndication class' static ValidateSignedContentRating() and VerifySignature() methods in the ReallySimpleRatedSyndication.cs module of the ReallySimpleRatedSyndication project. Listing 13.2, earlier in this chapter, shows the methods.

The ValidateSignedContentRating() method accepts the RSS feed retrieved from the REST RSS service, gets the public key of the trusted content rating organization from the trusted people store of the current user's certificate store, and passes that certificate, along with the signed data containing the content rating and the links to content, to the VerifySignature() method. That method confirms that the data was signed with the private key corresponding to the public key of the trusted content rating organization. Then the VerifiySignedContentRating() method extracts the signed content rating and links to content from the RSS feed.

6.

Build the solution.

7.

Modify the SafeFeeder project's Debug properties, as shown in Figure 13.4, so as to provide Feed.xml as the command-line argument.

Figure 13.4. Providing Feed.xml as a command-line argument to SafeFeeder.


8.

Right-click on the SafeFeeder project, and choose Debug, Start New Instance from the context menu. The SafeFeeder console application that hosts the REST RSS service should start.

9.

After you see activity in the console of the SafeFeeder application, right-click on the SafeReader project, and choose Debug, Start New Instance from the context menu.

The content of the feed should appear in the SafeReader, as shown in Figure 13.5, because the content rating specified in the Feed.xml RSS document indicates that the content is suitable for all ages. Now the service will be modified to provide an RSS feed that is suitable only for adults.



Figure 13.5. Accessing an RSS feed that is safe for children.


10.

Stop debugging.

11.

Modify the SafeFeeder project's Debug properties so as to provide NaughtyFeed.xml as the command-line argument, as shown in Figure 13.6.

Figure 13.6. Providing NaughtyFeed.xml as a command-line argument to SafeFeeder.


12.

Right-click on the SafeFeeder project, and choose Debug, Start New Instance from the context menu. The SafeFeeder console application that hosts the REST RSS service should start.

13.

After you see activity in the console of the SafeFeeder application, right-click on the SafeReader project, and choose Debug, Start New Instance from the context menu.

This time the RSS aggregator should display a message like the one shown in Figure 13.7, saying that the content of the RSS feed is too naughty to be shown. The reason is that the content rating of the NaughtyFeed.xml RSS document indicates that it is suitable for viewing only by adults.

Figure 13.7. Prohibited from accessing an RSS feed that is only for adults.





Presenting Microsoft Communication Foundation. Hands-on.
Microsoft Windows Communication Foundation: Hands-on
ISBN: 0672328771
EAN: 2147483647
Year: 2006
Pages: 132

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net