Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -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 | */ |
Thomas Vachuska | a026be7 | 2015-12-07 16:00:37 -0800 | [diff] [blame] | 16 | |
| 17 | package org.onlab.rest.exceptions; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 18 | |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | |
| 22 | import javax.ws.rs.core.Response; |
| 23 | import javax.ws.rs.ext.ExceptionMapper; |
| 24 | |
Thomas Vachuska | b4258a9 | 2015-06-16 11:40:49 -0700 | [diff] [blame] | 25 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 26 | |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 27 | /** |
| 28 | * Base exception mapper implementation. |
| 29 | */ |
| 30 | public abstract class AbstractMapper<E extends Throwable> implements ExceptionMapper<E> { |
| 31 | |
| 32 | /** |
Ray Milkey | 8fba1c8 | 2015-11-17 09:23:24 -0800 | [diff] [blame] | 33 | * Holds the current exception for use in subclasses. |
| 34 | */ |
| 35 | protected Throwable error; |
| 36 | |
| 37 | /** |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 38 | * 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 Milkey | 8fba1c8 | 2015-11-17 09:23:24 -0800 | [diff] [blame] | 46 | error = exception; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 47 | 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 Milkey | 8fba1c8 | 2015-11-17 09:23:24 -0800 | [diff] [blame] | 60 | error = exception; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 61 | ObjectMapper mapper = new ObjectMapper(); |
Thomas Vachuska | b4258a9 | 2015-06-16 11:40:49 -0700 | [diff] [blame] | 62 | String message = messageFrom(exception); |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 63 | ObjectNode result = mapper.createObjectNode() |
| 64 | .put("code", status.getStatusCode()) |
Thomas Vachuska | b4258a9 | 2015-06-16 11:40:49 -0700 | [diff] [blame] | 65 | .put("message", message); |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 66 | return Response.status(status).entity(result.toString()); |
| 67 | } |
Thomas Vachuska | b4258a9 | 2015-06-16 11:40:49 -0700 | [diff] [blame] | 68 | |
| 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 Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 85 | } |