All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.xlcloud.console.controllers.i18n.LocalizedExceptionResolver Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2012 AMG.lab, a Bull Group Company
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.xlcloud.console.controllers.i18n;

import org.xlcloud.console.controllers.request.exception.ExceptionMessageResolver;
import org.xlcloud.rest.exception.AuthenticateException;
import org.xlcloud.rest.exception.BaseException;
import org.xlcloud.rest.exception.BaseException.ExceptionDetailsBase;
import org.xlcloud.rest.exception.DuplicatedEntityException;
import org.xlcloud.rest.exception.ForbiddenException;
import org.xlcloud.rest.exception.InternalErrorException;
import org.xlcloud.rest.exception.ObjectNotFoundException;
import org.xlcloud.rest.exception.QuotaException;
import org.xlcloud.rest.exception.ValidationException;

/**
 * Resolves localized messages of {@link BaseException}s.
 *
 * @author Krzysztof Szafrański, AMG.net
 */
public class LocalizedExceptionResolver {

    private final BaseException exception;
    private final String errorKey;
    private final MessageResolver messages;
    
    public LocalizedExceptionResolver(MessageResolver messages, BaseException exception, String errorKey) {
        this.exception = exception;
        this.errorKey = errorKey;
        this.messages = messages;
    }
    
    public String getErrorText() {
        return messages.getValue(getErrorKey());
    }
    
    private String getErrorKey() {
        if (errorKey != null)                                       { return errorKey; }
        else if (exception instanceof QuotaException)               { return "error.quota"; }
        else if (exception instanceof ForbiddenException)           { return "error.forbidden"; }
        else if (exception instanceof ObjectNotFoundException)      { return "error.notFound"; }
        else if (exception instanceof DuplicatedEntityException)    { return "error.duplicated"; }
        else if (exception instanceof AuthenticateException)        { return "error.authenticate"; }
        else if (exception instanceof ValidationException)          { return "error.validation"; }
        else if (exception instanceof InternalErrorException)       { return "error.server.internal"; }
        else                                                        { return "error"; }
    }
    
    public String getDetailsText() {
        if (exception instanceof QuotaException)                    { return getQuotaExceptionDetailsText(exception.getDetails()); }
        else if (exception instanceof ForbiddenException)           { return getForbiddenExceptionDetailsText(exception.getDetails()); }
        else if (exception instanceof ObjectNotFoundException)      { return getObjectNotFoundExceptionDetailsText(exception.getDetails()); }
        else if (exception instanceof DuplicatedEntityException)    { return getDuplicatedEntityExceptionDetailsText(exception.getDetails()); }
        else if (exception instanceof AuthenticateException)        { return getAuthenticateExceptionDetailsText(exception.getDetails()); }
        else if (exception instanceof ValidationException)          { return getValidationExceptionDetailsText(exception.getDetails()); }
        else if (exception instanceof InternalErrorException)       { return getInternalErrorExceptionDetailsText(exception.getDetails()); }
        else {
            return new ExceptionMessageResolver(exception).getExceptionMessage();
        }
    }

    private String getQuotaExceptionDetailsText(ExceptionDetailsBase detailsBase) {
        QuotaException.ExceptionDetails details = (QuotaException.ExceptionDetails) detailsBase;
        return messages.getValue("error.quota." + details.getResource());
    }
    
    private String getForbiddenExceptionDetailsText(ExceptionDetailsBase detailsBase) {
        ForbiddenException.ExceptionDetails details = (ForbiddenException.ExceptionDetails) detailsBase;
        return details.getForbiddenAction() != null
                ? messages.getValue("error.forbidden." + details.getForbiddenAction())
                : messages.applyParams(messages.getValue("error.forbidden.default"), details.getResource(), details.getMethod());
    }
    
    private String getObjectNotFoundExceptionDetailsText(ExceptionDetailsBase detailsBase) {
        ObjectNotFoundException.ExceptionDetails details = (ObjectNotFoundException.ExceptionDetails) detailsBase;
        return messages.applyParams(messages.getValue("error.notFound." + details.getObjectType()), details.getObjectId());
    }
    
    private String getDuplicatedEntityExceptionDetailsText(ExceptionDetailsBase detailsBase) {
        DuplicatedEntityException.ExceptionDetails details = (DuplicatedEntityException.ExceptionDetails) detailsBase;
        return messages.applyParams(messages.getValue("error.duplicated." + details.getEntityType()), details.getEntityId());
    }
    
    private String getAuthenticateExceptionDetailsText(ExceptionDetailsBase detailsBase) {
        AuthenticateException.ExceptionDetails details = (AuthenticateException.ExceptionDetails) detailsBase;
        return messages.getValue("error.authenticate." + details.getAuthenticationFailure());
    }
    
    private String getValidationExceptionDetailsText(ExceptionDetailsBase detailsBase) {
        ValidationException.ExceptionDetails details = (ValidationException.ExceptionDetails) detailsBase;
        return messages.applyParams(messages.getValue("error.validation." + details.getValidationFailure()), details.getInvalidField());
    }
    
    private String getInternalErrorExceptionDetailsText(ExceptionDetailsBase detailsBase) {
        InternalErrorException.ExceptionDetails details = (InternalErrorException.ExceptionDetails) detailsBase;
        return messages.getValue("error.internal." + details.getErrorType());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy