Catalog alignment
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / servlets / ExceptionHandlerEndpoint.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.servlets;
22
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import com.jcabi.aspects.Loggable;
26 import io.swagger.annotations.Api;
27 import io.swagger.annotations.ApiOperation;
28 import io.swagger.annotations.ApiResponse;
29 import io.swagger.annotations.ApiResponses;
30 import org.openecomp.sdc.be.dao.api.ActionStatus;
31 import org.openecomp.sdc.be.impl.ComponentsUtils;
32 import org.openecomp.sdc.common.log.wrappers.Logger;
33 import org.openecomp.sdc.exception.ResponseFormat;
34 import org.springframework.stereotype.Controller;
35
36 import javax.ws.rs.GET;
37 import javax.ws.rs.Path;
38 import javax.ws.rs.Produces;
39 import javax.ws.rs.core.MediaType;
40 import javax.ws.rs.core.Response;
41
42 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
43 @Path("/v1/catalog")
44 @Api(value = "ExceptionHandling Endpoint")
45 @Produces(MediaType.APPLICATION_JSON)
46 @Controller
47 public class ExceptionHandlerEndpoint {
48     private static final Logger log = Logger.getLogger(ExceptionHandlerEndpoint.class);
49     private final Gson gson = new GsonBuilder().setPrettyPrinting().create();
50     private final ComponentsUtils componentsUtils;
51
52     ExceptionHandlerEndpoint(ComponentsUtils componentsUtils) {
53         this.componentsUtils = componentsUtils;
54     }
55
56     @GET
57     @Path("/handleException")
58     @ApiOperation(value = "Handle exception", httpMethod = "GET", response = Response.class)
59     @ApiResponses(value = {@ApiResponse(code = 500, message = "Internal Error")})
60     public Response sendError() {
61         log.debug("Request is received");
62
63         ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR);
64         return Response.status(responseFormat.getStatus())
65                 .entity(gson.toJson(responseFormat.getRequestError()))
66                 .build();
67     }
68
69 }