- Home
- Products
- Integration
- Tutorial
- Barcode FAQ
- Purchase
- Company
How to serve newsfeeds in Java
How to serve newsfeeds QR Code JIS X 0510 Creator In Java Using Barcode creator for Java Control to generate, create QR Code ISO/IEC18004 image in Java applications. www.OnBarcode.comScanning QR Code In Java Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications. www.OnBarcode.comThe write() method can be called from a Java web application, but we also want to be able to call it from the command line, so we define a standard Java main() method. The user must pass in the four command-line parameters below, and if she does not do so, we print out a usage message to help her along 1! . Draw UPC-A In Java Using Barcode maker for Java Control to generate, create UPC-A Supplement 5 image in Java applications. www.OnBarcode.comData Matrix 2d Barcode Maker In Java Using Barcode creation for Java Control to generate, create Data Matrix image in Java applications. www.OnBarcode.comdepotDir The full path to the directory in which the File Depot files exist depotUrl The base URL where the File Depot is made available on the Web file The filename of the newsfeed file to be generated format The name of the newsfeed format to be generated (for example, rss_0.91, rss_0.92, rss_1.0, rss_2.0, or atom_1.0) Paint Data Matrix ECC200 In Java Using Barcode generation for Java Control to generate, create Data Matrix image in Java applications. www.OnBarcode.comLinear 1D Barcode Creation In Java Using Barcode maker for Java Control to generate, create 1D image in Java applications. www.OnBarcode.comNext, we create a File Depot object for the depotDir passed in by the caller and a DepotNewsfeedWriter for that depot 1@ . Finally, we create a java.io.FileWriter to write the filename specified by the user, and we write the newsfeed out to that writer 1# . Now we have a Java-based newsfeed writer class that can be used for generating static or dynamic depot newsfeeds. Let s see how to use this class to generate dynamic newsfeeds from a Java web application. EAN13 Encoder In Java Using Barcode creation for Java Control to generate, create GS1 - 13 image in Java applications. www.OnBarcode.comGenerating EAN-8 Supplement 2 Add-On In Java Using Barcode drawer for Java Control to generate, create GTIN - 8 image in Java applications. www.OnBarcode.com8.4.3 Serving the File Depot newsfeed in Java
Make Quick Response Code In Java Using Barcode encoder for Java Control to generate, create QR Code image in Java applications. www.OnBarcode.comGenerating Quick Response Code In .NET Using Barcode drawer for ASP.NET Control to generate, create QR Code JIS X 0510 image in ASP.NET applications. www.OnBarcode.comWith DepotNewsfeedWriter, generating the depot newsfeed from within a Java web application is easy. All we have to do is call the writer, pass it a Depot object, and tell it which format we wish to generate. Let s examine how this is done in DepotNewsfeedServlet.java, shown in listing 8.2. Encode Code 39 In .NET Using Barcode encoder for .NET framework Control to generate, create Code 39 Extended image in .NET applications. www.OnBarcode.comQR Encoder In Java Using Barcode printer for Android Control to generate, create QR image in Android applications. www.OnBarcode.comListing 8.2 DepotNewsfeedServlet.java
ANSI/AIM Code 39 Encoder In None Using Barcode printer for Excel Control to generate, create Code 39 Full ASCII image in Microsoft Excel applications. www.OnBarcode.comEAN 13 Recognizer In C#.NET Using Barcode decoder for Visual Studio .NET Control to read, scan read, scan image in .NET applications. www.OnBarcode.compackage com.manning.blogapps.chapter08.web; import java.io.IOException; import javax.servlet.*; import javax.servlet.http.*; import com.manning.blogapps.chapter08.filedepot.*; public class DepotNewsfeedServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext application = this.getServletContext(); Depot depot = (Depot) application.getAttribute("depot"); if (depot == null) { depot = new FileDepot(request.getRealPath("/depot")); application.setAttribute("depot", depot); } Barcode Printer In VS .NET Using Barcode creator for .NET Control to generate, create Barcode image in VS .NET applications. www.OnBarcode.comDrawing Code 128 Code Set C In .NET Framework Using Barcode encoder for Visual Studio .NET Control to generate, create Code 128A image in Visual Studio .NET applications. www.OnBarcode.comGenerating newsfeeds with Java
Scan GS1 - 12 In None Using Barcode recognizer for Software Control to read, scan read, scan image in Software applications. www.OnBarcode.comPrinting Barcode In None Using Barcode creation for Microsoft Word Control to generate, create Barcode image in Word applications. www.OnBarcode.comtry { String format = request.getParameter("format"); if (format == null) format = "rss_2.0"; if (format.startsWith("rss")) { response.setContentType( "application/rss+xml;charset=utf-8"); } else { response.setContentType( "application/atom+xml;charset=utf-8"); } Data Matrix 2d Barcode Creator In Visual C#.NET Using Barcode encoder for VS .NET Control to generate, create Data Matrix ECC200 image in VS .NET applications. www.OnBarcode.comQR Code 2d Barcode Printer In Objective-C Using Barcode generation for iPad Control to generate, create QR Code image in iPad applications. www.OnBarcode.comString url = request.getRequestURL().toString(); String depotUrl = url.substring(0, url.lastIndexOf("/")); DepotNewsfeedWriter depotWriter = new DepotNewsfeedWriter(depot); depotWriter.write(response.getWriter(), depotUrl, format); } catch (Exception ex) { String msg = "ERROR: generating newsfeed"; log(msg, ex); response.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg); } } } Let s discuss the code. The DepotNewsfeedServlet class is a run-of-the-mill Java Servlet that can write a newsfeed for a depot. The first thing we do in the doGet() method is either find or create a Depot implementation at the application scope b. If we do not find one, we create one in a subdirectory named depot in the application s Servlet Context directory C. We look for a format parameter in the request d, which must be one of the standard ROME format types. (Valid options are rss_0.91, rss_0.92, rss_1.0, and atom_1.0.) If we don t find one, we default to rss_2.0. Next, we use the format to determine which content-type to set. If the format type starts with rss e, we use the RSS content-type application/rss+xml. Otherwise, we use the Atom content-type application/atom+xml. Finally, we determine the base URL of the application F and call the depot writer to write out the newsfeed G. And that brings us to the end of our Java example. Now let s discuss how to do the same thing in the .NET world. How to serve newsfeeds
8.5 Generating newsfeeds with C# In section 8.2.4 we listed three options for newsfeed generation: newsfeed library, XML, and template language. Unfortunately, only two of those options are really viable for C# developers. Why is that The Windows RSS platform s Feeds API would be the obvious choice, but supports only newsfeed parsing and offers no support for newsfeed generation. And, there are two open source newsfeed libraries for .NET, but they are incomplete and seem to have been abandoned. Eventually, Microsoft may introduce a server-side Feeds API for newsfeed generation, but until then, the best option for C# developers is to use the XML classes that are built into the .NET Framework. That s what you ll learn to do in this section. The .NET framework includes a class called System.Xml.XmlTextWriter that makes it easy to write XML to any output stream. As you call the writer s writeStartElement() and writeEndElement() methods, the XML elements come streaming out, one by one. The advantages of using this technique are: Stream style API is well suited for large newsfeeds. Simple API makes it easy to write XML elements and attributes. The technique takes care of escaping content that is not valid XML. The XmlTextWriter is built into .NET, so there are no libraries to download and install. The technique works for both static and dynamic newsfeed generation. There are also disadvantages: Overall, XmlTextWriter is more difficult and error prone than using a newsfeed library. Invalid XML is possible because the XmlTextWriter does not check for invalid characters in attribute values and element names, improper character encoding, or duplicate attributes. Invalid newsfeeds are possible because this approach does not check for conformance to any newsfeed specifications.
|
|