io.reactivex.netty.protocol.http.server.HttpServerRequest Java Examples

The following examples show how to use io.reactivex.netty.protocol.http.server.HttpServerRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RxMovieServer.java    From ribbon with Apache License 2.0 6 votes vote down vote up
private Observable<Void> handleRegisterMovie(HttpServerRequest<ByteBuf> request, final HttpServerResponse<ByteBuf> response) {
    System.out.println("Http request -> register movie: " + request.getPath());
    return request.getContent().flatMap(new Func1<ByteBuf, Observable<Void>>() {
        @Override
        public Observable<Void> call(ByteBuf byteBuf) {
            String formatted = byteBuf.toString(Charset.defaultCharset());
            System.out.println("    movie: " + formatted);
            try {
                Movie movie = Movie.from(formatted);
                movies.put(movie.getId(), movie);
                response.setStatus(HttpResponseStatus.CREATED);
            } catch (Exception e) {
                System.err.println("Invalid movie content");
                e.printStackTrace();
                response.setStatus(HttpResponseStatus.BAD_REQUEST);
            }
            return response.close();
        }
    });
}
 
Example #2
Source File: BookmarksService.java    From ReactiveLab with Apache License 2.0 6 votes vote down vote up
@Override
protected Observable<Void> handleRequest(HttpServerRequest<?> request, HttpServerResponse<ServerSentEvent> response) {
    List<String> videoIds = request.getQueryParameters().get("videoId");

    int latency = 1;
    if (Random.randomIntFrom0to100() > 80) {
        latency = 10;
    }

    return Observable.from(videoIds).map(videoId -> {
        Map<String, Object> video = new HashMap<>();
        video.put("videoId", videoId);
        video.put("position", (int) (Math.random() * 5000));
        return video;
    }).flatMap(video -> response.writeStringAndFlush("data: " + SimpleJson.mapToJson(video) + "\n"))
            .delay(latency, TimeUnit.MILLISECONDS).doOnCompleted(response::close); // simulate latency
}
 
Example #3
Source File: GeoService.java    From ReactiveLab with Apache License 2.0 6 votes vote down vote up
@Override
protected Observable<Void> handleRequest(HttpServerRequest<?> request, HttpServerResponse<ServerSentEvent> response) {
    return request.getContent().flatMap(i -> {
        List<String> ips = request.getQueryParameters().get("ip");
        Map<String, Object> data = new HashMap<>();
        for (String ip : ips) {
            Map<String, Object> ip_data = new HashMap<>();
            ip_data.put("country_code", "GB");
            ip_data.put("longitude", "-0.13");
            ip_data.put("latitude", "51.5");
            data.put(ip, ip_data);
        }
        return response.writeStringAndFlush("data: " + SimpleJson.mapToJson(data) + "\n")
                .doOnCompleted(response::close);
    }).delay(10, TimeUnit.MILLISECONDS);
}
 
Example #4
Source File: RouteForDeviceHome.java    From ReactiveLab with Apache License 2.0 6 votes vote down vote up
public Observable<Void> handle(HttpServerRequest<ByteBuf> request, HttpServerResponse<ByteBuf> response) {
    List<String> userId = request.getQueryParameters().get("userId");
    if (userId == null || userId.size() != 1) {
        return StartGatewayServer.writeError(request, response, "A single 'userId' is required.");
    }

    return new UserCommand(userId).observe().flatMap(user -> {
        Observable<Map<String, Object>> catalog = new PersonalizedCatalogCommand(user).observe()
                .flatMap(catalogList -> catalogList.videos().<Map<String, Object>> flatMap(
                        video -> {
                            Observable<Bookmark> bookmark = new BookmarkCommand(video).observe();
                            Observable<Rating> rating = new RatingsCommand(video).observe();
                            Observable<VideoMetadata> metadata = new VideoMetadataCommand(video).observe();
                            return Observable.zip(bookmark, rating, metadata, (b, r, m) -> combineVideoData(video, b, r, m));
                        }));

        Observable<Map<String, Object>> social = new SocialCommand(user).observe().map(s -> {
            return s.getDataAsMap();
        });

        return Observable.merge(catalog, social);
    }).flatMap(data -> {
        String json = SimpleJson.mapToJson(data);
        return response.writeStringAndFlush("data: " + json + "\n");
    });
}
 
