You are viewing a plain text version of this content. The canonical link for it is here.
Posted to soap-dev@xml.apache.org by du...@apache.org on 2001/01/26 16:12:43 UTC

cvs commit: xml-axis/java/src/org/apache/axis/handlers RPCDispatchHandler.java

dug         01/01/26 07:12:43

  Modified:    java     README
               java/src/org/apache/axis AxisFault.java
               java/src/org/apache/axis/handlers RPCDispatchHandler.java
  Added:       java/samples/stockquote GetQuote.java README
                        StockQuoteService.java deploy.xml
  Removed:     java/src/org/apache/axis/client GetQuoteClient.java
  Log:
  Added a stockquote sample.
  Made RPCDispatchHandler more generic - but still only works for strings.
    (return type can be anything that looks nice when toString is call)
  Updated README
  
  Revision  Changes    Path
  1.2       +4 -7      xml-axis/java/README
  
  Index: README
  ===================================================================
  RCS file: /home/cvs/xml-axis/java/README,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- README	2001/01/26 12:54:52	1.1
  +++ README	2001/01/26 15:12:38	1.2
  @@ -26,16 +26,17 @@
   EchoClient:
     - Just send the server an xml file and get it back
   
  -GetQuoteClient:
  -  - Hardcoded client for stock quote sample
  -
   TestClient:
     - Allows you to run a service - actually it just allows you to
       specify the SOAPAction header line on the command line.  It sends
       a static SOAPEnvelope.
   
   
  +** Also, look at the samples\stockquote example.  This is a full blown
  +** service in action!
  +
   
  +
   Format of deployment xml files:
   -------------------------------
   (This is temporary until WSDD is ready)
  @@ -88,8 +89,4 @@
   Any of these allow for 'option' sub-elements to allow for name/value pairs
   of options.
   
  -
  -Misc:
  ------
  -GetQuoteClient actually works!!  It's very very hardcoded but it works.
   
  
  
  
  1.1                  xml-axis/java/samples/stockquote/GetQuote.java
  
  Index: GetQuote.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights 
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer. 
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:  
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Axis" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written 
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation and was
   * originally based on software copyright (c) 1999, International
   * Business Machines, Inc., http://www.ibm.com.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  package samples.stockquote ;
  
  import java.net.*;
  import java.io.*;
  import java.util.*;
  
  public class GetQuote {
  
    public static void main(String args[]) {
  
      String hdr = "POST /axis/servlet/AxisServlet HTTP/1.0\n" +
                   "Host: localhost:8080\n" +
                   "Content-Type: text/xml;charset=utf-8\n" +
                   "SOAPAction: urn:xmltoday-delayed-quotes\n";
  
      String msg ;
      String msg1 = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"" +
                    "http://schemas.xmlsoap.org/soap/envelope/\" " +
                    "xmlns:xsi=\"http://www.w3.org/1999/XMLSchema-instance\"" + 
                    " xmlns:xsd=\"http://www.w3.org/1999/XMLSchema\">\n" +
                    "<SOAP-ENV:Body>\n" +
                    "<m:getQuote xmlns:m=\"urn:xmltoday-delayed-quotes\">" ;
      String msg2 = "</m:getQuote>" +
                    "</SOAP-ENV:Body>\n" +
                    "</SOAP-ENV:Envelope>" ;
  
      try {
        String  host   = "localhost" ;
        int     port   = 8080 ;
        String  symbol = null ;
  
        for ( int i = 0 ; i < args.length ; i++ ) {
          if ( args[i].charAt(0) == '-' ) {
            switch( args[i].toLowerCase().charAt(1) ) {
              case 'h': if ( args[i].length() > 2 )
                          host = args[i].substring(2);
                        break ;
              case 'p': if ( args[i].length() > 2 )
                          port = Integer.parseInt(args[i].substring(2));
                        break ;
              default: System.err.println( "Unknown option '" + 
                                           args[i].charAt(1) + "'" );
                       System.exit(1);
            }
          }
          else
            symbol = args[i] ;
        }
  
        if ( symbol == null ) {
          System.err.println( "Usage: GetQuote <symbol>" );
          System.exit(1);
        }
  
        msg = msg1 + "<symbol>" + symbol + "</symbol>" + msg2 ;
  
        String         cl = "Content-Length: " + msg.length() + "\n\n" ;
        Socket         sock = new Socket( host, port );
        InputStream    inp = sock.getInputStream();
        OutputStream   out = sock.getOutputStream();
        byte           b ;
  
        out.write( hdr.getBytes() );
        out.write( cl.getBytes() );
        out.write( msg.getBytes() );
  
        while ( (b = (byte) inp.read()) != -1 ) 
          System.out.write( b );
      }
      catch( Exception e ) {
        System.err.println( e );
      }
    };
  
  };
  
  
  
  1.1                  xml-axis/java/samples/stockquote/README
  
  Index: README
  ===================================================================
  Deploy:
    java org.apache.axis.client.AdminClient deploy.xml
  
  Usage:
    java samples.stockquote.GetQuoteClient <symbol>
  
  
  
  1.1                  xml-axis/java/samples/stockquote/StockQuoteService.java
  
  Index: StockQuoteService.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 2000 The Apache Software Foundation.  All rights 
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer. 
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:  
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "AXIS" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written 
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation and was
   * originally based on software copyright (c) 2000, International
   * Business Machines, Inc., http://www.apache.org.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  package samples.stockquote ;
  
  import java.io.*;
  import java.net.URL;
  
  import org.w3c.dom.*;
  import org.xml.sax.InputSource ;
  import org.apache.xerces.parsers.* ;
  import org.apache.xerces.framework.* ;
  
  /**
   * See \samples\stockquote\readme for info.
   *
   * @author Sanjiva Weerawarana (sanjiva@watson.ibm.com)
   */
  public class StockQuoteService {
    public float getQuote (String symbol) throws Exception {
      // get a real (delayed by 20min) stockquote from 
      // http://www.xmltoday.com/examples/stockquote/. The IP addr 
      // below came from the host that the above form posts to ..
  
      URL          url = new URL( "http://www.xmltoday.com/examples/" +
                                  "stockquote/getxmlquote.vep?s="+symbol );
  
      DOMParser    parser = new DOMParser();
      InputSource  inp    = new InputSource( url.openStream() );
  
      parser.parse( inp );
  
      Document     doc  = parser.getDocument();
      Element      elem = doc.getDocumentElement ();
      NodeList     list = elem.getElementsByTagName( "price" );
  
      elem = (Element) list.item( 0 );
      String quoteStr = elem.getAttribute ("value");
      try {
        return Float.valueOf(quoteStr).floatValue();
      } catch (NumberFormatException e1) {
        // maybe its an int?
        try {
          return Integer.valueOf(quoteStr).intValue() * 1.0F;
        } catch (NumberFormatException e2) {
          return -1.0F;
        }
      }
    }
  }
  
  
  
  1.1                  xml-axis/java/samples/stockquote/deploy.xml
  
  Index: deploy.xml
  ===================================================================
  <!-- Use this file to deploy some handlers/chains and services  -->
  <!-- Two ways to do this:                                       -->
  <!--   java org.apache.axis.utils.Admin deploy.xml              -->
  <!--      from the same dir that the Axis engine runs           -->
  <!-- or                                                         -->
  <!--   java org.apache.axis.client.AdminClient deploy.xml       -->
  <!--      after the axis server is running                      -->
  <!-- This file will be replaced by WSDD once it's ready         -->
  
  <deploy>
    <service name="urn:xmltoday-delayed-quotes" handler="JavaRPC" >
      <option name="className" value="samples.stockquote.StockQuoteService" />
      <option name="methodName" value="getQuote" />
    </service>
  </deploy>
  
  
  
  1.3       +1 -0      xml-axis/java/src/org/apache/axis/AxisFault.java
  
  Index: AxisFault.java
  ===================================================================
  RCS file: /home/cvs/xml-axis/java/src/org/apache/axis/AxisFault.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- AxisFault.java	2001/01/24 14:38:41	1.2
  +++ AxisFault.java	2001/01/26 15:12:40	1.3
  @@ -107,6 +107,7 @@
     }
   
     public void setFaultDetails(Element[] details) {
  +    if ( details == null ) return ;
       faultDetails = new Vector( details.length );
       for ( int loop = 0 ; loop < details.length ; loop++ )
         faultDetails.add( details[loop] );
  
  
  
  1.2       +28 -23    xml-axis/java/src/org/apache/axis/handlers/RPCDispatchHandler.java
  
  Index: RPCDispatchHandler.java
  ===================================================================
  RCS file: /home/cvs/xml-axis/java/src/org/apache/axis/handlers/RPCDispatchHandler.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- RPCDispatchHandler.java	2001/01/26 01:18:39	1.1
  +++ RPCDispatchHandler.java	2001/01/26 15:12:42	1.2
  @@ -76,41 +76,46 @@
     public void invoke(MessageContext msgContext) throws AxisFault {
       System.err.println("In RPCDispatchHandler");
   
  -    Document  doc = (Document) msgContext.getIncomingMessage().getAs("Document");
  -
       // Find the service we're invoking
       Handler service = (Handler) msgContext.getProperty( "ServiceHandler" );
   
       // Now get the service specific info 
  -    String  cls    = (String) service.getOption( "className" );
  -    String  method = (String) service.getOption( "methodName" );
  +    String  clsName    = (String) service.getOption( "className" );
  +    String  methodName = (String) service.getOption( "methodName" );
   
  -    
       try {
  -      Class        c = Class.forName(cls);
  -      Object       o = c.newInstance();
  -      SOAPEnvelope env = null ;
  -      env = (SOAPEnvelope) msgContext.getIncomingMessage().getAs("SOAPEnvelope");
  -      RPCBody body = env.getAsRPCBody();
  -      System.err.println( "Method: " + body.getMethodName() );
  -      Vector    args = body.getArgs();
  -      String    str  = ((RPCArg)args.get(0)).getValue();
  -      Float    result ;
  -
  -      Method m = c.getMethod( body.getMethodName(), new Class[] {str.getClass()} );
  -      result = (Float) m.invoke( o, new Object[] { str } );
  -      System.err.println("result=" + result );
  +      // We know we're doing a java call so find the class, create a
  +      // new instance of it.
  +      int          i ;
  +      Class        cls   = Class.forName(clsName);
  +      Object       obj   = cls.newInstance();
  +      Message      inMsg = msgContext.getIncomingMessage();
  +      SOAPEnvelope env   = (SOAPEnvelope) inMsg.getAs("SOAPEnvelope");
  +      RPCBody      body  = env.getAsRPCBody();
  +      String       mName = body.getMethodName();
  +      Vector       args  = body.getArgs();        //RPCArg's
  +
  +      if ( !methodName.equals(mName) )
  +        throw new AxisFault( "AxisServier.error", "Method names don't match",
  +                             null, null );  // Should they??
  +
  +      Class[]  argClasses = new Class[ args.size() ];
  +      Object[] argValues  = new Object[ args.size()];
  +      for ( i = 0 ; i < args.size() ; i++ ) {
  +        argClasses[i] = Class.forName("java.lang.String") ;
  +        argValues[i]  = ((RPCArg)args.get(i)).getValue() ; // only String 4now
  +      }
  +
  +      Method method = cls.getMethod( mName, argClasses );
  +      Object objRes = method.invoke( obj, argValues );
   
  -      Message msg = new Message( result.toString(), "String" );
  -      msgContext.setOutgoingMessage( msg );
  +      Message outMsg = new Message( objRes.toString(), "String" );
  +      msgContext.setOutgoingMessage( outMsg );
       }
       catch( Exception e ) {
         e.printStackTrace();
         throw new AxisFault( e );
       }
  -
  -    // Right now its just an echo - but I'll add more once the
  -    // reader/writer is there
     }
   
     public void undo(MessageContext msgContext) {