本文整理汇总了Java中com.sun.net.httpserver.HttpHandler类的典型用法代码示例。如果您正苦于以下问题:Java HttpHandler类的具体用法?Java HttpHandler怎么用?Java HttpHandler使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
HttpHandler类属于com.sun.net.httpserver包,在下文中一共展示了HttpHandler类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getServlet
import com.sun.net.httpserver.HttpHandler; //导入依赖的package包/类
@Override
public HttpHandler getServlet() {
return exchange -> {
final ByteArrayOutputStream response = new ByteArrayOutputStream(1 << 20);
final OutputStreamWriter osw = new OutputStreamWriter(response);
TextFormat.write004(osw, registry.metricFamilySamples());
osw.flush();
osw.close();
response.flush();
response.close();
exchange.getResponseHeaders().set("Content-Type", TextFormat.CONTENT_TYPE_004);
exchange.getResponseHeaders().set("Content-Length", String.valueOf(response.size()));
exchange.sendResponseHeaders(200, response.size());
response.writeTo(exchange.getResponseBody());
exchange.close();
};
}
示例2: createHttpsServer
import com.sun.net.httpserver.HttpHandler; //导入依赖的package包/类
static HttpServer createHttpsServer() throws IOException, NoSuchAlgorithmException {
HttpsServer server = com.sun.net.httpserver.HttpsServer.create();
HttpContext context = server.createContext(PATH);
context.setHandler(new HttpHandler() {
@Override
public void handle(HttpExchange he) throws IOException {
he.getResponseHeaders().add("encoding", "UTF-8");
he.sendResponseHeaders(200, RESPONSE.length());
he.getResponseBody().write(RESPONSE.getBytes(StandardCharsets.UTF_8));
he.close();
}
});
server.setHttpsConfigurator(new Configurator(SSLContext.getDefault()));
server.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0);
return server;
}
示例3: createServer
import com.sun.net.httpserver.HttpHandler; //导入依赖的package包/类
public static HTTPTestServer createServer(HttpProtocolType protocol,
HttpAuthType authType,
HttpTestAuthenticator auth,
HttpSchemeType schemeType,
HttpHandler delegate,
String path)
throws IOException {
Objects.requireNonNull(authType);
Objects.requireNonNull(auth);
HttpServer impl = createHttpServer(protocol);
final HTTPTestServer server = new HTTPTestServer(impl, null, delegate);
final HttpHandler hh = server.createHandler(schemeType, auth, authType);
HttpContext ctxt = impl.createContext(path, hh);
server.configureAuthentication(ctxt, schemeType, auth, authType);
impl.start();
return server;
}
示例4: createProxy
import com.sun.net.httpserver.HttpHandler; //导入依赖的package包/类
public static HTTPTestServer createProxy(HttpProtocolType protocol,
HttpAuthType authType,
HttpTestAuthenticator auth,
HttpSchemeType schemeType,
HttpHandler delegate,
String path)
throws IOException {
Objects.requireNonNull(authType);
Objects.requireNonNull(auth);
HttpServer impl = createHttpServer(protocol);
final HTTPTestServer server = protocol == HttpProtocolType.HTTPS
? new HttpsProxyTunnel(impl, null, delegate)
: new HTTPTestServer(impl, null, delegate);
final HttpHandler hh = server.createHandler(schemeType, auth, authType);
HttpContext ctxt = impl.createContext(path, hh);
server.configureAuthentication(ctxt, schemeType, auth, authType);
impl.start();
return server;
}
示例5: run
import com.sun.net.httpserver.HttpHandler; //导入依赖的package包/类
/**
* run
*/
public void run() {
HttpServer server = null;
try {
server = HttpServer.create(new InetSocketAddress(16358), 0);
} catch (IOException e) {
Basic.caught(e);
}
server.createContext("/start", new HttpHandler() {
public void handle(HttpExchange httpExchange) throws IOException {
URI uri = httpExchange.getRequestURI();
System.err.println("URI: " + uri);
String command = uri.toString().substring(uri.toString().indexOf('?') + 1);
System.err.println("Command: " + command);
String response = "ok";
httpExchange.sendResponseHeaders(200, 2);
OutputStream outputStream = httpExchange.getResponseBody();
outputStream.write(response.getBytes());
outputStream.close();
}
});
server.setExecutor(null);
server.start();
}
示例6: run
import com.sun.net.httpserver.HttpHandler; //导入依赖的package包/类
public void run() {
try {
HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
server.createContext("/", new HttpHandler() {
@Override
public void handle(HttpExchange t) throws IOException {
System.out.println(t.getRequestURI());
String name = LockServer.validateRequest(LockServer.this.gson, t);
if (name == null) {
return;
}
System.out.println("Lock name is: " + name);
LockServer.this.handleLockServe(name, t);
}
});
server.start();
} catch (IOException ex) {
ex.printStackTrace();
}
}
示例7: MockArangoServer
import com.sun.net.httpserver.HttpHandler; //导入依赖的package包/类
public MockArangoServer(int port) throws IOException {
server = HttpServer.create(new InetSocketAddress(port), 0);
server.createContext("/", new HttpHandler() {
@Override
public void handle(HttpExchange exchange) throws IOException {
final String req = exchange.getRequestMethod() + " " + exchange.getRequestURI();
final ArangoResponse response = responses.get(req);
if (response != null) {
exchange.sendResponseHeaders(response.status,
response.body != null ? response.body.length() : 0);
if (response.body != null) {
final OutputStream out = exchange.getResponseBody();
out.write(response.body.getBytes());
out.close();
}
}
}
});
server.setExecutor(null);
}
示例8: start
import com.sun.net.httpserver.HttpHandler; //导入依赖的package包/类
@Before
public void start() throws IOException {
final String path = "/1234.xml";
// create the HttpServer
InetSocketAddress address = new InetSocketAddress(serverAddress, 0);
httpServer = com.sun.net.httpserver.HttpServer.create(address, 0);
// create and register our handler
HttpHandler handler = new ConfigurableHttpHandler(serverResponse);
httpServer.createContext(path, handler);
// start the server
httpServer.start();
port = httpServer.getAddress().getPort();
LOG.debug("started http server {}:{} with handler {}", serverAddress, port, handler);
httpAddress = String.format("http://%s:%d/1234.xml", serverAddress, port);
LOG.debug("test url is: {}", httpAddress);
}
示例9: createHttpExchange
import com.sun.net.httpserver.HttpHandler; //导入依赖的package包/类
private static HttpHandler createHttpExchange(String pathToServe) {
return httpExchange -> {
String requestUri = httpExchange.getRequestURI().toString();
String name = pathToServe + requestUri;
name = name.replaceFirst("/+", "/");
URL resource = HttpServerFromResourceInner.class.getResource(name);
try (OutputStream os = httpExchange.getResponseBody()) {
if (resource == null) {
log.warn("could not find " + requestUri);
httpExchange.sendResponseHeaders(404, 0);
} else {
byte[] bytes = Files.readAllBytes(Paths.get(resource.getFile()));
httpExchange.sendResponseHeaders(200, bytes.length);
os.write(bytes);
}
}
};
}
示例10: startHTTPServer
import com.sun.net.httpserver.HttpHandler; //导入依赖的package包/类
private void startHTTPServer() throws IOException {
Assert.assertNull(httpServer);
InetSocketAddress address = new InetSocketAddress("127.0.0.1", 8080);
httpServer = HttpServer.create(address, 100);
httpServer.start();
httpServer.createContext("/", new HttpHandler() {
@Override
public void handle(HttpExchange t) throws IOException {
String response = "<html><body><b>This is a unit test</b></body></html>";
t.sendResponseHeaders(200, response.length());
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
}
});
}
示例11: restGatewayReferenceTimeout
import com.sun.net.httpserver.HttpHandler; //导入依赖的package包/类
@Test
public void restGatewayReferenceTimeout() throws Exception {
HttpServer httpServer = HttpServer.create(new InetSocketAddress(8090), 10);
httpServer.setExecutor(null); // creates a default executor
httpServer.start();
HttpContext httpContext = httpServer.createContext("/forever", new HttpHandler() {
@Override
public void handle(HttpExchange exchange) {
try {
Thread.sleep(10000);
} catch (InterruptedException ie) {
//Ignore
}}});
try {
Message responseMsg = _consumerService2.operation("addGreeter").sendInOut("magesh");
} catch (Exception e) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
e.printStackTrace(new PrintStream(baos));
Assert.assertTrue(baos.toString().contains("SocketTimeoutException: Read timed out"));
}
httpServer.stop(0);
}
示例12: soapGatewayReferenceTimeout
import com.sun.net.httpserver.HttpHandler; //导入依赖的package包/类
@Test
public void soapGatewayReferenceTimeout() throws Exception {
Element input = SOAPUtil.parseAsDom("<test:sayHello xmlns:test=\"urn:switchyard-component-soap:test-ws:1.0\">"
+ " <arg0>Hello</arg0>"
+ "</test:sayHello>").getDocumentElement();
HttpServer httpServer = HttpServer.create(new InetSocketAddress(8090), 10);
httpServer.setExecutor(null); // creates a default executor
httpServer.start();
HttpContext httpContext = httpServer.createContext("/forever", new HttpHandler() {
public void handle(HttpExchange exchange) {
try {
Thread.sleep(10000);
} catch (InterruptedException ie) {
//Ignore
}}});
try {
Message responseMsg = _consumerService3.operation("sayHello").sendInOut(input);
} catch (Exception e) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
e.printStackTrace(new PrintStream(baos));
Assert.assertTrue(baos.toString().contains("SocketTimeoutException: Read timed out"));
}
httpServer.stop(0);
}
示例13: PiEzo
import com.sun.net.httpserver.HttpHandler; //导入依赖的package包/类
public PiEzo() throws IOException
{
InetSocketAddress anyhost = new InetSocketAddress(2111);
server = HttpServer.create(anyhost, 0);
HttpHandler userInterfaceHttpHander = new ClasspathResourceHttpHandler();
HttpHandler songsHttpHandler = new SongListHttpHander();
HttpHandler playSongHttpHandler = new PlaySongHttpHandler();
HttpHandler quitHttpHandler = new EndOfRunHttpHandler(server);
HttpHandler rtttlHttpHandler = new RtttlHttpHandler();
server.createContext("/", userInterfaceHttpHander);
server.createContext("/play/", playSongHttpHandler);
server.createContext("/quit", quitHttpHandler);
server.createContext("/rtttl", rtttlHttpHandler);
server.createContext("/songs", songsHttpHandler);
}
示例14: startOrUpdate
import com.sun.net.httpserver.HttpHandler; //导入依赖的package包/类
public static synchronized void startOrUpdate(InetSocketAddress addr, String filename,
HttpHandler handler)
throws IOException {
if (addr == null) {
throw new IllegalArgumentException("InetSocketAddress was null for HttpServer");
}
if (filename == null) {
throw new IllegalArgumentException("filename was null for HttpServer");
}
boolean toStart = false;
if (instance == null) {
instance = new JwHttpServer();
JwHttpServer.addr = addr;
server = HttpServer.create(addr,
/*system default backlog for TCP connections*/ 0);
// server.setExecutor(Executors.newCachedThreadPool());
toStart = true;
}
HttpContext context = server.createContext(filename, handler);
context.getFilters().add(new ParameterFilter());
if (toStart) {
server.start();
}
}
示例15: startServer
import com.sun.net.httpserver.HttpHandler; //导入依赖的package包/类
private HttpServer startServer(final String... response) throws IOException {
HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
server.createContext("/").setHandler(
new HttpHandler() {
int responseIndex = 0;
public void handle(HttpExchange exchange) throws IOException {
byte[] responseBytes;
if (responseIndex < response.length) {
responseBytes = response[responseIndex].getBytes(UTF_8);
responseIndex++;
} else {
responseBytes = new byte[0];
}
exchange.sendResponseHeaders(200, responseBytes.length);
OutputStream out = exchange.getResponseBody();
IOHelper.copyStream(new ByteArrayInputStream(responseBytes), out);
exchange.close();
}
});
server.start();
return server;
}