dblevins    2005/02/02 19:07:59

  Modified:    modules/core/src/java/org/openejb/server/soap
                        SoapHttpListener.java WSContainer.java
                        WSContainerGBean.java
  Added:       modules/core/src/java/org/openejb/server/soap
                        LightWeightServiceConfigurator.java
  Log:

  Revising the WSContainer to better support rpc/encoded strictly against a 
lightweight WSDL->Java mapping.
  Added validation for lightweigth mappings.
  Added ability to download the WSDL via http://foobar.com/someservice?wsdl
  
  Revision  Changes    Path
  1.2       +30 -2     
openejb/modules/core/src/java/org/openejb/server/soap/SoapHttpListener.java
  
  Index: SoapHttpListener.java
  ===================================================================
  RCS file: 
/home/projects/openejb/scm/openejb/modules/core/src/java/org/openejb/server/soap/SoapHttpListener.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- SoapHttpListener.java     22 Dec 2004 05:42:39 -0000      1.1
  +++ SoapHttpListener.java     3 Feb 2005 00:07:58 -0000       1.2
  @@ -46,6 +46,9 @@
   
   
   import java.io.IOException;
  +import java.io.OutputStream;
  +import java.io.InputStream;
  +import java.net.URL;
   
   import org.codehaus.xfire.MessageContext;
   import org.openejb.server.httpd.HttpListener;
  @@ -61,6 +64,7 @@
       }
   
       public void onMessage(HttpRequest req, HttpResponse res) throws 
IOException {
  +
           MessageContext context = new MessageContext("not-used", null, 
res.getOutputStream(), null, req.getURI().toString());
           context.setRequestStream(req.getInputStream());
   
  @@ -73,7 +77,31 @@
           }
   
           res.setContentType("text/xml");
  -        container.invoke(context);
  +
  +        if (req.getQueryParameter("wsdl") != null){
  +            URL wsdlURL = container.getWsdlURL();
  +            InputStream in = null;
  +            OutputStream out = null;
  +            try {
  +                in = wsdlURL.openStream();
  +                out = res.getOutputStream();
  +                byte[] buffer = new byte[1024];
  +                for (int read = in.read(buffer); read > 0; read = 
in.read(buffer) ) {
  +                    System.out.write(buffer, 0, read);
  +                    out.write(buffer, 0 ,read);
  +                }
  +            } finally {
  +                if (in != null) {
  +                    in.close();
  +                }
  +                if (out != null) {
  +                    out.flush();
  +                    out.close();
  +                }
  +            }
  +        } else {
  +            container.invoke(context);
  +        }
       }
   
   }
  
  
  
  1.2       +11 -11    
openejb/modules/core/src/java/org/openejb/server/soap/WSContainer.java
  
  Index: WSContainer.java
  ===================================================================
  RCS file: 
