Search in sources :

Example 21 with NotFoundException

use of com.infiniteautomation.mango.util.exception.NotFoundException in project ma-core-public by infiniteautomation.

the class UsersService method getByIdViaCache.

public User getByIdViaCache(int id) {
    return dao.doInTransaction((tx) -> {
        String username = dao.getXidById(id);
        if (username == null) {
            throw new NotFoundException();
        }
        User user = get(username);
        if (user.getId() != id) {
            throw new IllegalStateException("User was updated while retrieving from cache");
        }
        return user;
    });
}
Also used : User(com.serotonin.m2m2.vo.User) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException)

Example 22 with NotFoundException

use of com.infiniteautomation.mango.util.exception.NotFoundException in project ma-core-public by infiniteautomation.

the class PublishedPointImporter method importImpl.

@Override
protected void importImpl() {
    String xid = json.getString("xid");
    PublishedPointVO vo = null;
    PublisherVO publisherVO = null;
    DataPointVO dataPointVO = null;
    if (StringUtils.isBlank(xid)) {
        xid = service.generateUniqueXid();
    } else {
        try {
            vo = service.get(xid);
        } catch (NotFoundException e) {
        }
    }
    if (vo == null) {
        String pubXid = json.getString("publisherXid");
        try {
            publisherVO = publisherService.get(pubXid);
        } catch (NotFoundException e) {
            addFailureMessage("emport.publishedPoint.badPublisherReference", xid);
            return;
        }
        String dpXid = json.getString("dataPointXid");
        try {
            dataPointVO = dataPointService.get(dpXid);
        } catch (NotFoundException e) {
            addFailureMessage("emport.publishedPoint.badDataPointReference", xid);
            return;
        }
        vo = publisherVO.getDefinition().createPublishedPointVO(publisherVO, dataPointVO);
        vo.setXid(xid);
    }
    if (vo != null) {
        try {
            // The VO was found or successfully created. Finish reading it in.
            ctx.getReader().readInto(vo, json);
            boolean isnew = vo.isNew();
            if (Common.runtimeManager.getLifecycleState() == ILifecycleState.RUNNING) {
                if (isnew) {
                    service.insert(vo);
                } else {
                    service.update(vo.getId(), vo);
                }
                addSuccessMessage(isnew, "emport.publishedPoint.prefix", xid);
            } else {
                addFailureMessage("emport.publishedPoint.runtimeManagerNotRunning", xid);
            }
        } catch (ValidationException e) {
            setValidationMessages(e.getValidationResult(), "emport.publishedPoint.prefix", xid);
        } catch (TranslatableJsonException e) {
            addFailureMessage("emport.publishedPoint.prefix", xid, e.getMsg());
        } catch (JsonException e) {
            addFailureMessage("emport.publishedPoint.prefix", xid, getJsonExceptionMessage(e));
        }
    }
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) PublisherVO(com.serotonin.m2m2.vo.publish.PublisherVO) PublishedPointVO(com.serotonin.m2m2.vo.publish.PublishedPointVO)

Example 23 with NotFoundException

use of com.infiniteautomation.mango.util.exception.NotFoundException in project ma-core-public by infiniteautomation.

the class RoleImporter method importImpl.

@Override
protected void importImpl() {
    String xid = json.getString("xid");
    String name = json.getString("name");
    RoleVO vo = null;
    if (StringUtils.isBlank(xid)) {
        xid = service.generateUniqueXid();
    } else {
        try {
            vo = service.get(xid);
        } catch (NotFoundException e) {
        }
    }
    if (vo == null) {
        vo = new RoleVO(Common.NEW_ID, xid, name);
    }
    try {
        // Read into the VO to get all properties
        ctx.getReader().readInto(vo, json);
        boolean isnew = vo.getId() == Common.NEW_ID;
        if (isnew) {
            service.insert(vo);
        } else {
            service.update(vo.getId(), vo);
        }
        addSuccessMessage(isnew, "emport.role.prefix", xid);
    } catch (ValidationException e) {
        setValidationMessages(e.getValidationResult(), "emport.role.prefix", xid);
    } catch (JsonException e) {
        addFailureMessage("emport.role.prefix", xid, getJsonExceptionMessage(e));
    }
}
Also used : JsonException(com.serotonin.json.JsonException) RoleVO(com.serotonin.m2m2.vo.role.RoleVO) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException)

Example 24 with NotFoundException

use of com.infiniteautomation.mango.util.exception.NotFoundException in project ma-core-public by infiniteautomation.

