Personal favorites Lotuser advanced series-use Lotus Domino Java proxy to consume Web Services

Source: Internet
Author: User
As a new feature of IBM Lotus Domino 7, Web service design elements are the main way to publish services to the world. But do you know how easy it is to use the Apache Axis framework to consume Web Services? This article describes how to use Apache Axis and Lotus Domino 7 to create Web service consumers.

A Web Service is a system design that allows two or more computers to interact with each other in the network. The main advantage of this service is that it is a standard solution for sending objects between multiple computers with different operating systems and application servers. For example, our company uses Web Services to publish articles from a computer running Microsoft. NET Framework to an IBM Lotus Domino-based intranet. We recently completed another project to publish a special report on Web services on the Lotus Domino 7 server. The only thing we have to negotiate with is the Web Services Description Language (WSDL) file.

This article shows you how to create a Domino Java proxy to consume Web Services. It uses Apache Axis to generate Java stubs and read the WSDL file. We provide a database example that contains Web services that can be used in the following sections of this article. For more information, see "Download.

This document assumes that you are an experienced Lotus Notes and Domino application developer and are familiar with the new Domino Web service design elements. To learn more about the new Domino Web service design elements, see the developerWorks Lotus article "Lotus Notes/Domino 7 Web Services ".

Web Service Components

The Web Service contains several components. These components include:

  • WSDL File
  • Service-Oriented Architecture (SOA)
  • Various protocols

 

WSDL File

WSDL is a Web service installation adhesive (glue ). Each Web Service publishes a WSDL file that contains information about the methods and independent variables it can process and the objects returned by different methods. This document uses a database containing a simple Web Service (in this instance, it is the Lotus Domino 7 database ).

Although this Web Service is simple, the WSDL file of this service is very detailed. Of course, the reason for this details is that the WSDL file uses the detailed format, XML. However, considering that the WSDL file contains descriptions of all objects sent and received by the Web service, the file size is not too large. The WSDL file of the Web service example in this article has a Java class named ComplexClass. The code snippet is as follows:

<complexType name="MyComplexClass">            <sequence>            <element name="alpha" nillable="true" type="xsd:string"/>            <element name="calcA" type="xsd:int"/>            <element name="calcB" type="xsd:int"/>            <element name="result" nillable="true" type="xsd:int"/>            </sequence>            </complexType>

Service-Oriented Architecture

Service-Oriented Architecture (SOA) has appeared in the past decade. Simply put, SOA is a software architecture that focuses more on business rather than public, technology-driven solutions. It is basically designed as a consumer-provider architecture for Web Services. The SOA method separates various layers. Consumers interact with business objects through the service layer rather than directly interacting.

For more information about SOA, see the SOA and Web services beginner page on developerWorks SOA and Web services.

Protocols used by Web Services

Web service messages are not restricted by HTTP. It can also use SMTP, Extensible Messaging and Presence Protocol (XMPP), or even FTP. However, SOAP must be observed; otherwise, it is difficult for the receiving system to interpret messages. On older systems, you can find XML-Remote Procedure Call (XML-RPC), but SOAP is currently the correct protocol.

The advantage of Web services is that you do not have to study various protocols and standards: all these are handled by application servers and development tools. In this example, Apache Axis helps you explain WSDL and generate Java code stubs.

Database example

Before using the Apache Axis tool, you must have a Web service that publishes the WSDL file. In the "Download" section of this article, there are two database examples: WebServiceConsumer. nsf and MyWebService. nsf. Their names indicate their functions. After downloading MyWebService. nsf to the Lotus Domino 7 server, mark the Web service design element-MyWebservice with the ID that can run the restricted proxy. WebServiceConsumer. nsf is for reference only.



Back to Top

Apache Axis

Apache Axis is a complete framework that includes the SOAP server and many different tools that generate and deploy Web Services. The communication part and the WSDL2Java method are used in this article. However, you must first install Apache Axis.

Prerequisites

Before you can build, test, and deploy the solution described in this article, you must meet the following conditions:

  • Lotus Domino 7 server and IBM Lotus Domino Designer
  • Java software development kit (JDK)
  • Apache Axis framework

 

Assume that you have installed Lotus Domino 7 and you know how to obtain and run the Java environment. (The "references" section of this article provides all links that must be downloaded .)

