Blob Blame History Raw
From 4407282c46d3f3b25fe2c13eb67d30f3a5b9dfd0 Mon Sep 17 00:00:00 2001
From: Mikolaj Izdebski <mizdebsk@redhat.com>
Date: Fri, 30 Jan 2015 13:16:40 +0100
Subject: [PATCH 06/14] Port from Simple 4 to Jetty 9

---
 subprojects/javascript/javascript.gradle           |  2 +-
 .../http/simple/SimpleHttpFileServerFactory.java   | 38 +++++------
 .../simple/internal/SimpleFileServerContainer.java | 75 ----------------------
 3 files changed, 20 insertions(+), 95 deletions(-)
 delete mode 100644 subprojects/javascript/src/main/groovy/org/gradle/plugins/javascript/envjs/http/simple/internal/SimpleFileServerContainer.java

diff --git a/subprojects/javascript/javascript.gradle b/subprojects/javascript/javascript.gradle
index 0f60c8c..5fd1eef 100644
--- a/subprojects/javascript/javascript.gradle
+++ b/subprojects/javascript/javascript.gradle
@@ -19,7 +19,7 @@ dependencies {
 
     compile "org.mozilla:rhino:1.7R3"
     compile libraries.gson // used by JsHint
-    compile "org.simpleframework:simple:4.1.21" // used by http package in envjs
+    compile libraries.jetty // used by http package in envjs
     compile project(':core'), project(":plugins")
     compile libraries.inject
 
diff --git a/subprojects/javascript/src/main/groovy/org/gradle/plugins/javascript/envjs/http/simple/SimpleHttpFileServerFactory.java b/subprojects/javascript/src/main/groovy/org/gradle/plugins/javascript/envjs/http/simple/SimpleHttpFileServerFactory.java
index 8a4f692..6983db2 100644
--- a/subprojects/javascript/src/main/groovy/org/gradle/plugins/javascript/envjs/http/simple/SimpleHttpFileServerFactory.java
+++ b/subprojects/javascript/src/main/groovy/org/gradle/plugins/javascript/envjs/http/simple/SimpleHttpFileServerFactory.java
@@ -16,43 +16,43 @@
 
 package org.gradle.plugins.javascript.envjs.http.simple;
 
+import org.eclipse.jetty.server.Handler;
+import org.eclipse.jetty.server.Server;
+import org.eclipse.jetty.server.handler.DefaultHandler;
+import org.eclipse.jetty.server.handler.HandlerList;
+import org.eclipse.jetty.server.handler.ResourceHandler;
 import org.gradle.api.UncheckedIOException;
 import org.gradle.internal.concurrent.Stoppable;
 import org.gradle.plugins.javascript.envjs.http.HttpFileServer;
 import org.gradle.plugins.javascript.envjs.http.HttpFileServerFactory;
-import org.gradle.plugins.javascript.envjs.http.simple.internal.SimpleFileServerContainer;
-import org.simpleframework.http.core.Container;
-import org.simpleframework.http.core.ContainerServer;
-import org.simpleframework.http.resource.FileContext;
-import org.simpleframework.transport.Server;
-import org.simpleframework.transport.connect.Connection;
-import org.simpleframework.transport.connect.SocketConnection;
 
 import java.io.File;
-import java.io.IOException;
-import java.net.InetSocketAddress;
 
 public class SimpleHttpFileServerFactory implements HttpFileServerFactory {
 
     public HttpFileServer start(File contentRoot, int port) {
-        Container container = new SimpleFileServerContainer(new FileContext(contentRoot));
-
         try {
-            final Server server = new ContainerServer(container);
-            Connection connection = new SocketConnection(server);
-            InetSocketAddress address = new InetSocketAddress(port);
-            InetSocketAddress usedAddress = (InetSocketAddress)connection.connect(address);
+            final Server server = new Server(8080);
+
+            ResourceHandler handler = new ResourceHandler();
+            handler.setResourceBase(contentRoot.getPath());
+
+            HandlerList handlers = new HandlerList();
+            handlers.setHandlers(new Handler[] { handler, new DefaultHandler() });
+            server.setHandler(handlers);
+
+            server.start();
 
-            return new SimpleHttpFileServer(contentRoot, usedAddress.getPort(), new Stoppable() {
+            return new SimpleHttpFileServer(contentRoot, port, new Stoppable() {
                 public void stop() {
                     try {
-                        server.stop();
-                    } catch (IOException e) {
+                        server.join();
+                    } catch (InterruptedException e) {
                         throw new UncheckedIOException(e);
                     }
                 }
             });
-        } catch (IOException e) {
+        } catch (Exception e) {
             throw new UncheckedIOException(e);
         }
     }
diff --git a/subprojects/javascript/src/main/groovy/org/gradle/plugins/javascript/envjs/http/simple/internal/SimpleFileServerContainer.java b/subprojects/javascript/src/main/groovy/org/gradle/plugins/javascript/envjs/http/simple/internal/SimpleFileServerContainer.java
deleted file mode 100644
index 0f3e260..0000000
--- a/subprojects/javascript/src/main/groovy/org/gradle/plugins/javascript/envjs/http/simple/internal/SimpleFileServerContainer.java
+++ /dev/null
@@ -1,75 +0,0 @@
-/*
- * Copyright 2012 the original author or authors.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.gradle.plugins.javascript.envjs.http.simple.internal;
-
-import org.apache.commons.io.IOUtils;
-import org.gradle.api.UncheckedIOException;
-import org.simpleframework.http.Request;
-import org.simpleframework.http.Response;
-import org.simpleframework.http.core.Container;
-import org.simpleframework.http.resource.Context;
-import org.simpleframework.http.resource.Index;
-
-import java.io.*;
-import java.nio.charset.Charset;
-
-public class SimpleFileServerContainer implements Container {
-
-    private final Context context;
-
-    public SimpleFileServerContainer(Context context) {
-        this.context = context;
-    }
-
-    public void handle(Request req, Response resp) {
-        Index requestIndex = context.getIndex(req.getTarget());
-        File targetFile = requestIndex.getFile();
-
-        if (!targetFile.exists()) {
-            resp.setCode(404);
-            try {
-                resp.getPrintStream().println(String.format("File '%s' does not exist", targetFile.getAbsolutePath()));
-                resp.commit();
-            } catch (IOException e) {
-                throw new UncheckedIOException(e);
-            }
-        }
-
-        String contentType = requestIndex.getContentType();
-        resp.set("Content-Type", contentType);
-
-        OutputStream output = null;
-        try {
-            output = resp.getOutputStream();
-
-            if (contentType.startsWith("text/")) {
-                resp.set("Content-Encoding", Charset.defaultCharset().name());
-                Reader input = new FileReader(requestIndex.getFile());
-                IOUtils.copy(input, output);
-                IOUtils.closeQuietly(input);
-            } else {
-                InputStream input = new FileInputStream(requestIndex.getFile());
-                IOUtils.copy(input, output);
-                IOUtils.closeQuietly(input);
-            }
-        } catch (IOException e) {
-            throw new UncheckedIOException(e);
-        } finally {
-            IOUtils.closeQuietly(output);
-        }
-    }
-}
-- 
2.5.5