Clover icon

Magnolia REST Integration 2.1

  1. Project Clover database Fri Mar 16 2018 18:18:02 CET
  2. Package info.magnolia.rest

File RestExceptionMapper.java

 

Coverage histogram

../../../img/srcFileCovDistChart7.png
41% of files have more coverage

Code metrics

20
26
1
1
121
65
12
0.46
26
1
12

Classes

Class Line # Actions
RestExceptionMapper 62 26 0% 12 16
0.6595744566%
 

Contributing tests

This file is covered by 6 tests. .

Source view

1    /**
2    * This file Copyright (c) 2012-2018 Magnolia International
3    * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10    * This file is distributed in the hope that it will be
11    * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12    * implied warranty of MERCHANTABILITY or FITNESS FOR A
13    * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14    * Redistribution, except as permitted by whichever of the GPL
15    * or MNA you select, is prohibited.
16    *
17    * 1. For the GPL license (GPL), you can redistribute and/or
18    * modify this file under the terms of the GNU General
19    * Public License, Version 3, as published by the Free Software
20    * Foundation. You should have received a copy of the GNU
21    * General Public License, Version 3 along with this program;
22    * if not, write to the Free Software Foundation, Inc., 51
23    * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24    *
25    * 2. For the Magnolia Network Agreement (MNA), this file
26    * and the accompanying materials are made available under the
27    * terms of the MNA which accompanies this distribution, and
28    * is available at http://www.magnolia-cms.com/mna.html
29    *
30    * Any modifications to this file must keep this entire header
31    * intact.
32    *
33    */
34    package info.magnolia.rest;
35   
36    import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR;
37   
38    import javax.ws.rs.BadRequestException;
39    import javax.ws.rs.NotAcceptableException;
40    import javax.ws.rs.NotAllowedException;
41    import javax.ws.rs.NotAuthorizedException;
42    import javax.ws.rs.NotFoundException;
43    import javax.ws.rs.WebApplicationException;
44    import javax.ws.rs.container.ResourceInfo;
45    import javax.ws.rs.core.Context;
46    import javax.ws.rs.core.MediaType;
47    import javax.ws.rs.core.Response;
48    import javax.ws.rs.core.Response.StatusType;
49    import javax.ws.rs.ext.ExceptionMapper;
50    import javax.ws.rs.ext.Provider;
51   
52    import org.apache.commons.lang3.StringUtils;
53    import org.jboss.resteasy.spi.ReaderException;
54    import org.jboss.resteasy.spi.WriterException;
55    import org.slf4j.Logger;
56    import org.slf4j.LoggerFactory;
57   
58    /**
59    * Maps exceptions encountered to response status codes.
60    */
61    @Provider
 
62    public class RestExceptionMapper implements ExceptionMapper<Throwable> {
63    private static final Logger log = LoggerFactory.getLogger(RestExceptionMapper.class);
64   
65    @Context
66    private ResourceInfo resourceInfo;
67   
 
68  6 toggle @Override
69    public Response toResponse(Throwable exception) {
70  6 StatusType status = INTERNAL_SERVER_ERROR;
71  6 String errorCode = "unknown";
72  6 String errorMessage = exception.getMessage();
73   
74  6 if (exception instanceof WebApplicationException) {
75  4 if (exception instanceof NotAuthorizedException) {
76  0 errorCode = "notAuthorized";
77  4 } else if (exception instanceof BadRequestException) {
78  0 errorCode = "badRequest";
79  4 } else if (exception instanceof NotAllowedException) {
80  1 errorCode = "methodNotAllowed";
81  3 } else if (exception instanceof NotAcceptableException) {
82  0 errorCode = "notAcceptable";
83  3 } else if (exception instanceof NotFoundException) {
84  3 errorCode = "notFound";
85  0 } else if (exception instanceof ReaderException) {
86  0 errorCode = "readerError";
87  0 } else if (exception instanceof WriterException) {
88  0 errorCode = "writerError";
89    }
90   
91    // Trim sensitive information of JAX-RS implementation.
92  4 if (errorMessage.startsWith("RESTEASY")) {
93  1 errorMessage = StringUtils.substringAfter(errorMessage, ": ");
94    }
95   
96  4 Response response = ((WebApplicationException) exception).getResponse();
97  4 status = response.getStatusInfo();
98   
99    /**
100    * If requested media type is not supported, JSON is returned as fallback.
101    * For example, curl -i -X GET '.../.rest/delivery/website/v1' -u username:password -H "Accept: application/something".
102    *
103    * If no resource method is matched, JSON is returned as fallback.
104    * For example, a request like ".../.rest/delivery/something" (without "v1" part) doesn't match any resource.
105    */
106  4 if (exception instanceof NotAcceptableException
107    || resourceInfo.getResourceMethod() == null) {
108  4 return Response.status(status)
109    .type(MediaType.APPLICATION_JSON)
110    .entity(new RestError(errorCode, errorMessage))
111    .build();
112    }
113    }
114   
115  2 log.error("Exception thrown executing REST endpoint, returning {}", status.getStatusCode(), exception);
116   
117  2 return Response.status(status)
118    .entity(new RestError(errorCode, errorMessage))
119    .build();
120    }
121    }