You are receiving this email because you elected to receive email from Sun Microsystems, Inc. To update your communications preferences, please see the link at the bottom of this message. We respect your privacy and post our privacy policy prominently on our Web site http://sun.com/privacy/ Please do not reply to the mailed version of the newsletter, this alias is not monitored. Feedback options are listed in the footer for both content and delivery issues. |
May 26, 2006 |
| ||
Core Java Technologies NEWSLETTER | |||
Here you'll find links to the latest products, tools, resources, and events relating to development with the Java Platform, Standard Edition (Java SE, formerly referred to as J2SE). |
|
|
Comments and Contact Information: There are three ways to send your feedback about the Core Java Technologies Newsletter: For comment about the content of this newsletter, fill out the Rate and Review form, above. For technical assistance about Newsletter delivery, broken links, or subscribe/unsubscribe help, fill out the web form. Write to us at: SDN Editorial Team, MS: UMPK18-113, 18 Network Circle, Menlo Park, CA 94025, USA Subscribe/Unsubscribe: Subscribe to other Java technology newsletters:
- To subscribe, visit the SDN Newsletters and Publications page, select the newsletters you want to subscribe to and click "Update." - To unsubscribe, go to the subscriptions page, uncheck the appropriate checkbox, and click "Update." IMPORTANT: Please read our Licensing, Terms of Use, and Privacy policies: http://developer.java.sun.com/berkeley_license.html http://www.sun.com/share/text/termsofuse.html Privacy Statement: Sun respects your online time and privacy (http://sun.com/privacy). You have received this based on your email preferences. If you would prefer not to receive this information, please follow the steps at the bottom of this message to unsubscribe. Archives: You'll find the Core Java Newsletter archives at: http://java.sun.com/developer/techDocs/Newsletters/jdc_newsletters.html © 2006 Sun Microsystems, Inc. All Rights Reserved. For information on Sun's trademarks see: http://sun.com/suntrademarks Java, J2EE, J2SE, J2ME, and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. Sun Microsystems, Inc. 10 Network Circle, MPK10-209 Menlo Park, CA 94025 US |
Please unsubscribe me from this newsletter. |
You are receiving this e-mail because you elected to receive e-mail from Sun Microsystems, Inc. To update your communications preferences, please see the link at the bottom of this message. We respect your privacy and post our privacy policy prominently on our Web site http://sun.com/privacy/ Please do not reply to the mailed version of the newsletter, this alias is not monitored. Feedback options are listed in the footer for both content and delivery issues. |
| ||||||
In this Issue | ||
Welcome to the Enterprise Java Technologies Tech Tips for May 30, 2006. Here you'll get tips on using enterprise Java technologies and APIs, such as those in Java 2 Platform, Enterprise Edition (J2EE) and Java Platform, Enterprise Edition 5 (Java EE 5). This issue covers: » Using JAX-WS-Based Web Services with SSL » Document Handling in Web Services Applications These tips were developed using an open source implementation of Java EE 5 called GlassFish. You can download GlassFish from the GlassFish Project page. You can download the sample archive for the tip Using JAX-WS-Based Web Services with SSL at http://java.sun.com/developer/EJTechTips/download/ttmay2006ws-ssl.zip. You can download the sample archive for the Document Handling in Web Services Applications at http://java.sun.com/developer/EJTechTips/download/ttmay2006doc-ws.zip. Any use of this code and/or information below is subject to the license terms. See the Subscribe/Unsubscribe note at the end of this newsletter to subscribe to Tech Tips that focus on technologies and products in other Java platforms. | ||
USING JAX-WS-BASED WEB SERVICES WITH SSL | ||
by Shing Wai Chan In Java EE 5, a web service that is based on JAX-WS can be implemented either as an EJB endpoint or servlet endpoint. An example of a JAX-WS-based web service implemented as an EJB endpoint is provided in the Tech Tip Developing Web Services Using EJB 3.0. A JAX-WS-based web service implemented as a servlet endpoint is provided in the Tech Tip Developing Web Services Using JAX-WS. Irrespective of the way a web service is implemented, most enterprise applications, including those that use web services, need to run in a secure environment. Transport Layer Security (TLS)/Secure Sockets Layer (SSL) is a point-to-point secure transport mechanism that can be used for authentication, message integrity, and confidentiality. TLS/SSL This Tech Tip shows you how to construct a JAX-WS-based web service that runs with SSL, and how to access the web service from an application client. A sample package accompanies the Tech Tip. It demonstrates a Java client accessing a JAX-WS web service using SSL. Examples are provided for web services implemented as EJB and servlet endpoints. The sample uses an open source reference implementation of Java EE 5 called GlassFish. You can download GlassFish from the GlassFish Community Downloads page. Write the Class for the Web Service Endpoint Let's begin by writing a Java class for the web service. SSL has no impact on the Java code for the web service endpoint. The same code works for web services that use SSL or that don't use SSL. In Java EE 5, you can use annotations to easily construct a JAX-WS web service. Here's an example of a web service implemented as an EJB endpoint: package ejbws; import javax.annotation.Resource; import javax.ejb.Stateless; import javax.jws.WebService; import javax.xml.ws.WebServiceContext; @Stateless @WebService public class HelloEjb { @Resource WebServiceContext wsContext; public String hello(String msg) { return "Ejb WS: " + wsContext.getUserPrincipal() + ": " + msg; } } The Here's the same web service implemented as a servlet endpoint: package servletws; import javax.annotation.Resource; import javax.jws.WebService; import javax.xml.ws.WebServiceContext; @WebService public class HelloServlet { @Resource WebServiceContext wsContext; public String hello(String msg) { return "Servlet WS: " + wsContext.getUserPrincipal() + ": " + msg; } } Specify Security Information in Deployment Descriptors To use SSL in a web service that is implemented as an EJB endpoint, you need to specify security information in a vendor-specific deployment descriptor (in this tip, One important aspect of secure communication through SSL is server authentication, that is, confirming the identity of the server to the client. Another aspect is client authentication, where the server confirms the identity of the client. In SSL, you can have either server authentication or the combination of server and client authentication (but not client authentication alone). This tip uses the term "mutual authentication" to mean the combination of server and client authentication. (Note however that other documents might attach a different meaning to mutual authentication. For example, in some documents, the term client authentication is synonymous with mutual authentication.) To enable SSL server authentication, you need to set the <ejb> <ejb-name>HelloEjb</ejb-name> <webservice-endpoint> <port-component-name>HelloEjb</port-component-name> <transport-guarantee>CONFIDENTIAL</transport-guarantee> </webservice-endpoint> </ejb> For the <security-constraint> <web-resource-collection> <web-resource-name>Secure Area</web-resource-name> <url-pattern>/HelloServletService/HelloServlet </url-pattern> <http-method>POST</http-method> </web-resource-collection> <auth-constraint> <role-name>EMPLOYEE</role-name> </auth-constraint> <user-data-constraint> <transport-guarantee>CONFIDENTIAL</transport-guarantee> </user-data-constraint> </security-constraint> Notice the In GlassFish, WSDL files are protected by SSL for endpoints with SSL or SSL mutual authentication. For SSL mutual authentication, you need to set the <ejb> <ejb-name>HelloEjb</ejb-name> <webservice-endpoint> <port-component-name>HelloEjb</port-component-name> <login-config> <auth-method>CLIENT-CERT</auth-method> <realm>certificate</realm> </login-config> <transport-guarantee>CONFIDENTIAL</transport-guarantee> </webservice-endpoint> </ejb> <security-constraint> <web-resource-collection> <web-resource-name>Secure Area</web-resource-name> <url-pattern>/HelloServletService/HelloServlet </url-pattern> <http-method>POST</http-method> </web-resource-collection> <auth-constraint> </role-name>EMPLOYEE</role-name> </auth-constraint> <user-data-constraint> <transport-guarantee>CONFIDENTIAL</transport-guarantee> </user-data-constraint> </security-constraint> <login-config> <auth-method>CLIENT-CERT</auth-method> <realm-name>certificate</realm-name> </login-config> No deployment descriptor is needed for the client program. Package and Deploy the Web Service Application Packaging and deploying a web service application that uses SSL is the same as for a web service application that doesn't use SSL. Keep in mind however, that if the web service is implemented as a servlet endpoint in an enterprise archive (that is, a Write the Client After deploying the web service, you can access it from a client program. The client program for a web service application that uses SSL is essentially the same as one that doesn't use SSL. The major difference is that instead of using HTTP as the internet protocol, you need to use HTTPS. In the client, you use the @WebServiceRef(wsdlLocation= "https://serverName:8181/HelloEjbService/HelloEjb?WSDL") private static HelloEjbService helloEjbService; You can then access the port for the web service and invoke the web service: HelloEjb helloEjbPort = helloEjbService.getHelloEjbPort(); helloEjbPort.hello("Hello World"); Client-side artifacts are generated by accessing the WSDL file through HTTPS. To do that, you need to specify the location of a truststore file for the server and its password in the environment variable You can specify the location of the server's truststore file and its password in various ways, for example, you can specify them in an <exec executable="wsimport"> <env key="VMARGS" value="-Djavax.net.ssl.trustStore=${truststore.location} -Djavax.net.ssl.trustStorePassword=${ssl.password}"/> <arg line="-keep -d ${client.build} https://serverName:8181/HelloEjbService/HelloEjb?WSDL"/> </exec> Set Up the Client Truststore and Keystore In addition to a truststore file for the server, you also need a truststore file for the client. The client validates the server certificate against a set of certificates in its truststore. For a secure connection to be made using SSL, the client-side truststore needs to trust the server certificate. For SSL mutual authentication, the server-side truststore also needs to trust the client certificate. If you modify the server-side truststore, you need to restart the server (this allows use of the new certificate). You might not need to do this in a production environment because production certificates are signed by a common Certificate Authority (CA). The GlassFish builds include certificates for several common root CAs. For SSL mutual authentication, you need to provide your own key in a client keystore (a file that contains the client's keys and certificates). You can use keytool, a key and certificate management utility in JDK 5.0, to generate the keystore. Run the Client Before you run the client that access a web service with SSL, you need to set the value of the environment variable, -Djavax.net.ssl.trustStore=${truststore.location} -Djavax.net.ssl.trustStorePassword=${ssl.password} For SSL mutual authentication, set the value of -Djavax.net.ssl.trustStore=${truststore.location} -Djavax.net.ssl.trustStorePassword=${ssl.password} -Djavax.net.ssl.keyStore =${keystore.location} -Djavax.net.ssl.keyStorePassword=${ssl.password} Running the Sample Code To install and run the sample code that accompanies this tip:
About the Author Shing Wai Chan is a senior member of the Sun Java Application Server and Java EE SDK development teams. He has been focusing on development projects that relate to security, annotations, CMP, B2B and B2C. Back to Top | ||
DOCUMENT HANDLING IN WEB SERVICES APPLICATIONS | ||
by Deep Singh Java EE provides various technology choices for handling XML documents. Three of these technologies are Java Architecture for XML Binding (JAXB), Streaming API for XML (StAX), and the Document Object Model (DOM) API. This Tech Tip compares these choices, and shows the technologies in use in a sample application. JAXB JAXB technology provides a way to bind XML schemas to Java objects so that developers can easily process data in their Java applications. The JAXB API provide methods to unmarshal an XML document into a Java object and marshal a Java object into an XML document. For more information about JAXB, see the Tech Tip What's New in JAXB 2.0. A significant advantage of using JAXB is that you can compile the schema (or dtd) to generate a Java content tree, and then work with plain Java objects. JAXB is not particularly good in cases where complex schemas are involved and you want to work with only a small set of content. StAX StAX is a streaming API for processing XML documents. It's an event-driven, "pull" parser that reads and writes XML documents. For more information about StAX, see the Tech Tip Introducing the Sun Java Streaming XML Parser. StAX's bidirectional features, small memory footprint, and low processor requirements give it an advantage over APIs such as JAXB or DOM. StAX is particularly effective in extracting a small set of information from a large document. The primary drawback in using StAX is that you get a narrow view of the document -- essentially you have to know what processing you will do before reading the XML document. Another drawback is that StAX is difficult to use if you return XML documents that follow complex schema. DOM DOM is platform-neutral and language-neutral API that enables programs to dynamically update the contents of XML documents. For more information about DOM, see the Tech Tip Using the Document Object Model. DOM creates an in-memory object representation of an entire XML document. This allows extreme flexibility in parsing, navigating, and updating the contents of a document. DOM's drawbacks are high memory requirements and the need for more powerful processing capabilities. The sample application: comparing JAXB, StAX, and DOM A sample application accompanies this tip. It demonstrates JAXB, StAX, and DOM in use to handle an XML document that is passed as input to a web service. The sample application involves credit card authorization. The application client code calls a web service to authorize payment on a credit card provided by a customer. The client sends credit card and card holder information contained in an XML document to the web service. The client can process the XML document using JAXB, StAX or DOM. The sample demonstrates the XML document processing using all three technologies. This section describes how to install and run the sample application. Setting up your environment The sample uses an open source reference implementation of Java EE 5 called GlassFish. If you haven't already done so, download GlassFish from the GlassFish Community Downloads page. To build and run the sample, you also need JDK 5.0, which you can download from the J2SE 5.0 downloads page, and Apache Ant 1.6.5, which is in the GlassFish bundle (in Windows, it's in the Installing the sample application Download the sample package and unzip its contents. The root directory for the sample is Building the web service The sample builds a web service using a WSDL file. The steps involved in doing this are:
Create a WSDL file based on XML schemas A WSDL file is packaged with the sample. You can find the WSDL file, Schema files The two schema files that the WSDL file imports are <element name="AuthorizationRequest" type="tns:AuthorizationRequest"/> <element name="authorizePaymentSTAX" type="xsd:string"/> <element name="authorizePaymentDOM"> <complexType> <sequence> <any maxOccurs="1"/> </sequence> </complexType> </element> The type value Generate portable artifact The sample uses the <target name="generate-server" depends="prepare"> <wsimport ... wsdl="${server.wsdl}"> <binding dir="${confdir}" includes="${server.config}"/> </wsimport> </target> The After the artifacts are generated, they're compiled using the javac compiler. In the sample, this is done using an Write an endpoint implementation class The sample provides an endpoint implementation class, Here is the method that makes use of JAXB: public AuthorizationStatus authorizePaymentJAXB( AuthorizationRequest parameters) throws com.sun.techtip.cardservice.service.CardServiceException { try { CreditCard card = parameters.getCreditCard(); CardUser user = parameters.getCardUser(); // For sake of simplicity, read only limited // information about credit card and credit card user String cardNumber = card.getCardNumber(); String lastName = user.getLname(); // Emulate charge authorization dao.authorizeCharge(cardNumber, lastName); // Create and populate authorization status object ObjectFactory of = new ObjectFactory(); AuthorizationStatus status = of.createAuthorizationStatus(); status.setAuthorizationToken(dao.authorizationToken); ... return status; The method receives a JAXB-generated object as a parameter. The method retrieves relevant JAXB objects: Here is the method that uses StAX for performing the same authorization task. The parameter passed to the method is a String object, which is actually an XML document. public String authorizePaymentSTAX(String parameters) throws CardServiceException { try { // Create the StAX reader to read the XML document StringReader strReader = new StringReader(parameters); XMLInputFactory staxFactory = XMLInputFactory.newInstance(); staxFactory.setProperty( XMLInputFactory.IS_VALIDATING, Boolean.FALSE); XMLStreamReader xmlReader = staxFactory.createXMLStreamReader(strReader); StAX parsers use an // For sake of simplicity, we read only limited information // about credit card and credit card user String cardNumber= null; String lastName = null; while (xmlReader.hasNext()) { if (xmlReader.isStartElement()) { String sName = xmlReader.getLocalName(); if (sName.equals("cardNumber")) { cardNumber = xmlReader.getElementText(); } if (sName.equals("lname")) { lastName = xmlReader.getElementText(); } } xmlReader.next(); } xmlReader.close(); The business logic in the application authorizes the charge on the card. Then the method creates the response XML document. In the code below, notice that an // Emulate charge authorization dao.authorizeCharge(cardNumber, lastName); // Create response XML document StringWriter strWr = new StringWriter(); XMLStreamWriter resvStatusWr = XMLOutputFactory.newInstance().createXMLStreamWriter(strWr); resvStatusWr.writeStartDocument(); resvStatusWr.writeStartElement( "ns0", "AuthorizationStatus", "urn:CardService"); resvStatusWr.writeStartElement( "ns0", "authorizationToken", "urn:CardService"); resvStatusWr.writeCharacters(dao.authorizationToken); resvStatusWr.writeEndElement(); ... resvStatusWr.writeEndDocument(); return strWr.toString(); Here is the method that uses DOM. The parameter to the method gives the root element of the XML document sent by the client. The method uses it to parse the document and gather the required information about the credit card and its user. public AuthorizeStatusDOM authorizePaymentDOM( AuthorizePaymentDOM parameters) throws CardServiceException { try { Element element = (Element)parameters.getAny(); // For sake of simplicity, we read only limited // information about credit card and credit card user Node cardNode = ( element.getElementsByTagName("cardNumber")).item(0); String cardNumber = cardNode.getFirstChild().getNodeValue(); Node lnameNode = (element.getElementsByTagName("fname")).item(0); String lastName = lnameNode.getFirstChild().getNodeValue(); As before, the method calls the application's business logic to authorize the credit charge. Next, the method creates the response using a // Emulate charge authorization dao.authorizeCharge(cardNumber, lastName); SOAPFactory sf = SOAPFactory.newInstance(); SOAPElement root = sf.createElement( "AuthorizationStatus","ns0","urn:CardService"); SOAPElement token = sf.createElement("authorizationToken"); token.addTextNode(dao.authorizationToken); root.addChildElement(token); ... ObjectFactory of = new ObjectFactory(); AuthorizeStatusDOM status = of.createAuthorizeStatusDOM(); status.setAny(root); return status; Compile the web service, package it as a WAR file, and deploy it You need to compile the service implementation class as well as portable artifacts for the web service that were generated earlier by running Building the client Here are the steps to build the web service client:
Write the client class A standalone web service client, Here's the method in the client that calls the service using JAXB. A JAXB public void testAuthorizePaymentJAXB( String authrequestXMLfile) throws Exception { try { // Get the unmarshaller to convert XML into Object JAXBContext jc = JAXBContext.newInstance("creditcard"); Unmarshaller u = jc.createUnmarshaller(); // Unmarshal the XML document into authorization // request object JAXBElement cardAuthorizationElement = (JAXBElement)u.unmarshal( new FileInputStream(authrequestXMLfile)); AuthorizationRequest cardAuthorizationRequest = (AuthorizationRequest) cardAuthorizationElement.getValue(); // Make web service call AuthorizationStatus reply = stubC.authorizePaymentJAXB(cardAuthorizationRequest); Notice that the response from the service is a JAXB authorization status object. Also, As an alternative to using an XML document, the client could populate relevant JAXB objects and then make the service call. You can see this approach in the method Here's the method in the client that calls the service using StAX. This method passes the XML document to the service in the form of a String object. The response from the service is also a String. The public void testAuthorizePaymentSTAX( String authrequestXMLfile) throws Exception { // Read XML document into a string String authRequest = readStringFromFileRaw(authrequestXMLfile); try { // Make web service call String reply = stubC.authorizePaymentSTAX(authRequest); Finally, here is the method in the client that uses DOM to make the web service call. The utility method public void testAuthorizePaymentDOM(String authrequestXMLfile) throws Exception { // Get document root element Element authRequest = createElementMessage(authrequestXMLfile); try { // Populate authorization request object ObjectFactory of = new ObjectFactory(); AuthorizePaymentDOM msg = of.createAuthorizePaymentDOM(); msg.setAny(authRequest); // Make web service call AuthorizeStatusDOM reply = stubC.authorizePaymentDOM(msg); Generate portable artifacts Generate client-side portable artifacts using the WSDL on server. This is done in the sample using an Running the sample To run the sample:
About the Author Deep Singh is a staff member of Java Performance Engineering group at Sun Microsystems. Back to Top | ||
DEVELOPER ASSISTANCE | ||
Need programming advice on Java EE? Try Developer Expert Assistance http://developers.sun.com/services/expertassistance/ Back to Top | ||
Comments? Send your feedback on the Tech Tips: http://developers.sun.com/contact/feedback.jsp?category=newslet Subscribe to the following newsletters for the latest information about technologies and products in other Java platforms:
IMPORTANT: Please read our Terms of Use, Privacy, and Licensing policies: http://www.sun.com/share/text/termsofuse.html http://www.sun.com/privacy/ http://developer.java.sun.com/berkeley_license.html ARCHIVES: You'll find the Enterprise Java Technologies Tech Tips archives at: http://java.sun.com/developer/EJTechTips/index.html © 2006 Sun Microsystems, Inc. All Rights Reserved. For information on Sun's trademarks see: http://sun.com/suntrademarks Java, J2EE, J2SE, J2ME, and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. |
Please unsubscribe me from this newsletter. |
Born on shraavana shudha chauthi of dundubhi naama samvaswara, Im kiran alias kini alias kiri bought up by loving parents. Being from agricultural family I have learnt plowing, carting but never learnt climbing trees. Now away from home I have lost touch with the agricultural skills.