Class OslcStreams


  • public class OslcStreams
    extends Object
    Transforms an OSLC entity into a stream and vice-verse.

    This capability is usually the responsibility of the JAX-RS framework. However there are cases where the list of providers used or the priority of those providers cannot be controlled. This class uses a fixed list of providers that does not depend on the ones registered at JAX-RS level, thus complete independence on the deployment.

    The providers used here are the ones that can handle OSLC resources (service provider catalog, change request, etc.) for the following well known formats:

    • application/rdf+xml
    • application/xml
    • text/xml
    • application/json
    • text/turtle
    • application/x-oslc-compact+xml
    • application/x-oslc-compact+json

    Jakarta JSON providers are also registered, as they are used by some OSLC services, to handle JsonObject and JsonArray instances for following formats:

    • application/json
    • application/javascript

    Any other type of entity or format is unsupported and methods will throw an exception, as it's considered a developer error to use OslcStreams in such case.

    Since:
    1.13.0
    • Method Detail

      • toEntity

        public static <T> T toEntity​(Class<T> type,
                                     HttpServletRequest request,
                                     HttpHeaders headers)
                              throws WebApplicationException
        Reads a type from the request stream. This method can be used for implementing a JAX-RS PUT or POST service that must extract an entity from the stream of the request. Both the servlet request and headers should be injected as fields of the service class.
        Type Parameters:
        T - the type of entity to read.
        Parameters:
        type - the type that is to be read from the entity stream.
        request - the servlet request from which an entity must be read.
        headers - the HTTP headers associated with HTTP entity.
        Returns:
        the entity that was read from the stream. This method never returns null; an exception is raised if the entity cannot be deserialized.
        Throws:
        WebApplicationException - if the underlying MessageBodyReader provider raises one or a I/O error occurs. The exception embeds a Bad Request response if the entity cannot be deserialized from the request input stream, e.g. when the stream is empty or contains bytes that are not valid regarding the content type.
        IllegalArgumentException - if either the type or format is not supported by this class.
        See Also:
        MessageBodyReader.readFrom(Class, Type, java.lang.annotation.Annotation[], MediaType, MultivaluedMap, InputStream)
      • toEntity

        public static <T> T toEntity​(Class<T> type,
                                     Type genericType,
                                     HttpServletRequest request,
                                     HttpHeaders headers)
                              throws WebApplicationException
        Reads a type from the request stream. This method can be used for implementing a JAX-RS PUT or POST service that must extract an entity from the stream of the request. Both the servlet request and headers should be injected as fields of the service class.
        Type Parameters:
        T - the type of entity to read.
        Parameters:
        type - the type that is to be read from the entity stream.
        genericType - the genericType parameter gives the java.lang.reflect.Type of the object that should be produced.
        request - the servlet request from which an entity must be read.
        headers - the HTTP headers associated with HTTP entity.
        Returns:
        the entity that was read from the stream. This method never returns null; an exception is raised if the entity cannot be deserialized.
        Throws:
        WebApplicationException - if the underlying MessageBodyReader provider raises one or a I/O error occurs. The exception embeds a Bad Request response if the entity cannot be deserialized from the request input stream, e.g. when the stream is empty or contains bytes that are not valid regarding the content type.
        IllegalArgumentException - if either the type or format is not supported by this class.
        Since:
        3.0.0
        See Also:
        MessageBodyReader.readFrom(Class, Type, java.lang.annotation.Annotation[], MediaType, MultivaluedMap, InputStream)
      • toEntity

        public static <T> T toEntity​(Class<T> type,
                                     Type genericType,
                                     InputStream entityStream,
                                     MediaType mediaType)
                              throws IOException
        Reads a type from the request stream. This method can be used on client side to extract an entity from the stream of a response.
        Type Parameters:
        T - the type of entity to read.
        Parameters:
        type - the type that is to be read from the entity stream.
        genericType - the genericType parameter gives the java.lang.reflect.Type of the object that should be produced.
        entityStream - the stream containing an entity to de-serialize.
        mediaType - the format of the stream.
        Returns:
        the type that was read from the stream.
        Throws:
        IOException - if an IO error arises or to wrap an exception raised by the underlying MessageBodyReader provider.
        IllegalArgumentException - if either the type or format is not supported by this class.
        Since:
        3.0.0
        See Also:
        MessageBodyReader.readFrom(Class, Type, java.lang.annotation.Annotation[], MediaType, MultivaluedMap, InputStream)