blob: c7be9b572e75e9d84ecd9679acc5d90e5edb5431 [file] [log] [blame]
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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 */
Thomas Vachuskaa026be72015-12-07 16:00:37 -080016
17package org.onlab.rest.exceptions;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080018
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21
22import javax.ws.rs.core.Response;
23import javax.ws.rs.ext.ExceptionMapper;
24
Thomas Vachuskab4258a92015-06-16 11:40:49 -070025import static com.google.common.base.Strings.isNullOrEmpty;
26
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080027/**
28 * Base exception mapper implementation.
29 */
30public abstract class AbstractMapper<E extends Throwable> implements ExceptionMapper<E> {
31
32 /**
Ray Milkey8fba1c82015-11-17 09:23:24 -080033 * Holds the current exception for use in subclasses.
34 */
35 protected Throwable error;
36
37 /**
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080038 * Returns the response status to be given when the exception occurs.
39 *
40 * @return response status
41 */
42 protected abstract Response.Status responseStatus();
43
44 @Override
45 public Response toResponse(E exception) {
Ray Milkey8fba1c82015-11-17 09:23:24 -080046 error = exception;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080047 return response(responseStatus(), exception).build();
48 }
49
50 /**
51 * Produces a response builder primed with the supplied status code
52 * and JSON entity with the status code and exception message.
53 *
54 * @param status response status
55 * @param exception exception to encode
56 * @return response builder
57 */
58 protected Response.ResponseBuilder response(Response.Status status,
59 Throwable exception) {
Ray Milkey8fba1c82015-11-17 09:23:24 -080060 error = exception;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080061 ObjectMapper mapper = new ObjectMapper();
Thomas Vachuskab4258a92015-06-16 11:40:49 -070062 String message = messageFrom(exception);
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080063 ObjectNode result = mapper.createObjectNode()
64 .put("code", status.getStatusCode())
Thomas Vachuskab4258a92015-06-16 11:40:49 -070065 .put("message", message);
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080066 return Response.status(status).entity(result.toString());
67 }
Thomas Vachuskab4258a92015-06-16 11:40:49 -070068
69 /**
70 * Produces a response message from the supplied exception. Either it will
71 * use the exception message, if there is one, or it will use the top
72 * stack-frame message.
73 *
74 * @param exception exception from which to produce a message
75 * @return response message
76 */
77 protected String messageFrom(Throwable exception) {
78 if (isNullOrEmpty(exception.getMessage())) {
79 StackTraceElement[] trace = exception.getStackTrace();
80 return trace.length == 0 ? "Unknown error" : trace[0].toString();
81 }
82 return exception.getMessage();
83 }
84
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080085}