Example #5
Source File: HttpKeyEvaluationContext.java    From karyon with Apache License 2.0 6 votes vote down vote up
/**
 * Parses (if not done previously) and returns the path component in the URI.
 *
 * @param httpRequest HTTP request for which the URI path is to be returned.
 *
 * @return The path component of the URI (as returned by {@link HttpRequest#getUri()} or {@code null} if the
 * URI is null.
 */
String getRequestUriPath(HttpServerRequest<?> httpRequest) {
    String uri = httpRequest.getUri();
    if (null == uri) {
        return null;
    }

    if (null == queryStringDecoder) {
        if (null == channel) {
            queryStringDecoder = new QueryStringDecoder(uri);
        } else {
            queryStringDecoder = getOrCreateQueryStringDecoder(httpRequest);
        }
    }

    return queryStringDecoder.nettyDecoder().path();
}
 
Example #6
Source File: HttpKeyEvaluationContext.java    From karyon with Apache License 2.0 6 votes vote down vote up
private QueryStringDecoder getOrCreateQueryStringDecoder(HttpServerRequest<?> request) {
    if (null == request) {
        throw new NullPointerException("Request can not be null.");
    }
    String uri = request.getUri();
    if (null == uri) {
        return null;
    }

    Attribute<QueryStringDecoder> queryDecoderAttr = channel.attr(queryDecoderKey);

    QueryStringDecoder _queryStringDecoder = queryDecoderAttr.get();

    if (null == _queryStringDecoder) {
        _queryStringDecoder = new QueryStringDecoder(uri);
        queryDecoderAttr.setIfAbsent(_queryStringDecoder);
    }
    return _queryStringDecoder;
}
 
Example #7
Source File: HttpKeyEvaluationContext.java    From karyon with Apache License 2.0 6 votes vote down vote up
public static QueryStringDecoder getOrCreateQueryStringDecoder(HttpServerRequest<?> request,
                                                               ChannelHandlerContext channelHandlerContext) {
    if (null == request) {
        throw new NullPointerException("Request can not be null.");
    }

    String uri = request.getUri();
    if (null == uri) {
        return null;
    }

    Attribute<QueryStringDecoder> queryDecoderAttr = channelHandlerContext.attr(queryDecoderKey);

    QueryStringDecoder _queryStringDecoder = queryDecoderAttr.get();

    if (null == _queryStringDecoder) {
        _queryStringDecoder = new QueryStringDecoder(uri);
        queryDecoderAttr.setIfAbsent(_queryStringDecoder);
    }
    return _queryStringDecoder;
}
 
Example #8
Source File: UserService.java    From ReactiveLab with Apache License 2.0 6 votes vote down vote up
@Override
protected Observable<Void> handleRequest(HttpServerRequest<?> request, HttpServerResponse<ServerSentEvent> response) {
    List<String> userIds = request.getQueryParameters().get("userId");
    if (userIds == null || userIds.size() == 0) {
        return writeError(request, response, "At least one parameter of 'userId' must be included.");
    }
    return Observable.from(userIds).map(userId -> {
        Map<String, Object> user = new HashMap<>();
        user.put("userId", userId);
        user.put("name", "Name Here");
        user.put("other_data", "goes_here");
        return user;
    }).flatMap(user -> response.writeStringAndFlush("data: " + SimpleJson.mapToJson(user) + "\n")
            .doOnCompleted(response::close))
            .delay(((long) (Math.random() * 500) + 500), TimeUnit.MILLISECONDS); // simulate latency
}
 
