blob: f49202dd73f569737ce29977c6addd0d9b9a22c0 [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 /**
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 Vachuskab4258a92015-06-16 11:40:49 -070054 String message = messageFrom(exception);
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080055 ObjectNode result = mapper.createObjectNode()
56 .put("code", status.getStatusCode())
Thomas Vachuskab4258a92015-06-16 11:40:49 -070057 .put("message", message);
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080058 return Response.status(status).entity(result.toString());
59 }
Thomas Vachuskab4258a92015-06-16 11:40:49 -070060
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 Vachuskaca60f2b2014-11-06 01:34:28 -080077}