Clover icon

sunshower-sdk

  1. Project Clover database Sat May 12 2018 05:15:40 UTC
  2. Package io.sunshower.sdk.v1

File ExceptionMappings.java

 

Coverage histogram

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

Code metrics

4
27
11
9
156
129
13
0.48
2.45
1.22
1.18

Classes

Class Line # Actions
ExceptionMappings 27 20 6
0.928571492.9%
ExceptionMappings.ExceptionResponse 81 0 0
-1.0 -
BadRequestResponse 86 1 1
1.0100%
NotFoundExceptionResponse 96 1 1
0.00%
RepositoryExceptionResponse 107 1 1
0.00%
NoSuchElementException 118 1 1
1.0100%
ConstraintViolationResponse 128 1 1
0.00%
AuthenticationFailedResponse 138 1 1
1.0100%
ForbiddenResponse 148 1 1
1.0100%
 

Contributing tests

This file is covered by 16 tests. .

Source view

1    package io.sunshower.sdk.v1;
2   
3    import io.sunshower.core.security.InvalidCredentialException;
4    import io.sunshower.core.security.InvalidTokenException;
5    import org.springframework.security.access.AccessDeniedException;
6    import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException;
7   
8    import javax.persistence.EntityNotFoundException;
9    import javax.persistence.NoResultException;
10    import javax.ws.rs.Consumes;
11    import javax.ws.rs.NotFoundException;
12    import javax.ws.rs.Produces;
13    import javax.ws.rs.WebApplicationException;
14    import javax.ws.rs.core.MediaType;
15    import javax.ws.rs.core.Response;
16    import javax.ws.rs.ext.ExceptionMapper;
17    import javax.ws.rs.ext.Provider;
18    import java.util.HashMap;
19    import java.util.Map;
20    import java.util.Set;
21    import java.util.logging.Level;
22    import java.util.logging.Logger;
23   
24    @Provider
25    @Produces(MediaType.APPLICATION_JSON)
26    @Consumes(MediaType.APPLICATION_JSON)
 
27    public class ExceptionMappings implements ExceptionMapper<Throwable> {
28   
29    static final Logger logger = Logger.getLogger(ExceptionMappings.class.getName());
30   
31    static final Map<Class<? extends Throwable>, ExceptionResponse> mappings = new HashMap<>();
32   
 
33  1 toggle static {
34  1 register(AuthenticationCredentialsNotFoundException.class, new AuthenticationFailedResponse());
35   
36  1 register(AccessDeniedException.class, new ForbiddenResponse());
37   
38  1 register(InvalidCredentialException.class, new AuthenticationFailedResponse());
39   
40  1 register(InvalidTokenException.class, new AuthenticationFailedResponse());
41   
42  1 register(IllegalArgumentException.class, new BadRequestResponse());
43   
44  1 register(NoResultException.class, new NoSuchElementException());
45   
46  1 register(java.util.NoSuchElementException.class, new NoSuchElementException());
47   
48  1 register(EntityNotFoundException.class, new NotFoundExceptionResponse());
49   
50  1 register(NotFoundException.class, new NotFoundExceptionResponse());
51    }
52   
 
53  0 toggle public static Set<Map.Entry<Class<? extends Throwable>, ExceptionResponse>> getMappings() {
54  0 return mappings.entrySet();
55    }
56   
 
57  9 toggle static void register(Class<? extends Throwable> key, ExceptionResponse response) {
58  9 mappings.put(key, response);
59    }
60   
 
61  17 toggle @Override
62    public Response toResponse(Throwable exception) {
63  17 exception.printStackTrace();
64  17 if (exception instanceof WebApplicationException) {
65  3 return ((WebApplicationException) exception).getResponse();
66    }
67  14 final ExceptionResponse exceptionResponse = mappings.get(exception.getClass());
68  14 if (exceptionResponse == null) {
69  4 logger.log(
70    Level.WARNING,
71    "Encountered an exception while processing REST request: {0}",
72    exception.getMessage());
73  4 logger.log(Level.WARNING, "Details:", exception);
74  4 return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
75    .entity(exception.getMessage())
76    .build();
77    }
78  10 return exceptionResponse.create(exception);
79    }
80   
 
81    public interface ExceptionResponse {
82    Response create(Throwable throwable);
83    }
84    }
85   
 
86    class BadRequestResponse implements ExceptionMappings.ExceptionResponse {
 
87  1 toggle @Override
88    public Response create(Throwable throwable) {
89  1 return Response.status(Response.Status.BAD_REQUEST)
90    .type(MediaType.APPLICATION_JSON_TYPE)
91    .entity(new Message((throwable.getMessage())))
92    .build();
93    }
94    }
95   
 
96    class NotFoundExceptionResponse implements ExceptionMappings.ExceptionResponse {
97   
 
98  0 toggle @Override
99    public Response create(Throwable throwable) {
100  0 return Response.status(Response.Status.NOT_FOUND)
101    .type(MediaType.APPLICATION_JSON_TYPE)
102    .entity(new Message((throwable.getMessage())))
103    .build();
104    }
105    }
106   
 
107    class RepositoryExceptionResponse implements ExceptionMappings.ExceptionResponse {
108   
 
109  0 toggle @Override
110    public Response create(Throwable throwable) {
111  0 return Response.status(Response.Status.BAD_REQUEST)
112    .type(MediaType.APPLICATION_JSON_TYPE)
113    .entity(new Message(throwable.getMessage()))
114    .build();
115    }
116    }
117   
 
118    class NoSuchElementException implements ExceptionMappings.ExceptionResponse {
 
119  3 toggle @Override
120    public Response create(Throwable throwable) {
121  3 return Response.status(Response.Status.NOT_FOUND)
122    .type(MediaType.APPLICATION_JSON_TYPE)
123    .entity(new Message(throwable.getMessage()))
124    .build();
125    }
126    }
127   
 
128    class ConstraintViolationResponse implements ExceptionMappings.ExceptionResponse {
 
129  0 toggle @Override
130    public Response create(Throwable throwable) {
131  0 return Response.status(Response.Status.BAD_REQUEST)
132    .type(MediaType.APPLICATION_JSON_TYPE)
133    .entity(new Message(throwable.getMessage()))
134    .build();
135    }
136    }
137   
 
138    class AuthenticationFailedResponse implements ExceptionMappings.ExceptionResponse {
 
139  4 toggle @Override
140    public Response create(Throwable throwable) {
141  4 return Response.status(Response.Status.UNAUTHORIZED)
142    .type(MediaType.APPLICATION_JSON_TYPE)
143    .entity(new Message(throwable.getMessage()))
144    .build();
145    }
146    }
147   
 
148    class ForbiddenResponse implements ExceptionMappings.ExceptionResponse {
 
149  2 toggle @Override
150    public Response create(Throwable throwable) {
151  2 return Response.status(Response.Status.FORBIDDEN)
152    .type(MediaType.APPLICATION_JSON_TYPE)
153    .entity(new Message(throwable.getMessage()))
154    .build();
155    }
156    }