本文整理汇总了Java中io.reactivex.netty.protocol.http.server.HttpServer类的典型用法代码示例。如果您正苦于以下问题:Java HttpServer类的具体用法?Java HttpServer怎么用?Java HttpServer使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
HttpServer类属于io.reactivex.netty.protocol.http.server包,在下文中一共展示了HttpServer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createServer
import io.reactivex.netty.protocol.http.server.HttpServer; //导入依赖的package包/类
public HttpServer<ByteBuf, ByteBuf> createServer() {
try {
final String ideBasePath = new File(".").getCanonicalPath();
final File rootDirectory;
if (ideBasePath.contains(rootDir)) {
// for eclipse
rootDirectory = new File(".");
} else {
// for intellij
rootDirectory = new File(rootDir);
}
return RxNetty.createHttpServer(port,
RequestHandlerWithErrorMapper.from(
new LocalDirectoryRequestHandler(rootDirectory),
new FileErrorResponseMapper()));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
示例2: createServer
import io.reactivex.netty.protocol.http.server.HttpServer; //导入依赖的package包/类
private HttpServer<ByteBuf, ByteBuf> createServer(final int listenPort) {
HttpServer<ByteBuf, ByteBuf> server = new HttpServerBuilder<ByteBuf, ByteBuf>(listenPort, (
request, response) -> request
.getContent()
.compose(new RequestTransformer())
.filter(bidreq -> bidreq != null)
.compose(new ExternalRequestTransformer())
.compose(new ExternalRequestExecutor(client))
.flatMap(
resp -> {
response.setStatus(resp == null ? HttpResponseStatus.NO_CONTENT
: HttpResponseStatus.OK);
response.writeString(resp == null ? "" : resp.toString());
return response.close();
})).build();
LOG.info("Gateway started...");
return server;
}
示例3: init
import io.reactivex.netty.protocol.http.server.HttpServer; //导入依赖的package包/类
@Override
public void init() {
server = HttpServer.newServer()
.enableWireLogging(LogLevel.DEBUG)
.start((req, resp) -> {
if (req.getHeader(Constants.HAWKULAR_APM_TRACEID) == null) {
return resp.setStatus(HttpResponseStatus.BAD_REQUEST);
}
if (req.getHttpMethod() == HttpMethod.POST
|| req.getHttpMethod() == HttpMethod.PUT) {
req.getContent().subscribe(bb -> System.out.println("DATA = " + bb.toString()));
}
resp.setStatus(HttpResponseStatus.OK);
return resp;
}
);
super.init();
}
示例4: init
import io.reactivex.netty.protocol.http.server.HttpServer; //导入依赖的package包/类
@Override
public void init() {
server = HttpServer.newServer()
.enableWireLogging(LogLevel.DEBUG)
.start((req, resp) -> {
if (req.getHeader(Constants.HAWKULAR_APM_TRACEID) == null) {
return resp.setStatus(HttpResponseStatus.BAD_REQUEST);
}
if (req.getHttpMethod() == HttpMethod.POST
|| req.getHttpMethod() == HttpMethod.PUT) {
req.getContent().subscribe(bb -> System.out.println("DATA = " + bb.toString()));
}
return resp.writeString(Observable.just(HELLO_WORLD));
});
super.init();
}
示例5: testStreamDoesNotRunWhenSubscribeFails_mesos5xxResponse
import io.reactivex.netty.protocol.http.server.HttpServer; //导入依赖的package包/类
@Test
public void testStreamDoesNotRunWhenSubscribeFails_mesos5xxResponse() throws Throwable {
final RequestHandler<ByteBuf, ByteBuf> handler = (request, response) -> {
response.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
return response.close();
};
final HttpServer<ByteBuf, ByteBuf> server = RxNetty.createHttpServer(0, handler);
server.start();
final URI uri = URI.create(String.format("http://localhost:%d/api/v1/scheduler", server.getServerPort()));
final MesosClient<String, String> client = createClient(uri);
try {
client.openStream().await();
fail("Expect an exception to be propagated up because subscribe will 500");
} catch (Mesos5xxException e) {
// expected
final MesosClientErrorContext ctx = e.getContext();
assertThat(ctx.getStatusCode()).isEqualTo(500);
} finally {
server.shutdown();
}
}
示例6: testStreamDoesNotRunWhenSubscribeFails_mismatchContentType
import io.reactivex.netty.protocol.http.server.HttpServer; //导入依赖的package包/类
@Test
public void testStreamDoesNotRunWhenSubscribeFails_mismatchContentType() throws Throwable {
final RequestHandler<ByteBuf, ByteBuf> handler = (request, response) -> {
response.setStatus(HttpResponseStatus.OK);
response.getHeaders().setHeader("Content-Type", "application/json");
return response.close();
};
final HttpServer<ByteBuf, ByteBuf> server = RxNetty.createHttpServer(0, handler);
server.start();
final URI uri = URI.create(String.format("http://localhost:%d/api/v1/scheduler", server.getServerPort()));
final MesosClient<String, String> client = createClient(uri);
try {
client.openStream().await();
fail("Expect an exception to be propagated up because of content type mismatch");
} catch (MesosException e) {
// expected
final MesosClientErrorContext ctx = e.getContext();
assertThat(ctx.getStatusCode()).isEqualTo(200);
assertThat(ctx.getMessage()).isEqualTo("Response had Content-Type \"application/json\" expected \"text/plain;charset=utf-8\"");
} finally {
server.shutdown();
}
}
示例7: createServer
import io.reactivex.netty.protocol.http.server.HttpServer; //导入依赖的package包/类
public HttpServer<ByteBuf, ByteBuf> createServer() {
HttpServer<ByteBuf, ByteBuf> server = RxNetty.newHttpServerBuilder(port, new RequestHandler<ByteBuf, ByteBuf>() {
@Override
public Observable<Void> handle(HttpServerRequest<ByteBuf> request, final HttpServerResponse<ByteBuf> response) {
if (request.getPath().contains("/v2/apps")) {
if (request.getHttpMethod().equals(HttpMethod.POST)) {
return handleTest(request, response);
}
}
response.setStatus(HttpResponseStatus.NOT_FOUND);
return response.close();
}
}).pipelineConfigurator(PipelineConfigurators.<ByteBuf, ByteBuf>httpServerConfigurator()).enableWireLogging(LogLevel.ERROR).build();
System.out.println("RxTetstServer server started...");
return server;
}
示例8: providesKaryonTransport
import io.reactivex.netty.protocol.http.server.HttpServer; //导入依赖的package包/类
@Provides
public HttpServer providesKaryonTransport() {
SimpleUriRouter simpleUriRouter = new SimpleUriRouter();
simpleUriRouter.addUri("/foo", new RequestHandler() {
@Override
public Observable<Void> handle(HttpServerRequest request, HttpServerResponse response) {
response.writeAndFlush("Hello World", StringTransformer.DEFAULT_INSTANCE);
response.setStatus(HttpResponseStatus.OK);
return Observable.empty();
}
@Override
public Observable<Void> handle(Object input, Object output) {
return Observable.empty();
}
});
return KaryonTransport.newHttpServer(8888, simpleUriRouter);
}
示例9: main
import io.reactivex.netty.protocol.http.server.HttpServer; //导入依赖的package包/类
public static void main(String[] args) {
/**
* Start our HTTP server.
*/
HttpServer<ByteBuf, ByteBuf> server = startServer(8088);
/**
* Submit the request.
*/
createRequest("localhost", server.getServerPort())
/* Block till you get the response. In a real world application, one should not be blocked but chained
* into a response to the caller. */
.toBlocking()
/**
* Print each content of the response.
*/
.forEach(System.out::println);
}
示例10: startServer
import io.reactivex.netty.protocol.http.server.HttpServer; //导入依赖的package包/类
public static HttpServer<ByteBuf, ByteBuf> startServer(int port) {
/**
* Creates an HTTP server which returns "Hello World!" responses.
*/
return RxNetty.createHttpServer(port,
/*
* HTTP Request handler for RxNetty where you control what you write as the
* response for each and every request the server receives.
*/
(request, response) -> {
/**
* In a real server, you would be writing different responses based on the
* URI of the request.
* This example just returns a "Hello World!!" string unconditionally.
*/
return response.writeStringAndFlush("Hello World!!");
})
.start();
}
示例11: createServer
import io.reactivex.netty.protocol.http.server.HttpServer; //导入依赖的package包/类
public HttpServer<ByteBuf, ByteBuf> createServer() {
HttpServer<ByteBuf, ByteBuf> server = RxNetty.newHttpServerBuilder(port, new RequestHandler<ByteBuf, ByteBuf>() {
@Override
public Observable<Void> handle(HttpServerRequest<ByteBuf> request, final HttpServerResponse<ByteBuf> response) {
if (request.getPath().contains("/users")) {
if (request.getHttpMethod().equals(HttpMethod.GET)) {
return handleRecommendationsByUserId(request, response);
} else {
return handleUpdateRecommendationsForUser(request, response);
}
}
if (request.getPath().contains("/recommendations")) {
return handleRecommendationsBy(request, response);
}
if (request.getPath().contains("/movies")) {
return handleRegisterMovie(request, response);
}
response.setStatus(HttpResponseStatus.NOT_FOUND);
return response.close();
}
}).pipelineConfigurator(PipelineConfigurators.<ByteBuf, ByteBuf>httpServerConfigurator()).enableWireLogging(LogLevel.ERROR).build();
System.out.println("RxMovie server started...");
return server;
}
示例12: createServer
import io.reactivex.netty.protocol.http.server.HttpServer; //导入依赖的package包/类
public HttpServer<ByteBuf, ByteBuf> createServer() {
return RxNetty.createHttpServer(port, (request, response) -> {
HttpRequest httpRequest = new HttpRequest(request.getQueryParameters());
String content = getResponseContent(httpRequest);
return response.writeStringAndFlush(content);
});
}
示例13: createServer
import io.reactivex.netty.protocol.http.server.HttpServer; //导入依赖的package包/类
public HttpServer<ByteBuf, ServerSentEvent> createServer() {
HttpServer<ByteBuf, ServerSentEvent> server = RxNetty.createHttpServer(port,
(request, response) -> {
response.getHeaders().set(ACCESS_CONTROL_ALLOW_ORIGIN, "*");
response.getHeaders().set(CACHE_CONTROL, "no-cache");
response.getHeaders().set(CONNECTION, "keep-alive");
response.getHeaders().set(CONTENT_TYPE, "text/event-stream");
return getIntervalObservable(request, response);
}, PipelineConfigurators.<ByteBuf>serveSseConfigurator());
System.out.println("HTTP Server Sent Events server started...");
return server;
}
示例14: nioTest
import io.reactivex.netty.protocol.http.server.HttpServer; //导入依赖的package包/类
@Test
public void nioTest() throws Exception {
HttpServer<ByteBuf, ByteBuf> server = getServer();
TestSubscriber<String> ts = new TestSubscriber<>();
long start = System.currentTimeMillis();
// we use 10 since the default rxnetty thread pool size is 8
// you could also shrink the pool down for the same effect
// but I couldn't be bothered finding the settings
Observable.range(1, 10)
// flatMap runs async Observables concurrently
.flatMap(i ->
HttpClient.newClient(server.getServerAddress())
.createGet("/" + i)
.flatMap(response ->
response.getContent()
.map(bytes ->
bytes.toString(Charset.defaultCharset()) + " " +
"[response received on " + Thread.currentThread().getName() +
" at " + (System.currentTimeMillis() - start) + "]"
)
)
)
.doOnNext(System.out::println)
.subscribe(ts);
ts.awaitTerminalEvent();
server.shutdown();
}
示例15: getServer
import io.reactivex.netty.protocol.http.server.HttpServer; //导入依赖的package包/类
private HttpServer<ByteBuf, ByteBuf> getServer() {
return HttpServer.newServer()
.start((request, response) -> {
String requestThread = Thread.currentThread().getName();
// we need tp flush on each so that the response will 'stream'
return response.writeStringAndFlushOnEach(
// one numbered reply per second
Observable.interval(1, 1, TimeUnit.SECONDS, Schedulers.io())
.take(4)
.map(i ->
getResponseValue(request, requestThread, i)
)
);
});
}