Search in sources :

Example 1 with SimpleBeanProvider

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

the class RestLiIntTestServer method createServer.

public static HttpServer createServer(final Engine engine, int port, boolean useAsyncServletApi, int asyncTimeOut, List<? extends Filter> filters, final FilterChain filterChain, final boolean forceUseRestServer) {
    RestLiConfig config = new RestLiConfig();
    config.addResourcePackageNames(RESOURCE_PACKAGE_NAMES);
    config.setServerNodeUri(URI.create("http://localhost:" + port));
    config.setDocumentationRequestHandler(new DefaultDocumentationRequestHandler());
    config.addDebugRequestHandlers(new ParseqTraceDebugRequestHandler());
    config.setFilters(filters);
    GroupMembershipMgr membershipMgr = new HashGroupMembershipMgr();
    GroupMgr groupMgr = new HashMapGroupMgr(membershipMgr);
    GroupsRestApplication app = new GroupsRestApplication(groupMgr, membershipMgr);
    SimpleBeanProvider beanProvider = new SimpleBeanProvider();
    beanProvider.add("GroupsRestApplication", app);
    //using InjectMockResourceFactory to keep examples spring-free
    ResourceFactory factory = new InjectMockResourceFactory(beanProvider);
    //Todo this will have to change further to accomodate streaming tests - this is temporary
    final TransportDispatcher dispatcher;
    if (forceUseRestServer) {
        dispatcher = new DelegatingTransportDispatcher(new RestLiServer(config, factory, engine));
    } else {
        final StreamRequestHandler streamRequestHandler = new RestLiServer(config, factory, engine);
        dispatcher = new TransportDispatcher() {

            @Override
            public void handleRestRequest(RestRequest req, Map<String, String> wireAttrs, RequestContext requestContext, TransportCallback<RestResponse> callback) {
                throw new UnsupportedOperationException("This server only accepts streaming");
            }

            @Override
            public void handleStreamRequest(StreamRequest req, Map<String, String> wireAttrs, RequestContext requestContext, TransportCallback<StreamResponse> callback) {
                try {
                    streamRequestHandler.handleRequest(req, requestContext, new TransportCallbackAdapter<>(callback));
                } catch (Exception e) {
                    final Exception ex = RestException.forError(RestStatus.INTERNAL_SERVER_ERROR, e);
                    callback.onResponse(TransportResponseImpl.<StreamResponse>error(ex));
                }
            }
        };
    }
    return new HttpServerFactory(filterChain).createServer(port, HttpServerFactory.DEFAULT_CONTEXT_PATH, HttpServerFactory.DEFAULT_THREAD_POOL_SIZE, dispatcher, useAsyncServletApi ? HttpJettyServer.ServletType.ASYNC_EVENT : HttpJettyServer.ServletType.RAP, asyncTimeOut, !forceUseRestServer);
}
Also used : HttpServerFactory(com.linkedin.r2.transport.http.server.HttpServerFactory) TransportCallbackAdapter(com.linkedin.r2.transport.common.bridge.server.TransportCallbackAdapter) DelegatingTransportDispatcher(com.linkedin.restli.server.DelegatingTransportDispatcher) DefaultDocumentationRequestHandler(com.linkedin.restli.docgen.DefaultDocumentationRequestHandler) DelegatingTransportDispatcher(com.linkedin.restli.server.DelegatingTransportDispatcher) TransportDispatcher(com.linkedin.r2.transport.common.bridge.server.TransportDispatcher) ParseqTraceDebugRequestHandler(com.linkedin.restli.server.ParseqTraceDebugRequestHandler) HashGroupMembershipMgr(com.linkedin.restli.examples.groups.server.impl.HashGroupMembershipMgr) GroupMembershipMgr(com.linkedin.restli.examples.groups.server.api.GroupMembershipMgr) HashMapGroupMgr(com.linkedin.restli.examples.groups.server.impl.HashMapGroupMgr) GroupsRestApplication(com.linkedin.restli.examples.groups.server.rest.impl.GroupsRestApplication) RequestContext(com.linkedin.r2.message.RequestContext) RestLiServer(com.linkedin.restli.server.RestLiServer) SimpleBeanProvider(com.linkedin.restli.server.mock.SimpleBeanProvider) RestResponse(com.linkedin.r2.message.rest.RestResponse) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) InjectMockResourceFactory(com.linkedin.restli.server.mock.InjectMockResourceFactory) ResourceFactory(com.linkedin.restli.server.resources.ResourceFactory) RestException(com.linkedin.r2.message.rest.RestException) IOException(java.io.IOException) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) HashMapGroupMgr(com.linkedin.restli.examples.groups.server.impl.HashMapGroupMgr) GroupMgr(com.linkedin.restli.examples.groups.server.api.GroupMgr) StreamRequestHandler(com.linkedin.r2.transport.common.StreamRequestHandler) RestRequest(com.linkedin.r2.message.rest.RestRequest) InjectMockResourceFactory(com.linkedin.restli.server.mock.InjectMockResourceFactory) HashGroupMembershipMgr(com.linkedin.restli.examples.groups.server.impl.HashGroupMembershipMgr) RestLiConfig(com.linkedin.restli.server.RestLiConfig)

