Search in sources :

Example 1 with SocketProcessor

use of org.simpleframework.transport.SocketProcessor 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)1 InetSocketAddress (java.net.InetSocketAddress)1 SocketAddress (java.net.SocketAddress)1 ProcessingException (javax.ws.rs.ProcessingException)1 ContainerSocketProcessor (org.simpleframework.http.core.ContainerSocketProcessor)1 SocketProcessor (org.simpleframework.transport.SocketProcessor)1 Connection (org.simpleframework.transport.connect.Connection)1 SocketConnection (org.simpleframework.transport.connect.SocketConnection)1