Example #9
Source File: HelloWorldEndpoint.java    From karyon with Apache License 2.0 6 votes vote down vote up
public Observable<Void> sayHelloToUser(HttpServerRequest<ByteBuf> request, HttpServerResponse<ByteBuf> response) {
    JSONObject content = new JSONObject();

    int prefixLength = "/hello/to".length();
    String userName = request.getPath().substring(prefixLength);

    try {
        if (userName.isEmpty() || userName.length() == 1 /*The uri is /hello/to/ but no name */) {
            response.setStatus(HttpResponseStatus.BAD_REQUEST);
            content.put("Error", "Please provide a username to say hello. The URI should be /hello/to/{username}");
        } else {
            content.put("Message", "Hello " + userName.substring(1) /*Remove the / prefix*/ + " from Netflix OSS");
        }
    } catch (JSONException e) {
        logger.error("Error creating json response.", e);
        return Observable.error(e);
    }

    response.write(content.toString(), StringTransformer.DEFAULT_INSTANCE);
    return response.close();

}
 
Example #10
Source File: RxMovieServer.java    From ribbon with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #11
Source File: GovernatorHttpInterceptorSupport.java    From karyon with Apache License 2.0 6 votes vote down vote up
public GovernatorHttpInterceptorSupport<I, O> intercept(List<Class<? extends DuplexInterceptor<HttpServerRequest<I>, HttpServerResponse<O>>>> interceptors) {
    ArrayList<Class<? extends InboundInterceptor<HttpServerRequest<I>, HttpServerResponse<O>>>> ins =
            new ArrayList<Class<? extends InboundInterceptor<HttpServerRequest<I>, HttpServerResponse<O>>>>();
    ArrayList<Class<? extends OutboundInterceptor<HttpServerResponse<O>>>> outs =
            new ArrayList<Class<? extends OutboundInterceptor<HttpServerResponse<O>>>>();

    for (Class<? extends DuplexInterceptor<HttpServerRequest<I>, HttpServerResponse<O>>> interceptor : interceptors) {
        ins.add(interceptor);
        outs.add(interceptor);
    }

    HttpInClassHolder<I, O> inHolder = new HttpInClassHolder<I, O>(key, ins);
    interceptorSupport.inboundInterceptorClasses.add(inHolder);

    HttpOutClassHolder<I, O> outHolder = new HttpOutClassHolder<I, O>(key, outs);
    interceptorSupport.outboundInterceptorClasses.add(outHolder);
    return interceptorSupport;
}
 
Example #12
Source File: RxMovieServer.java    From ribbon with Apache License 2.0 6 votes vote down vote up
private Observable<Void> handleRecommendationsByUserId(HttpServerRequest<ByteBuf> request, HttpServerResponse<ByteBuf> response) {
    System.out.println("HTTP request -> recommendations by user id request: " + request.getPath());
    final String userId = userIdFromPath(request.getPath());
    if (userId == null) {
        response.setStatus(HttpResponseStatus.BAD_REQUEST);
        return response.close();
    }
    if (!userRecommendations.containsKey(userId)) {
        response.setStatus(HttpResponseStatus.NOT_FOUND);
        return response.close();
    }

    StringBuilder builder = new StringBuilder();
    for (String movieId : userRecommendations.get(userId)) {
        System.out.println("    returning: " + movies.get(movieId));
        builder.append(movies.get(movieId)).append('\n');
    }

    ByteBuf byteBuf = UnpooledByteBufAllocator.DEFAULT.buffer();
    byteBuf.writeBytes(builder.toString().getBytes(Charset.defaultCharset()));

    response.write(byteBuf);
    return response.close();
}
 
