Search in sources :

Example 11 with GenericRestException

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);
    }
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) HashMap(java.util.HashMap) MangoEmailContent(com.serotonin.m2m2.email.MangoEmailContent) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) Translations(com.serotonin.m2m2.i18n.Translations) GenericRestException(com.infiniteautomation.mango.rest.v2.exception.GenericRestException) IOException(java.io.IOException) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) GenericRestException(com.infiniteautomation.mango.rest.v2.exception.GenericRestException) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 12 with GenericRestException

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);
    }
}
Also used : MangoStoreClient(com.infiniteautomation.mango.rest.v2.util.MangoStoreClient) ResponseEntity(org.springframework.http.ResponseEntity) JsonString(com.serotonin.json.type.JsonString) ICoreLicense(com.serotonin.m2m2.ICoreLicense) BadRequestException(com.infiniteautomation.mango.rest.v2.exception.BadRequestException) GenericRestException(com.infiniteautomation.mango.rest.v2.exception.GenericRestException) FileNotFoundException(java.io.FileNotFoundException) ModuleRestV2Exception(com.infiniteautomation.mango.rest.v2.exception.ModuleRestV2Exception) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) GenericRestException(com.infiniteautomation.mango.rest.v2.exception.GenericRestException) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 13 with GenericRestException

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);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) NestedRuntimeException(org.springframework.core.NestedRuntimeException) ResponseEntity(org.springframework.http.ResponseEntity) User(com.serotonin.m2m2.vo.User) AbstractRestV2Exception(com.infiniteautomation.mango.rest.v2.exception.AbstractRestV2Exception) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServerErrorException(com.infiniteautomation.mango.rest.v2.exception.ServerErrorException) IOException(java.io.IOException) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) GenericRestException(com.infiniteautomation.mango.rest.v2.exception.GenericRestException) AbstractRestV2Exception(com.infiniteautomation.mango.rest.v2.exception.AbstractRestV2Exception) ServerErrorException(com.infiniteautomation.mango.rest.v2.exception.ServerErrorException) AccessDeniedException(com.infiniteautomation.mango.rest.v2.exception.AccessDeniedException) ValidationException(com.serotonin.m2m2.vo.exception.ValidationException) ResourceNotFoundException(com.infiniteautomation.mango.rest.v2.exception.ResourceNotFoundException) NestedRuntimeException(org.springframework.core.NestedRuntimeException) NotFoundException(com.serotonin.m2m2.vo.exception.NotFoundException) IOException(java.io.IOException) GenericRestException(com.infiniteautomation.mango.rest.v2.exception.GenericRestException) ValidationFailedRestException(com.infiniteautomation.mango.rest.v2.exception.ValidationFailedRestException) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException)

Aggregations

GenericRestException (com.infiniteautomation.mango.rest.v2.exception.GenericRestException)13 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)11 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)9 ResponseEntity (org.springframework.http.ResponseEntity)9 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)9 NotFoundRestException (com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)8 File (java.io.File)6 MultipartFile (org.springframework.web.multipart.MultipartFile)6 CommonsMultipartFile (org.springframework.web.multipart.commons.CommonsMultipartFile)6 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)5 FileModel (com.infiniteautomation.mango.rest.v2.model.filestore.FileModel)3 FileStoreDefinition (com.serotonin.m2m2.module.FileStoreDefinition)3 IOException (java.io.IOException)3 Path (java.nio.file.Path)3 ResourceNotFoundException (com.infiniteautomation.mango.rest.v2.exception.ResourceNotFoundException)2 DataPointRT (com.serotonin.m2m2.rt.dataImage.DataPointRT)2 IDataPointValueSource (com.serotonin.m2m2.rt.dataImage.IDataPointValueSource)2 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)2 ResultTypeException (com.serotonin.m2m2.rt.script.ResultTypeException)2 ScriptLog (com.serotonin.m2m2.rt.script.ScriptLog)2