package com.oreilly.jent.people.soap;

/**
 * In general, you may use the code in this book in your programs and 
 * documentation. You do not need to contact us for permission unless 
 * you're reproducing a significant portion of the code. For example, 
 * writing a program that uses several chunks of code from this book does 
 * not require permission. Selling or distributing a CD-ROM of examples 
 * from O'Reilly books does require permission. Answering a question by 
 * citing this book and quoting example code does not require permission. 
 * Incorporating a significant amount of example code from this book into 
 * your product's documentation does require permission.
 * 
 * We appreciate, but do not require, attribution. An attribution usually 
 * includes the title, author, publisher, and ISBN. For example: 
 * 
 *   "Java Enterprise in a Nutshell, Third Edition, 
 *    by Jim Farley and William Crawford 
 *    with Prakash Malani, John G. Norman, and Justin Gehtland. 
 *    Copyright 2006 O'Reilly Media, Inc., 0-596-10142-2."
 *  
 *  If you feel your use of code examples falls outside fair use or the 
 *  permission given above, feel free to contact us at 
 *  permissions@oreilly.com.
 */

import java.rmi.RemoteException;

import javax.xml.namespace.QName;
import javax.xml.rpc.Call;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceException;
import javax.xml.rpc.ServiceFactory;

public class EchoDIIClient {
    public static void main(String[] args) {
        // The endpoint, service, port and operation we want to invoke
        final String endpoint = 
            "http://localhost:9090/JEnt-examples/services/echo";
        final String serviceName =    "EchoService";
        final String portName =       "echo";
        final String operationName =  "echo";
        
        // Some namespaces we'll be using in our client call
        final String serviceNS =      "http://localhost:8080/JEnt-examples/services/echo";
        final String xsdNS =          "http://www.w3.org/2001/XMLSchema";
        final String jaxrpcNS =       "http://java.sun.com/jax-rpc-ri/internal";
        
        // Create qualified names for the service, port and operation
        QName serviceQName = new QName(serviceNS, serviceName);
        QName portQName = new QName(serviceNS, portName);
        QName operationQName = new QName(serviceNS, operationName);
        
        try {
            // Make a service factory
            ServiceFactory factory = ServiceFactory.newInstance();
            // Create a reference to the service
            //Service service = factory.createService(endpointURL, serviceQName);
            Service service = factory.createService(serviceQName);
            // Create a call to a particular port on the service
            Call call = service.createCall(portQName);
            // Set the endpoint URL for the call
            call.setTargetEndpointAddress(endpoint);
            
            // Set the return type of the operation we want to invoke
            call.setReturnType(new QName(xsdNS, "string"));
            // Set the operation we want to invoke
            call.setOperationName(operationQName);
            // Set the type of the single parameter to the operation
            call.addParameter("message", new QName(xsdNS, "string"),
                              ParameterMode.IN);
            // Create the actual operation arguments and put them in an
            // object array
            String msg = "Hi there";
            Object[] opParams  = {msg};
            // Invoke the operation and collect the result
            String resp = (String)call.invoke(opParams);
            System.out.println("Sent: \"" + msg +
                               "\", got response \"" + resp + "\"");
            
        }
        catch (ServiceException se) {
            System.err.println("Service generated an error: " + se.getMessage());
        }
        catch (RemoteException re) {
            System.err.println("Communication error: " + re.getMessage());
        }
    }
}
