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

org.xlcloud.rest.exception.ForbiddenException Maven / Gradle / Ivy

/*
 * 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.rest.exception;

import javax.xml.bind.annotation.XmlAttribute;

import com.fasterxml.jackson.annotation.JsonIgnore;

/**
 * Thrown to indicate that requirements to execute desired operation are not
 * fulfilled.
 * 
 * @author Andrzej Stasiak, AMG.net
 * @author Maciej Osyda, AMG.net
 * @author Piotr Kulasek-Szwed, AMG.net
 */
public class ForbiddenException extends BaseException {

    private static final long serialVersionUID = -8688907323060662883L;

    private static final Integer STATUS = 403;
    
    @Deprecated // For JAXB only.
    public ForbiddenException() {}

    public ForbiddenException(String message, String method, String resource, Reason reason) {
        super(STATUS, new ExceptionDetails(message, method, resource, reason));
    }

    public ForbiddenException(String message, String method, String resource) {
        super(STATUS, new ExceptionDetails(message, method, resource));
    }

    public ForbiddenException(String message, ForbiddenAction forbiddenAction) {
        super(STATUS, new ExceptionDetails(message, forbiddenAction));
    }

    public enum ForbiddenAction {
        INCORRECT_ACCESS_TOKEN,
        OPENSTACK_AUTHENTICATION,
        RETRIEVE_X_AUTH_TOKEN,
        REGISTER_IMAGE,
        UNAUTHORIZED
    }
    
    public enum Reason {
        DENIAL,
        RESTRICTION,
        ERROR
    }

    public static class ExceptionDetails extends ExceptionDetailsBase {

        private static final long serialVersionUID = 1L;

        @XmlAttribute
        private String method;

        @XmlAttribute
        private String resource;

        @XmlAttribute
        private Reason reason;

        @XmlAttribute
        private ForbiddenAction forbiddenAction;
        
        @Deprecated // For JAXB only.
        public ExceptionDetails() {}
        
        private ExceptionDetails(String message, String method, String resource, Reason reason, ForbiddenAction forbiddenAction) {
            super(message);
            this.method = method;
            this.resource = resource;
            this.reason = reason;
            this.forbiddenAction = forbiddenAction;
        }

        public ExceptionDetails(String message, String method, String resource, Reason reason) {
            this(message, method, resource, reason, null);
        }

        public ExceptionDetails(String message, String method, String resource) {
            this(message, method, resource, null, null);
        }

        public ExceptionDetails(String message, ForbiddenAction forbiddenAction) {
            this(message, null, null, null, forbiddenAction);
        }
        
        public String getMethod() {
            return method;
        }
        
        public void setMethod(String method) {
            this.method = method;
        }

        public String getResource() {
            return resource;
        }
        
        public void setResource(String resource) {
            this.resource = resource;
        }

        public Reason getReason() {
            return reason;
        }
        
        public void setReason(Reason reason) {
            this.reason = reason;
        }
        
        public ForbiddenAction getForbiddenAction() {
            return forbiddenAction;
        }

        public void setForbiddenAction(ForbiddenAction forbiddenAction) {
            this.forbiddenAction = forbiddenAction;
        }

        @JsonIgnore
        public String getDetails() {
            return forbiddenAction != null ? "forbiddenAction="+forbiddenAction : "method="+method+",resource="+resource+",reason="+reason;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy