Search in sources :

Example 6 with RestMethodHandler

use of password.pwm.ws.server.RestMethodHandler in project pwm by pwm-project.

the class RestHealthServer method doPwmHealthPlainGet.

@RestMethodHandler(method = HttpMethod.GET, produces = HttpContentType.plain)
private RestResultBean doPwmHealthPlainGet(final RestRequest restRequest) throws PwmUnrecoverableException {
    final boolean requestImmediateParam = restRequest.readParameterAsBoolean(PARAM_IMMEDIATE_REFRESH);
    try {
        final HealthMonitor.CheckTimeliness timeliness = determineDataTimeliness(requestImmediateParam);
        final String resultString = restRequest.getPwmApplication().getHealthMonitor().getMostSevereHealthStatus(timeliness).toString() + "\n";
        StatisticsManager.incrementStat(restRequest.getPwmApplication(), Statistic.REST_HEALTH);
        return RestResultBean.withData(resultString);
    } catch (Exception e) {
        final String errorMessage = "unexpected error executing web service: " + e.getMessage();
        final ErrorInformation errorInformation = new ErrorInformation(PwmError.ERROR_UNKNOWN, errorMessage);
        return RestResultBean.fromError(restRequest, errorInformation);
    }
}
Also used : ErrorInformation(password.pwm.error.ErrorInformation) HealthMonitor(password.pwm.health.HealthMonitor) IOException(java.io.IOException) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) RestMethodHandler(password.pwm.ws.server.RestMethodHandler)

Example 7 with RestMethodHandler

use of password.pwm.ws.server.RestMethodHandler in project pwm by pwm-project.

the class RestServletTest method testActionHandlerReturnTypes.

@Test
public void testActionHandlerReturnTypes() throws IllegalAccessException, InstantiationException {
    final Set<Class<? extends RestServlet>> classMap = getClasses();
    for (final Class<? extends RestServlet> restServlet : classMap) {
        if (restServlet.getAnnotation(RestWebServer.class) == null) {
            Assert.fail(restServlet.getName() + " is missing annotation type of " + RestWebServer.class.getName());
        }
        Collection<Method> methods = JavaHelper.getAllMethodsForClass(restServlet);
        final Set<RestMethodHandler> seenHandlers = new HashSet<>();
        for (final Method method : methods) {
            final RestMethodHandler methodHandler = method.getAnnotation(RestMethodHandler.class);
            if (methodHandler != null) {
                final String returnTypeName = method.getReturnType().getName();
                final RestMethodHandler restMethodHandler = method.getAnnotation(RestMethodHandler.class);
                if (!returnTypeName.equals(RestResultBean.class.getName())) {
                    boolean requiresRestResultBeanReturnType = true;
                    if (restMethodHandler != null) {
                        if (Arrays.asList(restMethodHandler.produces()).contains(HttpContentType.plain)) {
                            requiresRestResultBeanReturnType = false;
                        }
                    }
                    if (requiresRestResultBeanReturnType) {
                        Assert.fail("method " + restServlet.getName() + ":" + method.getName() + " should have return type of " + RestResultBean.class.getName());
                    }
                }
                final Class[] paramTypes = method.getParameterTypes();
                if (paramTypes == null || paramTypes.length != 1) {
                    Assert.fail("method " + restServlet.getName() + ":" + method.getName() + " should have exactly one parameter");
                }
                final String paramTypeName = paramTypes[0].getName();
                if (!paramTypeName.equals(RestRequest.class.getName())) {
                    Assert.fail("method " + restServlet.getName() + ":" + method.getName() + " parameter type must be type " + RestRequest.class.getName());
                }
                if (seenHandlers.contains(methodHandler)) {
                    Assert.fail("duplicate " + RestMethodHandler.class + " assertions on class " + restServlet.getName());
                }
                seenHandlers.add(methodHandler);
            }
        }
    }
}
Also used : Method(java.lang.reflect.Method) RestServlet(password.pwm.ws.server.RestServlet) RestMethodHandler(password.pwm.ws.server.RestMethodHandler) RestWebServer(password.pwm.ws.server.RestWebServer) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 8 with RestMethodHandler

use of password.pwm.ws.server.RestMethodHandler in project pwm by pwm-project.

the class RestRandomPasswordServer method doPostRandomPasswordForm.

@RestMethodHandler(method = HttpMethod.POST, consumes = HttpContentType.form, produces = HttpContentType.json)
public RestResultBean doPostRandomPasswordForm(final RestRequest restRequest) throws PwmUnrecoverableException {
    final JsonInput jsonInput = new JsonInput();
    jsonInput.username = restRequest.readParameterAsString("username", PwmHttpRequestWrapper.Flag.BypassValidation);
    jsonInput.strength = restRequest.readParameterAsInt("strength", 0);
    jsonInput.maxLength = restRequest.readParameterAsInt("maxLength", 0);
    jsonInput.minLength = restRequest.readParameterAsInt("minLength", 0);
    jsonInput.chars = restRequest.readParameterAsString("chars", PwmHttpRequestWrapper.Flag.BypassValidation);
    jsonInput.noUser = restRequest.readParameterAsBoolean("noUser");
    try {
        final JsonOutput jsonOutput = doOperation(restRequest, jsonInput);
        final RestResultBean restResultBean = RestResultBean.withData(jsonOutput);
        return restResultBean;
    } catch (PwmException e) {
        LOGGER.error(restRequest.getSessionLabel(), "error executing rest-json random password request: " + e.getMessage(), e);
        return RestResultBean.fromError(restRequest, e.getErrorInformation());
    } catch (Exception e) {
        final String errorMessage = "unexpected error executing web service: " + e.getMessage();
        final ErrorInformation errorInformation = new ErrorInformation(PwmError.ERROR_UNKNOWN, errorMessage);
        return RestResultBean.fromError(restRequest, errorInformation);
    }
}
Also used : PwmException(password.pwm.error.PwmException) ErrorInformation(password.pwm.error.ErrorInformation) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) PwmException(password.pwm.error.PwmException) IOException(java.io.IOException) RestResultBean(password.pwm.ws.server.RestResultBean) RestMethodHandler(password.pwm.ws.server.RestMethodHandler)

