Search in sources :

Example 6 with RestLiConfig

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

the class MockHttpServerFactory method create.

/**
   * Creates a {@link HttpServer} that contains a {@link RestLiServer} to be used for testing a set of Rest.li
   * resources.
   *
   * The {@link HttpServer} uses an empty {@link FilterChain} and uses "/" as the context path.
   *
   * If the server is run in async mode (by calling this function with the last parameter {@code true}), the
   * timeout used is {@link #ASYNC_TIMEOUT}.
   *
   * Both the async and sync servers will use {@link #NUM_THREADS} threads.
   *
   * The server is started by calling {@link com.linkedin.r2.transport.http.server.HttpServer#start()}
   * The server is stopped by calling {@link com.linkedin.r2.transport.http.server.HttpServer#stop()}
   *
   * @param port the port the server will run on on localhost
   * @param resourcePackageNames the names of the packages that contain the Rest.li resources that you wish to serve
   * @param beans beans you want to inject into your Rest.li resource.
   * @param enableAsync true if the server should be async, false otherwise
   * @return a {@link HttpServer} created with the above parameters
   */
public static HttpServer create(int port, String[] resourcePackageNames, Map<String, Object> beans, boolean enableAsync) {
    RestLiConfig config = createConfig(port);
    config.addResourcePackageNames(resourcePackageNames);
    return create(port, config, beans, enableAsync);
}
Also used : RestLiConfig(com.linkedin.restli.server.RestLiConfig)

Example 7 with RestLiConfig

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

the class MockHttpServerFactory method createConfig.

/**
   * Creates {@link RestLiConfig} to be used by a {@link RestLiServer}
   *
   * @param port the port the server will run on
   * @return
   */
private static RestLiConfig createConfig(int port) {
    RestLiConfig restLiConfig = new RestLiConfig();
    restLiConfig.setServerNodeUri(URI.create(LOCALHOST + port));
    restLiConfig.setDocumentationRequestHandler(new DefaultDocumentationRequestHandler());
    return restLiConfig;
}
Also used : DefaultDocumentationRequestHandler(com.linkedin.restli.docgen.DefaultDocumentationRequestHandler) RestLiConfig(com.linkedin.restli.server.RestLiConfig)

Example 8 with RestLiConfig

use of com.linkedin.restli.server.RestLiConfig 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 9 with RestLiConfig

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

the class RestLiResourceModelExporter method export.

/**
   * @param apiName the name of the API
   * @param classpath classpath to to load the resources. this is purely for Javadoc Doclet {@link RestLiDoclet}
   * @param sourcePaths paths to scan for resource Java source files. this is purely for Javadoc Doclet {@link RestLiDoclet}
   *                    if both resourcePackages and resourceClasses is null, all classes defined in the directories will be scanned
   * @param resourcePackages packages to scan for resources
   * @param resourceClasses specific classes as resources
   * @param outdir directory in which to output the IDL files
   * @param additionalDocProviders names of additional classes in the classpath that implement DocsProvider, if empty,
   *                      only javadoc will be supported.
   * @return a result that includes collection of files generated and modified. Note: getSourceFiles() on the result
   * will always return an empty List as the code generation operates on classpaths and the ClassLoader and not files.
   * @throws IOException could be {@link java.io.FileNotFoundException} if unable to write the output file,
   *                     otherwise, {@link IOException} if failure happened when writing the output file
   */
public GeneratorResult export(String apiName, String[] classpath, String[] sourcePaths, String[] resourcePackages, String[] resourceClasses, String outdir, List<DocsProvider> additionalDocProviders) throws IOException {
    final RestLiConfig config = new RestLiConfig();
    if (resourcePackages != null) {
        config.addResourcePackageNames(resourcePackages);
    }
    final Map<String, String> classFileNames = new HashMap<String, String>();
    for (String path : sourcePaths) {
        classFileNames.putAll(FileClassNameScanner.scan(path));
    }
    Collection<String> sourceFileNames = null;
    if (resourceClasses != null || resourcePackages == null) {
        if (resourceClasses != null) {
            config.addResourceClassNames(resourceClasses);
            sourceFileNames = new ArrayList<String>(resourceClasses.length);
            for (String resourceClass : resourceClasses) {
                final String resourceFileName = classFileNames.get(resourceClass);
                if (resourceFileName == null) {
                    log.warn("Unable to find source file for class " + resourceClass + " .  No documentation will be generated for it.");
                } else {
                    sourceFileNames.add(resourceFileName);
                }
            }
        } else {
            config.addResourceClassNames(classFileNames.keySet());
            sourceFileNames = classFileNames.values();
        }
    }
    log.info("Executing Rest.li annotation processor...");
    final RestLiApiBuilder apiBuilder = new RestLiApiBuilder(config);
    final Map<String, ResourceModel> rootResourceMap = apiBuilder.build();
    if (rootResourceMap.isEmpty()) {
        return new Result();
    }
    // We always include the doc provider for javadoc
    DocsProvider javadocProvider = new DocletDocsProvider(apiName, classpath, sourcePaths, resourcePackages);
    DocsProvider docsProvider;
    if (additionalDocProviders == null || additionalDocProviders.isEmpty()) {
        docsProvider = javadocProvider;
    } else {
        // dynamically load doc providers for additional language, if available
        List<DocsProvider> languageSpecificDocsProviders = new ArrayList<DocsProvider>();
        languageSpecificDocsProviders.add(javadocProvider);
        languageSpecificDocsProviders.addAll(MultiLanguageDocsProvider.loadExternalProviders(additionalDocProviders));
        docsProvider = new MultiLanguageDocsProvider(languageSpecificDocsProviders);
    }
    log.info("Registering source files with doc providers...");
    docsProvider.registerSourceFiles(classFileNames.values());
    log.info("Exporting IDL files...");
    final GeneratorResult result = generateIDLFiles(apiName, outdir, rootResourceMap, docsProvider);
    log.info("Done!");
    return result;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) GeneratorResult(com.linkedin.pegasus.generator.GeneratorResult) DocsProvider(com.linkedin.restli.internal.server.model.ResourceModelEncoder.DocsProvider) RestLiApiBuilder(com.linkedin.restli.internal.server.model.RestLiApiBuilder) GeneratorResult(com.linkedin.pegasus.generator.GeneratorResult) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) RestLiConfig(com.linkedin.restli.server.RestLiConfig)