Example 2 with SimpleBeanProvider

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

the class TestPhotoResource method initResource.

// function annotated with @BeforeMethod will run once before each test starts
@BeforeMethod
public void initResource() {
    // the photo resource requires dependency injection to work
    // we use InjectResourceFactory from pegasus to manually inject the dependency
    SimpleBeanProvider beanProvider = new SimpleBeanProvider();
    final PhotoDatabase photoDb = new PhotoDatabaseImpl(10);
    beanProvider.add("photoDb", photoDb);
    beanProvider.add("albumDb", new AlbumDatabaseImpl(10));
    beanProvider.add("albumEntryDb", new AlbumEntryDatabaseImpl(photoDb, 3));
    final InjectResourceFactory factory = new InjectResourceFactory(beanProvider);
    final Map<String, ResourceModel> pathRootResourceMap = buildResourceModels(PhotoResource.class);
    factory.setRootResources(pathRootResourceMap);
    _res = factory.create(PhotoResource.class);
    Assert.assertNotNull(_res);
    Assert.assertNotNull(_res.getDb());
}
Also used : SimpleBeanProvider(com.linkedin.restli.server.mock.SimpleBeanProvider) InjectResourceFactory(com.linkedin.restli.server.resources.InjectResourceFactory) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 3 with SimpleBeanProvider

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

the class TestAlbumEntryResource method initResource.

// function annotated with @BeforeMethod will run once before any test starts
@BeforeMethod
public void initResource() {
    // the photo resource requires dependency injection to work
    // we use InjectResourceFactory from pegasus to manually inject the dependency
    SimpleBeanProvider beanProvider = new SimpleBeanProvider();
    final PhotoDatabase photoDb = new PhotoDatabaseImpl(10);
    beanProvider.add("photoDb", photoDb);
    beanProvider.add("albumDb", new AlbumDatabaseImpl(10));
    beanProvider.add("albumEntryDb", new AlbumEntryDatabaseImpl(photoDb, 3));
    final InjectResourceFactory factory = new InjectResourceFactory(beanProvider);
    final Map<String, ResourceModel> pathRootResourceMap = buildResourceModels(PhotoResource.class, AlbumResource.class, AlbumEntryResource.class);
    factory.setRootResources(pathRootResourceMap);
    _photoRes = factory.create(PhotoResource.class);
    Assert.assertNotNull(_photoRes);
    Assert.assertNotNull(_photoRes.getDb());
    _albumRes = factory.create(AlbumResource.class);
    Assert.assertNotNull(_albumRes);
    Assert.assertNotNull(_albumRes.getDb());
    _entryRes = factory.create(AlbumEntryResource.class);
    Assert.assertNotNull(_entryRes);
    makeData();
}
Also used : SimpleBeanProvider(com.linkedin.restli.server.mock.SimpleBeanProvider) InjectResourceFactory(com.linkedin.restli.server.resources.InjectResourceFactory) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 4 with SimpleBeanProvider

use of com.linkedin.restli.server.mock.SimpleBeanProvider 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

SimpleBeanProvider (com.linkedin.restli.server.mock.SimpleBeanProvider)4 TransportDispatcher (com.linkedin.r2.transport.common.bridge.server.TransportDispatcher)2 HttpServerFactory (com.linkedin.r2.transport.http.server.HttpServerFactory)2 DefaultDocumentationRequestHandler (com.linkedin.restli.docgen.DefaultDocumentationRequestHandler)2 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)2 DelegatingTransportDispatcher (com.linkedin.restli.server.DelegatingTransportDispatcher)2 RestLiConfig (com.linkedin.restli.server.RestLiConfig)2 RestLiServer (com.linkedin.restli.server.RestLiServer)2 InjectMockResourceFactory (com.linkedin.restli.server.mock.InjectMockResourceFactory)2 InjectResourceFactory (com.linkedin.restli.server.resources.InjectResourceFactory)2 ResourceFactory (com.linkedin.restli.server.resources.ResourceFactory)2 BeforeMethod (org.testng.annotations.BeforeMethod)2 RequestContext (com.linkedin.r2.message.RequestContext)1 RestException (com.linkedin.r2.message.rest.RestException)1 RestRequest (com.linkedin.r2.message.rest.RestRequest)1 RestResponse (com.linkedin.r2.message.rest.RestResponse)1 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)1 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)1 StreamRequestHandler (com.linkedin.r2.transport.common.StreamRequestHandler)1 TransportCallbackAdapter (com.linkedin.r2.transport.common.bridge.server.TransportCallbackAdapter)1