com.google.api.server.spi.discovery.AbstractDiscoveryProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of endpoints-framework Show documentation
Show all versions of endpoints-framework Show documentation
A framework for building RESTful web APIs.
The newest version!
package com.google.api.server.spi.discovery;
import com.google.api.server.spi.config.model.ApiConfig;
import com.google.api.server.spi.config.model.ApiKey;
import com.google.api.server.spi.response.NotFoundException;
import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.flogger.FluentLogger;
/**
* Base class for providing discovery data.
*/
abstract class AbstractDiscoveryProvider implements DiscoveryProvider {
protected static final FluentLogger logger = FluentLogger.forEnclosingClass();
private static final Function CONFIG_TO_ROOTLESS_KEY =
new Function() {
@Override public ApiKey apply(ApiConfig config) {
return new ApiKey(config.getName(), config.getVersion(), null /* root */);
}
};
private final ImmutableList apiConfigs;
private final ImmutableListMultimap configsByKey;
AbstractDiscoveryProvider(ImmutableList apiConfigs) {
this.apiConfigs = apiConfigs;
this.configsByKey = FluentIterable.from(apiConfigs).index(CONFIG_TO_ROOTLESS_KEY);
}
ImmutableList getAllApiConfigs() {
return apiConfigs;
}
ImmutableList getApiConfigs(String name, String version)
throws NotFoundException {
ApiKey key = new ApiKey(name, version, null /* root */);
ImmutableList configs = configsByKey.get(key);
if (configs.isEmpty()) {
logger.atInfo().log("No configuration found for name: %s, version: %s", name, version);
throw new NotFoundException("Not Found");
}
return configs;
}
static Iterable rewriteConfigsWithRoot(Iterable configs,
String root) {
ApiConfig.Factory factory = new ApiConfig.Factory();
return FluentIterable.from(configs).transform(new RootRemapperFunction(root, factory));
}
private static class RootRemapperFunction implements Function {
private final String root;
private final ApiConfig.Factory factory;
RootRemapperFunction(String root, ApiConfig.Factory factory) {
this.root = root;
this.factory = factory;
}
@Override public ApiConfig apply(ApiConfig input) {
ApiConfig copy = factory.copy(input);
copy.setRoot(root);
return copy;
}
}
}