Example #13
Source File: PersonalizedCatalogService.java    From ReactiveLab with Apache License 2.0 5 votes vote down vote up
@Override
protected Observable<Void> handleRequest(HttpServerRequest<?> request, HttpServerResponse<ServerSentEvent> response) {
    return Observable.from(request.getQueryParameters().get("userId")).map(userId -> {
        Map<String, Object> userData = new HashMap<>();
        userData.put("user_id", userId);

        userData.put("list_title", "Really quirky and over detailed list title!");
        userData.put("other_data", "goes_here");
        userData.put("videos", Arrays.asList(12345, 23456, 34567, 45678, 56789, 67890));
        return userData;
    }).flatMap(list -> response.writeStringAndFlush("data: " + SimpleJson.mapToJson(list) + "\n"))
            .delay(((long) (Math.random() * 100) + 20), TimeUnit.MILLISECONDS)
            .doOnCompleted(response::close); // simulate latency
}
 
Example #14
Source File: ServletStyleConstraintTest.java    From karyon with Apache License 2.0 5 votes vote down vote up
@Test
public void testQueryDecoderCache() throws Exception {
    ServletStyleUriConstraintKey<ByteBuf> key = new ServletStyleUriConstraintKey<>("d/a/b/c", "d");
    Channel mockChannel = new MockChannelHandlerContext("mock").channel();
    HttpKeyEvaluationContext context = new HttpKeyEvaluationContext(mockChannel);
    HttpServerRequest<ByteBuf> request = newRequest("/d/a/b/c/");
    boolean keyApplicable = key.apply(request, context);
    Assert.assertTrue("Exact match servlet style constraint failed.", keyApplicable);
    HttpKeyEvaluationContext context2 = new HttpKeyEvaluationContext(mockChannel);
    request = newRequest("/x/y/z");
    keyApplicable = key.apply(request, context2);
    Assert.assertFalse("Query decoder cache not cleared..", keyApplicable);
}
 
Example #15
Source File: RxMovieServer.java    From ribbon with Apache License 2.0 5 votes vote down vote up
private Observable<Void> handleUpdateRecommendationsForUser(HttpServerRequest<ByteBuf> request, final HttpServerResponse<ByteBuf> response) {
    System.out.println("HTTP request -> update recommendations for user: " + request.getPath());
    final String userId = userIdFromPath(request.getPath());
    if (userId == null) {
        response.setStatus(HttpResponseStatus.BAD_REQUEST);
        return response.close();
    }
    return request.getContent().flatMap(new Func1<ByteBuf, Observable<Void>>() {
        @Override
        public Observable<Void> call(ByteBuf byteBuf) {
            String movieId = byteBuf.toString(Charset.defaultCharset());
            System.out.println(format("    updating: {user=%s, movie=%s}", userId, movieId));
            synchronized (this) {
                Set<String> recommendations;
                if (userRecommendations.containsKey(userId)) {
                    recommendations = userRecommendations.get(userId);
                } else {
                    recommendations = new ConcurrentSet<String>();
                    userRecommendations.put(userId, recommendations);
                }
                recommendations.add(movieId);
            }
            response.setStatus(HttpResponseStatus.OK);
            return response.close();
        }
    });
}
 
Example #16
Source File: VideoMetadataService.java    From ReactiveLab with Apache License 2.0 5 votes vote down vote up
@Override
protected Observable<Void> handleRequest(HttpServerRequest<?> request, HttpServerResponse<ServerSentEvent> response) {
    List<String> videoIds = request.getQueryParameters().get("videoId");
    return Observable.from(videoIds).map(videoId -> {
        Map<String, Object> video = new HashMap<>();
        video.put("videoId", videoId);
        video.put("title", "Video Title");
        video.put("other_data", "goes_here");
        return video;
    }).flatMap(video -> response.writeStringAndFlush("data: " + SimpleJson.mapToJson(video) + "\n")
            .doOnCompleted(response::close))
            .delay(((long) (Math.random() * 20) + 20), TimeUnit.MILLISECONDS); // simulate latency
}
 
