Search in sources :

Example 1 with Connection

use of org.simpleframework.transport.connect.Connection 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)

Example 2 with Connection

use of org.simpleframework.transport.connect.Connection in project jersey by jersey.

the class SimpleContainerFactory method _create.

private static SimpleServer _create(final URI address, final SSLContext context, final SimpleContainer container, final UnsafeValue<SocketProcessor, IOException> serverProvider) throws ProcessingException {
    if (address == null) {
        throw new IllegalArgumentException(LocalizationMessages.URI_CANNOT_BE_NULL());
    }
    final String scheme = address.getScheme();
    int defaultPort = org.glassfish.jersey.server.spi.Container.DEFAULT_HTTP_PORT;
    if (context == null) {
        if (!scheme.equalsIgnoreCase("http")) {
            throw new IllegalArgumentException(LocalizationMessages.WRONG_SCHEME_WHEN_USING_HTTP());
        }
    } else {
        if (!scheme.equalsIgnoreCase("https")) {
            throw new IllegalArgumentException(LocalizationMessages.WRONG_SCHEME_WHEN_USING_HTTPS());
        }
        defaultPort = org.glassfish.jersey.server.spi.Container.DEFAULT_HTTPS_PORT;
    }
    int port = address.getPort();
    if (port == -1) {
        port = defaultPort;
    }
    final InetSocketAddress listen = new InetSocketAddress(port);
    final Connection connection;
    try {
        final SimpleTraceAnalyzer analyzer = new SimpleTraceAnalyzer();
        final SocketProcessor server = serverProvider.get();
        connection = new SocketConnection(server, analyzer);
        final SocketAddress socketAddr = connection.connect(listen, context);
        container.onServerStart();
        return new SimpleServer() {

            @Override
            public void close() throws IOException {
                container.onServerStop();
                analyzer.stop();
                connection.close();
            }

            @Override
            public int getPort() {
                return ((InetSocketAddress) socketAddr).getPort();
            }

            @Override
            public boolean isDebug() {
                return analyzer.isActive();
            }

            @Override
            public void setDebug(boolean enable) {
                if (enable) {
                    analyzer.start();
                } else {
                    analyzer.stop();
                }
            }
        };
    } catch (final IOException ex) {
        throw new ProcessingException(LocalizationMessages.ERROR_WHEN_CREATING_SERVER(), ex);
    }
}
Also used : SocketProcessor(org.simpleframework.transport.SocketProcessor) ContainerSocketProcessor(org.simpleframework.http.core.ContainerSocketProcessor) SocketConnection(org.simpleframework.transport.connect.SocketConnection) InetSocketAddress(java.net.InetSocketAddress) Connection(org.simpleframework.transport.connect.Connection) SocketConnection(org.simpleframework.transport.connect.SocketConnection) IOException(java.io.IOException) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) ProcessingException(javax.ws.rs.ProcessingException)

Aggregations

IOException (java.io.IOException)2 InetSocketAddress (java.net.InetSocketAddress)2 Connection (org.simpleframework.transport.connect.Connection)2 SocketConnection (org.simpleframework.transport.connect.SocketConnection)2 SocketAddress (java.net.SocketAddress)1 ProcessingException (javax.ws.rs.ProcessingException)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 Container (org.simpleframework.http.core.Container)1 ContainerServer (org.simpleframework.http.core.ContainerServer)1 ContainerSocketProcessor (org.simpleframework.http.core.ContainerSocketProcessor)1 FileContext (org.simpleframework.http.resource.FileContext)1 Server (org.simpleframework.transport.Server)1 SocketProcessor (org.simpleframework.transport.SocketProcessor)1