Announcement Announcement Module
Collapse
No announcement yet.
AbstractStaxStreamPayloadEndpoint still not working Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • AbstractStaxStreamPayloadEndpoint still not working

    Hi!
    This is my test code to parse a request message and create a response with
    an AbstractStaxStreamPayloadEndpoint:

    Code:
     public class OfficeRegisterWSEndpoint extends AbstractStaxStreamPayloadEndpoint {
    
       protected void invokeInternal(XMLStreamReader streamReader, XMLStreamWriter streamWriter) {
    
            try {
                while (streamReader.hasNext()) {
                    int event = streamReader.next();
                    switch (event) {
                    case XMLStreamConstants.END_DOCUMENT:
                        streamReader.close();
                        break;
                    case XMLStreamConstants.START_ELEMENT:
                        if (streamReader.getLocalName().equals("officeId")) {
                            event = streamReader.next();
                            if (event == XMLStreamConstants.CHARACTERS) {
                                if (!streamReader.isWhiteSpace())
                                Long officeId = Long.valueOf(streamReader.getText());
                                //call a servie method with id
                                createResponse(streamWriter);
                                return;
                            }
                        }
                    default:
                        break;
                    }
                }
            } catch (XMLStreamException e) {
                e.printStackTrace();
            }
        }
    
        private void createResponse(XMLStreamWriter writer) {
            
            try {
            writer.writeStartElement("TestResponse");
            writer.writeStartElement("TestElement");
            writer.writeCharacters("xyz");
            writer.writeEndElement();
            writer.writeEndElement();    
            writer.flush();
            writer.close();
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    Parsing of the message works fine but writing the response fails somehow.
    No response is created at all. There are no exceptions in the log or on stdout but i guess somewhere an exception is silently caught and not treated or logged.
    thx & regards!

  • #2
    Ok i found a workaround for my problem...

    From AbstractStaxStreamPayloadEndpoint:

    Code:
    private XMLStreamWriter getStreamWriter(Result result) {
            XMLStreamWriter streamWriter = null;
            if (result instanceof StaxResult) {
                StaxResult staxResult = (StaxResult) result;
                streamWriter = staxResult.getXMLStreamWriter();
            }
            if (streamWriter == null) {
                try {
                    streamWriter = getOutputFactory().createXMLStreamWriter(result);
                }
                catch (XMLStreamException ex) {
                    // ignore
                }
            }
            return streamWriter;
        }
    That's the catch block that silently swallows my XmlStreamException.
    And the exception is the same as in my previous posting: <Can not create a STaX writer for a SAXResult -- not (yet) implemented.>
    --> see http://forum.springframework.org/showthread.php?t=26392


    To get writing with XmlStreamWriter working i changed the createStreamWriter method of the inner class
    ResponseCreatingStreamWriter within AbstractStaxStreamPayloadEndpoint to:

    Code:
    private void createStreamWriter() throws XMLStreamException {          
                    // as a final resort, use a stream, and transform that at
                    // endDocument()
                    os = new ByteArrayOutputStream();
                    streamWriter = getOutputFactory().createXMLStreamWriter(os);
                }
            }
    and of course in the invokeInternal() of my endpoint class i added a writeEndDocument() at the end of reponse creation.
    Now the response message is created properly. This is a workaround only,
    to really fix it either the woodstox parser must implement Stax writing from SaxResult or the spring-ws code must be changed to return a StreamResult.

    Comment


    • #3
      Originally posted by lasse_stromberg
      Ok i found a workaround for my problem...

      From AbstractStaxStreamPayloadEndpoint:

      Code:
      private XMLStreamWriter getStreamWriter(Result result) {
              XMLStreamWriter streamWriter = null;
              if (result instanceof StaxResult) {
                  StaxResult staxResult = (StaxResult) result;
                  streamWriter = staxResult.getXMLStreamWriter();
              }
              if (streamWriter == null) {
                  try {
                      streamWriter = getOutputFactory().createXMLStreamWriter(result);
                  }
                  catch (XMLStreamException ex) {
                      // ignore
                  }
              }
              return streamWriter;
          }
      That's the catch block that silently swallows my XmlStreamException.
      And the exception is the same as in my previous posting: <Can not create a STaX writer for a SAXResult -- not (yet) implemented.>
      --> see http://forum.springframework.org/showthread.php?t=26392
      It silently catches the exception, because it is not a fatal error. If a XMLStreamWriter cannot be created from the payload source of the response, a byte array buffer is used in ResponseCreatingStreamWriter (os). Later, that byte array is transformed into the response payload when endDocument() is called.

      I do admit that this solution was not very good: it basically required a endDocument() call. So I changed it: it now does the transforming process (if necessary) as part of ResponseCreateingStreamWriter.close(). That method is invoked by the AbstractStaxStreamPayloadEndpoint itself, so it is always called.

      Originally posted by lasse_stromberg
      To get writing with XmlStreamWriter working i changed the createStreamWriter method of the inner class
      ResponseCreatingStreamWriter within AbstractStaxStreamPayloadEndpoint to:

      Code:
      private void createStreamWriter() throws XMLStreamException {          
                      // as a final resort, use a stream, and transform that at
                      // endDocument()
                      os = new ByteArrayOutputStream();
                      streamWriter = getOutputFactory().createXMLStreamWriter(os);
                  }
              }
      and of course in the invokeInternal() of my endpoint class i added a writeEndDocument() at the end of reponse creation.
      Now the response message is created properly. This is a workaround only,
      to really fix it either the woodstox parser must implement Stax writing from SaxResult or the spring-ws code must be changed to return a StreamResult.
      This workaround is basically the same as in the main codebase. Calling endDocument() is no longer a requirement, though.

      Could you update your sources from Subversion, and see if the current code works for you? Thanks,

      Comment

      Working...
      X