Example #17
Source File: MockService.java    From ReactiveLab with Apache License 2.0 5 votes vote down vote up
@Override
protected Observable<Void> handleRequest(HttpServerRequest<?> request, HttpServerResponse<ServerSentEvent> response) {
    List<String> _id = request.getQueryParameters().get("id");
    if (_id == null || _id.size() != 1) {
        return writeError(request, response, "Please provide a numerical 'id' value. It can be a random number (uuid). Received => " + _id);
    }
    long id = Long.parseLong(String.valueOf(_id.get(0)));

    int delay = getParameter(request, "delay", 50); // default to 50ms server-side delay
    int itemSize = getParameter(request, "itemSize", 128); // default to 128 bytes item size (assuming ascii text)
    int numItems = getParameter(request, "numItems", 10); // default to 10 items in a list

    // no more than 100 items
    if (numItems < 1 || numItems > 100) {
        return writeError(request, response, "Please choose a 'numItems' value from 1 to 100.");
    }

    // no larger than 50KB per item
    if (itemSize < 1 || itemSize > 1024 * 50) {
        return writeError(request, response, "Please choose an 'itemSize' value from 1 to 1024*50 (50KB).");
    }

    // no larger than 60 second delay
    if (delay < 0 || delay > 60000) {
        return writeError(request, response, "Please choose a 'delay' value from 0 to 60000 (60 seconds).");
    }

    response.setStatus(HttpResponseStatus.OK);
    return MockResponse.generateJson(id, delay, itemSize, numItems)
            .flatMap(json -> response.writeStringAndFlush("data:" + json + "\n"))
            .doOnCompleted(response::close);
}
 
Example #18
Source File: SocialService.java    From ReactiveLab with Apache License 2.0 5 votes vote down vote up
@Override
protected Observable<Void> handleRequest(HttpServerRequest<?> request, HttpServerResponse<ServerSentEvent> response) {
    return Observable.from(request.getQueryParameters().get("userId")).map(userId -> {
        Map<String, Object> user = new HashMap<>();
        user.put("userId", userId);
        user.put("friends", Arrays.asList(randomUser(), randomUser(), randomUser(), randomUser()));
        return user;
    }).flatMap(list -> response.writeStringAndFlush("data: " + SimpleJson.mapToJson(list) + "\n"))
            .delay(((long) (Math.random() * 100) + 20), TimeUnit.MILLISECONDS).doOnCompleted(response::close); // simulate latency
}
 
Example #19
Source File: AbstractMiddleTierService.java    From ReactiveLab with Apache License 2.0 5 votes vote down vote up
protected static int getParameter(HttpServerRequest<?> request, String key, int defaultValue) {
    List<String> v = request.getQueryParameters().get(key);
    if (v == null || v.size() != 1) {
        return defaultValue;
    } else {
        return Integer.parseInt(String.valueOf(v.get(0)));
    }
}
 
Example #20
Source File: ProxyHandler.java    From Prana with Apache License 2.0 5 votes vote down vote up
private void populateRequestHeaders(HttpServerRequest<ByteBuf> serverRequest, HttpClientRequest<ByteBuf> request) {
    Set<String> headerNames = serverRequest.getHeaders().names();
    for (String name : headerNames) {
        if (name.contains("content-length")) {
            continue;
        }
        request.getHeaders().add(name, serverRequest.getHeaders().getHeader(name));
    }
    // Normally always request gzipped from the server. But can be overridden with a Dynamic Property.
    if (PROXY_REQ_ACCEPT_ENCODING != null && PROXY_REQ_ACCEPT_ENCODING.length() > 0) {
        request.getHeaders().addHeader("accept-encoding", PROXY_REQ_ACCEPT_ENCODING);
    }
    //TODO Write X-Forwarded-Host, X-Forwarded-Port, X-Forwarded-Proto, X-Forwarded-For in the headers
}
 
