Search in sources :

Example 1 with SocketConnection

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

use of org.simpleframework.transport.connect.SocketConnection in project lobcder by skoulouzis.

the class SslSimpletonServer method initHttps.

/**
 * Setting up certificates
 *    EG C:\Program Files\Java\jdk1.6.0_10\bin>keytool -genkey -keystore certs -keyalg rsa -alias jamie -storepass serverkspw -keypass serverpw
 *    note that 'first name last name' should be machine name
 *
 * @param connection
 * @param port
 */
protected SocketConnection initHttps(int port) {
    SSLServerSocketFactory fac = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
    log.info("initHttps: port: " + port + " sslProtocol: " + sslProtocol + " keystoreAlgorithm:" + keystoreAlgorithm);
    try {
        KeyStore keystore = KeyStore.getInstance(keystoreType);
        keystore.load(new FileInputStream(keystoreFile), keystorePassword.toCharArray());
        log.info("listing aliases defined in keystore");
        Enumeration<String> aliases = keystore.aliases();
        while (aliases.hasMoreElements()) {
            String a = aliases.nextElement();
            log.info(" - alias: " + a);
            Certificate cert = keystore.getCertificate(a);
            log.info("   - cert type: " + cert.getType());
            log.info("   - algorithm: " + cert.getPublicKey().getAlgorithm());
            log.info("   - format: " + cert.getPublicKey().getFormat());
        }
        KeyManagerFactory kmf = KeyManagerFactory.getInstance(keystoreAlgorithm);
        kmf.init(keystore, keystorePassword.toCharArray());
        X509TrustManager trustManager = new AnonymousTrustManager();
        X509TrustManager[] trustManagers = new X509TrustManager[] { trustManager };
        // An SSLContext is an environment for implementing JSSE. It is used to create a ServerSocketFactory
        SSLContext sslc = SSLContext.getInstance(sslProtocol);
        sslc.init(kmf.getKeyManagers(), trustManagers, null);
        ContainerServer processor = new ContainerServer(this, 25);
        org.simpleframework.transport.Server secure = new SecureProcessor(processor, sslc);
        SocketConnection ssl = new SocketConnection(secure);
        InetSocketAddress address = new InetSocketAddress(port);
        ssl.connect(address, sslc);
        log.debug("server running on: " + address);
        return ssl;
    } catch (java.net.BindException ex) {
        throw new RuntimeException("Couldnt bind to port: " + port);
    } catch (KeyManagementException ex) {
        throw new RuntimeException(ex);
    } catch (UnrecoverableKeyException ex) {
        throw new RuntimeException(ex);
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    } catch (NoSuchAlgorithmException ex) {
        throw new RuntimeException(ex);
    } catch (CertificateException ex) {
        throw new RuntimeException(ex);
    } catch (KeyStoreException ex) {
        throw new RuntimeException(ex);
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyManagementException(java.security.KeyManagementException) ContainerServer(org.simpleframework.http.core.ContainerServer) UnrecoverableKeyException(java.security.UnrecoverableKeyException) SocketConnection(org.simpleframework.transport.connect.SocketConnection) SSLServerSocketFactory(javax.net.ssl.SSLServerSocketFactory) SSLContext(javax.net.ssl.SSLContext) IOException(java.io.IOException) KeyStoreException(java.security.KeyStoreException) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) KeyStoreException(java.security.KeyStoreException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyManagerFactory(javax.net.ssl.KeyManagerFactory) X509TrustManager(javax.net.ssl.X509TrustManager) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 3 with SocketConnection

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

Example 4 with SocketConnection

use of org.simpleframework.transport.connect.SocketConnection 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 5 with SocketConnection

use of org.simpleframework.transport.connect.SocketConnection in project lobcder by skoulouzis.

the class SimpletonServer method start.

public void start() {
    stopped = false;
    try {
        connection = new SocketConnection(this);
    } catch (Exception ex) {
        throw new RuntimeException("Couldnt create socket connection", ex);
    }
    initHttp(connection, httpPort);
    thMonitor = new Thread(new TaskMonitor());
    thMonitor.start();
}
Also used : SocketConnection(org.simpleframework.transport.connect.SocketConnection) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)5 SocketConnection (org.simpleframework.transport.connect.SocketConnection)5 InetSocketAddress (java.net.InetSocketAddress)4 SocketAddress (java.net.SocketAddress)2 Container (org.simpleframework.http.core.Container)2 ContainerServer (org.simpleframework.http.core.ContainerServer)2 Connection (org.simpleframework.transport.connect.Connection)2 FileInputStream (java.io.FileInputStream)1 KeyManagementException (java.security.KeyManagementException)1 KeyStore (java.security.KeyStore)1 KeyStoreException (java.security.KeyStoreException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 UnrecoverableKeyException (java.security.UnrecoverableKeyException)1 Certificate (java.security.cert.Certificate)1 CertificateException (java.security.cert.CertificateException)1 X509Certificate (java.security.cert.X509Certificate)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 KeyManagerFactory (javax.net.ssl.KeyManagerFactory)1 SSLContext (javax.net.ssl.SSLContext)1 SSLServerSocketFactory (javax.net.ssl.SSLServerSocketFactory)1