Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.restconf.api; |
| 17 | |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame^] | 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 22 | import javax.ws.rs.WebApplicationException; |
| 23 | import javax.ws.rs.core.Response; |
| 24 | |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame^] | 25 | import java.util.ArrayList; |
| 26 | import java.util.Optional; |
| 27 | |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 28 | import static javax.ws.rs.core.Response.Status; |
| 29 | |
| 30 | /** |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame^] | 31 | * Exceptions raised during RESTCONF operations. This class extends WebApplicationException. |
| 32 | * To comply with the RESTCONF specification on error handling, the parameters given |
| 33 | * when creating this exception will be used to create one |
| 34 | * {@link org.onosproject.restconf.api.RestconfError}. Additional |
| 35 | * {@link org.onosproject.restconf.api.RestconfError}s may be added subsequently. |
| 36 | * This exception should be converted to a Json object using the toRestconfError() |
| 37 | * method before being passed back to the caller in a response |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 38 | */ |
| 39 | public class RestconfException extends WebApplicationException { |
| 40 | |
| 41 | // This is a randomly generated value. A WebApplicationException class is required to define it. |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame^] | 42 | private static final long SERIAL_VERSION_UID = 1275970654684007046L; |
| 43 | |
| 44 | private ArrayList<RestconfError> restconfErrors = new ArrayList<>(); |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 45 | |
| 46 | /** |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame^] | 47 | * Constructs a new RESTCONF server error exception based of an existing exception. |
| 48 | * The caller raising this exception may pass in a HTTP error status code |
| 49 | * and an error message. The error code will be displayed to the RESTCONF |
| 50 | * client as part of the response from the RESTCONF server. The error message |
| 51 | * is a string which may be saved in a log file and may be later retrieved by |
| 52 | * the getMessage() method. The parameters given will be formed in to a |
| 53 | * {@link org.onosproject.restconf.api.RestconfError}. Additional errors may be |
| 54 | * added after construction. |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 55 | * |
| 56 | * @param message the detailed error message |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame^] | 57 | * @param throwable The existing exception that caused this response. |
| 58 | * The message from this exception will be used as error-info |
| 59 | * @param errorTag A Restconf Error tag |
| 60 | * @param status HTTP error status. Developers are asked to ensure that the correct one for the error-tag is used |
| 61 | * @param errorPath An optional path that gives the item that caused the exception |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 62 | * @throws IllegalArgumentException in case the status code is null or is not from |
| 63 | * javax.ws.rs.core.Response.Status.Family |
| 64 | * status code family |
| 65 | */ |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame^] | 66 | public RestconfException(String message, Throwable throwable, |
| 67 | RestconfError.ErrorTag errorTag, Status status, |
| 68 | Optional<String> errorPath) { |
| 69 | super(message, throwable, Response.status(status).build()); |
| 70 | RestconfError.Builder bldr = RestconfError |
| 71 | .builder(RestconfError.ErrorType.APPLICATION, errorTag) |
| 72 | .errorMessage(message); |
| 73 | if (throwable != null) { |
| 74 | bldr.errorInfo(throwable.getMessage()); |
| 75 | } |
| 76 | if (errorPath.isPresent()) { |
| 77 | bldr.errorPath(errorPath.get()); |
| 78 | } |
| 79 | addToErrors(bldr.build()); |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /** |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame^] | 83 | * Constructs a new RESTCONF server error exception. |
| 84 | * The caller raising this exception may pass in a HTTP error status code |
| 85 | * and an error message. The error code will be displayed to the RESTCONF |
| 86 | * client as part of the response from the RESTCONF server. The error message |
| 87 | * is a string which may be saved in a log file and may be later retrieved by |
| 88 | * the getMessage() method. The parameters given will be formed in to a |
| 89 | * {@link org.onosproject.restconf.api.RestconfError}. Additional errors may be |
| 90 | * added after construction. |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 91 | * |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame^] | 92 | * @param message the detailed error message |
| 93 | * @param errorTag A Restconf Error tag |
| 94 | * @param status HTTP error status. Developers are asked to ensure that the correct one for the error-tag is used |
| 95 | * @param errorPath An optional path that gives the item that caused the exception |
| 96 | * @param errorInfo An optional string with more info about the error |
| 97 | * @throws IllegalArgumentException in case the status code is null or is not from |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 98 | * javax.ws.rs.core.Response.Status.Family |
| 99 | * status code family |
| 100 | */ |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame^] | 101 | public RestconfException(String message, RestconfError.ErrorTag errorTag, |
| 102 | Status status, Optional<String> errorPath, |
| 103 | Optional<String> errorInfo) { |
| 104 | super(message, null, Response.status(status).build()); |
| 105 | RestconfError.Builder bldr = RestconfError |
| 106 | .builder(RestconfError.ErrorType.APPLICATION, errorTag) |
| 107 | .errorMessage(message); |
| 108 | if (errorInfo.isPresent()) { |
| 109 | bldr.errorInfo(errorInfo.get()); |
| 110 | } |
| 111 | if (errorPath.isPresent()) { |
| 112 | bldr.errorPath(errorPath.get()); |
| 113 | } |
| 114 | addToErrors(bldr.build()); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Allows additional RestconfErrors to be added to the exception. |
| 119 | * @param restconfError An additional RestconfError to be added to the response |
| 120 | */ |
| 121 | public void addToErrors(RestconfError restconfError) { |
| 122 | restconfErrors.add(restconfError); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Convert the RestconfException and all of its RestconfErrors in to a Json object. |
| 127 | * @return A json node generated from the RestconfException |
| 128 | */ |
| 129 | public ObjectNode toRestconfErrorJson() { |
| 130 | ObjectMapper mapper = new ObjectMapper(); |
| 131 | ArrayNode errorArray = mapper.createArrayNode(); |
| 132 | restconfErrors.forEach(error -> errorArray.add(error.toJson())); |
| 133 | ObjectNode errorsNode = (ObjectNode) mapper.createObjectNode(); |
| 134 | errorsNode.put("ietf-restconf:errors", errorArray); |
| 135 | return errorsNode; |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 136 | } |
| 137 | } |