Jonathan Hart | bc7a8bf | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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 | |
| 17 | package org.onosproject.rest.exceptions; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import org.onlab.rest.exceptions.AbstractMapper; |
| 22 | import org.onosproject.net.config.InvalidConfigException; |
| 23 | import org.onosproject.net.config.InvalidFieldException; |
| 24 | |
| 25 | import javax.ws.rs.core.Response; |
| 26 | |
| 27 | /** |
| 28 | * Maps InvalidConfigException to JSON output. |
| 29 | */ |
| 30 | public class InvalidConfigExceptionMapper extends AbstractMapper<InvalidConfigException> { |
| 31 | |
| 32 | @Override |
| 33 | protected Response.Status responseStatus() { |
| 34 | return Response.Status.BAD_REQUEST; |
| 35 | } |
| 36 | |
| 37 | @Override |
| 38 | protected Response.ResponseBuilder response(Response.Status status, Throwable exception) { |
| 39 | error = exception; |
| 40 | |
| 41 | InvalidConfigException ex = (InvalidConfigException) exception; |
| 42 | |
| 43 | ObjectMapper mapper = new ObjectMapper(); |
| 44 | String message = messageFrom(exception); |
| 45 | ObjectNode result = mapper.createObjectNode() |
| 46 | .put("code", status.getStatusCode()) |
| 47 | .put("message", message) |
| 48 | .put("subjectKey", ex.subjectKey()) |
| 49 | .put("subject", ex.subject()) |
| 50 | .put("configKey", ex.configKey()); |
| 51 | |
| 52 | if (ex.getCause() instanceof InvalidFieldException) { |
| 53 | InvalidFieldException fieldException = (InvalidFieldException) ex.getCause(); |
| 54 | result.put("field", fieldException.field()) |
| 55 | .put("reason", fieldException.reason()); |
| 56 | } |
| 57 | |
| 58 | return Response.status(status).entity(result.toString()); |
| 59 | } |
| 60 | } |