Table 1 shows the file structure used in our system. If you choose to copy this structure, you can reuse the batch file and class path mentioned later, or you can choose to use your own policy. We use a working name for each directory. A batch file is driven by variables, so you can easily modify one or two lines of code to run it in your system.

Table 1. Recommended file structure

Work name Path
Server Path C: \ lotus \ DominoR7StandAlone
JDK version 2 C: \ java \ j2re1.4.2 _ 09
Apache Axis C: \ java \ Axis
Working directory C: \ java \ consumer

Download and install Apache Axis

After downloading Apache Axis, decompress the software package to the Axis directory. (See the recommended path in Table 1 .) This article uses two features provided by Apache Axis. One is communication class. In fact, you do not need to know anything except to import them into the Domino environment. Another feature is the wsdl2java tool. Apache Axis can use this tool to read the WSDL file and generate a complete Java stub for all required methods and objects.

You can start the shell and run the Apache Axis class. However, it is inconvenient to start each command to use a class path with more than 100 characters. One solution is to set the complete class path required for Apache Axis in the general Java class path, but we prefer another solution: Maintain the batch file for setting the class path. The following code snippet shows a batch file named wsdl2java. bat, which exists in the working directory.

@echo OFF            rem *******************************************************            rem ***     wsdl2java.bat                              ****            rem *******************************************************            set AXISPATH=c:\java\AXIS\lib            set JAVAPATH=C:\java\j2re1.4.2_09            set DOMINOJAVAPATH=C:\lotus\DominoR7StandAlone\jvm\lib\ext            set CP=%AXISPATH%\axis.jar            set CP=%CP%;%AXISPATH%\commons-logging-1.0.4.jar            set CP=%CP%;%AXISPATH%\commons-discovery-0.2.jar            set CP=%CP%;%AXISPATH%\jaxrpc.jar            set CP=%CP%;%AXISPATH%\saaj.jar            set CP=%CP%;%DOMINOJAVAPATH%\mail.jar            set CP=%CP%;%DOMINOJAVAPATH%\activation.jar            set CP=%CP%;%AXISPATH%\wsdl4j-1.5.1.jar            %JAVAPATH%\bin\java -cp %CP% org.apache.axis.wsdl.WSDL2Java %1


Back to Top

Create a Web service consumption proxy

Create a proxy by obtaining the WSDL file from the Web service. Assume that you are using the provided Web Service-MyWebservice-and have copied it to the Lotus Domino 7 server. To view the WSDL file, point the browser to the database and run the following command? Open the Web service in WSDL, as shown in 1.

Figure 1. Example of displaying a WSDL file in a browser

After confirming that the WSDL file appears, you can save the WSDL file to the working directory, or use the URL to direct the Apache Axis toolkit to the WSDL resource in the network. Now, you can start generating Java stubs from the WSDL file.



Back to Top

Generate a Java file from the WSDL File

The WSDL2Java function can take the file name as a parameter, as shown below:

C:\java\consumer>wsd12java.bat MyWebservice.xml

Or use the complete Uniform Resource Indicator (URI) of the WSDL file as the parameter:

C:\java\consumer>wsd12java.bat http://localhost/mywebservice.nsf/MyWebService?WSDL

To work locally, download and save the WSDL file to the working directory using the name MyWebservice. xml, as shown in the first command.

Note:The Web service we use is only for demonstration purposes (for example, the namespace is not used ). Therefore, you can see a new directory named DefaultNamespace in the working directory.

The running result of wsdl2java. bat is to generate five Java files:

  • DominoSoapBindingStub. java
  • HelloWorld. java
  • HelloWorldService. java
  • HelloWorldServiceLocator. java
  • MyComplexClass. java

 

Only two of these five files are really important: HelloWorld. java and MyComplexClass. java. The other three are used to execute the call and parse the response. If you want to learn more about the Apache Axis framework, we recommend that you visit Apache Axis wiki.

HelloWorld. java is an interface that describes the methods and independent variables that Web services can process. MyComplexClass. java is the JavaBeans version of The MyComplexClass object. When the getMyComplexClass () method is called, the Web Service uses it to respond.

Now, create a new script library in database Domino 7 and use it to consume Web Services.



Back to Top

