Search in sources :

Example 1 with PrototypeResourceFactory

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

the class RestliServlet method buildR2ServletFromServletParams.

private AbstractR2Servlet buildR2ServletFromServletParams(ServletConfig servletConfig) {
    ResourceFactory resourceFactory = new PrototypeResourceFactory();
    RestLiConfig config = new RestLiConfig();
    config.setResourcePackageNamesSet(getResourcePackageSet(servletConfig));
    final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(getParseqThreadPoolSize(servletConfig));
    Engine engine = new EngineBuilder().setTaskExecutor(scheduler).setTimerScheduler(scheduler).build();
    DelegatingTransportDispatcher dispatcher = new DelegatingTransportDispatcher(new RestLiServer(config, resourceFactory, engine));
    boolean useAsync = getUseAsync(servletConfig);
    long asyncTimeOut = getAsyncTimeout(servletConfig);
    if (useAsync && servletConfig.getServletContext().getMajorVersion() < 3) {
        throw new IllegalArgumentException("This servlet is configured with useAsync=true, but the current servlet " + "context does not support the required Servlet API 3.0.");
    }
    if (!useAsync) {
        log.info("Initializing Rest.li with a thread based request handling.  Set useAsync=true on a Servlet API 3.0 container to enable Rest.li's async servlet.");
        return new RAPServlet(dispatcher);
    } else {
        log.info("Initializing Rest.li with an async request handling enabled.");
        return new AsyncR2Servlet(dispatcher, asyncTimeOut);
    }
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) RAPServlet(com.linkedin.r2.transport.http.server.RAPServlet) ResourceFactory(com.linkedin.restli.server.resources.ResourceFactory) PrototypeResourceFactory(com.linkedin.restli.server.resources.PrototypeResourceFactory) AsyncR2Servlet(com.linkedin.r2.transport.http.server.AsyncR2Servlet) PrototypeResourceFactory(com.linkedin.restli.server.resources.PrototypeResourceFactory) EngineBuilder(com.linkedin.parseq.EngineBuilder) Engine(com.linkedin.parseq.Engine)

Example 2 with PrototypeResourceFactory

use of com.linkedin.restli.server.resources.PrototypeResourceFactory 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)

Aggregations

PrototypeResourceFactory (com.linkedin.restli.server.resources.PrototypeResourceFactory)2 DataMap (com.linkedin.data.DataMap)1 JacksonDataCodec (com.linkedin.data.codec.JacksonDataCodec)1 Engine (com.linkedin.parseq.Engine)1 EngineBuilder (com.linkedin.parseq.EngineBuilder)1 RequestContext (com.linkedin.r2.message.RequestContext)1 RestRequest (com.linkedin.r2.message.rest.RestRequest)1 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)1 RestResponse (com.linkedin.r2.message.rest.RestResponse)1 AsyncR2Servlet (com.linkedin.r2.transport.http.server.AsyncR2Servlet)1 RAPServlet (com.linkedin.r2.transport.http.server.RAPServlet)1 CompoundKey (com.linkedin.restli.common.CompoundKey)1 EmptyRecord (com.linkedin.restli.common.EmptyRecord)1 ResourceSpecImpl (com.linkedin.restli.common.ResourceSpecImpl)1 RestLiConfig (com.linkedin.restli.server.RestLiConfig)1 RestLiServer (com.linkedin.restli.server.RestLiServer)1 ResourceFactory (com.linkedin.restli.server.resources.ResourceFactory)1 IOException (java.io.IOException)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 Test (org.testng.annotations.Test)1