<chapter id="RESTEasy_Embedded_Container">
<title>Embedded Containers</title>

    <para>Resteasy has a few different plugins for different embedabble HTTP and/or Servlet containers if use Resteasy in
    a test environment, or within an environment where you do not want a Servlet engine dependency.</para>

    <section>
        <title>Sun JDK HTTP Server</title>
        <para>
            The Sun JDK comes with a simple HTTP server implementation (com.sun.net.httpserver.HttpServer) which you
            can run Resteasy on top of.
        </para>
    <programlisting> <![CDATA[
      HttpServer httpServer = HttpServer.create(new InetSocketAddress(port), 10);
      contextBuilder = new HttpContextBuilder();
      contextBuilder.getDeployment().getActualResourceClasses().add(SimpleResource.class);
      HttpContext context = contextBuilder.bind(httpServer);
      context.getAttributes().put("some.config.info", "42");
      httpServer.start();

      contextBuilder.cleanup();
      httpServer.stop(0);
    ]]></programlisting>
    <para>
        Create your HttpServer the way you want then use the org.jboss.resteasy.plugins.server.sun.http.HttpContextBuilder to initialize Resteasy
        and bind it to an HttpContext.  The HttpContext attributes are available by injecting in a org.jboss.resteasy.spi.ResteasyConfiguration
        interface using @Context within your provider and resource classes.

    </para>

    <para>Maven project you must include is:</para>
<programlisting> <![CDATA[
  <dependency>
      <groupId>org.jboss.resteasy</groupId>
      <artifactId>resteasy-jdk-http</artifactId>
      <version>2.3.5.Final</version>
  </dependency>
]]></programlisting>

    </section>

    <section>
        <title>TJWS Embeddable Servlet Container</title>
<para>

RESTeasy integrates with the TJWS Embeddable Servlet container.  It comes with this distribution, or you can reference the Maven artifact.  You must also provide
    a servlet API dependency as well.
</para>

<programlisting> <![CDATA[
  <dependency>
      <groupId>org.jboss.resteasy</groupId>
      <artifactId>tjws</artifactId>
      <version>2.3.5.Final</version>
  </dependency>

  <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
  </dependency>
]]></programlisting>

<para>

From the distribution, move the jars in resteasy-jaxrs.war/WEB-INF/lib into your classpath.  You must both programmatically register your JAX-RS beans using the embedded server's Registry.  Here's an example:
</para>
<para>

<programlisting>

@Path("/")
public class MyResource {

   @GET
   public String get() { return "hello world"; }
 

   public static void main(String[] args) throws Exception 
   {
      TJWSEmbeddedJaxrsServer tjws = new TJWSEmbeddedJaxrsServer();
      tjws.setPort(8080);
      tjws.start();
      tjws.getRegistry().addPerRequestResource(RestEasy485Resource.class);
   }
}

</programlisting>
</para>
<para>

The server can either host non-encrypted or SSL based resources, but not both.  See the Javadoc for TJWSEmbeddedJaxrsServer as well as its superclass TJWSServletServer.  The TJWS website is also a good place for information.
</para>
<para>


</para>
<para>

If you want to use Spring, see the SpringBeanProcessor.  Here's a pseudo-code example
</para>
<para>

<programlisting>

   public static void main(String[] args) throws Exception 
   {
      final TJWSEmbeddedJaxrsServer tjws = new TJWSEmbeddedJaxrsServer();
      tjws.setPort(8081);

      tjws.start();
      org.jboss.resteasy.plugins.server.servlet.SpringBeanProcessor processor = new SpringBeanProcessor(tjws.getDeployment().getRegistry(), tjws.getDeployment().getFactory();
      ConfigurableBeanFactory factory = new XmlBeanFactory(...);
      factory.addBeanPostProcessor(processor);
   }
</programlisting>
      </para>
    </section>
    <section>
        <title>Netty</title>
        <para>
            Resteasy has integration with the popular Netty project as well..
        </para>
    <programlisting> <![CDATA[
   public static void start(ResteasyDeployment deployment) throws Exception
   {
      netty = new NettyJaxrsServer();
      netty.setDeployment(deployment);
      netty.setPort(TestPortProvider.getPort());
      netty.setRootResourcePath("");
      netty.setSecurityDomain(null);
      netty.start();
   }
    ]]></programlisting>

    <para>Maven project you must include is:</para>
<programlisting> <![CDATA[
  <dependency>
      <groupId>org.jboss.resteasy</groupId>
      <artifactId>resteasy-netty</artifactId>
      <version>2.3.5.Final</version>
  </dependency>
]]></programlisting>

    </section>

</chapter>
