Java tutorial
/* * Copyright 2014 Netflix, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.schibsted.triathlon.service; import java.nio.charset.Charset; import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.regex.Matcher; import java.util.regex.Pattern; import io.netty.buffer.ByteBuf; import io.netty.buffer.UnpooledByteBufAllocator; import io.netty.handler.codec.http.HttpMethod; import io.netty.handler.codec.http.HttpResponseStatus; import io.netty.handler.logging.LogLevel; import io.netty.util.internal.ConcurrentSet; import io.reactivex.netty.RxNetty; import io.reactivex.netty.pipeline.PipelineConfigurators; import io.reactivex.netty.protocol.http.server.HttpServer; import io.reactivex.netty.protocol.http.server.HttpServerRequest; import io.reactivex.netty.protocol.http.server.HttpServerResponse; import io.reactivex.netty.protocol.http.server.RequestHandler; import rx.Observable; import rx.functions.Func1; import static java.lang.String.*; /** * The client examples assume that the movie server runs on the default port 8080. * * @author Tomasz Bak */ public class RxTestServer { public static final int DEFAULT_PORT = 8088; private final int port; private HttpServer<ByteBuf, ByteBuf> server; public RxTestServer(int port) { this.port = port; } 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; } private Observable<Void> handleTest(HttpServerRequest<ByteBuf> request, final HttpServerResponse<ByteBuf> response) { response.setStatus(HttpResponseStatus.OK); ByteBuf byteBuf = UnpooledByteBufAllocator.DEFAULT.buffer(); byteBuf.writeBytes("test".getBytes(Charset.defaultCharset())); response.write(byteBuf); return response.close(); } public void start() { server = createServer(); server.start(); } public void shutdown() { try { server.shutdown(); } catch (InterruptedException e) { e.printStackTrace(); } } public static void main(final String[] args) { new RxTestServer(DEFAULT_PORT).createServer().startAndWait(); } }