本文整理汇总了Java中io.reactivex.netty.protocol.http.server.RequestHandler类的典型用法代码示例。如果您正苦于以下问题:Java RequestHandler类的具体用法?Java RequestHandler怎么用?Java RequestHandler使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
RequestHandler类属于io.reactivex.netty.protocol.http.server包,在下文中一共展示了RequestHandler类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setUp
import io.reactivex.netty.protocol.http.server.RequestHandler; //导入依赖的package包/类
/**
* Setups the test environment.
*
* @throws Exception if any error occurs
*/
@Before
public void setUp() throws Exception {
instance = new HealthCheckTaskExecutor();
server = RxNetty.createHttpServer(PORT, new RequestHandler<ByteBuf, ByteBuf>() {
@Override
public Observable<Void> handle(HttpServerRequest<ByteBuf> request, HttpServerResponse<ByteBuf> response) {
if ("/health".equals(request.getPath())) {
return response.writeStringAndFlush("{\"status\": \"UP\", \"service\": {\"status\": \"UP\"}}");
}
response.setStatus(HttpResponseStatus.NOT_FOUND);
return response.close();
}
}).start();
}
示例2: setup
import io.reactivex.netty.protocol.http.server.RequestHandler; //导入依赖的package包/类
@Setup
public void setup() {
server = RxNetty.createHttpServer(SERVER_PORT, new RequestHandler<ByteBuf, ByteBuf>() {
public rx.Observable handle(HttpServerRequest<ByteBuf> request,
HttpServerResponse<ByteBuf> response) {
return response.flush();
}
});
server.start();
client = new OkHttpClient();
client.setRetryOnConnectionFailure(false);
okFeign = Feign.builder()
.client(new feign.okhttp.OkHttpClient(client))
.target(FeignTestInterface.class, "http://localhost:" + SERVER_PORT);
queryRequest = new Request.Builder()
.url("http://localhost:" + SERVER_PORT + "/?Action=GetUser&Version=2010-05-08&limit=1")
.build();
}
示例3: testStreamDoesNotRunWhenSubscribeFails_mesos5xxResponse
import io.reactivex.netty.protocol.http.server.RequestHandler; //导入依赖的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();
}
}
示例4: testStreamDoesNotRunWhenSubscribeFails_mismatchContentType
import io.reactivex.netty.protocol.http.server.RequestHandler; //导入依赖的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();
}
}
示例5: createServer
import io.reactivex.netty.protocol.http.server.RequestHandler; //导入依赖的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;
}
示例6: providesKaryonTransport
import io.reactivex.netty.protocol.http.server.RequestHandler; //导入依赖的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);
}
示例7: createServer
import io.reactivex.netty.protocol.http.server.RequestHandler; //导入依赖的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;
}
示例8: main
import io.reactivex.netty.protocol.http.server.RequestHandler; //导入依赖的package包/类
public static void main(final String[] args) {
final int port = 8080;
RxNetty.createHttpServer(port, new RequestHandler<ByteBuf, ByteBuf>() {
@Override
public Observable<Void> handle(HttpRequest<ByteBuf> request, final HttpResponse<ByteBuf> response) {
System.out.println("New request recieved");
System.out.println(request.getHttpMethod() + " " + request.getUri() + ' ' + request.getHttpVersion());
for (Map.Entry<String, String> header : request.getHeaders().entries()) {
System.out.println(header.getKey() + ": " + header.getValue());
}
// This does not consume request content, need to figure out an elegant/correct way of doing that.
return response.writeStringAndFlush("Welcome!!! \n\n");
}
}).startAndWait();
}
示例9: handle
import io.reactivex.netty.protocol.http.server.RequestHandler; //导入依赖的package包/类
@Override
public Observable<Void> handle(HttpServerRequest<ByteBuf> request, HttpServerResponse<ByteBuf> response) {
RequestHandler<ByteBuf, ByteBuf> handler = findRequestHandler(request.getUri());
if (handler != null) {
return handler.handle(request, response);
} else {
response.setStatus(HttpResponseStatus.NOT_FOUND);
return response.close();
}
}
示例10: findRequestHandler
import io.reactivex.netty.protocol.http.server.RequestHandler; //导入依赖的package包/类
private RequestHandler<ByteBuf, ByteBuf> findRequestHandler(String uri) {
for (String uriKey : uriHandlers.keySet()) {
if (uri.equals(uriKey) || uri.startsWith(uriKey + PATH_DELIM)) {
return uriHandlers.get(uriKey);
}
}
return null;
}
示例11: testStreamDoesNotRunWhenSubscribeFails_mesos4xxResponse
import io.reactivex.netty.protocol.http.server.RequestHandler; //导入依赖的package包/类
@Test
public void testStreamDoesNotRunWhenSubscribeFails_mesos4xxResponse() throws Throwable {
final String errorMessage = "Error message that should come from the server";
final RequestHandler<ByteBuf, ByteBuf> handler = (request, response) -> {
response.setStatus(HttpResponseStatus.BAD_REQUEST);
final byte[] msgBytes = errorMessage.getBytes(StandardCharsets.UTF_8);
response.getHeaders().setHeader("Content-Length", msgBytes.length);
response.getHeaders().setHeader("Content-Type", "text/plain;charset=utf-8");
response.writeBytes(msgBytes);
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 400");
} catch (Mesos4xxException e) {
// expected
final MesosClientErrorContext ctx = e.getContext();
assertThat(ctx.getStatusCode()).isEqualTo(400);
assertThat(ctx.getMessage()).isEqualTo(errorMessage);
} finally {
server.shutdown();
}
}
示例12: testBurstyObservable_missingBackpressureException
import io.reactivex.netty.protocol.http.server.RequestHandler; //导入依赖的package包/类
@Test
@Ignore
public void testBurstyObservable_missingBackpressureException() throws Throwable {
final String subscribedMessage = "{\"type\": \"SUBSCRIBED\",\"subscribed\": {\"framework_id\": {\"value\":\"12220-3440-12532-2345\"},\"heartbeat_interval_seconds\":15.0}";
final String heartbeatMessage = "{\"type\":\"HEARTBEAT\"}";
final byte[] hmsg = heartbeatMessage.getBytes(StandardCharsets.UTF_8);
final byte[] hbytes = String.format("%d\n", heartbeatMessage.getBytes().length).getBytes(StandardCharsets.UTF_8);
final RequestHandler<ByteBuf, ByteBuf> handler = (request, response) -> {
response.setStatus(HttpResponseStatus.OK);
response.getHeaders().setHeader("Content-Type", "text/plain;charset=utf-8");
writeRecordIOMessage(response, subscribedMessage);
for (int i = 0; i < 20000; i++) {
response.writeBytes(hbytes);
response.writeBytes(hmsg);
}
return response.flush();
};
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 = createClientForStreaming(uri).build();
try {
client.openStream().await();
fail("Expect an exception to be propagated up due to backpressure");
} catch (MissingBackpressureException e) {
// expected
e.printStackTrace();
assertThat(e.getMessage()).isNullOrEmpty();
} finally {
server.shutdown();
}
}
示例13: testBurstyObservable_unboundedBufferSucceeds
import io.reactivex.netty.protocol.http.server.RequestHandler; //导入依赖的package包/类
@Test
public void testBurstyObservable_unboundedBufferSucceeds() throws Throwable {
msgNo = 0;
final int numMessages = 20000;
final String subscribedMessage = "{\"type\": \"SUBSCRIBED\",\"subscribed\": {\"framework_id\": {\"value\":\"12220-3440-12532-2345\"},\"heartbeat_interval_seconds\":15.0}";
final String heartbeatMessage = "{\"type\":\"HEARTBEAT\"}";
final RequestHandler<ByteBuf, ByteBuf> handler = (request, response) -> {
response.setStatus(HttpResponseStatus.OK);
response.getHeaders().setHeader("Content-Type", "text/plain;charset=utf-8");
writeRecordIOMessage(response, subscribedMessage);
for (int i = 0; i < numMessages; i++) {
writeRecordIOMessage(response, heartbeatMessage);
}
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 = createClientForStreaming(uri)
.onBackpressureBuffer()
.build();
try {
client.openStream().await();
} finally {
// 20000 heartbeats PLUS 1 subscribe
assertEquals("All heartbeats received (plus the subscribed)", 1 + numMessages, msgNo);
server.shutdown();
}
}
示例14: IndexResource
import io.reactivex.netty.protocol.http.server.RequestHandler; //导入依赖的package包/类
public IndexResource() {
endpoint = new HelloEndpoint();
delegate = new SimpleUriRouter<>();
delegate
.addUri("/", new RequestHandler<ByteBuf, ByteBuf>() {
@Override
public Observable<Void> handle(HttpServerRequest<ByteBuf> request,
final HttpServerResponse<ByteBuf> response) {
return endpoint.getHello()
.flatMap(new Func1<String, Observable<Void>>() {
@Override
public Observable<Void> call(String body) {
String instanceId = "";
String userdata = "";
try{
instanceId = execCmd("curl http://metadata/computeMetadata/v1/instance/id -H Metadata-Flavor:Google") + execCmd("wget -q -O - http://instance-data/latest/meta-data/instance-id");
userdata = System.getenv("USERDATA");
} catch (Exception e){
e.printStackTrace();
}
response.writeString("<html><head><style>body{text-align:center;font-family:'Lucida Grande'}</style></head><body><img src='http://kenzan.com/wp-content/themes/kenzan/images/logo-reg.png' /><h2>Example Spinnaker Application</h2><h3>Instance Id " + instanceId + "</h3><h3>$USERDATA ENV VAR: " + userdata + "</h3></body></html>");
return response.close();
}
});
}
});
}
示例15: getHandler
import io.reactivex.netty.protocol.http.server.RequestHandler; //导入依赖的package包/类
@Override
protected RequestHandler<ByteBuf, ByteBuf> getHandler() {
ArrayList<InstanceInfo> instanceInfos = new ArrayList<>();
instanceInfos.add(InstanceInfo.Builder.newBuilder().setAppName("foo").setVIPAddress("bar").setHostName("host1").build());
instanceInfos.add(InstanceInfo.Builder.newBuilder().setAppName("foo").setVIPAddress("bar").setHostName("host2").build());
when(hostService.getHosts("foo")).thenReturn(instanceInfos);
return new HostsHandler(hostService, new ObjectMapper());
}