Class AbstractCollectionJaxbProvider

All Implemented Interfaces:
MessageBodyReader<Object>, MessageBodyWriter<Object>
Direct Known Subclasses:
XmlCollectionJaxbProvider

public abstract class AbstractCollectionJaxbProvider extends AbstractJaxbProvider<Object>
An abstract provider for T[], Collection&lt;T&gt;, and its subtypes as long as they have the public default constructor or are interfaces implemented by one the following classes: T must be a JAXB type annotated with XmlRootElement.

Implementing classes may extend this class to provide specific marshalling and unmarshalling behaviour.

When unmarshalling a UnmarshalException will result in a WebApplicationException being thrown with a status of 400 (Client error), and a JAXBException will result in a WebApplicationException being thrown with a status of 500 (Internal Server error).

When marshalling a JAXBException will result in a WebApplicationException being thrown with a status of 500 (Internal Server error).

Author:
Paul Sandoz, Martin Matula
  • Constructor Details

    • AbstractCollectionJaxbProvider

      public AbstractCollectionJaxbProvider(Providers ps)
    • AbstractCollectionJaxbProvider

      public AbstractCollectionJaxbProvider(Providers ps, MediaType mt)
  • Method Details

    • isReadable

      public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType)
    • isWriteable

      public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType)
    • verifyCollectionSubclass

      public static boolean verifyCollectionSubclass(Class<?> type)
    • verifyArrayType

      public static boolean verifyArrayType(Class type, AbstractCollectionJaxbProvider.JaxbTypeChecker checker)
      The method could be used to check if given type is an array of JAXB beans. It allows customizing the "is this a JAXB bean?" part.
      Parameters:
      type - the array to be checked
      checker - allows JAXB bean check customization
      Returns:
      true if given type is an array of JAXB beans
    • verifyGenericType

      public static boolean verifyGenericType(Type genericType, AbstractCollectionJaxbProvider.JaxbTypeChecker checker)
      The method could be used to check if given type is a collection of JAXB beans. It allows customizing the "is this a JAXB bean?" part.
      Parameters:
      genericType - the type to be checked
      checker - allows JAXB bean check customization
      Returns:
      true if given type is a collection of JAXB beans
    • writeTo

      public final void writeTo(Object t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String,Object> httpHeaders, OutputStream entityStream) throws IOException
      Throws:
      IOException
    • writeCollection

      public abstract void writeCollection(Class<?> elementType, Collection<?> t, MediaType mediaType, Charset c, jakarta.xml.bind.Marshaller m, OutputStream entityStream) throws jakarta.xml.bind.JAXBException, IOException
      Write a collection of JAXB objects as child elements of the root element.
      Parameters:
      elementType - the element type in the collection.
      t - the collection to marshall
      mediaType - the media type
      c - the charset
      m - the marshaller
      entityStream - the output stream to marshall the collection
      Throws:
      jakarta.xml.bind.JAXBException - in case the marshalling of element collection fails.
      IOException - in case of any other I/O error while marshalling the collection of JAXB objects.
    • readFrom

      public final Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String,String> httpHeaders, InputStream inputStream) throws IOException
      Throws:
      IOException
    • getXMLStreamReader

      protected abstract XMLStreamReader getXMLStreamReader(Class<?> elementType, MediaType mediaType, jakarta.xml.bind.Unmarshaller unmarshaller, InputStream entityStream) throws XMLStreamException
      Get the XMLStreamReader for unmarshalling.
      Parameters:
      elementType - the individual element type.
      mediaType - the media type.
      unmarshaller - the unmarshaller as a carrier of possible config options.
      entityStream - the input stream.
      Returns:
      the XMLStreamReader.
      Throws:
      XMLStreamException - in case XMLStreamReader retrieval fails.
    • getElementClass

      protected static Class getElementClass(Class<?> type, Type genericType)
    • getRootElementName

      protected final String getRootElementName(Class<?> elementType)
      Construct the name of the root element from it's Java type name.
      Parameters:
      elementType - element Java type.
      Returns:
      constructed root element name for a given element Java type.
    • getElementName

      protected static String getElementName(Class<?> elementType)
      Get the element name for a given Java type.

      In case the element is annotated with a XmlRootElement annotation and the specified element name is not default, the method returns the specified element name in the annotation. Otherwise, the method returns the name of the element class instead.

      Parameters:
      elementType - element Java type.
      Returns:
      element name for a given element Java type.