Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2005-2011 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.adapter.method.jaxb;
 
 import java.io.Reader;
 import java.io.Writer;
 import java.net.URL;
 
 
Abstract base class for MethodArgumentResolver and MethodReturnValueHandler implementations that use JAXB2. Creates javax.xml.bind.JAXBContext object lazily, and offers marshalling and unmarshalling methods.

Author(s):
Arjen Poutsma
Since:
2.0
 
 
     private final ConcurrentMap<ClassJAXBContextjaxbContexts = new ConcurrentHashMap<ClassJAXBContext>();

    
Marshals the given jaxbElement to the response payload of the given message context.

Parameters:
messageContext the message context to marshal to
clazz the clazz to create a marshaller for
jaxbElement the object to be marshalled
Throws:
javax.xml.bind.JAXBException in case of JAXB2 errors
 
     protected final void marshalToResponsePayload(MessageContext messageContextClass<?> clazzObject jaxbElement)
             throws JAXBException {
         Assert.notNull(messageContext"'messageContext' must not be null");
         Assert.notNull(clazz"'clazz' must not be null");
         Assert.notNull(jaxbElement"'jaxbElement' must not be null");
         if (.isDebugEnabled()) {
             .debug("Marshalling [" + jaxbElement + "] to response payload");
         }
         WebServiceMessage response = messageContext.getResponse();
         if (response instanceof StreamingWebServiceMessage) {
             StreamingWebServiceMessage streamingResponse = (StreamingWebServiceMessageresponse;
 
             StreamingPayload payload = new JaxbStreamingPayload(clazzjaxbElement);
             streamingResponse.setStreamingPayload(payload);
         }
         else {
             Result responsePayload = response.getPayloadResult();
             try {
                Jaxb2ResultCallback callback = new Jaxb2ResultCallback(clazzjaxbElement);
                TraxUtils.doWithResult(responsePayloadcallback);
            }
            catch (Exception ex) {
                throw convertToJaxbException(ex);
            }
        }
    }

    
Unmarshals the request payload of the given message context.

Parameters:
messageContext the message context to unmarshal from
clazz the class to unmarshal
Returns:
the unmarshalled object, or null if the request has no payload
Throws:
javax.xml.bind.JAXBException in case of JAXB2 errors
    protected final Object unmarshalFromRequestPayload(MessageContext messageContextClass<?> clazz)
            throws JAXBException {
        Source requestPayload = getRequestPayload(messageContext);
        if (requestPayload == null) {
            return null;
        }
        try {
            Jaxb2SourceCallback callback = new Jaxb2SourceCallback(clazz);
            TraxUtils.doWithSource(requestPayloadcallback);
            if (.isDebugEnabled()) {
                .debug("Unmarshalled payload request to [" + callback.result + "]");
            }
            return callback.result;
        }
        catch (Exception ex) {
            throw convertToJaxbException(ex);
        }
    }

    
Unmarshals the request payload of the given message context as javax.xml.bind.JAXBElement.

Parameters:
messageContext the message context to unmarshal from
clazz the class to unmarshal
Returns:
the unmarshalled element, or null if the request has no payload
Throws:
javax.xml.bind.JAXBException in case of JAXB2 errors
    protected final <T> JAXBElement<T> unmarshalElementFromRequestPayload(MessageContext messageContextClass<T> clazz)
            throws JAXBException {
        Source requestPayload = getRequestPayload(messageContext);
        if (requestPayload == null) {
            return null;
        }
        try {
            JaxbElementSourceCallback<T> callback = new JaxbElementSourceCallback<T>(clazz);
            TraxUtils.doWithSource(requestPayloadcallback);
            if (.isDebugEnabled()) {
                .debug("Unmarshalled payload request to [" + callback.result + "]");
            }
            return callback.result;
        }
        catch (Exception ex) {
            throw convertToJaxbException(ex);
        }
    }
    private Source getRequestPayload(MessageContext messageContext) {
        WebServiceMessage request = messageContext.getRequest();
        return request != null ? request.getPayloadSource() : null;
    }
        if (ex instanceof JAXBException) {
            return (JAXBExceptionex;
        }
        else {
            return new JAXBException(ex);
        }
    }

    
Creates a new javax.xml.bind.Marshaller to be used for marshalling objects to XML. Defaults to javax.xml.bind.JAXBContext.createMarshaller(), but can be overridden in subclasses for further customization.

Parameters:
jaxbContext the JAXB context to create a marshaller for
Returns:
the marshaller
Throws:
javax.xml.bind.JAXBException in case of JAXB errors
    protected Marshaller createMarshaller(JAXBContext jaxbContextthrows JAXBException {
        return jaxbContext.createMarshaller();
    }
    private Marshaller createMarshaller(Class<?> clazzthrows JAXBException {
        return createMarshaller(getJaxbContext(clazz));
    }

    
Creates a new javax.xml.bind.Unmarshaller to be used for unmarshalling XML to objects. Defaults to javax.xml.bind.JAXBContext.createUnmarshaller(), but can be overridden in subclasses for further customization.

Parameters:
jaxbContext the JAXB context to create a unmarshaller for
Returns:
the unmarshaller
Throws:
javax.xml.bind.JAXBException in case of JAXB errors
    protected Unmarshaller createUnmarshaller(JAXBContext jaxbContextthrows JAXBException {
        return jaxbContext.createUnmarshaller();
    }
    private Unmarshaller createUnmarshaller(Class<?> clazzthrows JAXBException {
        return createUnmarshaller(getJaxbContext(clazz));
    }
    private JAXBContext getJaxbContext(Class<?> clazzthrows JAXBException {
        Assert.notNull(clazz"'clazz' must not be null");
        JAXBContext jaxbContext = .get(clazz);
        if (jaxbContext == null) {
            jaxbContext = JAXBContext.newInstance(clazz);
            .putIfAbsent(clazzjaxbContext);
        }
        return jaxbContext;
    }
    // Callbacks
    private class Jaxb2SourceCallback implements TraxUtils.SourceCallback {
        private final Unmarshaller unmarshaller;
        private Object result;
        public Jaxb2SourceCallback(Class<?> clazzthrows JAXBException {
            this. = createUnmarshaller(clazz);
        }
        public void domSource(Node nodethrows JAXBException {
             = .unmarshal(node);
        }
        public void saxSource(XMLReader readerInputSource inputSourcethrows JAXBException {
             = .unmarshal(inputSource);
        }
        public void staxSource(XMLEventReader eventReaderthrows JAXBException {
             = .unmarshal(eventReader);
        }
        public void staxSource(XMLStreamReader streamReaderthrows JAXBException {
             = .unmarshal(streamReader);
        }
        public void streamSource(InputStream inputStreamthrows IOExceptionJAXBException {
             = .unmarshal(inputStream);
        }
        public void streamSource(Reader readerthrows IOExceptionJAXBException {
             = .unmarshal(reader);
        }
        public void source(String systemIdthrows Exception {
             = .unmarshal(new URL(systemId));
        }
    }
    private class JaxbElementSourceCallback<T> implements TraxUtils.SourceCallback {
        private final Unmarshaller unmarshaller;
        private final Class<T> declaredType;
        private JAXBElement<T> result;
        public JaxbElementSourceCallback(Class<T> declaredTypethrows JAXBException {
            this. = createUnmarshaller(declaredType);
            this. = declaredType;
        }
        public void domSource(Node nodethrows JAXBException {
             = .unmarshal(node);
        }
        public void saxSource(XMLReader readerInputSource inputSourcethrows JAXBException {
             = .unmarshal(new SAXSource(readerinputSource), );
        }
        public void staxSource(XMLEventReader eventReaderthrows JAXBException {
             = .unmarshal(eventReader);
        }
        public void staxSource(XMLStreamReader streamReaderthrows JAXBException {
             = .unmarshal(streamReader);
        }
        public void streamSource(InputStream inputStreamthrows IOExceptionJAXBException {
             = .unmarshal(new StreamSource(inputStream), );
        }
        public void streamSource(Reader readerthrows IOExceptionJAXBException {
             = .unmarshal(new StreamSource(reader), );
        }
        public void source(String systemIdthrows Exception {
             = .unmarshal(new StreamSource(systemId), );
        }
    }
    private class Jaxb2ResultCallback implements TraxUtils.ResultCallback {
        private final Marshaller marshaller;
        private final Object jaxbElement;
        private Jaxb2ResultCallback(Class<?> clazzObject jaxbElementthrows JAXBException {
            this. = createMarshaller(clazz);
            this. = jaxbElement;
        }
        public void domResult(Node nodethrows JAXBException {
            .marshal(node);
        }
        public void saxResult(ContentHandler contentHandlerLexicalHandler lexicalHandlerthrows JAXBException {
            .marshal(contentHandler);
        }
        public void staxResult(XMLEventWriter eventWriterthrows JAXBException {
            .marshal(eventWriter);
        }
        public void staxResult(XMLStreamWriter streamWriterthrows JAXBException {
            .marshal(streamWriter);
        }
        public void streamResult(OutputStream outputStreamthrows JAXBException {
            .marshal(outputStream);
        }
        public void streamResult(Writer writerthrows JAXBException {
            .marshal(writer);
        }
        public void result(String systemIdthrows Exception {
            .marshal(new StreamResult(systemId));
        }
    }
    private class JaxbStreamingPayload implements StreamingPayload {
        private final Object jaxbElement;
        private final Marshaller marshaller;
        private final QName name;
        private JaxbStreamingPayload(Class<?> clazzObject jaxbElementthrows JAXBException {
            JAXBContext jaxbContext = getJaxbContext(clazz);
            this. = jaxbContext.createMarshaller();
            this. = jaxbElement;
            JAXBIntrospector introspector = jaxbContext.createJAXBIntrospector();
            this. = introspector.getElementName(jaxbElement);
        }
        public QName getName() {
            return ;
        }
        public void writeTo(XMLStreamWriter streamWriterthrows XMLStreamException {
            try {
                .marshal(streamWriter);
            }
            catch (JAXBException ex) {
                throw new XMLStreamException("Could not marshal [" +  + "]: " + ex.getMessage(), ex);
            }
        }
    }
New to GrepCode? Check out our FAQ X