use of password.pwm.PwmApplicationMode in project pwm by pwm-project.
the class ContextManager method initialize.
public void initialize() {
try {
Locale.setDefault(PwmConstants.DEFAULT_LOCALE);
} catch (Exception e) {
outputError("unable to set default locale as Java machine default locale: " + e.getMessage());
}
Configuration configuration = null;
PwmApplicationMode mode = PwmApplicationMode.ERROR;
final ParameterReader parameterReader = new ParameterReader(servletContext);
final File applicationPath;
{
final String applicationPathStr = parameterReader.readApplicationPath();
if (applicationPathStr == null || applicationPathStr.isEmpty()) {
startupErrorInformation = new ErrorInformation(PwmError.ERROR_ENVIRONMENT_ERROR, "application path is not specified");
return;
} else {
applicationPath = new File(applicationPathStr);
}
}
File configurationFile = null;
try {
configurationFile = locateConfigurationFile(applicationPath);
configReader = new ConfigurationReader(configurationFile);
configReader.getStoredConfiguration().lock();
configuration = configReader.getConfiguration();
mode = startupErrorInformation == null ? configReader.getConfigMode() : PwmApplicationMode.ERROR;
if (startupErrorInformation == null) {
startupErrorInformation = configReader.getConfigFileError();
}
if (PwmApplicationMode.ERROR == mode) {
outputError("Startup Error: " + (startupErrorInformation == null ? "un-specified error" : startupErrorInformation.toDebugStr()));
}
} catch (Throwable e) {
handleStartupError("unable to initialize application due to configuration related error: ", e);
}
LOGGER.debug("configuration file was loaded from " + (configurationFile == null ? "null" : configurationFile.getAbsoluteFile()));
final Collection<PwmEnvironment.ApplicationFlag> applicationFlags = parameterReader.readApplicationFlags();
final Map<PwmEnvironment.ApplicationParameter, String> applicationParams = parameterReader.readApplicationParams();
try {
final PwmEnvironment pwmEnvironment = new PwmEnvironment.Builder(configuration, applicationPath).setApplicationMode(mode).setConfigurationFile(configurationFile).setContextManager(this).setFlags(applicationFlags).setParams(applicationParams).createPwmEnvironment();
pwmApplication = new PwmApplication(pwmEnvironment);
} catch (Exception e) {
handleStartupError("unable to initialize application: ", e);
}
final String threadName = JavaHelper.makeThreadName(pwmApplication, this.getClass()) + " timer";
taskMaster = new Timer(threadName, true);
taskMaster.schedule(new RestartFlagWatcher(), 1031, 1031);
boolean reloadOnChange = true;
long fileScanFrequencyMs = 5000;
{
if (pwmApplication != null) {
reloadOnChange = Boolean.parseBoolean(pwmApplication.getConfig().readAppProperty(AppProperty.CONFIG_RELOAD_ON_CHANGE));
fileScanFrequencyMs = Long.parseLong(pwmApplication.getConfig().readAppProperty(AppProperty.CONFIG_FILE_SCAN_FREQUENCY));
}
if (reloadOnChange) {
taskMaster.schedule(new ConfigFileWatcher(), fileScanFrequencyMs, fileScanFrequencyMs);
}
checkConfigForSaveOnRestart(configReader, pwmApplication);
}
}
use of password.pwm.PwmApplicationMode in project pwm by pwm-project.
the class ApplicationModeFilter method checkConfigModes.
private static ProcessStatus checkConfigModes(final PwmRequest pwmRequest) throws IOException, ServletException, PwmUnrecoverableException {
final PwmApplication pwmApplication = pwmRequest.getPwmApplication();
final PwmApplicationMode mode = pwmApplication.getApplicationMode();
final PwmURL pwmURL = pwmRequest.getURL();
if (mode == PwmApplicationMode.NEW) {
// check if current request is actually for the config url, if it is, just do nothing.
if (pwmURL.isCommandServletURL() || pwmURL.isRestService()) {
return ProcessStatus.Continue;
}
if (pwmURL.isConfigGuideURL() || pwmURL.isReferenceURL()) {
return ProcessStatus.Continue;
} else {
LOGGER.debug("unable to find a valid configuration, redirecting " + pwmURL + " to ConfigGuide");
pwmRequest.sendRedirect(PwmServletDefinition.ConfigGuide);
return ProcessStatus.Halt;
}
}
if (mode == PwmApplicationMode.ERROR) {
ErrorInformation rootError = ContextManager.getContextManager(pwmRequest.getHttpServletRequest().getSession()).getStartupErrorInformation();
if (rootError == null) {
rootError = new ErrorInformation(PwmError.ERROR_APP_UNAVAILABLE, "Application startup failed.");
}
pwmRequest.respondWithError(rootError);
return ProcessStatus.Halt;
}
// allow oauth
if (pwmURL.isOauthConsumer()) {
return ProcessStatus.Continue;
}
// block if public request and not running or in trial
if (!PwmConstants.TRIAL_MODE) {
if (mode != PwmApplicationMode.RUNNING) {
final boolean permittedURl = pwmURL.isResourceURL() || pwmURL.isIndexPage() || pwmURL.isConfigManagerURL() || pwmURL.isConfigGuideURL() || pwmURL.isCommandServletURL() || pwmURL.isReferenceURL() || pwmURL.isLoginServlet() || pwmURL.isLogoutURL() || pwmURL.isOauthConsumer() || pwmURL.isAdminUrl() || pwmURL.isRestService();
if (!permittedURl) {
final ErrorInformation errorInformation = new ErrorInformation(PwmError.ERROR_APPLICATION_NOT_RUNNING);
pwmRequest.respondWithError(errorInformation);
return ProcessStatus.Halt;
}
}
}
return ProcessStatus.Continue;
}
use of password.pwm.PwmApplicationMode in project pwm by pwm-project.
the class MacroMachine method effectiveScopes.
private static Set<MacroImplementation.Scope> effectiveScopes(final MacroImplementation.MacroRequestInfo macroRequestInfo) {
final Set<MacroImplementation.Scope> scopes = new HashSet<>();
scopes.add(MacroImplementation.Scope.Static);
final PwmApplication pwmApplication = macroRequestInfo.getPwmApplication();
final PwmApplicationMode mode = pwmApplication != null ? pwmApplication.getApplicationMode() : PwmApplicationMode.ERROR;
final boolean appModeOk = mode == PwmApplicationMode.RUNNING || mode == PwmApplicationMode.CONFIGURATION;
if (appModeOk) {
scopes.add(MacroImplementation.Scope.System);
if (macroRequestInfo.getUserInfo() != null) {
scopes.add(MacroImplementation.Scope.User);
}
}
return Collections.unmodifiableSet(scopes);
}
use of password.pwm.PwmApplicationMode in project pwm by pwm-project.
the class MainClass method loadPwmApplication.
private static PwmApplication loadPwmApplication(final File applicationPath, final Collection<PwmEnvironment.ApplicationFlag> flags, final Configuration config, final File configurationFile, final boolean readonly) throws LocalDBException, PwmUnrecoverableException {
final PwmApplicationMode mode = readonly ? PwmApplicationMode.READ_ONLY : PwmApplicationMode.RUNNING;
final Collection<PwmEnvironment.ApplicationFlag> applicationFlags = new HashSet<>();
if (flags == null) {
applicationFlags.addAll(PwmEnvironment.ParseHelper.readApplicationFlagsFromSystem(null));
} else {
applicationFlags.addAll(flags);
}
applicationFlags.add(PwmEnvironment.ApplicationFlag.CommandLineInstance);
final PwmEnvironment pwmEnvironment = new PwmEnvironment.Builder(config, applicationPath).setApplicationMode(mode).setConfigurationFile(configurationFile).setFlags(applicationFlags).createPwmEnvironment();
final PwmApplication pwmApplication = new PwmApplication(pwmEnvironment);
final PwmApplicationMode runningMode = pwmApplication.getApplicationMode();
if (runningMode != mode) {
out("unable to start application in required state '" + mode + "', current state: " + runningMode);
System.exit(-1);
}
return pwmApplication;
}
use of password.pwm.PwmApplicationMode in project pwm by pwm-project.
the class AbstractPwmFilter method doFilter.
@Override
public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse, final FilterChain filterChain) throws IOException, ServletException {
final HttpServletRequest req = (HttpServletRequest) servletRequest;
final HttpServletResponse resp = (HttpServletResponse) servletResponse;
final PwmApplicationMode mode = PwmApplicationMode.determineMode(req);
final boolean interested;
try {
final PwmURL pwmURL = new PwmURL(req);
interested = isInterested(mode, pwmURL);
} catch (Exception e) {
LOGGER.error("unexpected error processing filter chain during isInterested(): " + e.getMessage(), e);
resp.sendError(500, "unexpected error processing filter chain during isInterested");
return;
}
if (interested) {
PwmRequest pwmRequest = null;
try {
pwmRequest = PwmRequest.forRequest(req, resp);
} catch (PwmException e) {
final PwmURL pwmURL = new PwmURL(req);
if (pwmURL.isResourceURL()) {
filterChain.doFilter(req, resp);
return;
}
LOGGER.error(pwmRequest, "unexpected error processing filter chain: " + e.getMessage(), e);
}
try {
final PwmFilterChain pwmFilterChain = new PwmFilterChain(servletRequest, servletResponse, filterChain);
processFilter(mode, pwmRequest, pwmFilterChain);
} catch (PwmException e) {
LOGGER.error(pwmRequest, "unexpected error processing filter chain: " + e.getMessage(), e);
} catch (IOException e) {
LOGGER.debug(pwmRequest, "i/o error processing request: " + e.getMessage());
}
} else {
filterChain.doFilter(req, resp);
}
}
Aggregations