The WSIT Tutorial

Chapter 5 Message Optimization

This chapter provides instructions on how to configure message optimization in web service providers and clients.


Note –

Because of the special encoding/decoding requirements for message optimization, if a service uses message optimization, then a client of that service must support message optimization. Most web services stacks do support message optimization. In the rare case when you think that a legacy client, which does not support optimization, will access your service, do not use message optimization. In general, however, it is a safe and good practice to use message optimization.


This chapter covers the following topics:

Creating a Web Service

The starting point for developing a web service to use WSIT is a Java class file annotated with the javax.jws.WebService annotation.

For detailed instructions for how to use NetBeans IDE to create a web service, see Creating a Web Service.

Configuring Message Optimization in a Web Service

To use the IDE to configure a web service for message optimization, perform the following steps.

ProcedureTo Configure Message Optimization in a Web Service

  1. In the IDE Projects window, expand the Web Services node, right-click the CalculatorWS node, and choose Edit Web Service Attributes.

    The Web Service Attributes editor appears.

  2. Select the Optimize Transfer of Binary Data (MTOM) check box, as shown in Figure 5–1, and click OK.

    This setting configures the web service to optimize messages that it transmits and to decode optimized messages that it receives.

    Figure 5–1 Enabling MTOM

    Screen shot of MTOM checkbox

Deploying and Testing a Web Service with Message Optimization Enabled

Now that you have configured the web service to use message optimization, you can deploy and test it.

ProcedureTo Deploy and Test a Web Service with Message Optimization Enabled

To deploy and test the web service, perform the following steps.

  1. Right-click the project node and select Properties, then select Run.

  2. Type /CalculatorWSService?wsdl in the Relative URL field and click OK.

  3. Right-click the project node and choose Run Project.

    The IDE starts the web container, builds the application, and displays the WSDL file page in your browser.

Creating a Client to Consume a WSIT-enabled Web Service

Now that you have built and tested a web service that uses the WSIT Message Optimization technology, you can create a client that accesses and consumes that web service. The client will use the web service’s WSDL to create the functionality necessary to satisfy the interoperability requirements of the web service.

ProcedureTo Create a Client to Consume a WSIT-enabled Web Service

To create a client to access and consume the web service, perform the following steps.

  1. Choose File->New Project, select Web Application from the Web category, and click Next.

  2. Give the project a name, such as CalculatorWSServletClient.

  3. Make sure that the J2EE version is set to Java EE 5, then click Finish.

  4. Right-click the CalculatorWSServletClient node and select New ->Web Service Client.

    The New Web Service Client window appears.

  5. Cut and paste the URL of the web service that you want the client to consume into the WSDL URL field.

    For example, you might specify http://localhost:8080/CalculatorApplication/CalculatorWSService?wsdl, the URL of the CalculatorWS web service.

  6. Type org.me.calculator.client in the Package field and click Finish.

    The Projects tab displays the new web service client.

  7. Right-click the CalculatorWSServletClient project node and choose New->Servlet.

  8. Name the servlet ClientServlet, specify the package name (for example, org.me.calculator.client), and click Finish.

  9. To make the servlet the entry point to your application, right-click the project node, choose Properties, click Run, type /ClientServlet in the Relative URL field, and click OK.

  10. Double-click ClientServlet.java so that it opens in the Source Editor.

  11. In the Source Editor, remove the line that comments out the body of the processRequest method.

    This is the start-comment line that starts the section that comments out the code:

    /* TODO output your page here
  12. Delete the end-comment line that ends the section of commented out code:

    */
  13. Add some empty lines after the following line:

    out.println("<h1>Servlet ClientServlet at " +
             request.getContextPath () + "</h1>");
  14. Right-click in one of the empty lines that you added. Choose Web Service Client Resources->Call Web Service Operation.

    The Select Operation to Invoke dialog box appears.

  15. Browse to the Add operation and click OK.

    The processRequest method looks as follows (the added code is in bold below):

    protected void processRequest(HttpServletRequest
            request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet ClientServlet</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet ClientServlet at "
                 + request.getContextPath () + "</h1>");
        try { // Call Web Service Operation
            org.me.calculator.client.CalculatorWS port = service.getCalculatorWSPort();
            // TODO initialize WS operation arguments here
            int i = 0;
            int j = 0;
            // TODO process result here
            int result = port.add(i,  j);
            system.out.println("Result = "+result);
        } catch (Exception ex) {
            // TODO handle custom exceptions here
        }    out.println("</body>");
        out.println("</html>");
        out.close();
    }
  16. Change the value for int i and int j to other numbers, such as 3 and 4.

  17. Change the System.out.println statement to out.println.

  18. Add a line that prints out an exception, if an exception is thrown.

    The try/catch block should look as follows (new and changed lines are highlighted in bold text):

    try { 
        // Call Web Service Operation
        org.me.calculator.client.CalculatorWS port = service.getCalculatorWSPort();
             // TODO initialize WS operation arguments here
        int i = 3;
        int j = 4;
        // TODO process result here
        int result = port.add(i, j);
        out.println("<p>Result: " + result);
    } catch (Exception ex) {
        out.println("<p>Exception: " + ex);
    }
  19. Right-click the project node and choose Run Project.

    The server starts (if it was not running already) the application is built and deployed, and the browser opens and displays the calculation result.

Message Optimization and Secure Conversation

The Web Services Secure Conversation technology has message optimization benefits. While providing better message-level security it also improves the efficiency of multiple-message exchanges. It accomplishes this by providing basic mechanisms on top of which secure messaging semantics can be defined for multiple-message exchanges. This feature allows for contexts to be established so that potentially more efficient keys or new key material can be exchanged. The result is that the overall performance of subsequent message exchanges is improved.

For more information on how to use Secure Conversation, see Chapter 7, Using WSIT Security.