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);
}
}
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);
}
}
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations