blob: 0d9d94d34e148f9953edc0d70a97da738dcbddc4 [file] [log] [blame]
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -08001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -08003 *
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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.rest.exceptions;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080017
18import com.fasterxml.jackson.databind.ObjectMapper;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20
21import javax.ws.rs.core.Response;
22import javax.ws.rs.ext.ExceptionMapper;
23
Thomas Vachuskab4258a92015-06-16 11:40:49 -070024import static com.google.common.base.Strings.isNullOrEmpty;
25
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080026/**
27 * Base exception mapper implementation.
28 */
29public abstract class AbstractMapper<E extends Throwable> implements ExceptionMapper<E> {
30
31 /**
Ray Milkey8fba1c82015-11-17 09:23:24 -080032 * Holds the current exception for use in subclasses.
33 */
34 protected Throwable error;
35
36 /**
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080037 * Returns the response status to be given when the exception occurs.
38 *
39 * @return response status
40 */
41 protected abstract Response.Status responseStatus();
42
43 @Override
44 public Response toResponse(E exception) {
Ray Milkey8fba1c82015-11-17 09:23:24 -080045 error = exception;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080046 return response(responseStatus(), exception).build();
47 }
48
49 /**
50 * Produces a response builder primed with the supplied status code
51 * and JSON entity with the status code and exception message.
52 *
53 * @param status response status
54 * @param exception exception to encode
55 * @return response builder
56 */
57 protected Response.ResponseBuilder response(Response.Status status,
58 Throwable exception) {
Ray Milkey8fba1c82015-11-17 09:23:24 -080059 error = exception;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080060 ObjectMapper mapper = new ObjectMapper();
Thomas Vachuskab4258a92015-06-16 11:40:49 -070061 String message = messageFrom(exception);
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080062 ObjectNode result = mapper.createObjectNode()
63 .put("code", status.getStatusCode())
Thomas Vachuskab4258a92015-06-16 11:40:49 -070064 .put("message", message);
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080065 return Response.status(status).entity(result.toString());
66 }
Thomas Vachuskab4258a92015-06-16 11:40:49 -070067
68 /**
69 * Produces a response message from the supplied exception. Either it will
70 * use the exception message, if there is one, or it will use the top
71 * stack-frame message.
72 *
73 * @param exception exception from which to produce a message
74 * @return response message
75 */
76 protected String messageFrom(Throwable exception) {
77 if (isNullOrEmpty(exception.getMessage())) {
78 StackTraceElement[] trace = exception.getStackTrace();
79 return trace.length == 0 ? "Unknown error" : trace[0].toString();
80 }
81 return exception.getMessage();
82 }
83
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080084}