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