本文整理匯總了Java中com.sun.net.httpserver.HttpContext類的典型用法代碼示例。如果您正苦於以下問題:Java HttpContext類的具體用法?Java HttpContext怎麽用?Java HttpContext使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
HttpContext類屬於com.sun.net.httpserver包,在下文中一共展示了HttpContext類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: start
import com.sun.net.httpserver.HttpContext; //導入依賴的package包/類
public void start() {
try {
server = HttpServer.create(new InetSocketAddress(8000), 0);
} catch (Exception e) {
Assert.fail("Exception thrown: " + e.getMessage());
}
server.createContext("/unprotected", new TestHttpHandler());
HttpContext protectedContext = server.createContext("/protected", new TestHttpHandler());
protectedContext.setAuthenticator(new BasicAuthenticator("get") {
@Override
public boolean checkCredentials(String user, String pwd) {
return user.equals("admin") && pwd.equals("admin");
}
});
server.setExecutor(null);
server.start();
}
示例2: runSample
import com.sun.net.httpserver.HttpContext; //導入依賴的package包/類
/**
* Main function which runs the actual sample.
* @param authFile the auth file backing the web server
* @return true if sample runs successfully
* @throws Exception exceptions running the server
*/
public static boolean runSample(File authFile) throws Exception {
final String redirectUrl = "http://localhost:8000";
final ExecutorService executor = Executors.newCachedThreadPool();
try {
DelegatedTokenCredentials credentials = DelegatedTokenCredentials.fromFile(authFile, redirectUrl);
HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
HttpContext context = server.createContext("/", new MyHandler());
context.getAttributes().put("credentials", credentials);
server.setExecutor(Executors.newCachedThreadPool()); // creates a default executor
server.start();
// Use a browser to login within a minute
Thread.sleep(60000);
return true;
} finally {
executor.shutdown();
}
}
示例3: afterPropertiesSet
import com.sun.net.httpserver.HttpContext; //導入依賴的package包/類
@Override
public void afterPropertiesSet() throws IOException {
InetSocketAddress address = (this.hostname != null ?
new InetSocketAddress(this.hostname, this.port) : new InetSocketAddress(this.port));
this.server = HttpServer.create(address, this.backlog);
if (this.executor != null) {
this.server.setExecutor(this.executor);
}
if (this.contexts != null) {
for (String key : this.contexts.keySet()) {
HttpContext httpContext = this.server.createContext(key, this.contexts.get(key));
if (this.filters != null) {
httpContext.getFilters().addAll(this.filters);
}
if (this.authenticator != null) {
httpContext.setAuthenticator(this.authenticator);
}
}
}
if (this.logger.isInfoEnabled()) {
this.logger.info("Starting HttpServer at address " + address);
}
this.server.start();
}
示例4: httpd
import com.sun.net.httpserver.HttpContext; //導入依賴的package包/類
/**
* Creates and starts an HTTP or proxy server that requires
* Negotiate authentication.
* @param scheme "Negotiate" or "Kerberos"
* @param principal the krb5 service principal the server runs with
* @return the server
*/
public static HttpServer httpd(String scheme, boolean proxy,
String principal, String ktab) throws Exception {
MyHttpHandler h = new MyHttpHandler();
HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
HttpContext hc = server.createContext("/", h);
hc.setAuthenticator(new MyServerAuthenticator(
proxy, scheme, principal, ktab));
server.start();
return server;
}
示例5: createHttpsServer
import com.sun.net.httpserver.HttpContext; //導入依賴的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;
}
示例6: createServer
import com.sun.net.httpserver.HttpContext; //導入依賴的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;
}
示例7: createProxy
import com.sun.net.httpserver.HttpContext; //導入依賴的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;
}
示例8: start
import com.sun.net.httpserver.HttpContext; //導入依賴的package包/類
public void start() {
MyHttpHandler handler = new MyHttpHandler(_docroot);
InetSocketAddress addr = new InetSocketAddress(_port);
try {
_httpserver = HttpServer.create(addr, 0);
} catch (IOException ex) {
throw new RuntimeException("cannot create httpserver", ex);
}
//TestHandler is mapped to /test
HttpContext ctx = _httpserver.createContext(_context, handler);
_executor = Executors.newCachedThreadPool();
_httpserver.setExecutor(_executor);
_httpserver.start();
_address = "http://localhost:" + _httpserver.getAddress().getPort();
}
示例9: main
import com.sun.net.httpserver.HttpContext; //導入依賴的package包/類
public static void main(String[] args) throws IOException {
HttpServer server = HttpServer.create(new InetSocketAddress(HTTP_SERVER_PORT), 0);
HttpContext secureContext = server.createContext(DEMO_REST_BASIC_AUTH, new RestDemoHandler());
secureContext.setAuthenticator(new BasicAuthenticator("demo-auth") {
@Override
public boolean checkCredentials(String user, String pwd) {
return user.equals(USERNAME) && pwd.equals(PASSWORD);
}
});
server.createContext(DEMO_REST_NO_AUTH, new RestDemoHandler());
server.setExecutor(null);
System.out.println("[*] Waiting for messages.");
server.start();
}
示例10: JettyHttpExchange
import com.sun.net.httpserver.HttpContext; //導入依賴的package包/類
public JettyHttpExchange(HttpContext context, HttpServletRequest req,
HttpServletResponse resp)
{
this._context = context;
this._req = req;
this._resp = resp;
try
{
this._is = req.getInputStream();
this._os = resp.getOutputStream();
}
catch (IOException ex)
{
throw new RuntimeException(ex);
}
}
示例11: restGatewayReferenceTimeout
import com.sun.net.httpserver.HttpContext; //導入依賴的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.HttpContext; //導入依賴的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: startOrUpdate
import com.sun.net.httpserver.HttpContext; //導入依賴的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();
}
}
示例14: RpcHandler
import com.sun.net.httpserver.HttpContext; //導入依賴的package包/類
/**
* Create the JSON-RPC request handler
*
* @param rpcPort
* RPC port
* @param rpcAllowIp
* List of allowed host addresses
* @param rpcUser
* RPC user
* @param rpcPassword
* RPC password
*/
public RpcHandler(int rpcPort, List<InetAddress> rpcAllowIp, String rpcUser, String rpcPassword) {
this.rpcPort = rpcPort;
this.rpcAllowIp = rpcAllowIp;
this.rpcUser = rpcUser;
this.rpcPassword = rpcPassword;
//
// Create the HTTP server using a single execution thread
//
try {
server = HttpServer.create(new InetSocketAddress(rpcPort), 10);
HttpContext context = server.createContext("/", this);
context.setAuthenticator(new RpcAuthenticator(LSystem.applicationName));
server.setExecutor(null);
server.start();
BTCLoader.info(String.format("RPC handler started on port %d", rpcPort));
} catch (IOException exc) {
BTCLoader.error("Unable to set up HTTP server", exc);
}
}
示例15: afterPropertiesSet
import com.sun.net.httpserver.HttpContext; //導入依賴的package包/類
public void afterPropertiesSet() throws IOException {
InetSocketAddress address = (this.hostname != null ?
new InetSocketAddress(this.hostname, this.port) : new InetSocketAddress(this.port));
this.server = HttpServer.create(address, this.backlog);
if (this.executor != null) {
this.server.setExecutor(this.executor);
}
if (this.contexts != null) {
for (String key : this.contexts.keySet()) {
HttpContext httpContext = this.server.createContext(key, this.contexts.get(key));
if (this.filters != null) {
httpContext.getFilters().addAll(this.filters);
}
if (this.authenticator != null) {
httpContext.setAuthenticator(this.authenticator);
}
}
}
if (this.logger.isInfoEnabled()) {
this.logger.info("Starting HttpServer at address " + address);
}
this.server.start();
}