use of com.sun.net.httpserver.HttpContext in project jdk8u_jdk by JetBrains.
the class HeadTest method server.
static void server() throws Exception {
InetSocketAddress inetAddress = new InetSocketAddress(0);
HttpServer server = HttpServer.create(inetAddress, 5);
try {
server.setExecutor(Executors.newFixedThreadPool(5));
HttpContext chunkedContext = server.createContext("/chunked");
chunkedContext.setHandler(new HttpHandler() {
@Override
public void handle(HttpExchange msg) {
try {
try {
if (msg.getRequestMethod().equals("HEAD")) {
msg.getRequestBody().close();
msg.getResponseHeaders().add("Transfer-encoding", "chunked");
msg.sendResponseHeaders(200, -1);
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
} finally {
msg.close();
}
}
});
HttpContext clContext = server.createContext("/content");
clContext.setHandler(new HttpHandler() {
@Override
public void handle(HttpExchange msg) {
try {
try {
if (msg.getRequestMethod().equals("HEAD")) {
msg.getRequestBody().close();
msg.getResponseHeaders().add("Content-length", "1024");
msg.sendResponseHeaders(200, -1);
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
} finally {
msg.close();
}
}
});
server.start();
String urlStr = "http://localhost:" + server.getAddress().getPort() + "/";
System.out.println("Server is at " + urlStr);
// Run the chunked client
for (int i = 0; i < 10; i++) {
runClient(urlStr + "chunked/");
}
// Run the content length client
for (int i = 0; i < 10; i++) {
runClient(urlStr + "content/");
}
} finally {
// Stop the server
((ExecutorService) server.getExecutor()).shutdown();
server.stop(0);
}
}
use of com.sun.net.httpserver.HttpContext in project jdk8u_jdk by JetBrains.
the class Deadlock method main.
public static void main(String[] args) throws Exception {
Handler handler = new Handler();
InetSocketAddress addr = new InetSocketAddress(0);
HttpServer server = HttpServer.create(addr, 0);
HttpContext ctx = server.createContext("/test", handler);
BasicAuthenticator a = new BasicAuthenticator("foobar@test.realm") {
@Override
public boolean checkCredentials(String username, String pw) {
return "fred".equals(username) && pw.charAt(0) == 'x';
}
};
ctx.setAuthenticator(a);
ExecutorService executor = Executors.newCachedThreadPool();
server.setExecutor(executor);
server.start();
java.net.Authenticator.setDefault(new MyAuthenticator());
System.out.print("Deadlock: ");
for (int i = 0; i < 2; i++) {
Runner t = new Runner(server, i);
t.start();
t.join();
}
server.stop(2);
executor.shutdown();
if (error) {
throw new RuntimeException("test failed error");
}
if (count != 2) {
throw new RuntimeException("test failed count = " + count);
}
System.out.println("OK");
}
use of com.sun.net.httpserver.HttpContext in project pentaho-kettle by pentaho.
the class GetHandler method startHTTPServer.
private static void startHTTPServer() throws IOException {
httpServer = HttpServer.create(new InetSocketAddress(JobEntryHTTP_PDI208_Test.HTTP_HOST, JobEntryHTTP_PDI208_Test.HTTP_PORT), 10);
HttpContext uploadFileContext = httpServer.createContext("/uploadFile", new JobEntryHTTP_PDI_18044_Test_Handler());
// HttpContext getContext = httpServer.createContext( "/get", new GetHandler() );
uploadFileContext.setAuthenticator(new BasicAuthenticator("uploadFile") {
@Override
public boolean checkCredentials(String username, String password) {
return username.equals("admin") && password.equals("password");
}
});
httpServer.start();
}
Aggregations