Clover icon

Magnolia REST Integration 2.1.3

  1. Project Clover database Fri Oct 25 2019 12:33:17 CEST
  2. Package info.magnolia.rest

File RepositoryExceptionMapper.java

 

Coverage histogram

../../../img/srcFileCovDistChart8.png
23% of files have more coverage

Code metrics

12
25
1
1
97
52
7
0.28
25
1
7

Classes

Class Line # Actions
RepositoryExceptionMapper 59 25 0% 7 9
0.763157976.3%
 

Contributing tests

This file is covered by 5 tests. .

Source view

1    /**
2    * This file Copyright (c) 2017-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 com.google.common.base.CaseFormat.*;
37    import static javax.ws.rs.core.Response.Status.*;
38   
39    import javax.jcr.AccessDeniedException;
40    import javax.jcr.NoSuchWorkspaceException;
41    import javax.jcr.PathNotFoundException;
42    import javax.jcr.RepositoryException;
43    import javax.jcr.ValueFormatException;
44    import javax.jcr.nodetype.NoSuchNodeTypeException;
45    import javax.jcr.query.InvalidQueryException;
46    import javax.ws.rs.core.Response;
47    import javax.ws.rs.core.Response.Status;
48    import javax.ws.rs.ext.ExceptionMapper;
49    import javax.ws.rs.ext.Provider;
50   
51    import org.apache.commons.lang3.StringUtils;
52    import org.slf4j.Logger;
53    import org.slf4j.LoggerFactory;
54   
55    /**
56    * Repository exception mapper.
57    */
58    @Provider
 
59    public class RepositoryExceptionMapper implements ExceptionMapper<RepositoryException> {
60   
61    private static final Logger log = LoggerFactory.getLogger(RepositoryExceptionMapper.class);
62   
 
63  5 toggle @Override
64    public Response toResponse(RepositoryException exception) {
65  5 Status status = INTERNAL_SERVER_ERROR;
66  5 String errorCode = "unknown";
67  5 String errorMessage = exception.getMessage();
68   
69  5 if (exception instanceof AccessDeniedException) {
70  1 status = FORBIDDEN;
71  1 errorCode = "accessDenied";
72  4 } else if (exception instanceof NoSuchNodeTypeException) {
73  0 status = INTERNAL_SERVER_ERROR;
74  0 errorCode = "noSuchNodeType";
75  4 } else if (exception instanceof NoSuchWorkspaceException) {
76  0 status = INTERNAL_SERVER_ERROR;
77  0 errorCode = "noSuchWorkspace";
78  4 } else if (exception instanceof ValueFormatException) {
79  0 status = BAD_REQUEST;
80  0 errorCode = "invalidValueFormat";
81  4 } else if (exception instanceof InvalidQueryException) {
82  1 status = BAD_REQUEST;
83  1 errorCode = "invalidQuery";
84  1 String errorName = StringUtils.substringBefore(exception.getClass().getSimpleName(), "Exception");
85  1 errorMessage = StringUtils.capitalize(UPPER_CAMEL.to(LOWER_UNDERSCORE, errorName)).replaceAll("_", " ");
86  3 } else if (exception instanceof PathNotFoundException) {
87  1 status = NOT_FOUND;
88  1 errorCode = "pathNotFound";
89    }
90   
91  5 log.error("Exception thrown executing REST endpoint, returning {}", status.getStatusCode(), exception);
92   
93  5 return Response.status(status.getStatusCode())
94    .entity(new RestError(errorCode, errorMessage))
95    .build();
96    }
97    }