From 69bd2a29170d9b51ad564cafe0590f9893e8611d Mon Sep 17 00:00:00 2001
From: Yannick Lecaillez <yannick.lecaillez@forgerock.com>
Date: Mon, 02 May 2016 07:50:48 +0000
Subject: [PATCH] OPENDJ-2556: Avoid Servlet dependency in OpenDJ server by using grizzly-http.

---
 opendj-dsml-servlet/pom.xml                                                                    |    7 +++++++
 opendj-server-legacy/pom.xml                                                                   |   16 ++--------------
 opendj-server-legacy/src/main/java/org/opends/server/protocols/http/SdkConnectionAdapter.java  |    5 ++---
 opendj-server-legacy/src/main/java/org/opends/server/protocols/http/HTTPConnectionHandler.java |   16 ++--------------
 4 files changed, 13 insertions(+), 31 deletions(-)

diff --git a/opendj-dsml-servlet/pom.xml b/opendj-dsml-servlet/pom.xml
index c164268..1fc243a 100644
--- a/opendj-dsml-servlet/pom.xml
+++ b/opendj-dsml-servlet/pom.xml
@@ -36,6 +36,13 @@
     </properties>
 
     <dependencies>
+    	<!-- Servlet API -->
+        <dependency>
+      		<groupId>javax.servlet</groupId>
+      		<artifactId>javax.servlet-api</artifactId>
+      		<version>3.1.0</version>
+    	</dependency>
+
         <!-- ForgeRock libraries -->
         <dependency>
             <groupId>org.forgerock.commons</groupId>
diff --git a/opendj-server-legacy/pom.xml b/opendj-server-legacy/pom.xml
index 3b6677c..79b6f66 100644
--- a/opendj-server-legacy/pom.xml
+++ b/opendj-server-legacy/pom.xml
@@ -133,7 +133,7 @@
 
     <dependency>
       <groupId>org.forgerock.http</groupId>
-      <artifactId>chf-http-servlet</artifactId>
+      <artifactId>chf-http-grizzly</artifactId>
     </dependency>
 
     <!-- ForgeRock Common Audit libraries -->
@@ -173,19 +173,7 @@
       <artifactId>jul-to-slf4j</artifactId>
     </dependency>
 
-    <!-- servlet and mail -->
-    <dependency>
-      <groupId>javax.servlet</groupId>
-      <artifactId>javax.servlet-api</artifactId>
-      <version>3.1.0</version>
-    </dependency>
-
-    <dependency>
-      <groupId>org.glassfish.grizzly</groupId>
-      <artifactId>grizzly-http-servlet</artifactId>
-      <version>${grizzly-framework.version}</version>
-    </dependency>
-
+    <!-- mail -->
     <dependency>
       <groupId>com.sun.mail</groupId>
       <artifactId>javax.mail</artifactId>
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/protocols/http/HTTPConnectionHandler.java b/opendj-server-legacy/src/main/java/org/opends/server/protocols/http/HTTPConnectionHandler.java
index ec1950b..399d09e 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/protocols/http/HTTPConnectionHandler.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/protocols/http/HTTPConnectionHandler.java
@@ -19,6 +19,7 @@
 import static org.opends.messages.ProtocolMessages.*;
 import static org.opends.server.util.ServerConstants.*;
 import static org.opends.server.util.StaticUtils.*;
+import static org.forgerock.http.grizzly.GrizzlySupport.newGrizzlyHttpHandler;
 
 import java.io.IOException;
 import java.net.InetAddress;
@@ -45,7 +46,6 @@
 import org.forgerock.http.HttpApplicationException;
 import org.forgerock.http.handler.Handlers;
 import org.forgerock.http.io.Buffer;
-import org.forgerock.http.servlet.HttpFrameworkServlet;
 import org.forgerock.i18n.LocalizableMessage;
 import org.forgerock.i18n.slf4j.LocalizedLogger;
 import org.forgerock.opendj.config.server.ConfigChangeResult;
@@ -62,7 +62,6 @@
 import org.glassfish.grizzly.http.server.ServerConfiguration;
 import org.glassfish.grizzly.monitoring.MonitoringConfig;
 import org.glassfish.grizzly.nio.transport.TCPNIOTransport;
-import org.glassfish.grizzly.servlet.WebappContext;
 import org.glassfish.grizzly.ssl.SSLEngineConfigurator;
 import org.glassfish.grizzly.strategies.SameThreadIOStrategy;
 import org.glassfish.grizzly.utils.Charsets;
@@ -681,10 +680,7 @@
     }
 
     this.httpServer = createHttpServer();
-
-    // Register servlet as default servlet and also able to serve REST requests
-    createAndRegisterServlet("OpenDJ HTTP servlet", "", "/*");
-
+    this.httpServer.getServerConfiguration().addHttpHandler(newGrizzlyHttpHandler(new RootHttpApplication()));
     logger.trace("Starting HTTP server...");
     this.httpServer.start();
     logger.trace("HTTP server started");
@@ -748,14 +744,6 @@
     return server.getServerConfiguration().getMonitoringConfig().getHttpConfig();
   }
 
-  private void createAndRegisterServlet(final String servletName, final String... urlPatterns) throws Exception
-  {
-    // Create and deploy the Web app context
-    final WebappContext ctx = new WebappContext(servletName);
-    ctx.addServlet(servletName, new HttpFrameworkServlet(new RootHttpApplication())).addMapping(urlPatterns);
-    ctx.deploy(this.httpServer);
-  }
-
   private void stopHttpServer()
   {
     if (this.httpServer != null)
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/protocols/http/SdkConnectionAdapter.java b/opendj-server-legacy/src/main/java/org/opends/server/protocols/http/SdkConnectionAdapter.java
index 4838d5e..e73b185 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/protocols/http/SdkConnectionAdapter.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/protocols/http/SdkConnectionAdapter.java
@@ -23,8 +23,7 @@
 import java.util.LinkedHashSet;
 import java.util.concurrent.atomic.AtomicInteger;
 
-import javax.servlet.http.HttpServletResponse;
-
+import org.forgerock.http.protocol.Status;
 import org.forgerock.i18n.slf4j.LocalizedLogger;
 import org.forgerock.opendj.ldap.AbstractAsynchronousConnection;
 import org.forgerock.opendj.ldap.ByteString;
@@ -283,7 +282,7 @@
 
     // At this point, we try to log the request with OK status code.
     // If it was already logged, it will be a no op.
-    this.clientConnection.log(HttpServletResponse.SC_OK);
+    this.clientConnection.log(Status.OK.getCode());
 
     isClosed = true;
   }

--
Gitblit v1.10.0