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