Search in sources :

Example 6 with RestLiServer

use of com.linkedin.restli.server.RestLiServer in project rest.li by linkedin.

the class TestCharacterEncoding method testQueryParamValueEncoding.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "protocolVersions")
public void testQueryParamValueEncoding(ProtocolVersion protocolVersion) {
    RestLiConfig config = new RestLiConfig();
    config.setResourcePackageNames(QueryParamMockCollection.class.getPackage().getName());
    RestLiServer server = new RestLiServer(config, new PrototypeResourceFactory(), null);
    for (char c = 0; c < 256; ++c) {
        final String testValue = String.valueOf(c);
        GetRequest<EmptyRecord> req = new GetRequestBuilder<String, EmptyRecord>(QueryParamMockCollection.RESOURCE_NAME, EmptyRecord.class, new ResourceSpecImpl(Collections.<ResourceMethod>emptySet(), Collections.<String, DynamicRecordMetadata>emptyMap(), Collections.<String, DynamicRecordMetadata>emptyMap(), String.class, null, null, EmptyRecord.class, Collections.<String, CompoundKey.TypeInfo>emptyMap()), RestliRequestOptions.DEFAULT_OPTIONS).id("dummy").setParam(QueryParamMockCollection.VALUE_KEY, testValue).build();
        RestRequest restRequest = new RestRequestBuilder(RestliUriBuilderUtil.createUriBuilder(req, protocolVersion).build()).setMethod(req.getMethod().getHttpMethod().toString()).build();
        // N.B. since QueryParamMockCollection is implemented using the synchronous rest.li interface,
        // RestLiServer.handleRequest() will invoke the application resource *and* the callback
        // *synchronously*, ensuring that the all instances of the callback are invoked before the
        // loop terminates.
        server.handleRequest(restRequest, new RequestContext(), new Callback<RestResponse>() {

            @Override
            public void onError(Throwable e) {
                Assert.fail();
            }

            @Override
            public void onSuccess(RestResponse result) {
                try {
                    DataMap data = new JacksonDataCodec().readMap(result.getEntity().asInputStream());
                    Assert.assertEquals(data.get(QueryParamMockCollection.VALUE_KEY), testValue);
                    Assert.assertEquals(QueryParamMockCollection._lastQueryParamValue, testValue);
                } catch (IOException e) {
                    Assert.fail();
                }
            }
        });
    }
}
Also used : EmptyRecord(com.linkedin.restli.common.EmptyRecord) JacksonDataCodec(com.linkedin.data.codec.JacksonDataCodec) RestLiServer(com.linkedin.restli.server.RestLiServer) CompoundKey(com.linkedin.restli.common.CompoundKey) RestResponse(com.linkedin.r2.message.rest.RestResponse) IOException(java.io.IOException) DataMap(com.linkedin.data.DataMap) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) PrototypeResourceFactory(com.linkedin.restli.server.resources.PrototypeResourceFactory) ResourceSpecImpl(com.linkedin.restli.common.ResourceSpecImpl) RestLiConfig(com.linkedin.restli.server.RestLiConfig) Test(org.testng.annotations.Test)

Example 7 with RestLiServer

use of com.linkedin.restli.server.RestLiServer in project rest.li by linkedin.

the class TestMultiplexerRunMode method testMultiplexedAsyncGetRequest.

@Test(dataProvider = "multiplexerConfigurations")
public void testMultiplexedAsyncGetRequest(MultiplexerRunMode multiplexerRunMode) throws URISyntaxException, IOException, InterruptedException {
    RestLiConfig config = new RestLiConfig();
    config.addResourcePackageNames("com.linkedin.restli.server.multiplexer.resources");
    config.setMultiplexerRunMode(multiplexerRunMode);
    CountingEngine engine = engine();
    RestLiServer server = new RestLiServer(config, resourceFactory(), engine);
    IndividualRequest r0 = individualRequest("/users/0", null, Collections.<String, IndividualRequest>emptyMap());
    IndividualRequest r1 = individualRequest("/users/1", null, Collections.<String, IndividualRequest>emptyMap());
    IndividualRequest r2 = individualRequest("/users/2", null, ImmutableMap.of("0", r0, "1", r1));
    // request is seq(par(r0, r1), r2)
    RestRequest request = muxRestRequest(ImmutableMap.of("2", r2));
    CountDownLatch latch = new CountDownLatch(1);
    server.handleRequest(request, new RequestContext(), callback(latch));
    assertTrue(latch.await(5, TimeUnit.SECONDS));
    if (multiplexerRunMode == MultiplexerRunMode.SINGLE_PLAN) {
        assertEquals(engine.plansStarted(), 1);
    } else {
        // in MULTIPLE_PLANS mode: 1 task for multiplexed request itself + 3 individual tasks r0, r1, r2
        assertEquals(engine.plansStarted(), 1 + 3);
    }
}
Also used : IndividualRequest(com.linkedin.restli.common.multiplexer.IndividualRequest) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestLiServer(com.linkedin.restli.server.RestLiServer) RequestContext(com.linkedin.r2.message.RequestContext) CountingEngine(com.linkedin.parseq.CountingEngine) CountDownLatch(java.util.concurrent.CountDownLatch) RestLiConfig(com.linkedin.restli.server.RestLiConfig) Test(org.testng.annotations.Test)

