0 Replies Latest reply on Jun 21, 2004 5:43 PM by pockey

    Need help in catching custom exceptions on web service's cli

    pockey Newbie

      Hi

      I tried to catch a custom exception class, ServerException, on a static-stub client of a web service. However, I was not able to catch the ServerException. The client always caught SOAPFaultException. This SOAPFaultException contains ServerException string in its message.

      So it's tedious to obtain error codes or messages from the ServerException, and to distinguish different exception types.

      Could anyone give me some directions or point out what I did wrong with my code, so that I can catch custom exceptions directly please.

      My system info:
      Server: JBOSS3.2.3 with JBOSS.NET
      Client: Web-service client using Sun Java Web Services Developer Pack 1.3 library.

      Part of the source codes are below.
      Thanks you very much in advance
      Kay

      ServerException Class

      import java.io.Serializable;
      
      public class ServerException
       extends Exception implements Serializable
      {
       public ServerException()
       {
       super();
       }
      
       public ServerException(String msg)
       {
       super(msg);
       }
      }
      


      UserEndPoint stateless session bean
      ...
       /**
       * @ejb.interface-method
       * view-type="remote"
       * @jboss-net.web-method
       */
       public String createUser(UserValue user) throws ServerException
       {
       try {
      
       UsersDTO usersDTO = new UsersDTO();
       try {
       org.apache.commons.beanutils.BeanUtils.copyProperties(usersDTO, user);
       } catch (Exception e) {
       log.error("BeanUtils.copyProperties",e);
       throw new CreateException("BeanUtils.copyProperties "+e.getMessage());
       }
       UsersLocal usersLocal = getUsersLocalHome().create(usersDTO);
       return usersLocal.getUserID();
      
       } catch (javax.ejb.CreateException ce) {
       log.error("createUser UserID="+user.getUserID(), ce);
       throw new ServerException("problem creating user ");
       } catch (javax.ejb.EJBException e) {
       log.error("createUser UserID="+user.getUserID(), e);
       throw new ServerException("problem occured on the server");
       }
      ...
      


      JUNIT Client with Sun Java Web Services Developer Pack 1.3 library.

      ..
       public void testCreateUser() {
       try {
       // Prepare input
       UserValue user = new UserValue();
      
       // test
       String result = userManagementEndPointBean.createUser(user);
      
       } catch (ServerException ex) {
       System.out.println("Received1");
       ex.printStackTrace();
       } catch (SOAPFaultException ex) {
       System.out.println("Received2");
      
       System.out.println("Received3.2 with message " + ex.getMessage());
       System.out.println("Received3.2 with LocalizedMessage " + ex.getLocalizedMessage());
       System.out.println("Received3.2 with getFaultActor " + ex.getFaultActor());
       System.out.println("Received3.2 with getFaultString " + ex.getFaultString());
       System.out.println("Received3.2 with getFaultCode " + ex.getFaultCode());
      
       ex.printStackTrace();
       } catch (Exception ex) {
       System.out.println("Received4");
       }
       }
      ...
      


      Result from console is

      Received2
      Received3.2 with message com.j2ee.app.services.exceptions.ServerException: problem occured on the server
      Received3.2 with LocalizedMessage com.j2ee.app.services.exceptions.ServerException: problem occured on the server
      Received3.2 with getFaultActor null
      Received3.2 with getFaultString com.j2ee.app.services.exceptions.ServerException: problem occured on the server
      javax.xml.rpc.soap.SOAPFaultException: com.j2ee.app.services.exceptions.ServerException: problem occured on the server
      Received3.2 with getFaultCode {http://schemas.xmlsoap.org/soap/envelope/}Server.userException
      at com.sun.xml.rpc.client.StreamingSender._raiseFault(StreamingSender.java:360)
      at com.sun.xml.rpc.client.StreamingSender._send(StreamingSender.java:224)
      ...


      The stub on client generated by wscompile (Sun) is below. The code shows that ServerException is declared properly in WSDL, so wscompile could generate it.
      ...
       /*
       * implementation of createUser
       */
       public java.lang.String createUser(com.app.j2ee.usermanagement.ejb.staticstub.client.UserValue in0)
       throws com.app.j2ee.usermanagement.ejb.staticstub.client.ServerException, java.rmi.RemoteException {
      
       try {
      
       StreamingSenderState _state = _start(_handlerChain);
      
       InternalSOAPMessage _request = _state.getRequest();
       _request.setOperationCode(createUser_OPCODE);
       com.app.j2ee.usermanagement.ejb.staticstub.client.UserManagementImpl_createUser_RequestStruct _myUserManagementImpl_createUser_RequestStruct =
       new com.app.j2ee.usermanagement.ejb.staticstub.client.UserManagementImpl_createUser_RequestStruct();
      
       _myUserManagementImpl_createUser_RequestStruct.setIn0(in0);
      
       SOAPBlockInfo _bodyBlock = new SOAPBlockInfo(ns1_createUser_createUser_QNAME);
       _bodyBlock.setValue(_myUserManagementImpl_createUser_RequestStruct);
       _bodyBlock.setSerializer(ns1_myUserManagementImpl_createUser_RequestStruct_SOAPSerializer);
       _request.setBody(_bodyBlock);
      
       _state.getMessageContext().setProperty(HttpClientTransport.HTTP_SOAPACTION_PROPERTY, "UserManagementService");
      
       _send((String) _getProperty(ENDPOINT_ADDRESS_PROPERTY), _state);
      
       com.app.j2ee.usermanagement.ejb.staticstub.client.UserManagementImpl_createUser_ResponseStruct _myUserManagementImpl_createUser_ResponseStruct = null;
       Object _responseObj = _state.getResponse().getBody().getValue();
       if (_responseObj instanceof SOAPDeserializationState) {
       _myUserManagementImpl_createUser_ResponseStruct =
       (com.app.j2ee.usermanagement.ejb.staticstub.client.UserManagementImpl_createUser_ResponseStruct)((SOAPDeserializationState)_responseObj).getInstance();
       } else {
       _myUserManagementImpl_createUser_ResponseStruct =
       (com.app.j2ee.usermanagement.ejb.staticstub.client.UserManagementImpl_createUser_ResponseStruct)_responseObj;
       }
      
       return _myUserManagementImpl_createUser_ResponseStruct.getCreateUserReturn();
       } catch (com.app.j2ee.usermanagement.ejb.staticstub.client.ServerException e) {
       throw e;
       } catch (RemoteException e) {
       if (e.detail instanceof com.sun.xml.rpc.util.HeaderFaultException) {
       com.sun.xml.rpc.util.HeaderFaultException hfe = (com.sun.xml.rpc.util.HeaderFaultException) e.detail;
       SOAPHeaderBlockInfo headerBlock = (SOAPHeaderBlockInfo) hfe.getObject();
       Object obj = headerBlock.getValue();
       }
       // let this one through unchanged
       throw e;
       } catch (JAXRPCException e) {
       throw new RemoteException(e.getMessage(), e);
       } catch (Exception e) {
       if (e instanceof RuntimeException) {
       throw (RuntimeException)e;
       } else {
       throw new RemoteException(e.getMessage(), e);
       }
       }
       }