Example #21
Source File: ServletStyleConstraintTest.java    From karyon with Apache License 2.0 5 votes vote down vote up
@Test
public void testServletPathExtensionMatch() throws Exception {
    ServletStyleUriConstraintKey<ByteBuf> key = new ServletStyleUriConstraintKey<>("*.boo", "d");
    HttpKeyEvaluationContext context = new HttpKeyEvaluationContext(new MockChannelHandlerContext("mock").channel());
    HttpServerRequest<ByteBuf> request = newRequest("/d/a/b/c.boo");
    boolean keyApplicable = key.apply(request, context);
    Assert.assertTrue("Extension match servlet style constraint failed.", keyApplicable);
    String servletPath = key.getServletPath(request, context);
    Assert.assertEquals("Unexpected servlet path.", "", servletPath);
}
 
Example #22
Source File: StartMockService.java    From WSPerfLab with Apache License 2.0 5 votes vote down vote up
private static Observable<Void> handleRequest(HttpServerRequest<ByteBuf> request, HttpServerResponse<ByteBuf> response) {

        if (request.getUri().startsWith("/hello")) {
            return response.writeStringAndFlush("Hello world!");
        }

        List<String> _id = request.getQueryParameters().get("id");
        if (_id == null || _id.size() != 1) {
            return writeError(request, response,
                              "Please provide a numerical 'id' value. It can be a random number (uuid). Received => "
                              + _id);
        }
        long id = Long.parseLong(String.valueOf(_id.get(0)));

        int delay = getParameter(request, "delay", 50); // default to 50ms server-side delay
        int itemSize = getParameter(request, "itemSize", 128); // default to 128 bytes item size (assuming ascii text)
        int numItems = getParameter(request, "numItems", 10); // default to 10 items in a list

        // no more than 100 items
        if (numItems < 1 || numItems > 100) {
            return writeError(request, response, "Please choose a 'numItems' value from 1 to 100.");
        }

        // no larger than 50KB per item
        if (itemSize < 1 || itemSize > 1024 * 50) {
            return writeError(request, response, "Please choose an 'itemSize' value from 1 to 1024*50 (50KB).");
        }

        // no larger than 60 second delay
        if (delay < 0 || delay > 60000) {
            return writeError(request, response, "Please choose a 'delay' value from 0 to 60000 (60 seconds).");
        }

        response.setStatus(HttpResponseStatus.OK);
        return MockResponse.generateJson(id, delay, itemSize, numItems)
                           .doOnNext(json -> counter.add(CounterEvent.BYTES, json.readableBytes()))
                           .flatMap(response::writeAndFlush)
                           .doOnTerminate(response::close);
    }
 
Example #23
Source File: ServletStyleConstraintTest.java    From karyon with Apache License 2.0 5 votes vote down vote up
@Test
public void testServletPathPrefixMatch() throws Exception {
    ServletStyleUriConstraintKey<ByteBuf> key = new ServletStyleUriConstraintKey<>("d/a/*", "d");
    HttpKeyEvaluationContext context = new HttpKeyEvaluationContext(new MockChannelHandlerContext("mock").channel());
    HttpServerRequest<ByteBuf> request = newRequest("/d/a/b/c/");
    boolean keyApplicable = key.apply(request, context);
    Assert.assertTrue("Prefix match servlet style constraint failed.", keyApplicable);
    String servletPath = key.getServletPath(request, context);
    Assert.assertEquals("Unexpected servlet path.", "/a", servletPath);
}
 
Example #24
Source File: GovernatorHttpInterceptorSupport.java    From karyon with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public GovernatorHttpInterceptorSupport<I, O> intercept(Class<? extends DuplexInterceptor<HttpServerRequest<I>, HttpServerResponse<O>>> interceptor) {
    ArrayList<Class<? extends DuplexInterceptor<HttpServerRequest<I>, HttpServerResponse<O>>>> interceptors =
            new ArrayList<Class<? extends DuplexInterceptor<HttpServerRequest<I>, HttpServerResponse<O>>>>();
    interceptors.add(interceptor);
    return intercept(interceptors);
}
 
