Search in sources :

Example 1 with Container

use of org.simpleframework.http.core.Container in project sonarqube by SonarSource.

the class DefaultHttpDownloaderTest method startServer.

@BeforeClass
public static void startServer() throws IOException {
    socketConnection = new SocketConnection(new Container() {

        public void handle(Request req, Response resp) {
            try {
                if (req.getPath().getPath().contains("/redirect/")) {
                    resp.setCode(303);
                    resp.add("Location", "/");
                } else {
                    if (req.getPath().getPath().contains("/timeout/")) {
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            throw new IllegalStateException(e);
                        }
                    }
                    if (req.getPath().getPath().contains("/gzip/")) {
                        if (!"gzip".equals(req.getValue("Accept-Encoding"))) {
                            throw new IllegalStateException("Should accept gzip");
                        }
                        resp.set("Content-Encoding", "gzip");
                        GZIPOutputStream gzipOutputStream = new GZIPOutputStream(resp.getOutputStream());
                        gzipOutputStream.write("GZIP response".getBytes());
                        gzipOutputStream.close();
                    } else {
                        resp.getPrintStream().append("agent=" + req.getValues("User-Agent").get(0));
                    }
                }
            } catch (IOException e) {
                throw new IllegalStateException(e);
            } finally {
                try {
                    resp.close();
                } catch (IOException ignored) {
                }
            }
        }
    });
    SocketAddress address = socketConnection.connect(new InetSocketAddress("localhost", 0));
    baseUrl = String.format("http://%s:%d", ((InetSocketAddress) address).getAddress().getHostAddress(), ((InetSocketAddress) address).getPort());
}
Also used : Response(org.simpleframework.http.Response) Container(org.simpleframework.http.core.Container) SocketConnection(org.simpleframework.transport.connect.SocketConnection) GZIPOutputStream(java.util.zip.GZIPOutputStream) InetSocketAddress(java.net.InetSocketAddress) Request(org.simpleframework.http.Request) IOException(java.io.IOException) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) BeforeClass(org.junit.BeforeClass)

Example 2 with Container

use of org.simpleframework.http.core.Container in project gradle by gradle.

the class SimpleHttpFileServerFactory method start.

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);
        return new SimpleHttpFileServer(contentRoot, usedAddress.getPort(), new Stoppable() {

            public void stop() {
                try {
                    server.stop();
                } catch (IOException e) {
                    throw new UncheckedIOException(e);
                }
            }
        });
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
Also used : SimpleFileServerContainer(org.gradle.plugins.javascript.envjs.http.simple.internal.SimpleFileServerContainer) ContainerServer(org.simpleframework.http.core.ContainerServer) Container(org.simpleframework.http.core.Container) SimpleFileServerContainer(org.gradle.plugins.javascript.envjs.http.simple.internal.SimpleFileServerContainer) Server(org.simpleframework.transport.Server) HttpFileServer(org.gradle.plugins.javascript.envjs.http.HttpFileServer) ContainerServer(org.simpleframework.http.core.ContainerServer) SocketConnection(org.simpleframework.transport.connect.SocketConnection) InetSocketAddress(java.net.InetSocketAddress) Connection(org.simpleframework.transport.connect.Connection) SocketConnection(org.simpleframework.transport.connect.SocketConnection) Stoppable(org.gradle.internal.concurrent.Stoppable) UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(org.gradle.api.UncheckedIOException) FileContext(org.simpleframework.http.resource.FileContext)

Aggregations

IOException (java.io.IOException)2 InetSocketAddress (java.net.InetSocketAddress)2 Container (org.simpleframework.http.core.Container)2 SocketConnection (org.simpleframework.transport.connect.SocketConnection)2 SocketAddress (java.net.SocketAddress)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 UncheckedIOException (org.gradle.api.UncheckedIOException)1 Stoppable (org.gradle.internal.concurrent.Stoppable)1 HttpFileServer (org.gradle.plugins.javascript.envjs.http.HttpFileServer)1 SimpleFileServerContainer (org.gradle.plugins.javascript.envjs.http.simple.internal.SimpleFileServerContainer)1 BeforeClass (org.junit.BeforeClass)1 Request (org.simpleframework.http.Request)1 Response (org.simpleframework.http.Response)1 ContainerServer (org.simpleframework.http.core.ContainerServer)1 FileContext (org.simpleframework.http.resource.FileContext)1 Server (org.simpleframework.transport.Server)1 Connection (org.simpleframework.transport.connect.Connection)1