Search in sources :

Example 16 with PwmRequest

use of password.pwm.http.PwmRequest 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);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) PwmException(password.pwm.error.PwmException) PwmRequest(password.pwm.http.PwmRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) PwmURL(password.pwm.http.PwmURL) IOException(java.io.IOException) PwmApplicationMode(password.pwm.PwmApplicationMode) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) PwmException(password.pwm.error.PwmException)

Example 17 with PwmRequest

use of password.pwm.http.PwmRequest in project pwm by pwm-project.

the class RequestInitializationFilter method initializeServletRequest.

private void initializeServletRequest(final HttpServletRequest req, final HttpServletResponse resp, final FilterChain filterChain) throws IOException, ServletException {
    try {
        checkAndInitSessionState(req);
        PwmRequest.forRequest(req, resp);
    } catch (Throwable e) {
        LOGGER.error("can't load application: " + e.getMessage(), e);
        if (!(new PwmURL(req).isResourceURL())) {
            respondWithUnavailableError(req, resp);
            return;
        }
        return;
    }
    try {
        final PwmRequest pwmRequest = PwmRequest.forRequest(req, resp);
        checkIfSessionRecycleNeeded(pwmRequest);
        handleRequestInitialization(pwmRequest);
        addPwmResponseHeaders(pwmRequest);
        checkIdleTimeout(pwmRequest);
        try {
            handleRequestSecurityChecks(pwmRequest);
        } catch (PwmUnrecoverableException e) {
            LOGGER.error(pwmRequest, e.getErrorInformation());
            pwmRequest.respondWithError(e.getErrorInformation());
            if (PwmError.ERROR_INTRUDER_SESSION != e.getError()) {
                pwmRequest.invalidateSession();
            }
            return;
        }
    } catch (Throwable e) {
        final String logMsg = "can't init request: " + e.getMessage();
        if (e instanceof PwmException && ((PwmException) e).getError() != PwmError.ERROR_UNKNOWN) {
            LOGGER.error(logMsg);
        } else {
            LOGGER.error(logMsg, e);
        }
        if (!(new PwmURL(req).isResourceURL())) {
            respondWithUnavailableError(req, resp);
            return;
        }
        return;
    }
    filterChain.doFilter(req, resp);
}
Also used : PwmException(password.pwm.error.PwmException) PwmRequest(password.pwm.http.PwmRequest) PwmURL(password.pwm.http.PwmURL) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException)

Example 18 with PwmRequest

use of password.pwm.http.PwmRequest in project pwm by pwm-project.

the class ConfigEditorServlet method restSearchSettings.