/home/projects/openejb/scm/openejb/modules/core/src/java/org/openejb/server/soap/WSContainer.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- WSContainer.java  22 Dec 2004 05:42:39 -0000      1.1
  +++ WSContainer.java  3 Feb 2005 00:07:58 -0000       1.2
  @@ -48,6 +48,10 @@
   import java.net.URI;
   import java.net.URL;
   
  +import javax.wsdl.Definition;
  +import javax.wsdl.factory.WSDLFactory;
  +import javax.wsdl.xml.WSDLReader;
  +
   import org.codehaus.xfire.MessageContext;
   import org.codehaus.xfire.XFireRuntimeException;
   import org.codehaus.xfire.fault.Soap11FaultHandler;
  @@ -75,7 +79,7 @@
           this.service = null;
       }
   
  -    public WSContainer(EJBContainer ejbContainer, URI location, URL wsdlURL, 
String namespace, String encoding, String style) {
  +    public WSContainer(EJBContainer ejbContainer, Definition definition, URI 
location, URL wsdlURL, String namespace, String encoding, String style) {
           this.ejbContainer = ejbContainer;
           this.location = location;
           this.wsdlURL = wsdlURL;
  @@ -93,14 +97,9 @@
           service.setWSDLURL(wsdlURL);
           service.setServiceHandler(new SoapHandler(new 
JavaServiceHandler(this)));
           service.setFaultHandler(new Soap11FaultHandler());
  -        service.setAutoTyped(true);
   
  -        // Setup Type Mapping
  -        DefaultTypeMappingRegistry registry = new 
DefaultTypeMappingRegistry();
  -        registry.registerDefault(registry.createDefaultMappings());
  -        service.setTypeMappingRegistry(registry);
  -        service.initializeTypeMapping();
  -        service.initializeOperations();
  +        LightWeightServiceConfigurator configurator = new 
LightWeightServiceConfigurator(definition, service);
  +        configurator.configure();
       }
   
       public void invoke(MessageContext context) {
  @@ -111,11 +110,12 @@
               handler = (SoapHandler) service.getServiceHandler();
               handler.invoke(context);
           } catch (Exception e) {
  +            e.printStackTrace();
               if (e instanceof XFireRuntimeException) {
                   throw (XFireRuntimeException) e;
               } else if (handler != null) {
  -                //log.error("Fault occurred.", e);
  -                handler.handleFault(e, context);
  +                XFireFault fault = XFireFault.createFault(e);
  +                handler.handleFault(fault, context);
               } else {
                   throw new XFireRuntimeException("Couldn't process message.", 
e);
               }
  
  
  
  1.2       +8 -4      
openejb/modules/core/src/java/org/openejb/server/soap/WSContainerGBean.java
  
  Index: WSContainerGBean.java
  ===================================================================
  RCS file: 
/home/projects/openejb/scm/openejb/modules/core/src/java/org/openejb/server/soap/WSContainerGBean.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- WSContainerGBean.java     22 Dec 2004 05:42:39 -0000      1.1
  +++ WSContainerGBean.java     3 Feb 2005 00:07:58 -0000       1.2
  @@ -47,6 +47,7 @@
   import java.net.URI;
   import java.net.URL;
   import javax.management.ObjectName;
  +import javax.wsdl.Definition;
   
   import org.apache.geronimo.gbean.GBeanData;
   import org.apache.geronimo.gbean.GBeanInfo;
  @@ -68,6 +69,7 @@
           infoFactory.addOperation("invoke", new 
Class[]{MessageContext.class});
   
           infoFactory.addReference("EJBContainer", EJBContainer.class);
  +        infoFactory.addAttribute("definition", Definition.class, true);
           infoFactory.addAttribute("location", URI.class, true);
           infoFactory.addAttribute("wsdlURL", URL.class, true);
           infoFactory.addAttribute("namespace", String.class, true);
  @@ -76,6 +78,7 @@
   
           infoFactory.setConstructor(new String[]{
               "EJBContainer",
  +            "definition",
               "location",
               "wsdlURL",
               "namespace",
  @@ -89,20 +92,21 @@
           return GBEAN_INFO;
       }
   
  -    public static ObjectName addGBean(Kernel kernel, String name, ObjectName 
ejbContainer, URI location, URL wsdlURL, String namespace, String encoding, 
String style) throws GBeanAlreadyExistsException, GBeanNotFoundException {
  -        GBeanData gbean = createGBean(name, ejbContainer, location, wsdlURL, 
namespace, encoding, style);
  +    public static ObjectName addGBean(Kernel kernel, String name, ObjectName 
ejbContainer, Definition definition, URI location, URL wsdlURL, String 
namespace, String encoding, String style) throws GBeanAlreadyExistsException, 
GBeanNotFoundException {
  +        GBeanData gbean = createGBean(name, ejbContainer, definition, 
location, wsdlURL, namespace, encoding, style);
           kernel.loadGBean(gbean, WSContainer.class.getClassLoader());
           kernel.startGBean(gbean.getName());
           return gbean.getName();
       }
   
  -    public static GBeanData createGBean(String name, ObjectName 
ejbContainer, URI location, URL wsdlURL, String namespace, String encoding, 
String style) {
  +    public static GBeanData createGBean(String name, ObjectName 
ejbContainer, Definition definition, URI location, URL wsdlURL, String 
namespace, String encoding, String style) {
           assert ejbContainer != null : "EJBContainer objectname is null";
   
           ObjectName gbeanName = 
JMXUtil.getObjectName("openejb:type=WSContainer,name=" + name);
   
           GBeanData gbean = new GBeanData(gbeanName, 
WSContainerGBean.GBEAN_INFO);
           gbean.setReferencePattern("EJBContainer", ejbContainer);
  +        gbean.setAttribute("definition", definition);
           gbean.setAttribute("location", location);
           gbean.setAttribute("wsdlURL", wsdlURL);
           gbean.setAttribute("namespace", namespace);
  
  
  
  1.1                  
openejb/modules/core/src/java/org/openejb/server/soap/LightWeightServiceConfigurator.java
  
  Index: LightWeightServiceConfigurator.java
  ===================================================================
  /**
   * Redistribution and use of this software and associated documentation
   * ("Software"), with or without modification, are permitted provided
   * that the following conditions are met:
   *
   * 1. Redistributions of source code must retain copyright
   *    statements and notices.  Redistributions must also contain a
   *    copy of this document.
   *
   * 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 name "OpenEJB" must not be used to endorse or promote
   *    products derived from this Software without prior written
   *    permission of The OpenEJB Group.  For written permission,
   *    please contact [EMAIL PROTECTED]
   *
   * 4. Products derived from this Software may not be called "OpenEJB"
   *    nor may "OpenEJB" appear in their names without prior written
   *    permission of The OpenEJB Group. OpenEJB is a registered
   *    trademark of The OpenEJB Group.
   *
   * 5. Due credit should be given to the OpenEJB Project
   *    (http://openejb.org/).
   *
   * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
   * ``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 OPENEJB GROUP 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.
   *
   * Copyright 2001 (C) The OpenEJB Group. All Rights Reserved.
   *
   * $Id: LightWeightServiceConfigurator.java,v 1.1 2005/02/03 00:07:58 
dblevins Exp $
   */
  package org.openejb.server.soap;
  
  import java.lang.reflect.Method;
  import java.util.*;
  import javax.wsdl.Definition;
  import javax.wsdl.Input;
  import javax.wsdl.Operation;
  import javax.wsdl.Part;
  import javax.xml.namespace.QName;
  
  import org.codehaus.xfire.java.DefaultJavaService;
  import org.codehaus.xfire.java.Parameter;
  import org.codehaus.xfire.java.mapping.DefaultTypeMappingRegistry;
  import org.codehaus.xfire.java.mapping.TypeMapping;
  import org.codehaus.xfire.java.type.Type;
  
  /**
   * TODO: I would be great to be able to configure XFire during deployment and 
write it to the configuration
   */
  public class LightWeightServiceConfigurator extends 
org.apache.geronimo.webservices.WSDLVisitor {
      private DefaultJavaService service;
      private TypeMapping typeMappings;
      private Map parameterMap;
      private Map typeMap;
  
      public LightWeightServiceConfigurator(Definition definition, 
DefaultJavaService service) {
          super(definition);
  
          this.service = service;
          // Setup Type Mapping
          service.setAutoTyped(true);
          DefaultTypeMappingRegistry registry = new 
DefaultTypeMappingRegistry();
          typeMappings = registry.createDefaultMappings();
          registry.registerDefault(typeMappings);
          service.setTypeMappingRegistry(registry);
          service.initializeTypeMapping();
  
          parameterMap = new HashMap();
          typeMap = new HashMap();
      }
  
      public void configure() {
          this.walkTree();
      }
  
      protected void visit(Part part) {
          Type type = typeMappings.getType(part.getTypeName());
          Parameter parameter = new Parameter(new QName(part.getName()), type);
          parameterMap.put(part, parameter);
          typeMap.put(part, type.getTypeClass());
      }
  
      protected void visit(Operation wsdlOperation) {
  
          Method method = getMethod(wsdlOperation);
  
          org.codehaus.xfire.java.Operation xfireOperation = new 
org.codehaus.xfire.java.Operation(method);
  
          // setup input params
          Collection inParts = 
wsdlOperation.getInput().getMessage().getParts().values();
          for (Iterator iterator = inParts.iterator(); iterator.hasNext();) {
              Part part = (Part) iterator.next();
              Parameter inParameter = (Parameter) parameterMap.get(part);
              xfireOperation.addInParameter(inParameter);
          }
  
          // setup output param
          Iterator outParts = 
wsdlOperation.getOutput().getMessage().getParts().values().iterator();
          if (outParts.hasNext()) {
              Part part = (Part) outParts.next();
              Parameter outParameter = (Parameter) parameterMap.get(part);
              xfireOperation.addOutParameter(outParameter);
          }
  
          service.addOperation(xfireOperation);
      }
  
      private Method getMethod(Operation wsdlOperation) {
          Input input = wsdlOperation.getInput();
          List parts = 
input.getMessage().getOrderedParts(wsdlOperation.getParameterOrdering());
          Class[] types = new Class[parts.size()];
          for (int i = 0; i < parts.size(); i++) {
              types[i] = (Class) typeMap.get((Part) parts.get(i));
          }
  
          try {
              return 
service.getServiceClass().getMethod(wsdlOperation.getName(), types);
          } catch (NoSuchMethodException e) {
              throw new IllegalArgumentException("There is no method matching 
the operation named " + wsdlOperation.getName());
          }
      }
  }
  
  
  

Reply via email to