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);
}
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;
}
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();
}
}
});
}
}
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;
}
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;
}
Aggregations