Search in sources :

Example 1 with TenantNotFoundException

use of io.micronaut.multitenancy.exceptions.TenantNotFoundException in project micronaut-multitenancy by micronaut-projects.

the class SessionTenantResolver method resolveTenantIdentifier.

@Override
@NonNull
public Serializable resolveTenantIdentifier(@NonNull @NotNull HttpRequest<?> request) throws TenantNotFoundException {
    if (this.attribute == null) {
        throw new TenantNotFoundException("Tenant could not be resolved from HTTP Session, because session attribute name is not set");
    }
    Optional<Session> opt = request.getAttributes().get(HttpSessionFilter.SESSION_ATTRIBUTE, Session.class);
    if (!opt.isPresent()) {
        throw new TenantNotFoundException("Tenant could not be resolved from HTTP Session, if session not present");
    }
    Session session = opt.get();
    Optional<Object> tenantId = session.get(attribute);
    if (!tenantId.isPresent()) {
        throw new TenantNotFoundException("Tenant could not be resolved from HTTP Session, if session attribute (" + attribute + ") not present");
    }
    if (!(tenantId.get() instanceof Serializable)) {
        throw new TenantNotFoundException("Tenant was resolved from HTTP Session, but it is not serializable");
    }
    return (Serializable) tenantId.get();
}
Also used : Serializable(java.io.Serializable) TenantNotFoundException(io.micronaut.multitenancy.exceptions.TenantNotFoundException) Session(io.micronaut.session.Session) NonNull(io.micronaut.core.annotation.NonNull)

Aggregations

NonNull (io.micronaut.core.annotation.NonNull)1 TenantNotFoundException (io.micronaut.multitenancy.exceptions.TenantNotFoundException)1 Session (io.micronaut.session.Session)1 Serializable (java.io.Serializable)1