Search in sources :

Example 16 with RestResultBean

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());
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) PwmException(password.pwm.error.PwmException) ErrorInformation(password.pwm.error.ErrorInformation) PwmApplication(password.pwm.PwmApplication) StoredConfigurationImpl(password.pwm.config.stored.StoredConfigurationImpl) InputStream(java.io.InputStream) PwmSession(password.pwm.http.PwmSession) PwmOperationalException(password.pwm.error.PwmOperationalException) RestResultBean(password.pwm.ws.server.RestResultBean)

Example 17 with RestResultBean

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);
}
Also used : ArrayList(java.util.ArrayList) RestResultBean(password.pwm.ws.server.RestResultBean)

Example 18 with RestResultBean

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;
}
Also used : RestResultBean(password.pwm.ws.server.RestResultBean)

Example 19 with RestResultBean

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;
}
Also used : ErrorInformation(password.pwm.error.ErrorInformation) ServletException(javax.servlet.ServletException) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) ChaiUnavailableException(com.novell.ldapchai.exception.ChaiUnavailableException) PwmException(password.pwm.error.PwmException) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) RestResultBean(password.pwm.ws.server.RestResultBean)

Example 20 with RestResultBean

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;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Instant(java.time.Instant) ArrayList(java.util.ArrayList) TimeDuration(password.pwm.util.java.TimeDuration) AuditEvent(password.pwm.svc.event.AuditEvent) AuditRecord(password.pwm.svc.event.AuditRecord) RestResultBean(password.pwm.ws.server.RestResultBean)

Aggregations

RestResultBean (password.pwm.ws.server.RestResultBean)63 ErrorInformation (password.pwm.error.ErrorInformation)27 PwmUnrecoverableException (password.pwm.error.PwmUnrecoverableException)21 UserIdentity (password.pwm.bean.UserIdentity)16 PwmException (password.pwm.error.PwmException)16 HelpdeskProfile (password.pwm.config.profile.HelpdeskProfile)15 Instant (java.time.Instant)14 PwmOperationalException (password.pwm.error.PwmOperationalException)14 HashMap (java.util.HashMap)11 LinkedHashMap (java.util.LinkedHashMap)11 ChaiUnavailableException (com.novell.ldapchai.exception.ChaiUnavailableException)10 IOException (java.io.IOException)10 PwmApplication (password.pwm.PwmApplication)10 PwmSession (password.pwm.http.PwmSession)10 ConfigManagerBean (password.pwm.http.bean.ConfigManagerBean)9 ArrayList (java.util.ArrayList)8 ServletException (javax.servlet.ServletException)8 UserInfo (password.pwm.ldap.UserInfo)8 AuditRecordFactory (password.pwm.svc.event.AuditRecordFactory)8 HelpdeskAuditRecord (password.pwm.svc.event.HelpdeskAuditRecord)8