private void updateCacheFromLdap() throws ChaiUnavailableException, ChaiOperationException, PwmOperationalException, PwmUnrecoverableException { LOGGER.debug( PwmConstants.REPORTING_SESSION_LABEL, "beginning process to updating user cache records from ldap"); if (status != STATUS.OPEN) { return; } cancelFlag = false; reportStatus = new ReportStatusInfo(settings.getSettingsHash()); reportStatus.setInProgress(true); reportStatus.setStartDate(new Date()); try { final Queue<UserIdentity> allUsers = new LinkedList<>(getListOfUsers()); reportStatus.setTotal(allUsers.size()); while (status == STATUS.OPEN && !allUsers.isEmpty() && !cancelFlag) { final Date startUpdateTime = new Date(); final UserIdentity userIdentity = allUsers.poll(); try { if (updateCachedRecordFromLdap(userIdentity)) { reportStatus.setUpdated(reportStatus.getUpdated() + 1); } } catch (Exception e) { String errorMsg = "error while updating report cache for " + userIdentity.toString() + ", cause: "; errorMsg += e instanceof PwmException ? ((PwmException) e).getErrorInformation().toDebugStr() : e.getMessage(); final ErrorInformation errorInformation; errorInformation = new ErrorInformation(PwmError.ERROR_REPORTING_ERROR, errorMsg); LOGGER.error(PwmConstants.REPORTING_SESSION_LABEL, errorInformation.toDebugStr()); reportStatus.setLastError(errorInformation); reportStatus.setErrors(reportStatus.getErrors() + 1); } reportStatus.setCount(reportStatus.getCount() + 1); reportStatus.getEventRateMeter().markEvents(1); final TimeDuration totalUpdateTime = TimeDuration.fromCurrent(startUpdateTime); if (settings.isAutoCalcRest()) { avgTracker.addSample(totalUpdateTime.getTotalMilliseconds()); Helper.pause(avgTracker.avgAsLong()); } else { Helper.pause(settings.getRestTime().getTotalMilliseconds()); } } if (cancelFlag) { reportStatus.setLastError( new ErrorInformation( PwmError.ERROR_SERVICE_NOT_AVAILABLE, "report cancelled by operator")); } } finally { reportStatus.setFinishDate(new Date()); reportStatus.setInProgress(false); } LOGGER.debug( PwmConstants.REPORTING_SESSION_LABEL, "update user cache process completed: " + JsonUtil.serialize(reportStatus)); }
@Override void doCommand() throws Exception { final PwmApplication pwmApplication = cliEnvironment.getPwmApplication(); final File outputFile = (File) cliEnvironment.getOptions().get(CliParameters.REQUIRED_NEW_OUTPUT_FILE.getName()); Helper.pause(2000); final long startTime = System.currentTimeMillis(); final UserSearchEngine userSearchEngine = new UserSearchEngine(pwmApplication, SessionLabel.SYSTEM_LABEL); final UserSearchEngine.SearchConfiguration searchConfiguration = new UserSearchEngine.SearchConfiguration(); searchConfiguration.setEnableValueEscaping(false); searchConfiguration.setUsername("*"); final String systemRecordDelimiter = System.getProperty("line.separator"); final Writer writer = new BufferedWriter(new PrintWriter(outputFile, PwmConstants.DEFAULT_CHARSET.toString())); final Map<UserIdentity, Map<String, String>> results = userSearchEngine.performMultiUserSearch( searchConfiguration, Integer.MAX_VALUE, Collections.<String>emptyList()); out( "searching " + results.size() + " users for stored responses to write to " + outputFile.getAbsolutePath() + "...."); int counter = 0; for (final UserIdentity identity : results.keySet()) { final ChaiUser user = pwmApplication.getProxiedChaiUser(identity); final ResponseSet responseSet = pwmApplication.getCrService().readUserResponseSet(null, identity, user); if (responseSet != null) { counter++; out("found responses for '" + user + "', writing to output."); final RestChallengesServer.JsonChallengesData outputData = new RestChallengesServer.JsonChallengesData(); outputData.challenges = responseSet.asChallengeBeans(true); outputData.helpdeskChallenges = responseSet.asHelpdeskChallengeBeans(true); outputData.minimumRandoms = responseSet.getChallengeSet().minimumResponses(); outputData.username = identity.toDelimitedKey(); writer.write(JsonUtil.serialize(outputData)); writer.write(systemRecordDelimiter); } else { out("skipping '" + user.toString() + "', no stored responses."); } } writer.close(); out( "output complete, " + counter + " responses exported in " + TimeDuration.fromCurrent(startTime).asCompactString()); }