aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ComponentExceptionMapper.java
blob: fb226bb842a3f2bae8ba0803393b80561216802e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package org.openecomp.sdc.be.servlets;

import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
import org.openecomp.sdc.be.impl.ComponentsUtils;
import org.openecomp.sdc.exception.ResponseFormat;
import org.springframework.stereotype.Component;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

@Component
@Provider
public class ComponentExceptionMapper implements ExceptionMapper<ComponentException> {

    private final ComponentsUtils componentsUtils;
    protected Gson gson = new GsonBuilder().setPrettyPrinting().create();

    public ComponentExceptionMapper(ComponentsUtils componentsUtils) {
        this.componentsUtils = componentsUtils;
    }

    @Override
    public Response toResponse(ComponentException componentException) {
        ResponseFormat responseFormat = componentException.getResponseFormat();
        if (componentException.getResponseFormat()==null) {
            responseFormat = componentsUtils.getResponseFormat(componentException.getActionStatus(), componentException.getParams());
        }

        return Response.status(responseFormat.getStatus())
                .entity(gson.toJson(responseFormat.getRequestError()))
                .build();
    }

}