<chapter id="Installation_Configuration">
    <title>Installation/Configuration</title>

    <para>
        RESTEasy is installed and configured in different ways depending on which environment you are running in.
        If you are running in JBoss AS 6-M4 (milestone 4) or higher, resteasy is already bundled and integrated
        completely
        so there is very little you have to do. If you are running in a different distribution, there is some manual
        installation and configuration you will have to do.

    </para>

    <sect1>
        <title>Standalone Resteasy</title>
        <para>
            If you are using resteasy outside of JBoss AS 6, you will need to do a few manual steps to install and
            configure resteasy.
            RESTeasy is deployed as a WAR archive and thus depends on a Servlet container. We strongly suggest that you
            use Maven to build your WAR files as RESTEasy is split into
            a bunch of different modules. You can see an example Maven project in one of the examples in the examples/
            directory
        </para>
        <para>
            Also, when you download RESTeasy and unzip it you will see a lib/ directory that contains the libraries
            needed by resteasy.
            Copy these into your /WEB-INF/lib directory. Place your JAX-RS annotated class resources and providers
            within
            one or more jars within /WEB-INF/lib or your raw class files within /WEB-INF/classes.
        </para>
        <para>
            RESTeasy is implemented as a Servlet and deployed within a WAR file. If you open up the WEB-INF/web.xml in
            one of the example projects of your RESTeasy download you will see this:
        </para>
        <para>

            <programlisting>
                <![CDATA[
<web-app>
    <display-name>Archetype Created Web Application</display-name>

    <servlet>
        <servlet-name>Resteasy</servlet-name>
        <servlet-class>
            org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
        </servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>com.restfully.shop.services.ShoppingApplication</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>Resteasy</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

</web-app>
]]>
            </programlisting>
        </para>
        <para>
            The Resteasy servlet is responsible for initializing some basic components of RESTeasy.
        </para>
    </sect1>
    <sect1>
        <title>Configuration Switches</title>
        <para>Resteasy receives configuration options from &lt;context-param&gt; elements.</para>
        <para>
            <table frame="topbot">
                <tgroup cols="3" rowsep="1" colsep="1">
                    <thead>
                        <row>
                            <entry>
                                Option Name
                            </entry>
                            <entry>
                                Default Value
                            </entry>
                            <entry>
                                Description
                            </entry>
                        </row>
                    </thead>
                    <tbody>
                        <row>
                            <entry>
                                resteasy.servlet.mapping.prefix
                            </entry>
                            <entry>
                                no default
                            </entry>
                            <entry>
                                If the url-pattern for the Resteasy servlet-mapping is not /*
                            </entry>
                        </row>
                        <row>
                            <entry>
                                resteasy.scan
                            </entry>
                            <entry>
                                false
                            </entry>
                            <entry>
                                Automatically scan WEB-INF/lib jars and WEB-INF/classes directory for both @Provider and
                                JAX-RS resource classes (@Path, @GET, @POST etc..) and register them
                            </entry>
                        </row>
                        <row>
                            <entry>
                                resteasy.scan.providers
                            </entry>
                            <entry>
                                false
                            </entry>
                            <entry>
                                Scan for @Provider classes and register them
                            </entry>
                        </row>
                        <row>
                            <entry>
                                resteasy.scan.resources
                            </entry>
                            <entry>
                                false
                            </entry>
                            <entry>
                                Scan for JAX-RS resource classes
                            </entry>
                        </row>
                        <row>
                            <entry>
                                resteasy.providers
                            </entry>
                            <entry>
                                no default
                            </entry>
                            <entry>
                                A comma delimited list of fully qualified @Provider class names you want to register
                            </entry>
                        </row>
                        <row>
                            <entry>
                                resteasy.use.builtin.providers
                            </entry>
                            <entry>
                                true
                            </entry>
                            <entry>
                                Whether or not to register default, built-in @Provider classes. (Only available in
                                1.0-beta-5 and later)
                            </entry>
                        </row>
                        <row>
                            <entry>
                                resteasy.resources
                            </entry>
                            <entry>
                                no default
                            </entry>
                            <entry>
                                A comma delimited list of fully qualified JAX-RS resource class names you want to
                                register
                            </entry>
                        </row>
                        <row>
                            <entry>
                                resteasy.jndi.resources
                            </entry>
                            <entry>
                                no default
                            </entry>
                            <entry>
                                A comma delimited list of JNDI names which reference objects you want to register as
                                JAX-RS resources
                            </entry>
                        </row>
                        <row>
                            <entry>
                                javax.ws.rs.Application
                            </entry>
                            <entry>
                                no default
                            </entry>
                            <entry>
                                Fully qualified name of Application class to bootstrap in a spec portable way
                            </entry>
                        </row>
                        <row>
                            <entry>
                                resteasy.media.type.mappings
                            </entry>
                            <entry>
                                no default
                            </entry>
                            <entry>
                                Replaces the need for an Accept header by mapping file name extensions (like .xml or
                                .txt) to a media type. Used when the client
                                is unable to use a Accept header to choose a representation (i.e. a browser). See JAX-RS
                                Content Negotiation chapter for more details.
                            </entry>
                        </row>
                        <row>
                            <entry>
                                resteasy.language.mappings
                            </entry>
                            <entry>
                                no default
                            </entry>
                            <entry>
                                Replaces the need for an Accept-Language header by mapping file name extensions (like
                                .en or .fr) to a language. Used when the client
                                is unable to use a Accept-Language header to choose a language (i.e. a browser). See
                                JAX-RS Content Negotiation chapter for more details
                            </entry>
                        </row>
                        <row>
                            <entry>
                                resteasy.document.expand.entity.references
                            </entry>
                            <entry>
                                true
                            </entry>
                            <entry>
                                Expand external entities in org.w3c.dom.Document files
                            </entry>
                        </row>
                    </tbody>
                </tgroup>
            </table>

        </para>
        <para>

            The resteasy.servlet.mapping.prefix &lt;context param&gt; variable must be set if your servlet-mapping for
            the Resteasy servlet has a url-pattern other than /*. For example, if the url-pattern is
        </para>
        <para>

            <programlisting>

                &lt;servlet-mapping&gt;
                &lt;servlet-name&gt;Resteasy&lt;/servlet-name&gt;
                &lt;url-pattern&gt;/restful-services/*&lt;/url-pattern&gt;
                &lt;/servlet-mapping&gt;
            </programlisting>
        </para>
        <para>

            Then the value of resteasy-servlet.mapping.prefix must be:
        </para>
        <para>

            <programlisting>

                &lt;context-param&gt;
                &lt;param-name&gt;resteasy.servlet.mapping.prefix&lt;/param-name&gt;
                &lt;param-value&gt;/restful-services&lt;/param-value&gt;
                &lt;/context-param&gt;
            </programlisting>
        </para>
    </sect1>
    <sect1 id="javax.ws.rs.core.Application">
        <title>javax.ws.rs.core.Application</title>

        <para>

            The javax.ws.rs.core.Application class is a standard JAX-RS class that you may implement to provide
            information on your deployment. It is simply a class the lists all JAX-RS root resources and providers.
        </para>
        <para>

            <programlisting>
                /**
                * Defines the components of a JAX-RS application and supplies additional
                * metadata. A JAX-RS application or implementation supplies a concrete
                * subclass of this abstract class.
                */
                public abstract class Application
                {
                private static final Set&lt;Object&gt; emptySet = Collections.emptySet();

                /**
                * Get a set of root resource and provider classes. The default lifecycle
                * for resource class instances is per-request. The default lifecycle for
                * providers is singleton.
                * &lt;p/&gt;
                * &lt;p&gt;Implementations should warn about and ignore classes that do not
                * conform to the requirements of root resource or provider classes.
                * Implementations should warn about and ignore classes for which
                * {@link #getSingletons()} returns an instance. Implementations MUST
                * NOT modify the returned set.&lt;/p&gt;
                *
                * @return a set of root resource and provider classes. Returning null
                * is equivalent to returning an empty set.
                */
                public abstract Set&lt;Class&lt;?&gt;&gt; getClasses();

                /**
                * Get a set of root resource and provider instances. Fields and properties
                * of returned instances are injected with their declared dependencies
                * (see {@link Context}) by the runtime prior to use.
                * &lt;p/&gt;
                * &lt;p&gt;Implementations should warn about and ignore classes that do not
                * conform to the requirements of root resource or provider classes.
                * Implementations should flag an error if the returned set includes
                * more than one instance of the same class. Implementations MUST
                * NOT modify the returned set.&lt;/p&gt;
                * &lt;p/&gt;
                * &lt;p&gt;The default implementation returns an empty set.&lt;/p&gt;
                *
                * @return a set of root resource and provider instances. Returning null
                * is equivalent to returning an empty set.
                */
                public Set&lt;Object&gt; getSingletons()
                {
                return emptySet;
                }

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


        </para>
        <para>

            To use Application you must set a servlet init-param, javax.ws.rs.Application with a fully qualified class
            that implements Application. For example:
        </para>
        <para>

            <programlisting>    <![CDATA[
    <servlet>
        <servlet-name>Resteasy</servlet-name>
        <servlet-class>
            org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
        </servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>com.restfully.shop.services.ShoppingApplication</param-value>
        </init-param>
    </servlet> ]]> </programlisting>
        </para>
        <para>

            If you have this set, you should probably turn off automatic scanning as this will probably result in
            duplicate classes being registered.
        </para>
        <para>


        </para>
    </sect1>

    <sect1 id="listener">
        <title>RESTEasy as a ServletContextListener</title>

        <para>
            The initialization of RESTEasy can be performed within a ServletContextListener instead of within the
            Servlet. You may need this if you are writing custom Listeners that need to interact with RESTEasy at boot
            time. An example of this is the RESTEasy Spring integration that requires a Spring ServletContextListener.
            The org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap class is a ServletContextListener that
            configures an instance of an ResteasyProviderFactory and Registry. You can obtain instances of a
            ResteasyProviderFactory and Registry from the ServletContext attributes
            org.jboss.resteasy.spi.ResteasyProviderFactory and org.jboss.resteasy.spi.Registry. From these instances you
            can programmatically interact with RESTEasy registration interfaces.
        </para>

        <programlisting>
            <![CDATA[
<web-app>
   <listener>
      <listener-class>
         org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
      </listener-class>
   </listener>

  <!-- ** INSERT YOUR LISTENERS HERE!!!! -->

   <servlet>
      <servlet-name>Resteasy</servlet-name>
      <servlet-class>
         org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
      </servlet-class>
   </servlet>

   <servlet-mapping>
      <servlet-name>Resteasy</servlet-name>
      <url-pattern>/resteasy/*</url-pattern>
   </servlet-mapping>

</web-app>
]]>
        </programlisting>
    </sect1>
    <sect1 id="filter">
        <title>RESTEasy as a servlet Filter</title>

        <para>
            The downside of running Resteasy as a Servlet is that you cannot have static resources like .html and .jpeg
            files in the
            same path as your JAX-RS services. Resteasy allows you to run as a Filter instead. If a JAX-RS resource is
            not
            found under the URL requested, Resteasy will delegate back to the base servlet container to resolve URLs.
        </para>

        <programlisting>
            <![CDATA[
<web-app>
    <filter>
        <filter-name>Resteasy</filter-name>
        <filter-class>
            org.jboss.resteasy.plugins.server.servlet.FilterDispatcher
        </filter-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>com.restfully.shop.services.ShoppingApplication</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>Resteasy</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>]]>
        </programlisting>
    </sect1>
    <sect1>
        <title>Patching JBoss AS 7</title>
        <para>
            Resteasy is bundled with JBoss AS 7.  You will likely have the need to upgrade Resteasy in AS7.  The Resteasy
            distribution comes with a zip file called resteasy-jboss-modules-2.3.5.Final.zip.  In the top level directory
            of JBoss AS 7 (the one with the modules/ directory), unzip this file.  You should be patched with the latest
            and greatest Resteasy after this.
        </para>
    </sect1>
    <sect1>
        <title>Configuring in JBoss AS 7</title>
        <para>
            RESTEasy is bundled with JBoss AS 7 and completely integrated as per the requirements of Java EE 6.
            First you must at least provide an empty web.xml file.  You can of course deploy any custom servlet, filter
            or security constraint you want to within your web.xml, but the least amount of work is to create an empty
            web.xml file.  Also, resteasy context-params are available if you want to tweak turn on/off any specific
            resteasy feature.
        </para>
        <para>
            <programlisting>
                <![CDATA[
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
</web-app>
]]>
            </programlisting>
        </para>
        <para>
            Since we're not using a jax-rs servlet mapping, we must define an Application class that is annotated with
            the @ApplicationPath annotation.  If you return any empty set for by classes and singletons, your WAR will
            be scanned for JAX-RS annotation resource and provider classes.
        </para>
        <programlisting>
            import javax.ws.rs.ApplicationPath;
            import javax.ws.rs.core.Application;

            @ApplicationPath("/root-path")
            public class MyApplication extends Application
            {
            }
        </programlisting>
        <para>
            The Resteasy distribution has ported the "Restful Java" O'Reilly workbook examples to AS7.  You can
            find these under the directory examples/oreilly-workbook-as7.
        </para>
    </sect1>
    <sect1 id="RESTEasyLogging">
        <title>RESTEasyLogging</title>

        <para>

            RESTEasy supports logging via java.util.logging, Log4j, or Slf4j.  How it picks which framework to delegate to is
	    expressed in the following algorithm:
        </para>
	<itemizedlist>
	  <listitem>
	    <para>If log4j is in the application's classpath, log4j will be used</para>
	  </listitem>
	  <listitem>
	    <para>If slf4j is in the application's classpath, slf4j will be used</para>
	  </listitem>
	  <listitem>
	    <para>java.util.logging is the default if neither log4j or slf4j is in the classpath</para>
	  </listitem>
	  <listitem>
	    <para>If the servlet context param resteasy.logger.type is set to JUL, LOG4J, or SLF4J will override this default behavior</para>
	  </listitem>
	</itemizedlist>
        <para>
            The logging categories are still a work in progress, but the initial set should make it easier to
            troubleshoot issues. Currently, the framework has defined the following log categories:
        </para>
        <para>

            <table frame="topbot">
                <tgroup cols="2" rowsep="1" colsep="1">
                    <thead>
                        <row>
                            <entry>
                                Category
                            </entry>
                            <entry>
                                Function
                            </entry>
                        </row>
                    </thead>
                    <tbody>
                        <row>
                            <entry>
                                org.jboss.resteasy.core
                            </entry>
                            <entry>
                                Logs all activity by the core RESTEasy implementation
                            </entry>
                        </row>
                        <row>
                            <entry>
                                org.jboss.resteasy.plugins.providers
                            </entry>
                            <entry>
                                Logs all activity by RESTEasy entity providers
                            </entry>
                        </row>
                        <row>
                            <entry>
                                org.jboss.resteasy.plugins.server
                            </entry>
                            <entry>
                                Logs all activity by the RESTEasy server implementation.
                            </entry>
                        </row>
                        <row>
                            <entry>
                                org.jboss.resteasy.specimpl
                            </entry>
                            <entry>
                                Logs all activity by JAX-RS implementing classes
                            </entry>
                        </row>
                        <row>
                            <entry>
                                org.jboss.resteasy.mock
                            </entry>
                            <entry>
                                Logs all activity by the RESTEasy mock framework
                            </entry>
                        </row>
                    </tbody>
                </tgroup>
            </table>

        </para>
    </sect1>
</chapter>