Example 8 with RestLiServer

use of com.linkedin.restli.server.RestLiServer in project rest.li by linkedin.

the class RestLiExampleBasicServer method createServer.

public static HttpServer createServer() {
    // create Rest.li resource class information and initialize documentation generator
    // only the resource classes in the specified package names are visible for public
    final RestLiConfig config = new RestLiConfig();
    config.addResourcePackageNames("com.linkedin.restli.example.impl");
    config.setServerNodeUri(URI.create(getServerUrl()));
    config.setDocumentationRequestHandler(new DefaultDocumentationRequestHandler());
    // Create an instance of the Example Filter and add it to the config.
    RestLiExampleFilter filter = new RestLiExampleFilter();
    config.addFilter(filter);
    // demonstrate dynamic dependency injection
    final PhotoDatabase photoDb = new PhotoDatabaseImpl(10);
    final SimpleBeanProvider beanProvider = new SimpleBeanProvider();
    beanProvider.add("photoDb", photoDb);
    beanProvider.add("albumDb", new AlbumDatabaseImpl(10));
    beanProvider.add("albumEntryDb", new AlbumEntryDatabaseImpl(photoDb, 3));
    // using InjectMockResourceFactory to keep examples spring-free
    final ResourceFactory factory = new InjectMockResourceFactory(beanProvider);
    final TransportDispatcher dispatcher = new DelegatingTransportDispatcher(new RestLiServer(config, factory));
    return new HttpServerFactory(FilterChains.empty()).createServer(SERVER_PORT, dispatcher);
}
Also used : HttpServerFactory(com.linkedin.r2.transport.http.server.HttpServerFactory) RestLiServer(com.linkedin.restli.server.RestLiServer) SimpleBeanProvider(com.linkedin.restli.server.mock.SimpleBeanProvider) DelegatingTransportDispatcher(com.linkedin.restli.server.DelegatingTransportDispatcher) DefaultDocumentationRequestHandler(com.linkedin.restli.docgen.DefaultDocumentationRequestHandler) ResourceFactory(com.linkedin.restli.server.resources.ResourceFactory) InjectMockResourceFactory(com.linkedin.restli.server.mock.InjectMockResourceFactory) DelegatingTransportDispatcher(com.linkedin.restli.server.DelegatingTransportDispatcher) TransportDispatcher(com.linkedin.r2.transport.common.bridge.server.TransportDispatcher) PhotoDatabase(com.linkedin.restli.example.impl.PhotoDatabase) AlbumEntryDatabaseImpl(com.linkedin.restli.example.impl.AlbumEntryDatabaseImpl) AlbumDatabaseImpl(com.linkedin.restli.example.impl.AlbumDatabaseImpl) PhotoDatabaseImpl(com.linkedin.restli.example.impl.PhotoDatabaseImpl) InjectMockResourceFactory(com.linkedin.restli.server.mock.InjectMockResourceFactory) RestLiConfig(com.linkedin.restli.server.RestLiConfig)

Aggregations

RestLiServer (com.linkedin.restli.server.RestLiServer)8 RestLiConfig (com.linkedin.restli.server.RestLiConfig)7 RequestContext (com.linkedin.r2.message.RequestContext)6 RestRequest (com.linkedin.r2.message.rest.RestRequest)5 Test (org.testng.annotations.Test)5 Engine (com.linkedin.parseq.Engine)3 RestResponse (com.linkedin.r2.message.rest.RestResponse)3 TransportDispatcher (com.linkedin.r2.transport.common.bridge.server.TransportDispatcher)3 HttpServerFactory (com.linkedin.r2.transport.http.server.HttpServerFactory)3 DelegatingTransportDispatcher (com.linkedin.restli.server.DelegatingTransportDispatcher)3 InjectMockResourceFactory (com.linkedin.restli.server.mock.InjectMockResourceFactory)3 ResourceFactory (com.linkedin.restli.server.resources.ResourceFactory)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Trace (com.linkedin.parseq.trace.Trace)2 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)2 IndividualRequest (com.linkedin.restli.common.multiplexer.IndividualRequest)2 DefaultDocumentationRequestHandler (com.linkedin.restli.docgen.DefaultDocumentationRequestHandler)2 SimpleBeanProvider (com.linkedin.restli.server.mock.SimpleBeanProvider)2 IOException (java.io.IOException)2 DataMap (com.linkedin.data.DataMap)1