Bug 5529 - Restconf M5 - Clean-up sonar issues
[netconf.git] / restconf / sal-rest-connector / src / main / java / org / opendaylight / netconf / sal / restconf / impl / RestconfDocumentedException.java
1 /*
2  * Copyright (c) 2014 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.netconf.sal.restconf.impl;
10
11 import com.google.common.base.Preconditions;
12 import com.google.common.base.Throwables;
13 import com.google.common.collect.ImmutableList;
14 import com.google.common.collect.Lists;
15 import java.util.Collection;
16 import java.util.List;
17 import javax.ws.rs.WebApplicationException;
18 import javax.ws.rs.core.Response.Status;
19 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag;
20 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorType;
21 import org.opendaylight.yangtools.yang.common.RpcError;
22
23 /**
24  * Unchecked exception to communicate error information, as defined in the ietf restcong draft, to be sent to the
25  * client.
26  *
27  * @author Devin Avery
28  * @author Thomas Pantelis
29  * See also <a href="https://tools.ietf.org/html/draft-bierman-netconf-restconf-02">RESTCONF</a>
30  */
31 public class RestconfDocumentedException extends WebApplicationException {
32
33     private static final long serialVersionUID = 1L;
34
35     private final List<RestconfError> errors;
36     private final Status status;
37
38     /**
39      * Constructs an instance with an error message. The error type defaults to APPLICATION and the error tag defaults
40      * to OPERATION_FAILED.
41      *
42      * @param message
43      *            A string which provides a plain text string describing the error.
44      */
45     public RestconfDocumentedException(String message) {
46         this(message, RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.OPERATION_FAILED);
47     }
48
49     /**
50      * Constructs an instance with an error message, error type, error tag and exception cause.
51      *
52      * @param message
53      *            A string which provides a plain text string describing the error.
54      * @param errorType
55      *            The enumerated type indicating the layer where the error occurred.
56      * @param errorTag
57      *            The enumerated tag representing a more specific error cause.
58      * @param cause
59      *            The underlying exception cause.
60      */
61     public RestconfDocumentedException(String message, ErrorType errorType, ErrorTag errorTag, Throwable cause) {
62         this(cause, new RestconfError(errorType, errorTag, message, null, Throwables.getStackTraceAsString(cause)));
63     }
64
65     /**
66      * Constructs an instance with an error message, error type, and error tag.
67      *
68      * @param message
69      *            A string which provides a plain text string describing the error.
70      * @param errorType
71      *            The enumerated type indicating the layer where the error occurred.
72      * @param errorTag
73      *            The enumerated tag representing a more specific error cause.
74      */
75     public RestconfDocumentedException(String message, ErrorType errorType, ErrorTag errorTag) {
76         this(null, new RestconfError(errorType, errorTag, message));
77     }
78
79     /**
80      * Constructs an instance with an error message and exception cause.
81      * The stack trace of the exception is included in the error info.
82      *
83      * @param message
84      *            A string which provides a plain text string describing the error.
85      * @param cause
86      *            The underlying exception cause.
87      */
88     public RestconfDocumentedException(String message, Throwable cause) {
89         this(cause, new RestconfError(RestconfError.ErrorType.APPLICATION, RestconfError.ErrorTag.OPERATION_FAILED,
90                 message, null, Throwables.getStackTraceAsString(cause)));
91     }
92
93     /**
94      * Constructs an instance with the given error.
95      */
96     public RestconfDocumentedException(RestconfError error) {
97         this(null, error);
98     }
99
100     /**
101      * Constructs an instance with the given errors.
102      */
103     public RestconfDocumentedException(String message, Throwable cause, List<RestconfError> errors) {
104         // FIXME: We override getMessage so supplied message is lost for any public access
105         // this was lost also in original code.
106         super(cause);
107         if(!errors.isEmpty()) {
108             this.errors = ImmutableList.copyOf(errors);
109         } else {
110             this.errors = ImmutableList.of(new RestconfError(RestconfError.ErrorType.APPLICATION,
111                     RestconfError.ErrorTag.OPERATION_FAILED, message));
112         }
113
114         status = null;
115     }
116
117     /**
118      * Constructs an instance with the given RpcErrors.
119      */
120     public RestconfDocumentedException(String message, Throwable cause, Collection<RpcError> rpcErrors) {
121         this(message, cause, convertToRestconfErrors(rpcErrors));
122     }
123
124     /**
125      * Constructs an instance with an HTTP status and no error information.
126      *
127      * @param status
128      *            the HTTP status.
129      */
130     public RestconfDocumentedException(Status status) {
131         Preconditions.checkNotNull(status, "Status can't be null");
132         errors = ImmutableList.of();
133         this.status = status;
134     }
135
136     private RestconfDocumentedException(Throwable cause, RestconfError error) {
137         super(cause);
138         Preconditions.checkNotNull(error, "RestconfError can't be null");
139         errors = ImmutableList.of(error);
140         status = null;
141     }
142
143     private static List<RestconfError> convertToRestconfErrors(Collection<RpcError> rpcErrors) {
144         List<RestconfError> errorList = Lists.newArrayList();
145         if(rpcErrors != null) {
146             for (RpcError rpcError : rpcErrors) {
147                 errorList.add(new RestconfError(rpcError));
148             }
149         }
150
151         return errorList;
152     }
153
154
155     public List<RestconfError> getErrors() {
156         return errors;
157     }
158
159     public Status getStatus() {
160         return status;
161     }
162
163     @Override
164     public String getMessage() {
165         return "errors: " + errors + (status != null ? ", status: " + status : "");
166     }
167 }