1 Reply Latest reply on Aug 26, 2012 7:34 AM by ramy Hassan

    @XMLElement(required = true) Validation

    Vincenzo Esposito Newbie

      Dear Community,

      before anything thanks for all support.

       

      Configuration:

      JBoss 4.2.3.GA

      JBossWS Native 3.1.0.GA

       

      I'm just developing a simple test usingWebService. I have some constraints to be applied to service’s parameters. In particular I’m looking for away to assure that the service provider receives only requests with valid input parameters.

       

       

      This is the service code:

       

      TestService Interface

      import javax.jws.WebMethod;
      import javax.jws.WebService;
      import javax.jws.soap.SOAPBinding;
      import javax.jws.soap.SOAPBinding.ParameterStyle;
      import javax.jws.soap.SOAPBinding.Style;
      
      
      import org.jboss.wsf.spi.annotation.WebContext;
      
      
      @WebContext(contextRoot="/TestService")
      @WebService
      @SOAPBinding(style= Style.DOCUMENT, parameterStyle = ParameterStyle.WRAPPED)
      public interface TestService {
      
      
                @WebMethod
                public void sendInfo(TestObject to);
      
      }
      
      
      
      

       

      TestServiceBean

      import javax.ejb.Stateless;
      import javax.jws.WebMethod;
      import javax.jws.WebService;
      import org.jboss.wsf.spi.annotation.WebContext;
      
      
      @WebContext(contextRoot="/Test-Service")
      @WebService(serviceName="TestService", endpointInterface="testing.TestService")
      @Stateless
      public class TestServiceBean implements TestService {
      
      
                @WebMethod
                public void sendInfo(TestObject to) {
                     System.out.println(to.get_data());
                     System.out.println(to.get_key());
                }
      
      
      }
      
      

       

      TestObject Class

      import javax.xml.bind.annotation.XmlElement;
      
      
      public class TestObject {
      
      
                private String _data;
      
                private int _key;
      
                @XmlElement(name = "_data", required = true)
                public String get_data() {
                     return _data;
                }
      
      
                public void set_data(String _data) {
                     this._data = _data;
                }
      
      
                public int get_key() {
                     return _key;
                }
      
      
                public void set_key(int _key) {
                     this._key = _key;
                }
      
      
      
      }
      
      

       

      As you can see by code, analyzing TestObject class, Idecided that the "_data" attribute should be necessarly required and it must be not “nillable”. Once deployed, the generated WSDL is this one:

       

       

      TestServiceBean.wsdl

      <definitions name="TestService" targetNamespace="http://testing/" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://testing/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
       <types>
        <xs:schema targetNamespace="http://testing/" version="1.0" xmlns:tns="http://testing/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
         <xs:element name="sendInfo" type="tns:sendInfo"/>
         <xs:element name="sendInfoResponse" type="tns:sendInfoResponse"/>
         <xs:complexType name="sendInfo">
          <xs:sequence>
           <xs:element minOccurs="0" name="arg0" type="tns:testObject"/>
          </xs:sequence>
         </xs:complexType>
         <xs:complexType name="testObject">
          <xs:sequence>
           <xs:element name="_data" type="xs:string"/>
           <xs:element name="_key" type="xs:int"/>
          </xs:sequence>
         </xs:complexType>
         <xs:complexType name="sendInfoResponse">
          <xs:sequence/>
         </xs:complexType>
        </xs:schema>
       </types>
       <message name="TestService_sendInfoResponse">
        <part element="tns:sendInfoResponse" name="sendInfoResponse"/>
       </message>
       <message name="TestService_sendInfo">
        <part element="tns:sendInfo" name="sendInfo"/>
       </message>
       <portType name="TestService">
        <operation name="sendInfo" parameterOrder="sendInfo">
         <input message="tns:TestService_sendInfo"/>
         <output message="tns:TestService_sendInfoResponse"/>
        </operation>
       </portType>
       <binding name="TestServiceBinding" type="tns:TestService">
        <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
        <operation name="sendInfo">
         <soap:operation soapAction=""/>
         <input>
          <soap:body use="literal"/>
         </input>
         <output>
          <soap:body use="literal"/>
         </output>
        </operation>
       </binding>
       <service name="TestService">
        <port binding="tns:TestServiceBinding" name="TestServiceBeanPort">
         <soap:address location="http://127.0.0.1:8180/Test-Service/TestServiceBean"/>
        </port>
       </service>
      </definitions>
      
      

       

      Just analyzing the WSDL, I don't see anything about the "required" element, but using "wsconsume" tool I can obtain stubs to use client side. In detail, this is the generated TestObject stub

       

      Generated TestObject Stub detail

      @XmlAccessorType(XmlAccessType.FIELD)
      @XmlType(name = "testObject", propOrder = {
          "data",
          "key"
      })
      public class TestObject {
      
      
          @XmlElement(name = "_data", required = true)
          protected String data;
          @XmlElement(name = "_key")
          protected int key;
      .....
      }
      

       

      The generated Stub contains the"required" constraint over the "_data" attribute.

       

      In order to be consistent, I think that if Itry to use the TestObject omitting the "_data" value, JBossWS should not allow the service request. This is the client main method using to request the service.

       

      Main class

      public static void main(String[] args) {
      
                TestService_Service srv = new TestService_Service();
                TestService test_srv = srv.getTestServiceBeanPort();
      
                TestObject to = new TestObject();
                to.setKey(3);
      
                test_srv.sendInfo(to);
      
      }
      

       

      Running the client, it will perform the request without any Exception on its side and the "null" value for the "_data"attribute is received by service implementation.

       

      Now, what's wrong with the code? Is there anyexplaination about this issue? Why the WSDL do not contains the contraints over the "_data" value? Is there any validation before the SOAPMessage send?

       

      Thank you for support again.

       

      Vincenzo