public NetworkingParams(BasicConfig cfg) { address = cfg.entry("address").or("0.0.0.0"); port = cfg.entry("port").or(8080); workers = cfg.entry("workers").or(Runtime.getRuntime().availableProcessors()); bufSizeKB = cfg.entry("bufSizeKB").or(16); noDelay = cfg.entry("noDelay").or(false); maxPipeline = cfg.entry("maxPipeline").or(0); syncBufs = cfg.entry("syncBufs").or(true); blockingAccept = cfg.entry("blockingAccept").or(false); }
public static synchronized boolean scan(String... packages) { String appPath = Conf.APP.entry("path").str().getOrNull(); if (U.notEmpty(appPath)) { packages = appPath.split("\\s*,\\s*"); App.path(packages); } List<Class<?>> beans = App.findBeans(packages); beans(beans.toArray()); return !beans.isEmpty(); }
@Override public String generateState(Value<String> clientSecret, String sessionId) { if (OAUTH.is("stateless")) { return "OK"; } String rnd = Rnd.rndStr(10); String hash = Crypto.sha512(clientSecret.get() + rnd); return rnd + "_" + hash; }
public void configure() { url(config.entry("url").str().getOrNull()); username(config.entry("username").str().getOrNull()); password(config.entry("password").str().getOrNull()); driver(config.entry("driver").str().getOrNull()); poolProvider(config.entry("poolProvider").or(DEFAULT_POOL_PROVIDER)); if (U.isEmpty(driver) && U.notEmpty(url)) { driver(inferDriverFromUrl(url)); } }
private static void activateRootConfig() { U.must(Env.isInitialized()); String root = Env.root(); if (U.notEmpty(root) && !APP.has("jar")) { APP.set("jar", Msc.path(root, "app.jar")); } String appJar = APP.entry("jar").str().getOrNull(); if (U.notEmpty(appJar)) { ClasspathUtil.appJar(appJar); } boolean fancyByDefault = Env.dev() || System.console() != null; Log.options().fancy(LOG.entry("fancy").bool().or(fancyByDefault)); LogLevel logLevel = LOG.entry("level").to(LogLevel.class).getOrNull(); if (logLevel != null && !Env.test()) { Log.setLogLevel(logLevel); } if (GlobalCfg.quiet()) { Log.setLogLevel(LogLevel.ERROR); // overwrite the configured log level in quiet mode } }
@SuppressWarnings("unchecked") @Override public Object call(Method m, Object[] args) { U.must(!config.isEmpty(), "Cannot find configuration for the REST client interface: %s", clientInterface.getSimpleName()); Config cfg = config.sub(m.getName()); String verb = verbOf(cfg); String url = cfg.entry(verb).str().get(); U.must(!U.isEmpty(verb), "The [verb: url] entry is not configured for the method: %s", m); U.must(!U.isEmpty(url), "Cannot find 'url' configuration for the method: %s", m); Class<Object> retType = (Class<Object>) m.getReturnType(); Class<?>[] paramTypes = m.getParameterTypes(); Class<?> lastParamType = U.last(paramTypes); if (lastParamType != null && Callback.class.isAssignableFrom(lastParamType)) { // async result with callback U.must(retType.equals(void.class) || Future.class.isAssignableFrom(retType) || org.rapidoid.concurrent.Future.class.isAssignableFrom(retType)); Callback<Object> callback = (Callback<Object>) U.last(args); U.notNull(callback, "callback"); args = Arr.sub(args, 0, -1); String realUrl = String.format(url, args); OfType ofType = Metadata.get(U.last(m.getParameterAnnotations()), OfType.class); Class<Object> resultType = (Class<Object>) (ofType != null ? ofType.value() : Object.class); return REST.call(verb, realUrl, resultType, callback); } else { String realUrl = String.format(url, args); return REST.call(verb, realUrl, retType); } }
private void initScreen(Screen screen) { BasicConfig zone = HttpUtils.zone(req); String brand = zone.entry("brand").str().getOrNull(); String title = zone.entry("title").str().getOrNull(); String siteName = req.host(); if (U.isEmpty(siteName) || siteName.equals("localhost") || siteName.startsWith("localhost:") || siteName.equals("127.0.0.1") || siteName.startsWith("127.0.0.1:")) { siteName = "Rapidoid"; } screen.brand(U.or(brand, siteName)); screen.title(U.or(title, siteName)); screen.home(zone.entry("home").str().or("/")); screen.search(zone.entry("search").bool().or(false)); screen.navbar(zone.entry("navbar").bool().or(brand != null)); screen.fluid(zone.entry("fluid").bool().or(false)); String cdn = zone.entry("cdn").or("auto"); if (!"auto".equalsIgnoreCase(cdn)) { screen.cdn(Cls.bool(cdn)); } if (zone.has("menu")) { screen.menu(zone.sub("menu").toMap()); } }
public void configure() { url(config.entry("url").str().getOrNull()); username(config.entry("username").str().getOrNull()); password(config.entry("password").str().getOrNull()); driver(config.entry("driver").str().getOrNull()); poolProvider(config.entry("poolProvider").or(DEFAULT_POOL_PROVIDER)); if (U.isEmpty(driver) && U.notEmpty(url)) { driver(inferDriverFromUrl(url)); } }
@Override protected T retrieve() { return Cls.convert(src.getOrNull(), type); }
public static String getLoginURL(Req req, OAuthProvider provider, String oauthDomain) { if (OAUTH.isEmpty()) { Log.warn("OAuth is not configured!"); } String name = provider.getName().toLowerCase(); Config providerConfig = OAUTH.sub(name); Value<String> clientId = providerConfig.entry("clientId").str(); Value<String> clientSecret = providerConfig.entry("clientSecret").str(); String callbackPath = Msc.specialUri(name + "OauthCallback"); boolean popup = req.param("popup", null) != null; String redirectUrl = U.notEmpty(oauthDomain) ? oauthDomain + callbackPath : HttpUtils.constructUrl(req, callbackPath); String statePrefix = popup ? "P" : "N"; String state = statePrefix + STATE_CHECK.generateState(clientSecret, req.sessionId()); try { OAuthClientRequest request = OAuthClientRequest.authorizationLocation(provider.getAuthEndpoint()) .setClientId(clientId.str().get()).setRedirectURI(redirectUrl).setScope(provider.getEmailScope()) .setState(state).setResponseType("code").buildQueryMessage(); return request.getLocationUri(); } catch (OAuthSystemException e) { throw U.rte(e); } }
public static synchronized int cpus() { return Conf.ROOT.entry("cpus").or(Runtime.getRuntime().availableProcessors()); }
@SuppressWarnings("unchecked") private <T> T getInjectableByName(Object target, Class<T> type, String name, Map<String, Object> properties, boolean useConfig) { Object instance = properties != null ? properties.get(name) : null; if (instance == null && target != null && useConfig) { Config config = Conf.section(target.getClass()); if (type.equals(Boolean.class) || type.equals(boolean.class)) { instance = config.is(name); } else { String opt = config.entry(name).str().getOrNull(); if (opt != null) { instance = Cls.convert(opt, type); } } } return (T) instance; }
@Override public List<String> list() { String s = str().getOrNull(); return s != null ? U.list(s.split(",")) : U.list(); }
private static ThreadPoolExecutor newExecutor() { int threads = CONFIG.entry("threads").or(64); int maxThreads = CONFIG.entry("maxThreads").or(1024); int maxQueueSize = CONFIG.entry("maxQueueSize").or(1000000); BlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(maxQueueSize); return new ThreadPoolExecutor(threads, maxThreads, 300, TimeUnit.SECONDS, queue, new RapidoidThreadFactory("executor", true)); }
private static synchronized void initSecret() { String secret = Conf.ROOT.entry("secret").str().getOrNull(); char[] src; if (secret == null) { Log.warn("!Application secret was not specified, generating random secret!"); src = Str.toHex(randomBytes(128)).toCharArray(); } else { src = secret.toCharArray(); } secretKey = CryptoKey.from(src); }
@Override public Object execute(Req x) { String domain = this.domain.getOrNull(); String loginURL = OAuth.getLoginURL(x, provider, domain); return x.response().redirect(loginURL); }
public static synchronized String id() { if (state.uid == null) { state.uid = Conf.ROOT.entry("id").or(processName()); } return state.uid; }