Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2005-2010 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.springframework.ws.server.endpoint;
 
 
Abstract base class for endpoints that handle the message payload with event-based StAX. Allows subclasses to read the request with a XMLEventReader, and to create a response using a XMLEventWriter.

 
 public abstract class AbstractStaxEventPayloadEndpoint extends AbstractStaxPayloadEndpoint implements MessageEndpoint {
 
     private XMLEventFactory eventFactory;
 
     public final void invoke(MessageContext messageContextthrows Exception {
         XMLEventReader eventReader = getEventReader(messageContext.getRequest().getPayloadSource());
         XMLEventWriter streamWriter = new ResponseCreatingEventWriter(messageContext);
         invokeInternal(eventReaderstreamWritergetEventFactory());
         streamWriter.flush();
     }

    
Create a XMLEventFactory that this endpoint will use to create XMLEvents. Can be overridden in subclasses, adding further initialization of the factory. The resulting XMLEventFactory is cached, so this method will only be called once.

Returns:
the created XMLEventFactory
 
     protected XMLEventFactory createXmlEventFactory() {
         return XMLEventFactory.newInstance();
     }

    
Returns an XMLEventFactory to read XML from.
 
     private XMLEventFactory getEventFactory() {
         if ( == null) {
              = createXmlEventFactory();
         }
         return ;
     }
 
         if (source == null) {
             return null;
         }
         XMLEventReader eventReader = null;
         if (StaxUtils.isStaxSource(source)) {
             eventReader = StaxUtils.getXMLEventReader(source);
             if (eventReader == null) {
                 XMLStreamReader streamReader = StaxUtils.getXMLStreamReader(source);
                 if (streamReader != null) {
                     try {
                         eventReader = getInputFactory().createXMLEventReader(streamReader);
                     }
                     catch (XMLStreamException ex) {
                         eventReader = null;
                     }
                 }
             }
        }
        if (eventReader == null) {
            try {
                eventReader = getInputFactory().createXMLEventReader(source);
            }
            catch (XMLStreamException ex) {
                eventReader = null;
            }
            catch (UnsupportedOperationException ex) {
                eventReader = null;
            }
        }
        if (eventReader == null) {
            // as a final resort, transform the source to a stream, and read from that
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            transform(sourcenew StreamResult(os));
            ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
            eventReader = getInputFactory().createXMLEventReader(is);
        }
        return eventReader;
    }
    private XMLEventWriter getEventWriter(Result result) {
        XMLEventWriter eventWriter = null;
        if (StaxUtils.isStaxResult(result)) {
            eventWriter = StaxUtils.getXMLEventWriter(result);
        }
        if (eventWriter == null) {
            try {
                eventWriter = getOutputFactory().createXMLEventWriter(result);
            }
            catch (XMLStreamException ex) {
                // ignore
            }
        }
        return eventWriter;
    }

    
Template method. Subclasses must implement this. Offers the request payload as a XMLEventReader, and a XMLEventWriter to write the response payload to.

Parameters:
eventReader the reader to read the payload events from
eventWriter the writer to write payload events to
eventFactory an XMLEventFactory that can be used to create events
    protected abstract void invokeInternal(XMLEventReader eventReader,
                                           XMLEventConsumer eventWriter,
                                           XMLEventFactory eventFactorythrows Exception;

    
Implementation of the XMLEventWriter interface that creates a response WebServiceMessage as soon as any method is called, thus lazily creating the response.
    private class ResponseCreatingEventWriter implements XMLEventWriter {
        private XMLEventWriter eventWriter;
        private MessageContext messageContext;
        private ByteArrayOutputStream os;
        public ResponseCreatingEventWriter(MessageContext messageContext) {
            this. = messageContext;
        }
        public NamespaceContext getNamespaceContext() {
            return .getNamespaceContext();
        }
        public void setNamespaceContext(NamespaceContext contextthrows XMLStreamException {
            createEventWriter();
            .setNamespaceContext(context);
        }
        public void add(XMLEventReader readerthrows XMLStreamException {
            createEventWriter();
            while (reader.hasNext()) {
                add(reader.nextEvent());
            }
        }
        public void add(XMLEvent eventthrows XMLStreamException {
            createEventWriter();
            .add(event);
            if (event.isEndDocument()) {
                if ( != null) {
                    .flush();
                    // if we used an output stream cache, we have to transform it to the response again
                    try {
                        ByteArrayInputStream is = new ByteArrayInputStream(.toByteArray());
                        transform(new StreamSource(is), .getResponse().getPayloadResult());
                    }
                    catch (TransformerException ex) {
                        throw new XMLStreamException(ex);
                    }
                }
            }
        }
        public void close() throws XMLStreamException {
            if ( != null) {
                .close();
            }
        }
        public void flush() throws XMLStreamException {
            if ( != null) {
                .flush();
            }
        }
        public String getPrefix(String urithrows XMLStreamException {
            createEventWriter();
            return .getPrefix(uri);
        }
        public void setDefaultNamespace(String urithrows XMLStreamException {
            createEventWriter();
            .setDefaultNamespace(uri);
        }
        public void setPrefix(String prefixString urithrows XMLStreamException {
            createEventWriter();
            .setPrefix(prefixuri);
        }
        private void createEventWriter() throws XMLStreamException {
            if ( == null) {
                WebServiceMessage response = .getResponse();
                 = getEventWriter(response.getPayloadResult());
                if ( == null) {
                    // as a final resort, use a stream, and transform that at endDocument()
                     = new ByteArrayOutputStream();
                     = getOutputFactory().createXMLEventWriter();
                }
            }
        }
    }
New to GrepCode? Check out our FAQ X