1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rest.internal.exceptions

File XWikiRestExceptionMapper.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart5.png
74% of files have more coverage

Code metrics

6
9
1
1
70
38
4
0.44
9
1
4

Classes

Class Line # Actions
XWikiRestExceptionMapper 47 9 0% 4 8
0.550%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.rest.internal.exceptions;
21   
22    import javax.inject.Named;
23    import javax.inject.Singleton;
24    import javax.ws.rs.core.MediaType;
25    import javax.ws.rs.core.Response;
26    import javax.ws.rs.core.Response.Status;
27    import javax.ws.rs.ext.ExceptionMapper;
28    import javax.ws.rs.ext.Provider;
29   
30    import org.xwiki.component.annotation.Component;
31    import org.xwiki.query.QueryException;
32    import org.xwiki.rest.XWikiRestComponent;
33    import org.xwiki.rest.XWikiRestException;
34   
35    import com.xpn.xwiki.XWikiException;
36   
37    /**
38    * Exception mapper for XWikiRestException, the exception raised by REST resources in case of failure.
39    *
40    * @version $Id: a3d69d4fd05f34cf7b608c4f2ed52c030cff219d $
41    * @since 4.3M2
42    */
43    @Component
44    @Named("org.xwiki.rest.internal.exceptions.XWikiRestExceptionMapper")
45    @Provider
46    @Singleton
 
47    public class XWikiRestExceptionMapper implements ExceptionMapper<XWikiRestException>, XWikiRestComponent
48    {
 
49  2 toggle @Override
50    public Response toResponse(XWikiRestException exception)
51    {
52  2 Throwable cause = exception.getCause();
53   
54  2 if (cause instanceof XWikiException) {
55  2 XWikiException xwikiException = (XWikiException) cause;
56  2 if (xwikiException.getCode() == XWikiException.ERROR_XWIKI_ACCESS_DENIED) {
57  2 return Response.status(Status.UNAUTHORIZED).entity(exception.getMessage()).type(MediaType.TEXT_PLAIN)
58    .build();
59    }
60  0 } else if (cause instanceof QueryException) {
61  0 QueryException queryException = (QueryException) cause;
62   
63  0 return Response.serverError()
64    .entity(String.format("%s\n%s\n", exception.getMessage(), queryException.getCause().getMessage()))
65    .type(MediaType.TEXT_PLAIN).build();
66    }
67   
68  0 return Response.serverError().entity(exception.getMessage()).type(MediaType.TEXT_PLAIN).build();
69    }
70    }