Example 9 with RestMethodHandler

use of password.pwm.ws.server.RestMethodHandler in project pwm by pwm-project.

the class RestRandomPasswordServer method doPlainRandomPassword.

// This method is called if TEXT_PLAIN is request
@RestMethodHandler(method = HttpMethod.GET, produces = HttpContentType.plain)
public RestResultBean doPlainRandomPassword(final RestRequest restRequest) throws PwmUnrecoverableException {
    final JsonInput jsonInput = new JsonInput();
    jsonInput.username = restRequest.readParameterAsString("username", PwmHttpRequestWrapper.Flag.BypassValidation);
    jsonInput.strength = restRequest.readParameterAsInt("strength", 0);
    jsonInput.maxLength = restRequest.readParameterAsInt("maxLength", 0);
    jsonInput.minLength = restRequest.readParameterAsInt("minLength", 0);
    jsonInput.chars = restRequest.readParameterAsString("chars", PwmHttpRequestWrapper.Flag.BypassValidation);
    jsonInput.noUser = restRequest.readParameterAsBoolean("noUser");
    try {
        final JsonOutput jsonOutput = doOperation(restRequest, jsonInput);
        return RestResultBean.withData(jsonOutput.getPassword());
    } catch (Exception e) {
        LOGGER.error(restRequest.getSessionLabel(), "error executing rest-json random password request: " + e.getMessage(), e);
        final String errorMessage = "unexpected error executing web service: " + e.getMessage();
        final ErrorInformation errorInformation = new ErrorInformation(PwmError.ERROR_UNKNOWN, errorMessage);
        return RestResultBean.fromError(restRequest, errorInformation);
    }
}
Also used : ErrorInformation(password.pwm.error.ErrorInformation) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) PwmException(password.pwm.error.PwmException) IOException(java.io.IOException) RestMethodHandler(password.pwm.ws.server.RestMethodHandler)

Example 10 with RestMethodHandler

use of password.pwm.ws.server.RestMethodHandler in project pwm by pwm-project.

the class RestStatisticsServer method doPwmStatisticJsonGet.

@RestMethodHandler(method = HttpMethod.GET, consumes = HttpContentType.form, produces = HttpContentType.json)
public RestResultBean doPwmStatisticJsonGet(final RestRequest restRequest) throws PwmUnrecoverableException {
    final String statKey = restRequest.readParameterAsString("statKey", PwmHttpRequestWrapper.Flag.BypassValidation);
    final String statName = restRequest.readParameterAsString("statName", PwmHttpRequestWrapper.Flag.BypassValidation);
    final String days = restRequest.readParameterAsString("days", PwmHttpRequestWrapper.Flag.BypassValidation);
    try {
        final StatisticsManager statisticsManager = restRequest.getPwmApplication().getStatisticsManager();
        final JsonOutput jsonOutput = new JsonOutput();
        jsonOutput.EPS = addEpsStats(statisticsManager);
        if (statName != null && statName.length() > 0) {
            jsonOutput.nameData = doNameStat(statisticsManager, statName, days);
        } else {
            jsonOutput.keyData = doKeyStat(statisticsManager, statKey);
        }
        StatisticsManager.incrementStat(restRequest.getPwmApplication(), Statistic.REST_STATISTICS);
        final RestResultBean resultBean = RestResultBean.withData(jsonOutput);
        return resultBean;
    } catch (Exception e) {
        final String errorMsg = "unexpected error building json response: " + e.getMessage();
        final ErrorInformation errorInformation = new ErrorInformation(PwmError.ERROR_UNKNOWN, errorMsg);
        return RestResultBean.fromError(restRequest, errorInformation);
    }
}
Also used : ErrorInformation(password.pwm.error.ErrorInformation) StatisticsManager(password.pwm.svc.stats.StatisticsManager) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) RestResultBean(password.pwm.ws.server.RestResultBean) RestMethodHandler(password.pwm.ws.server.RestMethodHandler)

Aggregations

RestMethodHandler (password.pwm.ws.server.RestMethodHandler)12 ErrorInformation (password.pwm.error.ErrorInformation)9 PwmUnrecoverableException (password.pwm.error.PwmUnrecoverableException)8 IOException (java.io.IOException)5 RestResultBean (password.pwm.ws.server.RestResultBean)4 PwmException (password.pwm.error.PwmException)3 ChaiUser (com.novell.ldapchai.ChaiUser)2 ResponseSet (com.novell.ldapchai.cr.ResponseSet)2 ChaiException (com.novell.ldapchai.exception.ChaiException)2 ChaiUnavailableException (com.novell.ldapchai.exception.ChaiUnavailableException)2 Instant (java.time.Instant)2 ChallengeProfile (password.pwm.config.profile.ChallengeProfile)2 PwmOperationalException (password.pwm.error.PwmOperationalException)2 CrService (password.pwm.util.operations.CrService)2 ChallengeSet (com.novell.ldapchai.cr.ChallengeSet)1 ChaiProvider (com.novell.ldapchai.provider.ChaiProvider)1 Method (java.lang.reflect.Method)1 HashSet (java.util.HashSet)1 Locale (java.util.Locale)1 Test (org.junit.Test)1