blob: ecbe0afb69a49a177c55cde4a54eba1698a31f40 [file] [log] [blame]
Jonathan Hart54b83e82016-03-26 20:37:20 -07001/*
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
17package org.onosproject.rest.exceptions;
18
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import org.onlab.rest.exceptions.AbstractMapper;
22import org.onosproject.net.config.InvalidConfigException;
23import org.onosproject.net.config.InvalidFieldException;
24
25import javax.ws.rs.core.Response;
26
27/**
28 * Maps InvalidConfigException to JSON output.
29 */
30public 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}