Miscellaneous Extensions

Table of contents:

WebLogic provides a number of miscellaneous extensions that ease the processing of XML data. For instance, WebLogic extends the standard SAX input source. Instances of the weblogic.xml.sax.XMLInputSource class enable you to retrieve document header information such as the name of the root element or the public and system identifiers:

package weblogic.xml.sax;
public class XMLInputSource extends org.xml.sax.InputSource {
 public String getNamespaceURI( );
 public String getPublicId( );
 public String getRootTag( );
 public String getSystemId( );
 //...
}

This is useful when you need to decide how to process the XML data without having to complete a lengthy parse of a potentially large document. The following code snippet shows how to retrieve the root tag of an incoming XML document:

XMLInputSource xis = 
 new XMLInputSource(new java.io.StringBufferInputStream(""));
System.err.println(xis.getRootTag( ));

In addition, WebLogic provides a JMS extension whereby XML messages may be filtered on the value of an XPath expression. We covered this in Chapter 8.

18.7.1 Parsing XML in a Servlet

WebLogic provides a proprietary but convenient way for parsing the message body of an HTTP POST request made to a servlet. It allows you to use the setAttribute and getAttribute methods on the HttpServletRequest object to parse XML documents. However, it is not a feature supported by other J2EE-compliant servlet engines. WebLogic provides two special-purpose attributes in the request object. When you retrieve the value of the particular attribute, instead of returning its value, WebLogic parses the message body of the HTTP request.

WebLogic automatically uses the JAXP interface to create the appropriate parser, and lets the parser run through the contents of the body of the HTTP POST. To use the DOM parser, you need to retrieve the value of the org.w3c.dom.Document attribute from the HTTP request object and cast the return value to Document:

org.w3c.dom.Document doc = 
 (org.w3c.dom.Document) request.getAttribute("org.w3c.dom.Document");

To use the SAX parser, you need to assign a SAX handler instance as the value of the org.xml.sax.helpers.DefaultHandler request attribute. WebLogic then will automatically parse the message body of the HTTP POST request using the SAX handler you've supplied:

request.setAttribute("org.xml.sax.helpers.DefaultHandler", someHandler);

Example 18-8 illustrates how a simple servlet can parse the message body of an incoming POST request. In response, the servlet outputs the names of all the start elements found in the XML document.

Example 18-8. Parsing the body of a POST request

import weblogic.servlet.XMLProcessingException;
import org.xml.sax.helpers.DefaultHandler;
// ...
public void doPost(HttpServletRequest request,
 HttpServletResponse response)
 throws ServletException, IOException {
 try {
 final PrintWriter out = response.getWriter( );
 DefaultHandler printName = new DefaultHandler( ) { 
 public void startElement (String uri, String lName, String qName, 
 Attributes attr){
 out.println(qName);}; 
 };
 request.setAttribute("org.xml.sax.helpers.DefaultHandler",
 printName);

 } catch(XMLProcessingException ex) {
 ex.printStackTrace( );
 }
}

A Java client may send a POST request to the servlet, using an instance of the java.net.URLConnection class. Example 18-9 shows how a program can send XML data to an HTTP servlet. Once the client has established a connection with the servlet and delivered the XML data, it prints the response data returned from the servlet.

Example 18-9. Sending data to the XML servlet

 // Connect to our servlet
 URL u = new URL("http://10.0.10.10:7061/B");
 URLConnection uc = (URLConnection) u.openConnection( ); 
 uc.setRequestProperty("Content-Type","text/xml");
 uc.setDoOutput(true);
 uc.setDoInput(true); 
 // Send some XML
 PrintWriter pw = new PrintWriter(uc.getOutputStream( ));
 pw.print("I am You are");
 pw.close( );
 // Read the result
 BufferedReader in = 
 new BufferedReader(new InputStreamReader(uc.getInputStream( )));
 String inputLine;
 while ((inputLine = in.readLine( )) != null) {
 System.out.println(inputLine);
 }
 in.close( );

Running this code will yield the following output in the console window:

fine
dandy

18.7.2 Using the JSP Tag Library for XSL Transformations

WebLogic supplies a JSP tag library to help with XSL Transformations from within a JSP page. You can make the tag library available to a web application by placing the tag library xmlx-tags.jar under the WEB-INF/lib folder of the web application. Before this, however, you need to extract the tag library JAR from the WL_HOMEserverextxmlx.zip file. Finally, you need to specify a taglib element in the standard web.xml descriptor file to make the tag library visible to the web application:

 xmlx.tld
 /WEB-INF/lib/xmlx-tags.jar

Given this declaration, you now can reference the tag library from within a JSP page as follows:

<%@ taglib uri="xmlx.tld" prefix="x"%>

