You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by gd...@apache.org on 2001/08/17 22:44:01 UTC

cvs commit: xml-axis/java/test/utils TestXMLUtils.java

gdaniels    01/08/17 13:44:01

  Modified:    java/src/org/apache/axis/handlers JWSProcessor.java
               java/src/org/apache/axis/transport/http AxisServlet.java
               java/src/org/apache/axis/utils XMLUtils.java
               java/test/utils TestXMLUtils.java
  Added:       java/src/org/apache/axis/utils WSDLUtils.java
  Log:
  Enable auto-gen of WSDL (for JWS files only at the moment).
  
  Also revamp XMLUtils to enable sending documents to Writers
  as well as OutputStreams.
  
  Doing an HTTP GET on a JWS url will now return a little test page.
  Adding ?WSDL to the query string will cause the automatic
  generation of a WSDL file for the service, determined by introspection.
  
  This is a first cut, and I haven't tested the resultant WSDL against
  any clients yet.  There's a lot left to do, I just want to get this version
  checked in so I can continue work from home.
  
  Obvious things which need doing:
  
  - Support for non-JWS services (via URLMapper)
  - A more general framework for generating this data, which will
    allow handlers in a service's deployed processing path to
    influence the WSDL, indicating headers, encoding styles,
    etc...
  - Support deployment info for methods - right now we assume
    all methods are available.
  
  Revision  Changes    Path
  1.14      +6 -0      xml-axis/java/src/org/apache/axis/handlers/JWSProcessor.java
  
  Index: JWSProcessor.java
  ===================================================================
  RCS file: /home/cvs/xml-axis/java/src/org/apache/axis/handlers/JWSProcessor.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- JWSProcessor.java	2001/07/12 15:03:45	1.13
  +++ JWSProcessor.java	2001/08/17 20:44:00	1.14
  @@ -178,6 +178,12 @@
                   cl.registerClass( clsName, cFile );
               msgContext.setClassLoader( cl );
   
  +            if (msgContext.getProperty("is-http-get") != null) {
  +                Class c = cl.loadClass(clsName);
  +                msgContext.setProperty("JWSClass", c);
  +                return;
  +            }
  +
               /* Create a new RPCProvider - this will be the "service"   */
               /* that we invoke.                                                */
               /******************************************************************/
  
  
  
  1.35      +73 -4     xml-axis/java/src/org/apache/axis/transport/http/AxisServlet.java
  
  Index: AxisServlet.java
  ===================================================================
  RCS file: /home/cvs/xml-axis/java/src/org/apache/axis/transport/http/AxisServlet.java,v
  retrieving revision 1.34
  retrieving revision 1.35
  diff -u -r1.34 -r1.35
  --- AxisServlet.java	2001/08/15 21:30:17	1.34
  +++ AxisServlet.java	2001/08/17 20:44:00	1.35
  @@ -59,9 +59,11 @@
   import javax.servlet.* ;
   import javax.servlet.http.* ;
   import org.apache.axis.* ;
  +import org.apache.axis.registries.HandlerRegistry;
   import org.apache.axis.server.* ;
  -import org.apache.axis.utils.* ;
  +import org.apache.axis.utils.*;
   import org.apache.axis.message.*;
  +import org.w3c.dom.Document;
   
   /**
    *
  @@ -88,11 +90,78 @@
   
       public void doGet(HttpServletRequest req, HttpServletResponse res)
           throws ServletException, IOException {
  +
  +        ServletContext context = getServletConfig().getServletContext();
  +        MessageContext msgContext = new MessageContext(engine);
  +        HandlerRegistry hr = engine.getHandlerRegistry();
  +
  +        String realpath = context.getRealPath(req.getServletPath());
  +        if (realpath != null) 
  +            msgContext.setProperty(Constants.MC_REALPATH, realpath);
  +        
  +        if ((realpath!=null) && (realpath.endsWith(".jws"))) {
  +            try {
  +                // !!! Kludge for right now to allow the JWSProcessor to
  +                //     get the class without actually invoking it.
  +                msgContext.setProperty("is-http-get", "yes");
  +        
  +                Handler handler = hr.find("JWSProcessor");
  +                if (handler != null) {
  +                    handler.invoke(msgContext);
  +                    Handler serviceHandler = msgContext.getServiceHandler();
  +                    if (serviceHandler != null) {
  +                        res.getWriter().println("Got service " +
  +                                                serviceHandler);
  +                    }
  +                    Class cls = (Class)msgContext.getProperty("JWSClass");
  +                    
  +                    // !!! Need to make this an absolute URI
  +                    String url = req.getRequestURI();
  +                    
  +                    // !!! This should be something reasonable
  +                    String urn = "urn:service";
  +                    
  +                    // !!! This should come from the service itself
  +                    String description = "Some service";
  +                    
  +                    if (req.getParameter("WSDL") != null) {
  +                        res.setContentType("text/xml");
  +                        WSDLUtils.writeWSDLDoc(cls, url, urn, description,
  +                                          msgContext.getTypeMappingRegistry(),
  +                                          res.getWriter());
  +                        return;
  +                    } else {
  +                        res.setContentType("text/html");
  +                        res.getWriter().println("<h1>" + cls.getName() +
  +                                                "</h1>");
  +                        res.getWriter().println(
  +                               "<p>Hi there, this is an Axis service!</p>");
  +                        res.getWriter().println(
  +       "<i>Perhaps there'll be a form for invoking the service here...</i>");
  +                        res.getWriter().close();
  +                        return;
  +                    }
  +                }
  +            } catch (AxisFault fault) {
  +                res.getWriter().println("<pre>Fault - " + fault + " </pre>");
  +            } catch (Exception e) {
  +                  res.getWriter().println("<pre>Exception - " + e + "<br>");
  +                  e.printStackTrace(res.getWriter());
  +                  res.getWriter().println("</pre>");
  +            }
  +        }
  +
           res.setContentType("text/html");
  -        res.getWriter().println( "In doGet<p>" );
  -        res.getWriter().println(" TransportName = " + transportName);
  -    }
  +        res.getWriter().println( "<html><h1>Axis HTTP Servlet</h1>" );
  +        res.getWriter().println( "Hi, you've reached the Axis HTTP servlet." +
  +           "Normally you would be hitting this URL with a SOAP client " +
  +           "rather than a browser.");
   
  +        res.getWriter().println("<p>In case you're interested, my Axis " +
  +            "transport name appears to be '<b>" + transportName + "</b>'");
  +        res.getWriter().println("</html>");
  +    }
  +    
       public void doPost(HttpServletRequest req, HttpServletResponse res)
           throws ServletException, IOException {
           ServletConfig  config  = getServletConfig();
  
  
  
  1.15      +9 -5      xml-axis/java/src/org/apache/axis/utils/XMLUtils.java
  
  Index: XMLUtils.java
  ===================================================================
  RCS file: /home/cvs/xml-axis/java/src/org/apache/axis/utils/XMLUtils.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- XMLUtils.java	2001/07/19 16:20:42	1.14
  +++ XMLUtils.java	2001/08/17 20:44:01	1.15
  @@ -215,18 +215,22 @@
           return privateElementToString(doc.getDocumentElement(), false);
       }
   
  -    public static void privateElementToStream(Element element, OutputStream out,
  +    public static void privateElementToWriter(Element element, Writer writer,
                                                 boolean omitXMLDecl) {
  -        Writer writer = new OutputStreamWriter(out);
           DOM2Writer.serializeAsXML(element, writer, omitXMLDecl);
       }
       
  -    public static void ElementToStream(Element element, OutputStream out) {
  -        privateElementToStream(element, out, true);
  +    public static void ElementToWriter(Element element, Writer writer) {
  +        privateElementToWriter(element, writer, true);
       }
       
       public static void DocumentToStream(Document doc, OutputStream out) {
  -        privateElementToStream(doc.getDocumentElement(), out, false);
  +        Writer writer = new OutputStreamWriter(out);
  +        privateElementToWriter(doc.getDocumentElement(), writer, false);
  +    }
  +    
  +    public static void DocumentToWriter(Document doc, Writer writer) {
  +        privateElementToWriter(doc.getDocumentElement(), writer, false);
       }
   
       public static String getInnerXMLString(Element element) {
  
  
  
  1.1                  xml-axis/java/src/org/apache/axis/utils/WSDLUtils.java
  
  Index: WSDLUtils.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.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  package org.apache.axis.utils ;
  
  import java.io.* ;
  import java.lang.reflect.*;
  import java.util.ArrayList;
  import javax.wsdl.*;
  import javax.wsdl.factory.DefinitionFactory;
  import org.apache.axis.Constants;
  import org.apache.axis.encoding.TypeMappingRegistry;
  import org.w3c.dom.*;
  import com.ibm.wsdl.extensions.soap.*;
  
  /**
   * WSDL utility class, 1st cut.  Right now all the WSDL functionality for
   * dynamic Java->WSDL is in here - it probably wants to move elsewhere when
   * a more solid design stabilizes.
   * 
   * @author Glen Daniels (gdaniels@macromedia.com)
   */
  public class WSDLUtils {
      public static TypeMappingRegistry reg;
      
      public static void writeWSDLDoc(Class cls,
                                      String url, 
                                      String urn, 
                                      String description,
                                      TypeMappingRegistry typeMap,
                                      Writer writer) throws Exception
      {
          reg = typeMap;
          
          String name = cls.getName();
          
          DefinitionFactory factory = DefinitionFactory.newInstance("com.ibm.wsdl.factory.DefinitionFactoryImpl");
          Definition def = factory.newDefinition();
          Binding binding = def.createBinding();
          Service service = def.createService();
          
          def.setTargetNamespace(url); // !!! Probably not...
          
          def.addNamespace("serviceNS", url);
          def.addNamespace("soap", "soapNS");
          
          service.setQName(new javax.wsdl.QName(urn, name));
          def.addService(service);
          
          PortType portType = def.createPortType();
          portType.setUndefined(false);
          
          portType.setQName(new javax.wsdl.QName(url, name + "PortType"));
  
          Method[] methods = cls.getDeclaredMethods();
          ArrayList encodingList = new ArrayList();
          encodingList.add(Constants.URI_SOAP_ENC);
          
          Message msg;
          for(int i = 0, j = methods.length; i < j; i++) {
              Operation oper = def.createOperation();
              oper.setName(methods[i].getName());
              oper.setUndefined(false);
              
              Input input = def.createInput();
              
              msg = getRequestMessage(def, methods[i]);
              input.setMessage(msg);
              oper.setInput(input);
              
              def.addMessage(msg);
              
              msg = getResponseMessage(def, methods[i]);
              Output output = def.createOutput();
              output.setMessage(msg);
              oper.setOutput(output);
              
              def.addMessage(msg);
              
              portType.addOperation(oper);
              
              BindingOperation bindingOper = def.createBindingOperation();
              BindingInput bindingInput = def.createBindingInput();
              BindingOutput bindingOutput = def.createBindingOutput();
              
              bindingOper.setName(oper.getName());
              
              SOAPBody soapBody = new SOAPBody();
              soapBody.setUse("encoded");
              soapBody.setEncodingStyles(encodingList);
              
              bindingInput.addExtensibilityElement(soapBody);
              bindingOutput.addExtensibilityElement(soapBody);
              
              bindingOper.setBindingInput(bindingInput);
              bindingOper.setBindingOutput(bindingOutput);
              
              binding.addBindingOperation(bindingOper);
          }
          
          def.addPortType(portType);
          
          binding.setPortType(portType);
          binding.setUndefined(false);
          binding.setQName(new javax.wsdl.QName(url, name + "SoapBinding"));
          
          SOAPBinding soapBinding = new SOAPBinding();
          soapBinding.setStyle("rpc");
          soapBinding.setTransportURI("http://schemas.xmlsoap.org/soap/http");
          
          binding.addExtensibilityElement(soapBinding);
          
          def.addBinding(binding);
          
          Port port = def.createPort();
          
          port.setBinding(binding);
          port.setName("foo");
          
          SOAPAddress addr = new SOAPAddress();
          addr.setLocationURI(url);
          
          port.addExtensibilityElement(addr);
          
          service.addPort(port);
          
          com.ibm.wsdl.xml.WSDLWriter.writeWSDL(def, writer);
      }
  
      public static Message getRequestMessage(Definition def, Method method)
      {
          Message msg = def.createMessage();
          
          javax.wsdl.QName qName = new javax.wsdl.QName("", method.getName().concat("Request"));
  
          msg.setQName(qName);
          msg.setUndefined(false);
          
          Class[] parameters = method.getParameterTypes();
          for(int i = 0, j = parameters.length; i < j; i++) {
              addPartToMessage(def, msg, "arg" + i, parameters[i]);
          }
          
          return msg;
      }
      
      public static Message getResponseMessage(Definition def, Method method)
      {
          Message msg = def.createMessage();
          
          javax.wsdl.QName qName = new javax.wsdl.QName("", method.getName().concat("Response"));
  
          msg.setQName(qName);
          msg.setUndefined(false);
          
          Class type = method.getReturnType();
          addPartToMessage(def, msg, "result", type);
          
          return msg;
      }
      
      public static int n = 1;
      
      public static void addPartToMessage(Definition def, Message msg, String name, Class param)
      {
          Part part = def.createPart();
          org.apache.axis.utils.QName qName = reg.getTypeQName(param);
          String pref = def.getPrefix(qName.getNamespaceURI());
          if (pref == null) {
              def.addNamespace("ns" + n++, qName.getNamespaceURI());
          }
          
          javax.wsdl.QName typeQName = new javax.wsdl.QName(qName.getNamespaceURI(),
                                                            qName.getLocalPart());
          
          part.setTypeName(typeQName);
          part.setName(name);
          
          msg.addPart(part);
      }
  }
  
  
  
  1.6       +4 -3      xml-axis/java/test/utils/TestXMLUtils.java
  
  Index: TestXMLUtils.java
  ===================================================================
  RCS file: /home/cvs/xml-axis/java/test/utils/TestXMLUtils.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- TestXMLUtils.java	2001/08/07 18:58:48	1.5
  +++ TestXMLUtils.java	2001/08/17 20:44:01	1.6
  @@ -76,10 +76,10 @@
       /**
       * This test method is somewhat complex, but it solves a problem people have
       * asked me about, which is how to unit test a method that has void return
  -    * type but writes its output to an output stream.  So half the reason for
  +    * type but writes its output to a writer.  So half the reason for
       * creating and using it here is as a reference point.
       */
  -    public void testElementToStream() throws IOException
  +    public void testElementToWriter() throws IOException
       {
           /* Get the Document and one of its elements. */ 
           Reader xmlReader = (Reader)this.getTestXml("reader");
  @@ -95,13 +95,14 @@
           * will create a PipedInputStream in a separate thread.
           */
           PipedOutputStream out = new PipedOutputStream();
  +        OutputStreamWriter writer = new OutputStreamWriter(out);
           ConsumerPipe cpipe = new ConsumerPipe(out);
           
           /*
           * Call the method under test, passing the PipedOutStream to trap the
           * results.
           */
  -        XMLUtils.ElementToStream(elem, out);
  +        XMLUtils.ElementToWriter(elem, writer);
   
           /*
           * The output of the test will be piped to ConsumerPipe's PipedInputStream, which