<chapter id="Content_Marshalling_Providers">
   <title>Content Marshalling/Providers</title>
   <sect1 id="Default_Providers_and_default_JAX-RS_Content_Marshalling">
      <title>Default Providers and default JAX-RS Content Marshalling</title>

      <para>

         Resteasy can automatically marshal and unmarshal a few different message bodies.
      </para>
      <para>

         <table frame="topbot">
            <tgroup cols="2" rowsep="1" colsep="1">
               <thead>
                  <row>
                     <entry>
                        Media Types
                     </entry>
                     <entry>
                        Java Type
                     </entry>
                  </row>
               </thead>
               <tbody>
                  <row>
                     <entry>
                        application/*+xml, text/*+xml, application/*+json, application/*+fastinfoset, application/atom+*
                     </entry>
                     <entry>
                        JaxB annotated classes
                     </entry>
                  </row>
                  <row>
                     <entry>
                        application/*+xml, text/*+xml
                     </entry>
                     <entry>
                        org.w3c.dom.Document
                     </entry>
                  </row>
                  <row>
                     <entry>
                        */*
                     </entry>
                     <entry>
                        java.lang.String
                     </entry>
                  </row>
                  <row>
                     <entry>
                        */*
                     </entry>
                     <entry>
                        java.io.InputStream
                     </entry>
                  </row>
                  <row>
                     <entry>
                        text/plain
                     </entry>
                     <entry>
                        primtives, java.lang.String, or any type that has a String constructor, or static
                        valueOf(String) method for input, toString() for output
                     </entry>
                  </row>
                  <row>
                     <entry>
                        */*
                     </entry>
                     <entry>
                        javax.activation.DataSource
                     </entry>
                  </row>
                  <row>
                     <entry>
                        */*
                     </entry>
                     <entry>
                        java.io.File
                     </entry>
                  </row>
                  <row>
                     <entry>
                        */*
                     </entry>
                     <entry>
                        byte[]
                     </entry>
                  </row>
                  <row>
                     <entry>
                        application/x-www-form-urlencoded
                     </entry>
                     <entry>
                        javax.ws.rs.core.MultivaluedMap
                     </entry>
                  </row>
               </tbody>
            </tgroup>
         </table>

      </para>
      <para>

      </para>
      <para>

      </para>
      <para>


      </para>
   </sect1>
   <sect1 id="Content_Marshalling_with__Provider_classes">
      <title>Content Marshalling with @Provider classes</title>

      <para>

         The JAX-RS specification allows you to plug in your own request/response body reader and writers. To do this,
         you annotate a class
         with @Provider and specify the @Produces types for a writer and @Consumes types for a reader. You must
         also implement
         a MessageBodyReader/Writer interface respectively. Here is an example.
      </para>
      <para>


         The Resteasy ServletContextLoader will automatically scan your WEB-INF/lib and classes directories for classes
         annotated with @Provider or you can manually configure them in web.xml. See Installation/Configuration
      </para>
      <para>


      </para>
   </sect1>
   <sect1 id="MessageBodyWorkers">
      <title>Providers Utility Class</title>

      <para>

         javax.ws.rs.ext.Providers is a simple injectable interface that allows you to look up MessageBodyReaders,
          Writers, ContextResolvers, and ExceptionMappers. It is very useful, for instance, for implementing
          multipart providers. Content types that embed
         other random content types.
      </para>
      <para>

         <programlisting>