@ActionHandler(action = "search")
private ProcessStatus restSearchSettings(final PwmRequest pwmRequest) throws IOException, PwmUnrecoverableException {
    final Instant startTime = Instant.now();
    final ConfigManagerBean configManagerBean = getBean(pwmRequest);
    final String bodyData = pwmRequest.readRequestBodyAsString();
    final Map<String, String> valueMap = JsonUtil.deserializeStringMap(bodyData);
    final Locale locale = pwmRequest.getLocale();
    final RestResultBean restResultBean;
    final String searchTerm = valueMap.get("search");
    final StoredConfigurationImpl storedConfiguration = configManagerBean.getStoredConfiguration();
    if (searchTerm != null && !searchTerm.isEmpty()) {
        final ArrayList<StoredConfigurationImpl.ConfigRecordID> searchResults = new ArrayList<>(configManagerBean.getStoredConfiguration().search(searchTerm, locale));
        final ConcurrentHashMap<String, Map<String, SearchResultItem>> returnData = new ConcurrentHashMap<>();
        searchResults.parallelStream().filter(recordID -> recordID.getRecordType() == StoredConfigurationImpl.ConfigRecordID.RecordType.SETTING).forEach(recordID -> {
            final PwmSetting setting = (PwmSetting) recordID.getRecordID();
            final SearchResultItem item = new SearchResultItem(setting.getCategory().toString(), storedConfiguration.readSetting(setting, recordID.getProfileID()).toDebugString(locale), setting.getCategory().toMenuLocationDebug(recordID.getProfileID(), locale), storedConfiguration.isDefaultValue(setting, recordID.getProfileID()), recordID.getProfileID());
            final String returnCategory = item.getNavigation();
            returnData.putIfAbsent(returnCategory, new ConcurrentHashMap<>());
            returnData.get(returnCategory).put(setting.getKey(), item);
        });
        final TreeMap<String, Map<String, SearchResultItem>> outputMap = new TreeMap<>();
        for (final String key : returnData.keySet()) {
            outputMap.put(key, new TreeMap<>(returnData.get(key)));
        }
        restResultBean = RestResultBean.withData(outputMap);
        LOGGER.trace(pwmRequest, "finished search operation with " + returnData.size() + " results in " + TimeDuration.fromCurrent(startTime).asCompactString());
    } else {
        restResultBean = RestResultBean.withData(new ArrayList<StoredConfigurationImpl.ConfigRecordID>());
    }
    pwmRequest.outputJsonResult(restResultBean);
    return ProcessStatus.Halt;
}
Also used : Locale(java.util.Locale) SmsItemBean(password.pwm.bean.SmsItemBean) PwmSetting(password.pwm.config.PwmSetting) ServletException(javax.servlet.ServletException) AppProperty(password.pwm.AppProperty) PwmConstants(password.pwm.PwmConstants) PwmError(password.pwm.error.PwmError) Message(password.pwm.i18n.Message) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) ValueMetaData(password.pwm.config.stored.ValueMetaData) ErrorInformation(password.pwm.error.ErrorInformation) ByteArrayInputStream(java.io.ByteArrayInputStream) SessionLabel(password.pwm.bean.SessionLabel) Locale(java.util.Locale) RestResultBean(password.pwm.ws.server.RestResultBean) Map(java.util.Map) UserIdentity(password.pwm.bean.UserIdentity) HealthStatus(password.pwm.health.HealthStatus) PwmSettingCategory(password.pwm.config.PwmSettingCategory) PwmPasswordPolicy(password.pwm.config.profile.PwmPasswordPolicy) JsonUtil(password.pwm.util.java.JsonUtil) ValueFactory(password.pwm.config.value.ValueFactory) ProcessStatus(password.pwm.http.ProcessStatus) PwmOperationalException(password.pwm.error.PwmOperationalException) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) PwmSettingTemplate(password.pwm.config.PwmSettingTemplate) Instant(java.time.Instant) Serializable(java.io.Serializable) ConfigurationChecker(password.pwm.health.ConfigurationChecker) List(java.util.List) HttpMethod(password.pwm.http.HttpMethod) HealthRecord(password.pwm.health.HealthRecord) StoredValue(password.pwm.config.StoredValue) PasswordData(password.pwm.util.PasswordData) LDAPStatusChecker(password.pwm.health.LDAPStatusChecker) ConfigurationProperty(password.pwm.config.stored.ConfigurationProperty) ActionValue(password.pwm.config.value.ActionValue) ChaiUnavailableException(com.novell.ldapchai.exception.ChaiUnavailableException) ConfigManagerBean(password.pwm.http.bean.ConfigManagerBean) LdapBrowser(password.pwm.ldap.LdapBrowser) PwmSettingTemplateSet(password.pwm.config.PwmSettingTemplateSet) Configuration(password.pwm.config.Configuration) PwmLocaleBundle(password.pwm.i18n.PwmLocaleBundle) PwmSettingSyntax(password.pwm.config.PwmSettingSyntax) MacroMachine(password.pwm.util.macro.MacroMachine) PwmSession(password.pwm.http.PwmSession) ControlledPwmServlet(password.pwm.http.servlet.ControlledPwmServlet) PwmApplication(password.pwm.PwmApplication) SettingUIFunction(password.pwm.config.SettingUIFunction) StringUtil(password.pwm.util.java.StringUtil) RestRandomPasswordServer(password.pwm.ws.server.rest.RestRandomPasswordServer) HashMap(java.util.HashMap) StoredConfigurationImpl(password.pwm.config.stored.StoredConfigurationImpl) PwmRequest(password.pwm.http.PwmRequest) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) AbstractPwmServlet(password.pwm.http.servlet.AbstractPwmServlet) ResourceBundle(java.util.ResourceBundle) StringTokenizer(java.util.StringTokenizer) RemoteWebServiceValue(password.pwm.config.value.RemoteWebServiceValue) PwmException(password.pwm.error.PwmException) HealthTopic(password.pwm.health.HealthTopic) JspUrl(password.pwm.http.JspUrl) RandomPasswordGenerator(password.pwm.util.RandomPasswordGenerator) FileValue(password.pwm.config.value.FileValue) PwmApplicationMode(password.pwm.PwmApplicationMode) PwmHttpRequestWrapper(password.pwm.http.PwmHttpRequestWrapper) PwmLogger(password.pwm.util.logging.PwmLogger) IOException(java.io.IOException) ConfigManagerServlet(password.pwm.http.servlet.configmanager.ConfigManagerServlet) X509CertificateValue(password.pwm.config.value.X509CertificateValue) WebServlet(javax.servlet.annotation.WebServlet) DatabaseStatusChecker(password.pwm.health.DatabaseStatusChecker) PrivateKeyValue(password.pwm.config.value.PrivateKeyValue) SmsQueueManager(password.pwm.util.queue.SmsQueueManager) TreeMap(java.util.TreeMap) HttpsServerCertificateManager(password.pwm.util.secure.HttpsServerCertificateManager) Config(password.pwm.i18n.Config) Collections(java.util.Collections) TimeDuration(password.pwm.util.java.TimeDuration) HealthData(password.pwm.ws.server.rest.bean.HealthData) StoredConfigurationImpl(password.pwm.config.stored.StoredConfigurationImpl) Instant(java.time.Instant) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) PwmSetting(password.pwm.config.PwmSetting) ConfigManagerBean(password.pwm.http.bean.ConfigManagerBean) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) RestResultBean(password.pwm.ws.server.RestResultBean)

