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