本文整理匯總了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;
}