<![CDATA[
public interface Providers
{

   /**
    * Get a message body reader that matches a set of criteria. The set of
    * readers is first filtered by comparing the supplied value of
    * {@code mediaType} with the value of each reader's
    * {@link javax.ws.rs.Consumes}, ensuring the supplied value of
    * {@code type} is assignable to the generic type of the reader, and
    * eliminating those that do not match.
    * The list of matching readers is then ordered with those with the best
    * matching values of {@link javax.ws.rs.Consumes} (x/y > x&#47;* > *&#47;*)
    * sorted first. Finally, the
    * {@link MessageBodyReader#isReadable}
    * method is called on each reader in order using the supplied criteria and
    * the first reader that returns {@code true} is selected and returned.
    *
    * @param type        the class of object that is to be written.
    * @param mediaType   the media type of the data that will be read.
    * @param genericType the type of object to be produced. E.g. if the
    *                    message body is to be converted into a method parameter, this will be
    *                    the formal type of the method parameter as returned by
    *                    <code>Class.getGenericParameterTypes</code>.
    * @param annotations an array of the annotations on the declaration of the
    *                    artifact that will be initialized with the produced instance. E.g. if the
    *                    message body is to be converted into a method parameter, this will be
    *                    the annotations on that parameter returned by
    *                    <code>Class.getParameterAnnotations</code>.
    * @return a MessageBodyReader that matches the supplied criteria or null
    *         if none is found.
    */
   <T> MessageBodyReader<T> getMessageBodyReader(Class<T> type,
                                                 Type genericType, Annotation annotations[], MediaType mediaType);

   /**
    * Get a message body writer that matches a set of criteria. The set of
    * writers is first filtered by comparing the supplied value of
    * {@code mediaType} with the value of each writer's
    * {@link javax.ws.rs.Produces}, ensuring the supplied value of
    * {@code type} is assignable to the generic type of the reader, and
    * eliminating those that do not match.
    * The list of matching writers is then ordered with those with the best
    * matching values of {@link javax.ws.rs.Produces} (x/y > x&#47;* > *&#47;*)
    * sorted first. Finally, the
    * {@link MessageBodyWriter#isWriteable}
    * method is called on each writer in order using the supplied criteria and
    * the first writer that returns {@code true} is selected and returned.
    *
    * @param mediaType   the media type of the data that will be written.
    * @param type        the class of object that is to be written.
    * @param genericType the type of object to be written. E.g. if the
    *                    message body is to be produced from a field, this will be
    *                    the declared type of the field as returned by
    *                    <code>Field.getGenericType</code>.
    * @param annotations an array of the annotations on the declaration of the
    *                    artifact that will be written. E.g. if the
    *                    message body is to be produced from a field, this will be
    *                    the annotations on that field returned by
    *                    <code>Field.getDeclaredAnnotations</code>.
    * @return a MessageBodyReader that matches the supplied criteria or null
    *         if none is found.
    */
   <T> MessageBodyWriter<T> getMessageBodyWriter(Class<T> type,
                                                 Type genericType, Annotation annotations[], MediaType mediaType);

   /**
    * Get an exception mapping provider for a particular class of exception.
    * Returns the provider whose generic type is the nearest superclass of
    * {@code type}.
    *
    * @param type the class of exception
    * @return an {@link ExceptionMapper} for the supplied type or null if none
    *         is found.
    */
   <T extends Throwable> ExceptionMapper<T> getExceptionMapper(Class<T> type);

   /**
    * Get a context resolver for a particular type of context and media type.
    * The set of resolvers is first filtered by comparing the supplied value of
    * {@code mediaType} with the value of each resolver's
    * {@link javax.ws.rs.Produces}, ensuring the generic type of the context
    * resolver is assignable to the supplied value of {@code contextType}, and
    * eliminating those that do not match. If only one resolver matches the
    * criteria then it is returned. If more than one resolver matches then the
    * list of matching resolvers is ordered with those with the best
    * matching values of {@link javax.ws.rs.Produces} (x/y > x&#47;* > *&#47;*)
    * sorted first. A proxy is returned that delegates calls to
    * {@link ContextResolver#getContext(java.lang.Class)} to each matching context
    * resolver in order and returns the first non-null value it obtains or null
    * if all matching context resolvers return null.
    *
    * @param contextType the class of context desired
    * @param mediaType   the media type of data for which a context is required.
    * @return a matching context resolver instance or null if no matching
    *         context providers are found.
    */
   <T> ContextResolver<T> getContextResolver(Class<T> contextType,
                                             MediaType mediaType);
}
]]>
         </programlisting>
      </para>
      <para>


      </para>
      <para>

         A Providers instance is injectable into MessageBodyReader or Writers:
      </para>
      <para>

         <programlisting>
            @Provider
            @Consumes("multipart/fixed")
            public class MultipartProvider implements MessageBodyReader {

            private @Context Providers providers;

            ...

            }
         </programlisting>
      </para>
      <para>


      </para>
   </sect1>
   
   <sect1 id="Configuring_Document_Marshalling">
      <title>Configuring Document Marshalling</title>

      <para>
   
         XML document parsers are subject to a form of attack known as the XXE (Xml eXternal Entity) Attack
         (<ulink url="http://www.securiteam.com/securitynews/6D0100A5PU.html">http://www.securiteam.com/securitynews/6D0100A5PU.html</ulink>),
         in which expanding an external entity causes an unsafe file to be loaded.  For example, the document
      </para>
      
      <programlisting>
      &lt;?xml version="1.0"?&gt;
      &lt;!DOCTYPE foo
      [&lt;!ENTITY xxe SYSTEM "file:///etc/passwd"&gt;]&gt; 
      &lt;search&gt;
         &lt;user&gt;bill&lt;/user&gt;
         &lt;file&gt;&amp;xxe;&lt;file&gt;
      &lt;/search&gt;
      </programlisting>
      
      <para>
      
      could cause the passwd file to be loaded.
      </para>
      
      <para>
      
         Resteasy's built-in unmarshaller for org.w3c.dom.Document files will expand external entities by default, but it can be configured to replace them by the empty string by setting the context parameter "resteasy.document.expand.entity.references" to "false" in the web.xml file:
   
      </para>
      
      <programlisting>
         &lt;context-param&gt;
            &lt;param-name&gt;resteasy.document.expand.entity.references&lt;/param-name&gt;
            &lt;param-value&gt;false&lt;/param-value&gt;
         &lt;/context-param&gt;
      </programlisting>
   </sect1>
   
</chapter>
