use of com.infiniteautomation.mango.rest.v2.exception.GenericRestException in project ma-modules-public by infiniteautomation.
the class ServerRestV2Controller method sendTestEmail.
@PreAuthorize("isAdmin()")
@ApiOperation(value = "Send a test email", notes = "Sends email to supplied address")
@RequestMapping(method = RequestMethod.PUT, value = "/email/test")
public ResponseEntity<String> sendTestEmail(@RequestParam(value = "email", required = true, defaultValue = "") String email, @RequestParam(value = "username", required = true, defaultValue = "") String username, HttpServletRequest request) {
try {
Translations translations = Common.getTranslations();
Map<String, Object> model = new HashMap<>();
model.put("message", new TranslatableMessage("ftl.userTestEmail", username));
MangoEmailContent cnt = new MangoEmailContent("testEmail", model, translations, translations.translate("ftl.testEmail"), Common.UTF8);
EmailWorkItem.queueEmail(email, cnt);
return new ResponseEntity<String>(new TranslatableMessage("common.testEmailSent", email).translate(Common.getTranslations()), HttpStatus.OK);
} catch (Exception e) {
throw new GenericRestException(HttpStatus.INTERNAL_SERVER_ERROR, e);
}
}
use of com.infiniteautomation.mango.rest.v2.exception.GenericRestException in project ma-modules-public by infiniteautomation.
the class ModulesRestController method downloadLicense.
@PreAuthorize("isAdmin()")
@ApiOperation(value = "Download your license from the store", notes = "Admin Only")
@RequestMapping(method = RequestMethod.PUT, consumes = { "application/json" }, produces = { "application/json" }, value = "/download-license")
public ResponseEntity<Void> downloadLicense(@ApiParam(value = "Connection retries", required = false, defaultValue = "0", allowMultiple = false) @RequestParam(required = false, defaultValue = "0") int retries, @ApiParam(value = "User Credentials", required = true) @RequestBody(required = true) CredentialsModel model, HttpServletRequest request) {
try {
String storeUrl = Common.envProps.getString("store.url");
// Login to the store
MangoStoreClient client = new MangoStoreClient(storeUrl);
client.login(model.getUsername(), model.getPassword(), retries);
// Send the token request
String guid = Providers.get(ICoreLicense.class).getGuid();
String distributor = Common.envProps.getString("distributor");
String token = client.getLicenseToken(guid, distributor, retries);
// With the token we can make the request to download the file
String license = client.getLicense(token, retries);
saveLicense(license);
return new ResponseEntity<Void>(HttpStatus.OK);
} catch (Exception e) {
throw new GenericRestException(HttpStatus.INTERNAL_SERVER_ERROR, e);
}
}
use of com.infiniteautomation.mango.rest.v2.exception.GenericRestException in project ma-core-public by infiniteautomation.
the class MangoSpringExceptionHandler method handleExceptionInternal.
/* (non-Javadoc)
* @see org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler#handleExceptionInternal(java.lang.Exception, java.lang.Object, org.springframework.http.HttpHeaders, org.springframework.http.HttpStatus, org.springframework.web.context.request.WebRequest)
*/
@Override
protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body, HttpHeaders headers, HttpStatus status, WebRequest request) {
HttpServletRequest servletRequest = ((ServletWebRequest) request).getRequest();
HttpServletResponse servletResponse = ((ServletWebRequest) request).getResponse();
this.storeException(servletRequest, ex, status);
// Log all but not found exceptions
if (body instanceof ServerErrorException || body instanceof GenericRestException || !(body instanceof AbstractRestV2Exception))
ExceptionUtils.logWebException(ex, servletRequest, LOG);
if (this.browserHtmlRequestMatcher.matches(servletRequest)) {
String uri;
if (status == HttpStatus.FORBIDDEN) {
// browser HTML request
uri = ACCESS_DENIED;
User user = Common.getHttpUser();
if (user != null) {
uri = DefaultPagesDefinition.getUnauthorizedUri(servletRequest, servletResponse, user);
}
// Put exception into request scope (perhaps of use to a view)
servletRequest.setAttribute(WebAttributes.ACCESS_DENIED_403, ex);
// Set the 403 status code.
servletResponse.setStatus(HttpServletResponse.SC_FORBIDDEN);
} else {
uri = DefaultPagesDefinition.getErrorUri(servletRequest, servletResponse);
}
try {
servletResponse.sendRedirect(uri);
} catch (IOException e) {
LOG.error(e.getMessage(), e);
}
return null;
} else {
// To strip off the double messages generated by this...
if (ex instanceof NestedRuntimeException)
ex = (Exception) ((NestedRuntimeException) ex).getMostSpecificCause();
// If no body provided we will create one
if (body == null)
body = new GenericRestException(status, ex);
return new ResponseEntity<Object>(body, headers, status);
}
}
Aggregations