The tag library defines the main xslt tag, and two tags that can be used within its body: the xml and stylesheet tags. The xslt tag can be used in several different ways:

  • The XML data can be supplied within the body of the xml tag.
  • You can reference multiple XSLT stylesheets and, at runtime, determine which one is used for the transformation.
  • If the JSP uses an empty xslt tag, the XML data is grabbed from the URL used to access the JSP.

In all these cases, the JSP tag automatically uses the JAXP interface to acquire an XSL transformer and then apply the correct stylesheet to the XML data.

18.7.2.1 Using an interceptor

You can set up a mechanism whereby a request for an XML file automatically triggers an XSLT conversion, and the result of the transformation is then returned back to the client. In this case, the XML file references the XSLT stylesheet that ought to be used for the transformation. The trick is to map a servlet (or JSP) to a URL pattern so that the servlet (or JSP) can intercept the requests for the XML file and run the transformation. In this case, we'll use a JSP to intercept requests for the XML file(s). The JSP page intercepts the HTTP request. A simple JSP tag then automatically passes the requested file through an XSLT processor and sends the output of the transformation back to the client.

The JSP page looks quite simple it merely includes an empty xslt tag. The following code snippet lists the source for the interceptor.jsp file:

<%@ taglib uri="xmlx.tld" prefix="x"%>

Next, you need to register this JSP as a servlet and map it to a URL pattern say, /xslt/*. Do this by modifying the standard web.xml descriptor for the web application:



 interceptor
 interceptor.jsp


 interceptor
 /xslt/*

This will ensure that all requests that match the URL pattern /xslt/* are now delegated to the interceptor JSP. Because the interceptor.jsp contains an xslt tag with an empty body, the tag implementation will fetch the contents of the resource automatically. The location of the resource is determined implicitly by the rest of the URI that follows the servlet path. The JSP tag will read the XML data from the resource and then use the referenced XSLT stylesheet to execute the transformation, before returning the output back to the client. Now, suppose the web application contains an XML file test.xml:



Hello World!

Here, the XML file includes a reference to the XSLT stylesheet test.xsl:

 

 

 

If you place both test.xml and test.xsl under the root of the web application, a request for the URL http://server:port/webapp/xsl/test.xml will be redirected to the interceptor JSP automatically. The JSP will invoke the xslt tag, which will fetch the resource using the URI that follows the servlet path (i.e., /test.xml) and send this file through the configured XSLT processor. The HTML generated as a result of the transformation then will be returned to the client:


 

Hello World!

Of course, you can reduce the cost of XSLT processing by relying on WebLogic's support for caching, via either the JSP cache tags or the cache filters.

18.7.2.2 Using the XSLT tag

The XSLT tag also can be used in a more traditional manner i.e., in nonintercept mode. The following code sample shows how to execute a transformation when the URIs for the XML document and the XSLT stylesheet are attributes of the xslt tag:

<%@ taglib uri="xmlx.tld" prefix="x"%>
xml="test.xml" stylesheet="test.xsl"/>

In this case, the result of the XSL transformation also will be the output of the xslt tag. You can specify a number of subelements within the body of the xslt tag:

  • You can specify the source XML data within the body of an xml tag. If the xslt tag doesn't define an xml attribute or include an xml tag, the XML data is grabbed from the URI that follows the servlet path, as described earlier.
  • You can specify one or more stylesheet elements, whereby each tag declares a media attribute and a uri attribute that references an XSLT stylesheet. You also can define the XSL templates within the body of the stylesheet tags.

If the xslt tag declares a media attribute, its value determines which one of the stylesheets is eventually used. Example 18-10 illustrates how to use the media attribute to select from one of the stylesheets. Here, the JSP decides based on whether the output is meant for an HTML browser or a WAP browser (which supports WML).

Example 18-10. Using the xslt tag

<%@ taglib uri="xmlx.tld" prefix="x"%>
<%
 String mediaType = "html"; // Usually dynamically computed
 String content = "Hello World!"; 
%>


 <%=content%>
 
 

In this case, the JSP generates the same output as interceptor.jsp:


 

Hello World!

The media attribute acts as a selector variable for stylesheets listed in the body of the xslt tag. This way, the JSP can decide at runtime which stylesheet should be used for the XSL transformation.

Introduction

Web Applications

Managing the Web Server

Using JNDI and RMI

JDBC

Transactions

J2EE Connectors

JMS

JavaMail

Using EJBs

Using CMP and EJB QL

Packaging and Deployment

Managing Domains

Clustering

Performance, Monitoring, and Tuning

SSL

Security

XML

Web Services

JMX

Logging and Internationalization

SNMP



WebLogic. The Definitive Guide
WebLogic: The Definitive Guide
ISBN: 059600432X
EAN: 2147483647
Year: 2003
Pages: 187

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