Import Java files to Lotus Notes

The Web service consumption agent uses files generated by Apache Axis. The easiest way to import a Java file is to create a Java Script Library (do not confuse it with JavaScriptlibrary ). Import the file and remember to set the working directory as the basic path (see figure 2 ).

Figure 2. Save the Java stub script library generated by Apache Axis

Note:The basic directory is the working directory rather than the DefaultNamespace Directory, which is very important.

After the script library is put in a proper position and compiled, you can close it and create a new Java proxy. This proxy uses the Apache Axis client class. These classes are part of the Axis framework and must be imported.

The Apache Axis client is located in the Java archive file (JAR file) under the Axis directory. There are some options to publish these classes to the proxy, one of which is to directly import these classes in the proxy. But because of the natural nature of these classes-you can use them multiple times when you discover how easy Web consumption is-stored in other JAR files of IBM Lotus Notes for use. the \ jvm \ lib \ ext directory is a more natural method.

In the Lotus Notes Client Directory, copy the JAR file axis. jar, commons-discovery-0.2.jar, and commons-logging-1.0.4.jar from. \ Axis \ lib to. \ jvm \ lib \ ext. (You must restart the Lotus Notes client before using these additional files .) When Lotus Domino Designer is running, there are only a few steps left before calling the Web service.



Back to Top

Call the Lotus Notes agent of Web Services

After the JAR file is stored in the. \ jvm \ lib \ ext directory and the Lotus Domino Designer has been restarted, You can compile the Web consumption proxy code. Start with creating a common Java proxy. Next, open the Edit Project window to import the previously created script library. Close the Project Properties window and add required import lines as follows:

00 import lotus.domino.*;            01 import java.net.URL;            02 import org.apache.axis.client.Call;            03 import org.apache.axis.client.Service;            04 import DefaultNamespace.*;            05 import java.io.*;            06 public class JavaAgent extends AgentBase {            07  public void NotesMain() {            08   try {            09    Session session = getSession();            10    AgentContext agentContext = session.getAgentContext();            11            12    URL  endpoint = new URL("http://localhost:80/MyWebService.nsf/MyWebService");            13            14    Service service1 = new Service();            15    DominoSoapBindingStub service = new DominoSoapBindingStub(endpoint,service1);            16    String greeting=service.getGreeting();            17    System.out.println(greeting);            18            19    String greeting2=service.getGreeting("Lotus");            20    System.out.println(greeting2);            21            22  } catch(Exception e) {            23     e.printStackTrace();            24   }            25 }            26}

In row 3, create a standard Java URL pointing to the Web service you want to use. In row 3, instantiate the Service () class provided by Apache Axis. Then, the DominoSoapBindingStub service is instantiated in row 15th and provided using the newly created service1 object and the URI of row 12th. In row 3, use the value provided by the Web service method getGreeting () to set a new string.

Save the proxy and start the Java console. (This console is the only place where you can see any activity now .) When the debug window is opened, select a new database and run the agent, as shown in 3.

Figure 3. Java console after the completion of Contemporary management

As you can see, using Apache Axis and wsdl2java tools saves a lot of time. Previously, you had to sit down and manually decode SOAP messages when consuming Web Services in the Lotus Domino 5 proxy!



Back to Top

Run the Web service consumption proxy on the server

Now we have a powerful proxy that can use Web Services for interaction. For example, your organization's Intranet can book the latest weather forecast in a more exotic and accurate way than the previous Comma Separated Value (CSV) file. Web services can also be used with expensive Enterprise Resource Planning (ERP) systems.

No other steps are required to run this proxy on the server. Make sure that the "Set runtime security level" attribute is Set to "2. Allows restricted operations" in the Agent Properties dialog box, and the signatory can have proper access permissions.

Copy the necessary JAR files described earlier to the. \ jvm \ lib \ ext path of the Lotus Domino server. However, if you call the Domino Web service on the same server that is running the proxy, it may experience a deadlock.

Conclusion

This document describes how to build a Domino proxy to consume Web Services. Using the Apache Axis framework can save a lot of time and focus on business value. Apache Axis has been used for a while and is stable and reliable. Using such a third-party tool makes the system more vulnerable, but this risk is far less than the benefits that SOA can bring to your organization.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.