use of password.pwm.ws.server.RestResultBean in project pwm by pwm-project.
the class ConfigGuideUtils method restUploadConfig.
public static void restUploadConfig(final PwmRequest pwmRequest) throws PwmUnrecoverableException, IOException, ServletException {
final PwmApplication pwmApplication = pwmRequest.getPwmApplication();
final PwmSession pwmSession = pwmRequest.getPwmSession();
final HttpServletRequest req = pwmRequest.getHttpServletRequest();
if (pwmApplication.getApplicationMode() == PwmApplicationMode.RUNNING) {
final String errorMsg = "config upload is not permitted when in running mode";
final ErrorInformation errorInformation = new ErrorInformation(PwmError.CONFIG_UPLOAD_FAILURE, errorMsg, new String[] { errorMsg });
pwmRequest.respondWithError(errorInformation, true);
}
if (ServletFileUpload.isMultipartContent(req)) {
final InputStream uploadedFile = pwmRequest.readFileUploadStream(PwmConstants.PARAM_FILE_UPLOAD);
if (uploadedFile != null) {
try {
final StoredConfigurationImpl storedConfig = StoredConfigurationImpl.fromXml(uploadedFile);
final List<String> configErrors = storedConfig.validateValues();
if (configErrors != null && !configErrors.isEmpty()) {
throw new PwmOperationalException(new ErrorInformation(PwmError.CONFIG_FORMAT_ERROR, configErrors.get(0)));
}
ConfigGuideUtils.writeConfig(ContextManager.getContextManager(req.getSession()), storedConfig);
LOGGER.trace(pwmSession, "read config from file: " + storedConfig.toString());
final RestResultBean restResultBean = RestResultBean.forSuccessMessage(pwmRequest, Message.Success_Unknown);
pwmRequest.getPwmResponse().outputJsonResult(restResultBean);
req.getSession().invalidate();
} catch (PwmException e) {
final RestResultBean restResultBean = RestResultBean.fromError(e.getErrorInformation(), pwmRequest);
pwmRequest.getPwmResponse().outputJsonResult(restResultBean);
LOGGER.error(pwmSession, e.getErrorInformation().toDebugStr());
}
} else {
final ErrorInformation errorInformation = new ErrorInformation(PwmError.CONFIG_UPLOAD_FAILURE, "error reading config file: no file present in upload");
final RestResultBean restResultBean = RestResultBean.fromError(errorInformation, pwmRequest);
pwmRequest.getPwmResponse().outputJsonResult(restResultBean);
LOGGER.error(pwmSession, errorInformation.toDebugStr());
}
}
}
use of password.pwm.ws.server.RestResultBean in project pwm by pwm-project.
the class ConfigManagerCertificatesServlet method processAction.
protected void processAction(final PwmRequest pwmRequest) throws ServletException, IOException, ChaiUnavailableException, PwmUnrecoverableException {
final ConfigManagerCertificateAction action = readProcessAction(pwmRequest);
final ArrayList<CertificateDebugDataItem> certificateDebugDataItems = new ArrayList<>(makeCertificateDebugData(pwmRequest.getConfig()));
if (action != null && action == ConfigManagerCertificateAction.certificateData) {
final RestResultBean restResultBean = RestResultBean.withData(certificateDebugDataItems);
pwmRequest.outputJsonResult(restResultBean);
return;
}
pwmRequest.setAttribute(PwmRequestAttribute.ConfigHasCertificates, !certificateDebugDataItems.isEmpty());
pwmRequest.forwardToJsp(JspUrl.CONFIG_MANAGER_CERTIFICATES);
}
use of password.pwm.ws.server.RestResultBean in project pwm by pwm-project.
the class ClientApiServlet method processRestClientData.
@ActionHandler(action = "clientData")
public ProcessStatus processRestClientData(final PwmRequest pwmRequest) throws PwmUnrecoverableException, IOException, ChaiUnavailableException {
final String pageUrl = pwmRequest.readParameterAsString("pageUrl", PwmHttpRequestWrapper.Flag.BypassValidation);
final String etagParam = pwmRequest.readParameterAsString("etag", PwmHttpRequestWrapper.Flag.BypassValidation);
final int maxCacheAgeSeconds = 60 * 5;
final String eTagValue = makeClientEtag(pwmRequest.getPwmApplication(), pwmRequest.getPwmSession(), pwmRequest.getHttpServletRequest());
// check the incoming header;
final String ifNoneMatchValue = pwmRequest.readHeaderValueAsString("If-None-Match");
if (ifNoneMatchValue != null && ifNoneMatchValue.equals(eTagValue) && eTagValue.equals(etagParam)) {
pwmRequest.getPwmResponse().setStatus(304);
return ProcessStatus.Halt;
}
pwmRequest.getPwmResponse().setHeader(HttpHeader.ETag, eTagValue);
pwmRequest.getPwmResponse().setHeader(HttpHeader.Expires, String.valueOf(System.currentTimeMillis() + (maxCacheAgeSeconds * 1000)));
pwmRequest.getPwmResponse().setHeader(HttpHeader.Cache_Control, "public, max-age=" + maxCacheAgeSeconds);
final AppData appData = makeAppData(pwmRequest.getPwmApplication(), pwmRequest.getPwmSession(), pwmRequest.getHttpServletRequest(), pwmRequest.getPwmResponse().getHttpServletResponse(), pageUrl);
final RestResultBean restResultBean = RestResultBean.withData(appData);
pwmRequest.outputJsonResult(restResultBean);
return ProcessStatus.Halt;
}
use of password.pwm.ws.server.RestResultBean in project pwm by pwm-project.
the class ClientApiServlet method doGetStringsData.
@ActionHandler(action = "strings")
public ProcessStatus doGetStringsData(final PwmRequest pwmRequest) throws PwmUnrecoverableException, IOException, ChaiUnavailableException, ServletException {
final String bundleName = pwmRequest.readParameterAsString("bundle");
final int maxCacheAgeSeconds = 60 * 5;
final String eTagValue = makeClientEtag(pwmRequest.getPwmApplication(), pwmRequest.getPwmSession(), pwmRequest.getHttpServletRequest());
pwmRequest.getPwmResponse().setHeader(HttpHeader.ETag, eTagValue);
pwmRequest.getPwmResponse().setHeader(HttpHeader.Expires, String.valueOf(System.currentTimeMillis() + (maxCacheAgeSeconds * 1000)));
pwmRequest.getPwmResponse().setHeader(HttpHeader.Cache_Control, "public, max-age=" + maxCacheAgeSeconds);
try {
final LinkedHashMap<String, String> displayData = new LinkedHashMap<>(makeDisplayData(pwmRequest.getPwmApplication(), pwmRequest.getPwmSession(), bundleName));
final RestResultBean restResultBean = RestResultBean.withData(displayData);
pwmRequest.outputJsonResult(restResultBean);
} catch (Exception e) {
final String errorMSg = "error during rest /strings call for bundle " + bundleName + ", error: " + e.getMessage();
final ErrorInformation errorInformation = new ErrorInformation(PwmError.ERROR_UNKNOWN, errorMSg);
LOGGER.debug(pwmRequest, errorInformation);
pwmRequest.respondWithError(errorInformation);
}
return ProcessStatus.Halt;
}
use of password.pwm.ws.server.RestResultBean in project pwm by pwm-project.
the class AdminServlet method restAuditDataHandler.
@ActionHandler(action = "auditData")
private ProcessStatus restAuditDataHandler(final PwmRequest pwmRequest) throws PwmUnrecoverableException, IOException {
final Instant startTime = Instant.now();
final TimeDuration maxSearchTime = new TimeDuration(10, TimeUnit.SECONDS);
final int max = readMaxParameter(pwmRequest, 100, 10 * 1000);
final AuditEvent.Type auditDataType = AuditEvent.Type.valueOf(pwmRequest.readParameterAsString("type", AuditEvent.Type.USER.name()));
final ArrayList<AuditRecord> records = new ArrayList<>();
final Iterator<AuditRecord> iterator = pwmRequest.getPwmApplication().getAuditManager().readVault();
while (iterator.hasNext() && records.size() < max && TimeDuration.fromCurrent(startTime).isShorterThan(maxSearchTime)) {
final AuditRecord loopRecord = iterator.next();
if (auditDataType == loopRecord.getType()) {
records.add(loopRecord);
}
}
final HashMap<String, Object> resultData = new HashMap<>(Collections.singletonMap("records", records));
final RestResultBean restResultBean = RestResultBean.withData(resultData);
LOGGER.debug(pwmRequest.getPwmSession(), "output " + records.size() + " audit records.");
pwmRequest.outputJsonResult(restResultBean);
return ProcessStatus.Halt;
}
Aggregations