001/*
002 *  Copyright 2017 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.plugins.messagingconnector;
017
018import org.ametys.plugins.linkdirectory.dynamic.DynamicInformationException;
019
020/**
021 * Common exception for all messaging connectors.
022 */
023public class MessagingConnectorException extends RuntimeException
024{
025    /**
026     * Information about the exception
027     */
028    public enum ExceptionType
029    {
030        /** The connexion is not authorized for this specific user*/
031        UNAUTHORIZED,
032        /** Connexion timed out */
033        TIMEOUT,
034        /** Configuration exception (unknown host, host unreachable, 404, ...) */
035        CONFIGURATION_EXCEPTION,
036        /** Nothing defined */
037        UNKNOWN;
038    }
039    
040    private ExceptionType _type = ExceptionType.UNKNOWN;
041    /**
042     * Constructs a new exception with the specified detail message.
043     * @param message the detail message.
044     */
045    public MessagingConnectorException(String message)
046    {
047        this(message, ExceptionType.UNKNOWN);
048    }
049    /**
050     * Constructs a new exception with the specified detail message.
051     * @param message the detail message.
052     * @param type reason of the exception
053     */
054    public MessagingConnectorException(String message, ExceptionType type)
055    {
056        super(message);
057        this._type = type;
058    }
059
060    /**
061     * Constructs a new exception with the specified detail message and cause.
062     * @param message the detail message.
063     * @param cause the cause.
064     */
065    public MessagingConnectorException(String message, Throwable cause)
066    {
067        this(message, ExceptionType.UNKNOWN, cause);
068    }
069    
070    /**
071     * Constructs a new exception with the specified detail message and cause.
072     * @param message the detail message.
073     * @param type reason of the exception
074     * @param cause the cause.
075     */
076    public MessagingConnectorException(String message, ExceptionType type, Throwable cause)
077    {
078        super(message, cause);
079        this._type = type;
080    }
081
082    /**
083     * Constructs a new exception with the specified cause.
084     * @param cause the specified cause.
085     */
086    public MessagingConnectorException(Throwable cause)
087    {
088        this(ExceptionType.UNKNOWN, cause);
089    }
090
091    /**
092     * Constructs a new exception with the specified cause.
093     * @param type reason of the exception
094     * @param cause the specified cause.
095     */
096    public MessagingConnectorException(ExceptionType type, Throwable cause)
097    {
098        super(cause);
099        this._type = type;
100    }
101    
102    /**
103     * Get the type of exception
104     * @return {@link ExceptionType}
105     */
106    public ExceptionType getType()
107    {
108        return _type;
109    }
110    /**
111     * Create a DynamicInformationException from this MessagingConnectorException
112     * @return a new DynamicInformationException
113     */
114    public DynamicInformationException toDynamicInformationException()
115    {
116        DynamicInformationException.ExceptionType type = DynamicInformationException.ExceptionType.UNKNOWN;
117        switch(getType())
118        {
119            case CONFIGURATION_EXCEPTION:
120                type = DynamicInformationException.ExceptionType.CONFIGURATION_EXCEPTION;
121                break;
122            case TIMEOUT:
123                type = DynamicInformationException.ExceptionType.TIMEOUT;
124                break;
125            case UNAUTHORIZED:
126                type = DynamicInformationException.ExceptionType.UNAUTHORIZED;
127                break;
128            case UNKNOWN:
129            default:
130                type = DynamicInformationException.ExceptionType.UNKNOWN;
131                break;
132        }
133        return new DynamicInformationException(getMessage(), type, this);
134    }
135}