Example 19 with PwmRequest

use of password.pwm.http.PwmRequest in project pwm by pwm-project.

the class ConfigEditorServletUtils method readFileUploadToSettingValue.

public static FileValue readFileUploadToSettingValue(final PwmRequest pwmRequest, final int maxFileSize) throws PwmUnrecoverableException, IOException, ServletException {
    final Map<String, PwmRequest.FileUploadItem> fileUploads;
    try {
        fileUploads = pwmRequest.readFileUploads(maxFileSize, 1);
    } catch (PwmException e) {
        pwmRequest.outputJsonResult(RestResultBean.fromError(e.getErrorInformation(), pwmRequest));
        LOGGER.error(pwmRequest, "error during file upload: " + e.getErrorInformation().toDebugStr());
        return null;
    } catch (Throwable e) {
        final ErrorInformation errorInformation = new ErrorInformation(PwmError.ERROR_UNKNOWN, "error during file upload: " + e.getMessage());
        pwmRequest.outputJsonResult(RestResultBean.fromError(errorInformation, pwmRequest));
        LOGGER.error(pwmRequest, errorInformation);
        return null;
    }
    if (fileUploads.containsKey(PwmConstants.PARAM_FILE_UPLOAD)) {
        final PwmRequest.FileUploadItem uploadItem = fileUploads.get(PwmConstants.PARAM_FILE_UPLOAD);
        final Map<FileValue.FileInformation, FileValue.FileContent> newFileValueMap = new LinkedHashMap<>();
        newFileValueMap.put(new FileValue.FileInformation(uploadItem.getName(), uploadItem.getType()), new FileValue.FileContent(uploadItem.getContent()));
        return new FileValue(newFileValueMap);
    }
    final ErrorInformation errorInformation = new ErrorInformation(PwmError.ERROR_UNKNOWN, "no file found in upload");
    pwmRequest.outputJsonResult(RestResultBean.fromError(errorInformation, pwmRequest));
    LOGGER.error(pwmRequest, "error during file upload: " + errorInformation.toDebugStr());
    return null;
}
Also used : FileValue(password.pwm.config.value.FileValue) PwmRequest(password.pwm.http.PwmRequest) LinkedHashMap(java.util.LinkedHashMap) PwmException(password.pwm.error.PwmException) ErrorInformation(password.pwm.error.ErrorInformation)

Aggregations

PwmRequest (password.pwm.http.PwmRequest)19 PwmUnrecoverableException (password.pwm.error.PwmUnrecoverableException)10 PwmException (password.pwm.error.PwmException)9 IOException (java.io.IOException)8 JspTagException (javax.servlet.jsp.JspTagException)8 HttpServletRequest (javax.servlet.http.HttpServletRequest)7 MacroMachine (password.pwm.util.macro.MacroMachine)6 PwmSession (password.pwm.http.PwmSession)5 Locale (java.util.Locale)4 ErrorInformation (password.pwm.error.ErrorInformation)4 ChaiUnavailableException (com.novell.ldapchai.exception.ChaiUnavailableException)3 ServletException (javax.servlet.ServletException)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 PwmApplication (password.pwm.PwmApplication)3 Configuration (password.pwm.config.Configuration)3 LinkedHashMap (java.util.LinkedHashMap)2 PwmApplicationMode (password.pwm.PwmApplicationMode)2 PwmPasswordPolicy (password.pwm.config.profile.PwmPasswordPolicy)2 FileValue (password.pwm.config.value.FileValue)2 PwmURL (password.pwm.http.PwmURL)2