private static boolean determineWhetherToUseStrictArgumentExtractorMode(NinjaProperties ninjaProperties) { boolean useStrictArgumentExtractors = ninjaProperties.getBooleanWithDefault(NinjaConstant.NINJA_STRICT_ARGUMENT_EXTRACTORS, false); if (useStrictArgumentExtractors == false && nonStrictModeWarningLoggedAlready == false) { String message = "Using deprecated non-strict mode for injection of parameters into controller " + "(" + NinjaConstant.NINJA_STRICT_ARGUMENT_EXTRACTORS + " = false). " + "This mode will soon be removed from Ninja. Make sure you upgrade your application as soon as possible. " + "More: http://www.ninjaframework.org/documentation/basic_concepts/controllers.html 'A note about null and Optional'."; logger.warn(message); nonStrictModeWarningLoggedAlready = true; } return useStrictArgumentExtractors; }
/** * Whether diagnostics are enabled. If enabled then the default system/views * will be skipped and a detailed diagnostic error result will be returned * by the various methods in this class. You get precise feedback where * an error occurred including original source code. * * @return True if diagnostics are enabled otherwise false. */ public boolean isDiagnosticsEnabled() { // extra safety: only disable detailed diagnostic error pages // if both in DEV mode and diagnostics are enabled 0 return ninjaProperties.isDev() && ninjaProperties.getBooleanWithDefault(NinjaConstant.DIAGNOSTICS_KEY_NAME, Boolean.TRUE); }
@Inject public SessionImpl(Crypto crypto, CookieEncryption encryption, NinjaProperties ninjaProperties, Clock clock) { this.crypto = crypto; this.encryption = encryption; this.time = clock; // read configuration stuff: Integer sessionExpireTimeInSeconds = ninjaProperties .getInteger(NinjaConstant.sessionExpireTimeInSeconds); if (sessionExpireTimeInSeconds != null) { this.defaultSessionExpireTimeInMs = sessionExpireTimeInSeconds * 1000L; } else { this.defaultSessionExpireTimeInMs = null; } this.sessionExpireTimeInMs = defaultSessionExpireTimeInMs; this.sessionSendOnlyIfChanged = ninjaProperties.getBooleanWithDefault( NinjaConstant.sessionSendOnlyIfChanged, true); this.sessionTransferredOverHttpsOnly = ninjaProperties .getBooleanWithDefault( NinjaConstant.sessionTransferredOverHttpsOnly, true); this.sessionHttpOnly = ninjaProperties.getBooleanWithDefault( NinjaConstant.sessionHttpOnly, true); this.applicationCookieDomain = ninjaProperties .get(NinjaConstant.applicationCookieDomain); String applicationCookiePrefix = ninjaProperties .getOrDie(NinjaConstant.applicationCookiePrefix); this.sessionCookieName = applicationCookiePrefix + ninja.utils.NinjaConstant.SESSION_SUFFIX; }
@Inject public CookieEncryption(NinjaProperties properties) { Optional<SecretKeySpec> secretKeySpec = Optional.empty(); if (properties.getBooleanWithDefault(NinjaConstant.applicationCookieEncrypted, false)) { String secret = properties.getOrDie(NinjaConstant.applicationSecret); try { int maxKeyLengthBits = Cipher.getMaxAllowedKeyLength(ALGORITHM); if (maxKeyLengthBits == Integer.MAX_VALUE) { maxKeyLengthBits = 256; } secretKeySpec = Optional.of( new SecretKeySpec(secret.getBytes(), 0, maxKeyLengthBits / Byte.SIZE, ALGORITHM)); logger.info("Ninja session encryption is using {} / {} bit.", secretKeySpec.get().getAlgorithm(), maxKeyLengthBits); } catch (Exception exception) { logger.error("Can not create class to encrypt cookie.", exception); throw new RuntimeException(exception); } } this.secretKeySpec = secretKeySpec; }
@Override public String getRemoteAddr() { boolean isUsageOfXForwardedHeaderEnabled = ninjaProperties.getBooleanWithDefault( Context.NINJA_PROPERTIES_X_FORWARDED_FOR, false); if (isUsageOfXForwardedHeaderEnabled) { String forwardHeader = getHeader(X_FORWARD_HEADER); if (forwardHeader != null) { if (forwardHeader.contains(",")) { // sometimes the header is of form client ip,proxy 1 ip,proxy 2 ip,...,proxy n ip, // we just want the client forwardHeader = StringUtils.split(forwardHeader, ',')[0].trim(); } try { // If ip4/6 address string handed over, simply does pattern validation. InetAddress.getByName(forwardHeader); return forwardHeader; } catch (UnknownHostException e) { // give up } } } // fallback to the real remote address return getRealRemoteAddr(); }
@Inject public PostofficeCommonsmailImpl(CommonsmailHelper commonsmailHelper, NinjaProperties props) { this.commonsmailHelper = commonsmailHelper; this.smtpHost = props.getOrDie(PostofficeConstant.smtpHost); Integer smtpPort = props.getInteger(PostofficeConstant.smtpPort); if (smtpPort == null) { this.smtpPort = 25; } else { this.smtpPort = smtpPort; } Boolean smtpSsl = props.getBoolean(PostofficeConstant.smtpSsl); if (smtpSsl == null) { this.smtpSsl = false; } else { this.smtpSsl = smtpSsl; } this.smtpStartTLS = props.getBooleanWithDefault(PostofficeConstant.smtpStartTLS, false); this.smtpStartTLSRequired = props.getBooleanWithDefault(PostofficeConstant.smtpStartTLSRequired, false); this.smtpUser = props.get(PostofficeConstant.smtpUser); this.smtpPassword = props.get(PostofficeConstant.smtpPassword); Boolean smtpDebug = props.getBoolean(PostofficeConstant.smtpDebug); if (smtpDebug == null) { this.smtpDebug = false; } else { this.smtpDebug = smtpDebug; } }
if (ninjaProps.getBooleanWithDefault("metrics.jvm.enabled", false)) { if (ninjaProps.getBooleanWithDefault("metrics.logback.enabled", false)) { if (ninjaProps.getBooleanWithDefault("metrics.mbeans.enabled", true)) {
boolean useEtag = ninjaProperties.getBooleanWithDefault(HTTP_USE_ETAG, HTTP_USE_ETAG_DEFAULT);
@Start(order = 90) public void startService() { if (ninjaProperties.getBooleanWithDefault("metrics.librato.enabled", false)) { final String hostname = metricsService.getHostname(); final String username = ninjaProperties .getOrDie("metrics.librato.username"); final String apiKey = ninjaProperties .getOrDie("metrics.librato.apikey"); final String period = ninjaProperties.getWithDefault( "metrics.librato.period", "60s"); final int delay = TimeUtil.parseDuration(period); LibratoReporter.enable(LibratoReporter.builder( metricsService.getMetricRegistry(), username, apiKey, hostname), delay, TimeUnit.SECONDS); log.info( "Started Librato Metrics reporter for '{}', updating every {}", hostname, period); } }
@Start(order = 90) public void start() { if (ninjaProperties.getBooleanWithDefault("metrics.graphite.enabled", false)) { final int port = ninjaProperties.getIntegerWithDefault( "metrics.graphite.port", 2003); final boolean isPickled = ninjaProperties.getBooleanWithDefault( "metrics.graphite.pickled", false); final String period = ninjaProperties.getWithDefault(
@Override public void migrate() { // Get the connection credentials from application.conf String connectionUrl = ninjaProperties.getOrDie(NinjaConstant.DB_CONNECTION_URL); String connectionUsername = ninjaProperties.getOrDie(NinjaConstant.DB_CONNECTION_USERNAME); String connectionPassword = ninjaProperties.getOrDie(NinjaConstant.DB_CONNECTION_PASSWORD); // We migrate automatically => if you do not want that (eg in production) // set ninja.migration.run=false in application.conf Flyway flyway = new Flyway(); flyway.setDataSource(connectionUrl, connectionUsername, connectionPassword); // In testmode we are cleaning the database so that subsequent testcases // get a fresh database. if (ninjaProperties.getBooleanWithDefault(NinjaConstant.NINJA_MIGRATION_DROP_SCHEMA, ninjaProperties.isTest() ? true : false )) { flyway.clean(); } flyway.migrate(); }
@Start(order = 90) public void start() { if (ninjaProperties.getBooleanWithDefault("metrics.ganglia.enabled", false)) { final String hostname = metricsService.getHostname(); final String address = ninjaProperties .getOrDie("metrics.ganglia.address"); final int port = ninjaProperties.getIntegerWithDefault( "metrics.ganglia.port", 8649); final String period = ninjaProperties.getWithDefault( "metrics.ganglia.period", "60s"); final int delay = TimeUtil.parseDuration(period); try { GMetric ganglia = new GMetric(address, port, UDPAddressingMode.MULTICAST, 1); reporter = GangliaReporter .forRegistry(metricsService.getMetricRegistry()) .convertRatesTo(TimeUnit.SECONDS) .convertDurationsTo(TimeUnit.MILLISECONDS) .build(ganglia); reporter.start(delay, TimeUnit.SECONDS); log.info( "Started Ganglia Metrics reporter for '{}', updating every {}", hostname, period); } catch (IOException e) { log.error("Failed to start Ganglia reporter!", e); } } }
@Start(order = 90) public void start() { if (ninjaProperties.getBooleanWithDefault("metrics.influxdb.enabled", false)) {