Example 10 with RestLiConfig

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

the class RestLiSnapshotExporter method export.

public GeneratorResult export(String apiName, String[] classpath, String[] sourcePaths, String[] resourcePackages, String[] resourceClasses, String outdir, List<DocsProvider> additionalDocProviders) throws IOException {
    final RestLiConfig config = new RestLiConfig();
    if (resourcePackages != null) {
        config.addResourcePackageNames(resourcePackages);
    }
    final Map<String, String> classFileNames = new HashMap<String, String>();
    for (String path : sourcePaths) {
        classFileNames.putAll(FileClassNameScanner.scan(path));
    }
    Collection<String> sourceFileNames = null;
    if (resourceClasses != null || resourcePackages == null) {
        if (resourceClasses != null) {
            config.addResourceClassNames(resourceClasses);
            sourceFileNames = new ArrayList<String>(resourceClasses.length);
            for (String resourceClass : resourceClasses) {
                final String resourceFileName = classFileNames.get(resourceClass);
                if (resourceFileName == null) {
                    log.warn("Unable to find source file for class " + resourceClass + " .  No Javadoc will be generated for it.");
                } else {
                    sourceFileNames.add(resourceFileName);
                }
            }
        } else {
            config.addResourceClassNames(classFileNames.keySet());
        }
    }
    log.info("Executing Rest.li annotation processor...");
    final RestLiApiBuilder apiBuilder = new RestLiApiBuilder(config);
    final Map<String, ResourceModel> rootResourceMap = apiBuilder.build();
    if (rootResourceMap.isEmpty()) {
        return new SnapshotResult();
    }
    // We always include the doc provider for javadoc
    DocsProvider javadocProvider = new DocletDocsProvider(apiName, classpath, sourcePaths, resourcePackages);
    DocsProvider docsProvider;
    if (additionalDocProviders == null || additionalDocProviders.isEmpty()) {
        docsProvider = javadocProvider;
    } else {
        // dynamically load doc providers for additional language, if available
        List<DocsProvider> languageSpecificDocsProviders = new ArrayList<DocsProvider>();
        languageSpecificDocsProviders.add(javadocProvider);
        languageSpecificDocsProviders.addAll(MultiLanguageDocsProvider.loadExternalProviders(additionalDocProviders));
        docsProvider = new MultiLanguageDocsProvider(languageSpecificDocsProviders);
    }
    log.info("Registering source files with doc providers...");
    docsProvider.registerSourceFiles(classFileNames.values());
    log.info("Exporting snapshot files...");
    final GeneratorResult result = generateSnapshotFiles(apiName, outdir, rootResourceMap, docsProvider);
    log.info("Done!");
    return result;
}
Also used : MultiLanguageDocsProvider(com.linkedin.restli.tools.idlgen.MultiLanguageDocsProvider) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DocletDocsProvider(com.linkedin.restli.tools.idlgen.DocletDocsProvider) MultiLanguageDocsProvider(com.linkedin.restli.tools.idlgen.MultiLanguageDocsProvider) DocsProvider(com.linkedin.restli.internal.server.model.ResourceModelEncoder.DocsProvider) RestLiApiBuilder(com.linkedin.restli.internal.server.model.RestLiApiBuilder) DocletDocsProvider(com.linkedin.restli.tools.idlgen.DocletDocsProvider) GeneratorResult(com.linkedin.pegasus.generator.GeneratorResult) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) RestLiConfig(com.linkedin.restli.server.RestLiConfig)

Aggregations

RestLiConfig (com.linkedin.restli.server.RestLiConfig)12 RestLiServer (com.linkedin.restli.server.RestLiServer)7 RequestContext (com.linkedin.r2.message.RequestContext)6 RestRequest (com.linkedin.r2.message.rest.RestRequest)5 Test (org.testng.annotations.Test)5 RestResponse (com.linkedin.r2.message.rest.RestResponse)3 DefaultDocumentationRequestHandler (com.linkedin.restli.docgen.DefaultDocumentationRequestHandler)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Engine (com.linkedin.parseq.Engine)2 Trace (com.linkedin.parseq.trace.Trace)2 GeneratorResult (com.linkedin.pegasus.generator.GeneratorResult)2 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)2 TransportDispatcher (com.linkedin.r2.transport.common.bridge.server.TransportDispatcher)2 HttpServerFactory (com.linkedin.r2.transport.http.server.HttpServerFactory)2 IndividualRequest (com.linkedin.restli.common.multiplexer.IndividualRequest)2 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)2 DocsProvider (com.linkedin.restli.internal.server.model.ResourceModelEncoder.DocsProvider)2 RestLiApiBuilder (com.linkedin.restli.internal.server.model.RestLiApiBuilder)2 DelegatingTransportDispatcher (com.linkedin.restli.server.DelegatingTransportDispatcher)2 InjectMockResourceFactory (com.linkedin.restli.server.mock.InjectMockResourceFactory)2