Announcement Announcement Module
Collapse
No announcement yet.
Wanting to return the message from a custom Exception as a SOAP Fault string Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • #16
    I finally build my own resolver and got access to customizeFault(...).
    So Arjen suggested:
    Well, it's not a good idea to cast the Result to a StaxResult directly. It's better to create a Source (DOMSource or StreamSource), and to transform that source to the result, like so: code ...
    I try the call
    Code:
    transformer.transform(elSource, el.getResult());
    but only the first part was in the fault message missing the errorCode? Like this:

    Code:
              
      <detail>
        <e:myfaultdetails xmlns:e="Some-URI">
          <message>
            My application didn't work
          </message>
        </e:myfaultdetails>
      </detail>
    What could be the problem?

    thanks

    Comment


    • #17
      Could you perhaps list the entire piece of code that's in the exception resolver?

      Comment


      • #18
        Hi,
        It looks like the transformer does not like the format of the xml message if they are not within a element. If I add a dummy test element it's working:
        Code:
          
        <detail>
            <e:myfaultdetails xmlns:e="Some-URI">
              <test>
                <message>
                  My application didn't work
                </message>
                <text>
                   More information...
                </text>
              </test>
            </e:myfaultdetails>
          </detail>
        Here is the code:

        Code:
           
        private static final QName SERVICE_EXCEPTION_QNAME = new QName("http://www.mycompagny.com/services/project/wsdl/holiday", "ServiceExceptionDetail", "holi");
        
           @Override
           protected void customizeFault(Object endpoint, Exception serviceException, SoapFault soapFault)
           {
              try
              {
                 assert serviceException instanceof ProjectWebServiceException:"exception should be instanceof ProjectWebServiceException";
                 ProjectWebServiceException pwsException = (ProjectWebServiceException) serviceException;
                 SoapFaultDetail soapFaultDetail = soapFault.addFaultDetail();
                 ServiceExceptionDetail sed = pwsException.getDetails();
                 SoapFaultDetailElement el = soapFaultDetail.addFaultDetailElement(SERVICE_EXCEPTION_QNAME);
                 Source elSource = new StringSource(createXmlServiceExceptionDetails(sed));
                 
                 Transformer transformer;
                 transformer = TransformerFactory.newInstance().newTransformer();
                 transformer.transform(elSource, el.getResult());
              }
              catch(TransformerConfigurationException e)
              {
                 e.printStackTrace();
              }
              catch(TransformerFactoryConfigurationError e)
              {
                 e.printStackTrace();
              }
              catch(TransformerException e)
              {
                 e.printStackTrace();
              }
           }
        
           private static String createXmlServiceExceptionDetails(ServiceExceptionDetail exDetail)
           {
              StringBuilder sb = new StringBuilder();
              //sb.append("<test>"); //Its working if I use that dummy element level.
              sb.append("<messageId>\n\t").append(exDetail.getMessageId()).append("\n</messageId>");
              sb.append("<text>\n\t").append(exDetail.getText()).append("\n</text>");
              //sb.append("</test>");
              return sb.toString();
           }
        I try to modify the xml message by using "\n\t" or spaces or nothing between element but its always the same result.

        mapospring

        Comment


        • #19
          I guess a good night of sleep solve most of my problems!

          To be able to create the details with multiple elements I had to call the transformer more then once with one element for each call:

          Code:
                   
          transformer.transform(new StringSource("<funny> part </funny>"), el.getResult());         
          transformer.transform(new StringSource("<strange> test </strange>"), el.getResult());
          The code is a bit ugly but now its working!

          Comment


          • #20
            instead of calling transformer multiple times to add into the details, that makes the code look ugly. another way could be to use StreamSouce in the transformer


            StreamSource sc = new StreamSource(new ByteArrayInputStream(sb.toString().getBytes("UTF-8")));

            populate all your soapfault detail XML in sb = StringBuffer()

            Comment


            • #21
              how do you add namespaces to your elements within the detail area

              I'm trying to use the transformer to add more elements to my fault details like so :
              <detail>
              <ThisisANelement xmlns="mynamesapce">
              <UpdateDate xmlns=""> myDate </UpdateDate>
              </ThisisANelement>
              </detail>

              As can be seen the UpdateDate element's namespace is left empty. I want to be able to specify the child element's namespace. I also want to be able to just pass in some xml without having a xmlns tag added to it. ( I am going to get the xml from my DB and just populate the detail area with the xml with no changes needed. )

              I can get the namespace for the top level element in through the addDetailElement but I'm not sure how to have the child elements include their namespaces like in the UpdateDate element I have above.

              The code is like so :

              SoapFaultDetail detail = soapFault.addFaultDetail();
              SoapFaultDetailElement el = detail.addFaultDetailElement(new QName("mynamesapce","ThisisANelement"));
              Source elSource = new StringSource("<UpdateDate> myDate </UpdateDate>");

              ........
              ....

              transformer.transform(elSource, el.getResult());


              Thanks for the help

              Comment


              • #22
                How do you use SimpleSoapExceptionResolver? Can you annotate your methods or can you define a general rule to set faultString to the vlaue of exception.getMessage()?

                Comment


                • #23
                  The easiest way to create SOAP Faults from exceptions is to use the @SoapFault annotation to annotate your exceptions.

                  Comment

                  Working...
                  X