Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.rest.exceptions; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 17 | |
| 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | |
| 21 | import javax.ws.rs.core.Response; |
| 22 | import javax.ws.rs.ext.ExceptionMapper; |
| 23 | |
Thomas Vachuska | b4258a9 | 2015-06-16 11:40:49 -0700 | [diff] [blame] | 24 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 25 | |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 26 | /** |
| 27 | * Base exception mapper implementation. |
| 28 | */ |
| 29 | public abstract class AbstractMapper<E extends Throwable> implements ExceptionMapper<E> { |
| 30 | |
| 31 | /** |
| 32 | * Returns the response status to be given when the exception occurs. |
| 33 | * |
| 34 | * @return response status |
| 35 | */ |
| 36 | protected abstract Response.Status responseStatus(); |
| 37 | |
| 38 | @Override |
| 39 | public Response toResponse(E exception) { |
| 40 | return response(responseStatus(), exception).build(); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Produces a response builder primed with the supplied status code |
| 45 | * and JSON entity with the status code and exception message. |
| 46 | * |
| 47 | * @param status response status |
| 48 | * @param exception exception to encode |
| 49 | * @return response builder |
| 50 | */ |
| 51 | protected Response.ResponseBuilder response(Response.Status status, |
| 52 | Throwable exception) { |
| 53 | ObjectMapper mapper = new ObjectMapper(); |
Thomas Vachuska | b4258a9 | 2015-06-16 11:40:49 -0700 | [diff] [blame] | 54 | String message = messageFrom(exception); |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 55 | ObjectNode result = mapper.createObjectNode() |
| 56 | .put("code", status.getStatusCode()) |
Thomas Vachuska | b4258a9 | 2015-06-16 11:40:49 -0700 | [diff] [blame] | 57 | .put("message", message); |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 58 | return Response.status(status).entity(result.toString()); |
| 59 | } |
Thomas Vachuska | b4258a9 | 2015-06-16 11:40:49 -0700 | [diff] [blame] | 60 | |
| 61 | /** |
| 62 | * Produces a response message from the supplied exception. Either it will |
| 63 | * use the exception message, if there is one, or it will use the top |
| 64 | * stack-frame message. |
| 65 | * |
| 66 | * @param exception exception from which to produce a message |
| 67 | * @return response message |
| 68 | */ |
| 69 | protected String messageFrom(Throwable exception) { |
| 70 | if (isNullOrEmpty(exception.getMessage())) { |
| 71 | StackTraceElement[] trace = exception.getStackTrace(); |
| 72 | return trace.length == 0 ? "Unknown error" : trace[0].toString(); |
| 73 | } |
| 74 | return exception.getMessage(); |
| 75 | } |
| 76 | |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 77 | } |