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());
}
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);
}
}
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);
}
}
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);
}
}
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();
}
Aggregations