Refactoring Consolidation Service
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / servlets / ComponentExceptionMapper.java
1 package org.openecomp.sdc.be.servlets;
2
3 import javax.ws.rs.core.Response;
4 import javax.ws.rs.ext.ExceptionMapper;
5 import javax.ws.rs.ext.Provider;
6
7 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
8 import org.openecomp.sdc.be.impl.ComponentsUtils;
9 import org.openecomp.sdc.exception.ResponseFormat;
10 import org.springframework.stereotype.Component;
11
12 import com.google.gson.Gson;
13 import com.google.gson.GsonBuilder;
14
15 @Component
16 @Provider
17 public class ComponentExceptionMapper implements ExceptionMapper<ComponentException> {
18
19     private final ComponentsUtils componentsUtils;
20     protected Gson gson = new GsonBuilder().setPrettyPrinting().create();
21
22     public ComponentExceptionMapper(ComponentsUtils componentsUtils) {
23         this.componentsUtils = componentsUtils;
24     }
25
26     @Override
27     public Response toResponse(ComponentException componentException) {
28         ResponseFormat responseFormat = componentException.getResponseFormat();
29         if (componentException.getResponseFormat()==null) {
30             responseFormat = componentsUtils.getResponseFormat(componentException.getActionStatus(), componentException.getParams());
31         }
32
33         return Response.status(responseFormat.getStatus())
34                 .entity(gson.toJson(responseFormat.getRequestError()))
35                 .build();
36     }
37
38 }