the class MangoTokenAuthenticationProvider method authenticate.

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    if (!(authentication instanceof BearerAuthenticationToken)) {
        return null;
    }
    String bearerToken = (String) authentication.getCredentials();
    User user;
    Jws<Claims> jws;
    try {
        jws = tokenAuthenticationService.parse(bearerToken);
        user = tokenAuthenticationService.verify(jws);
    } catch (ExpiredJwtException e) {
        throw new CredentialsExpiredException("JWT token expired", e);
    } catch (UnsupportedJwtException | MalformedJwtException | IllegalArgumentException e) {
        // assume that this is not a JWT, allow the next AuthenticationProvider to process it
        return null;
    } catch (SignatureException | MissingClaimException | IncorrectClaimException e) {
        throw new BadCredentialsException("JWT signature verification error or claim incorrect", e);
    } catch (NotFoundException e) {
        throw new BadCredentialsException("Invalid username", e);
    } catch (Exception e) {
        throw new InternalAuthenticationServiceException("Error authenticating with JWT token", e);
    }
    userDetailsChecker.check(user);
    if (log.isDebugEnabled()) {
        log.debug("Successfully authenticated user using JWT token, header: " + jws.getHeader() + ", body: " + jws.getBody());
    }
    return new JwtAuthentication(user, bearerToken, jws, user.getAuthorities());
}
Also used : User(com.serotonin.m2m2.vo.User) Claims(io.jsonwebtoken.Claims) ExpiredJwtException(io.jsonwebtoken.ExpiredJwtException) CredentialsExpiredException(org.springframework.security.authentication.CredentialsExpiredException) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) InternalAuthenticationServiceException(org.springframework.security.authentication.InternalAuthenticationServiceException) SignatureException(io.jsonwebtoken.SignatureException) IncorrectClaimException(io.jsonwebtoken.IncorrectClaimException) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) UnsupportedJwtException(io.jsonwebtoken.UnsupportedJwtException) InternalAuthenticationServiceException(org.springframework.security.authentication.InternalAuthenticationServiceException) MissingClaimException(io.jsonwebtoken.MissingClaimException) IncorrectClaimException(io.jsonwebtoken.IncorrectClaimException) CredentialsExpiredException(org.springframework.security.authentication.CredentialsExpiredException) MalformedJwtException(io.jsonwebtoken.MalformedJwtException) ExpiredJwtException(io.jsonwebtoken.ExpiredJwtException) SignatureException(io.jsonwebtoken.SignatureException) AuthenticationException(org.springframework.security.core.AuthenticationException) MissingClaimException(io.jsonwebtoken.MissingClaimException) MalformedJwtException(io.jsonwebtoken.MalformedJwtException) UnsupportedJwtException(io.jsonwebtoken.UnsupportedJwtException)

Example 25 with NotFoundException

use of com.infiniteautomation.mango.util.exception.NotFoundException in project ma-core-public by infiniteautomation.

the class EmailAddressVerificationService method generateToken.

/**
 * Generate an email verification token
 *
 * @param userToUpdate   Optional, may be null
 * @param expirationDate Optional, may be null
 */
public String generateToken(String emailAddress, User userToUpdate, Date expirationDate) {
    if (userToUpdate == null) {
        this.ensurePublicRegistrationEnabled();
    }
    long verificationTime = System.currentTimeMillis();
    if (expirationDate == null) {
        int expiryDuration = this.systemSettings.getIntValue(EXPIRY_SYSTEM_SETTING);
        expirationDate = new Date(verificationTime + expiryDuration * 1000L);
    }
    runAs.runAs(runAs.systemSuperadmin(), () -> {
        try {
            // see if a different user is already using this address
            User existingUser = this.usersService.getUserByEmail(emailAddress);
            if (userToUpdate == null || existingUser.getId() != userToUpdate.getId()) {
                throw new EmailAddressInUseException(existingUser);
            }
        } catch (NotFoundException e) {
        // no existing user using this email address, proceed
        }
    });
    JwtBuilder builder = this.newToken(emailAddress, expirationDate);
    builder.setIssuedAt(new Date(verificationTime));
    if (userToUpdate != null) {
        this.usersService.ensureEditPermission(Common.getUser(), userToUpdate);
        builder.claim(USER_ID_CLAIM, userToUpdate.getId());
        builder.claim(USERNAME_CLAIM, userToUpdate.getUsername());
    }
    return this.sign(builder);
}
Also used : User(com.serotonin.m2m2.vo.User) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) JwtBuilder(io.jsonwebtoken.JwtBuilder) Date(java.util.Date)

Aggregations

NotFoundException (com.infiniteautomation.mango.util.exception.NotFoundException)74 ValidationException (com.infiniteautomation.mango.util.exception.ValidationException)26 JsonException (com.serotonin.json.JsonException)20 MangoPermission (com.infiniteautomation.mango.permission.MangoPermission)18 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)18 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)15 User (com.serotonin.m2m2.vo.User)15 PermissionHolder (com.serotonin.m2m2.vo.permission.PermissionHolder)15 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)12 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)10 DataSourceVO (com.serotonin.m2m2.vo.dataSource.DataSourceVO)9 JsonArray (com.serotonin.json.type.JsonArray)8 TranslatableIllegalArgumentException (com.infiniteautomation.mango.util.exception.TranslatableIllegalArgumentException)6 TranslatableRuntimeException (com.infiniteautomation.mango.util.exception.TranslatableRuntimeException)6 FileStore (com.serotonin.m2m2.vo.FileStore)6 AbstractEventHandlerVO (com.serotonin.m2m2.vo.event.AbstractEventHandlerVO)6 AbstractPointEventDetectorVO (com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO)6 PublisherVO (com.serotonin.m2m2.vo.publish.PublisherVO)6 ApiOperation (io.swagger.annotations.ApiOperation)6 IOException (java.io.IOException)6