Hitachi

Cosminexus V11 アプリケーションサーバ SOAPアプリケーション開発の手引


6.5 クライアント側の処理を実装する

実装用の新規クラスを作成し,クライアント側の処理を実装します。クライアント側の処理の実装は,SAAJを使用してください。実装後に新しく作成したクラスをコンパイルします。

クライアント側の処理の実装例を次に示します。

package messagesampleclient;
 
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
 
import javax.xml.soap.AttachmentPart;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPFactory;
import javax.xml.soap.SOAPMessage;
 
import com.cosminexus.cws.management.Management;
import com.cosminexus.cws.management.ClientID;
 
public class MessageSampleClient extends HttpServlet
{
    private static final String CONTENT_TYPE = "text/html; charset=Shift_JIS";
 
    //Client Identifier
    private ClientID cltID = null;
 
    public void init() throws ServletException
    {
        //Initializing the client
        cltID = Management.initializeClient();
    }
 
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        //Service endpoint url
        String SERVICE_URL = "http://localhost:8080/MessageSampleServer/services/MsgService";
 
        //Response message output 
        String strResultMessage = null;
        String strResultMessageAttach = null;
 
        //Connecting client to the current thread
        Management.connectClientIDtoCurrentThread(cltID);
 
        //Getting the parameters set by user 
        String strName = request.getParameter("Name");
        if (strName == null)
        {
            strName = "";
        }
 
        String strSection = request.getParameter("Section");
        if (strSection == null)
        {
            strSection = "";
        }
 
        String strTelephone = request.getParameter("Telephone");
        if (strTelephone == null)
        {
            strTelephone = "";
        }
 
        //Request and Response message
        SOAPMessage msgRequest, msgResponse;        
 
        try
        {
            //Creating the soapmessage using MessageFactory
            MessageFactory msgfactory = MessageFactory.newInstance();
            msgRequest = msgfactory.createMessage();
 
            //Creating the factory instance
            SOAPFactory factory = SOAPFactory.newInstance();
 
            //Adding retrieved parameters to the body
            SOAPBody bdy = msgRequest.getSOAPBody();
            SOAPElement bdyElmName = bdy.addChildElement("Name", "", "http://localhost");
            bdyElmName.addTextNode(strName);
            SOAPElement bdyElmSection = bdy.addChildElement("Section", "", "http://localhost");
            bdyElmSection.addTextNode(strSection); 
            SOAPElement bdyElmTelephone = bdy.addChildElement("Telephone", "", "http://localhost");
            bdyElmTelephone.addTextNode(strTelephone); 
            SOAPElement bdyElmAttachment = bdy.addChildElement("Attachment");
            bdyElmAttachment.setAttribute("href","attachment.txt");
 
            //Getting the path of the file to be attached
            String UserDir = System.getProperty("user.dir");
            String strFileSeperator=File.separator;
            String strContextPath =  (String)getServletConfig().getServletContext().getRealPath("/");
            String strAttachment_c4webcl_Dir = strContextPath + "attachment.txt";
            //Adding an attachment to the request message
            AttachmentPart apPart = msgRequest.createAttachmentPart(new java.io.FileInputStream(new File(strAttachment_c4webcl_Dir)),"text/plain");
            apPart.setContentLocation("attachment.txt");
            msgRequest.addAttachmentPart(apPart);
 
            //Create Connection
            SOAPConnectionFactory connFactory = SOAPConnectionFactory.newInstance();
            SOAPConnection conn = connFactory.createConnection();
            //Invoke the Service
            msgResponse = conn.call(msgRequest, SERVICE_URL);
 
            //Retrieving the body elements 
            SOAPBody body = msgResponse.getSOAPBody();
            Iterator itr = body.getChildElements();
 
            if (itr.hasNext())
            {
                SOAPElement elmResult = (SOAPElement)itr.next();
                strResultMessage = elmResult.getFirstChild().getNodeValue();
                if ( itr.hasNext() )
                {
                    SOAPElement elmResultAttach = (SOAPElement)itr.next();
                    strResultMessageAttach = elmResultAttach.getFirstChild().getNodeValue();
                }
            }
        }
        catch( Exception exp )
        {
            //Displaying the exception  
            response.setContentType(CONTENT_TYPE);
            PrintWriter out = response.getWriter();
            out.println("<html>");
            out.println("<head><title>MessageSampleClient</title></head>");
            out.println("<body>");
            out.println("Exception Occurred");
            out.println("<BR>Message : "+exp.getMessage());
            out.println("</body></html>");
            return;
        }
        finally
        {
            //Disconnecting the client thread
            Management.disconnectClientIDtoCurrentThread(cltID);
        }
 
        //Displaying the required output message 
        response.setContentType(CONTENT_TYPE);
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head><title>MessageSampleClient</title></head>");
        out.println("<body>");
        out.println("\"" + strResultMessage + "\" was sent by Client.");
        out.println("<BR><BR>Service is invoked.<BR>");
        out.println("<BR>Attachment's contents are : \n" + strResultMessageAttach + "<BR>");
        out.println("</body></html>");
 
    }
 
    public void destroy()
    {
        //Finalizing the client
        Management.finalizeClient(cltID);
    }
}

なお,これ以外の実装例については,次に示すディレクトリに格納しているサンプルプログラムを参照してください。ただし,サンプルプログラムとして提供するのは,SOAPアプリケーション開発支援機能だけです。

<Application Serverのインストールディレクトリ>/c4web/samples