Example #25
Source File: ServletStyleConstraintTest.java    From karyon with Apache License 2.0 5 votes vote down vote up
@Test
public void testServletPathExactMatch() throws Exception {
    ServletStyleUriConstraintKey<ByteBuf> key = new ServletStyleUriConstraintKey<>("d/a/b/c", "d");
    HttpKeyEvaluationContext context = new HttpKeyEvaluationContext(new MockChannelHandlerContext("mock").channel());
    HttpServerRequest<ByteBuf> request = newRequest("/d/a/b/c/");
    boolean keyApplicable = key.apply(request, context);
    Assert.assertTrue("Exact match servlet style constraint failed.", keyApplicable);
    String servletPath = key.getServletPath(request, context);
    Assert.assertEquals("Unexpected servlet path.", "/a/b/c/", servletPath);
}
 
Example #26
Source File: InterceptorConstraintTestBase.java    From karyon with Apache License 2.0 5 votes vote down vote up
protected static boolean doApply(InterceptorKey<HttpServerRequest<ByteBuf>, HttpKeyEvaluationContext> key, String uri,
                                 HttpMethod httpMethod) {
    DefaultHttpRequest nettyRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_0, httpMethod, uri);
    return key.apply(new HttpServerRequest<ByteBuf>(nettyRequest,
                                                    UnicastContentSubject.<ByteBuf>createWithoutNoSubscriptionTimeout()),
                     new HttpKeyEvaluationContext(new MockChannelHandlerContext("mock").channel()));
}
 
Example #27
Source File: KaryonHttpModuleTest.java    From karyon with Apache License 2.0 5 votes vote down vote up
@Override
public Observable<Void> handle(HttpServerRequest<ByteBuf> request, HttpServerResponse<ByteBuf> response) {
    if (request.getPath().contains("/sendOK")) {
        response.setStatus(HttpResponseStatus.OK);
    } else if (request.getPath().contains("/sendNotFound")) {
        response.setStatus(HttpResponseStatus.NOT_FOUND);
    } else {
        response.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
    }
    return Observable.empty();
}
 
Example #28
Source File: ServletStyleUriConstraintKey.java    From karyon with Apache License 2.0 5 votes vote down vote up
@Override
public boolean apply(HttpServerRequest<I> request, HttpKeyEvaluationContext context) {
    String requestUriPath = context.getRequestUriPath(request);
    boolean matches = false;
    if (null != requestUriPath) {
        matches = matcher.match(requestUriPath);
    }
    return matches;
}
 
Example #29
Source File: MethodConstraintKey.java    From karyon with Apache License 2.0 5 votes vote down vote up
@Override
public boolean apply(HttpServerRequest<I> request, HttpKeyEvaluationContext context) {
    boolean matches = request.getHttpMethod().equals(method);
    if (logger.isDebugEnabled()) {
        logger.debug("Result for HTTP method constraint for method {} and required method {} : {}",
                     request.getHttpMethod(), method, matches);
    }

    return matches;
}
 
Example #30
Source File: Karyon.java    From karyon with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new {@link KaryonServer} that has a single HTTP server instance which delegates all request
 * handling to {@link RequestHandler}.
 * The {@link HttpServer} is created using {@link KaryonTransport#newHttpServer(int, HttpRequestHandler)}
 *
 * @param port Port for the server.
 * @param handler Request Handler
 * @param bootstrapModules Additional bootstrapModules if any.
 *
 * @return {@link KaryonServer} which is to be used to start the created server.
 */
public static KaryonServer forRequestHandler(int port, final RequestHandler<ByteBuf, ByteBuf> handler,
                                             BootstrapModule... bootstrapModules) {
    HttpServer<ByteBuf, ByteBuf> httpServer =
            KaryonTransport.newHttpServer(port, new RequestHandler<ByteBuf, ByteBuf>() {
                @Override
                public Observable<Void> handle(HttpServerRequest<ByteBuf> request,
                                               HttpServerResponse<ByteBuf> response) {
                    return handler.handle(request, response);
                }
            });
    return new RxNettyServerBackedServer(httpServer, bootstrapModules);
}