7603d499c59c176d032ead402f5a6b9caef5b7e1
[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.v3.oas.annotations.Operation;
27 import io.swagger.v3.oas.annotations.media.Content;
28 import io.swagger.v3.oas.annotations.media.Schema;
29 import io.swagger.v3.oas.annotations.responses.ApiResponse;
30 import io.swagger.v3.oas.annotations.servers.Server;
31 import io.swagger.v3.oas.annotations.servers.Servers;
32 import io.swagger.v3.oas.annotations.tags.Tag;
33 import io.swagger.v3.oas.annotations.tags.Tags;
34 import org.openecomp.sdc.be.dao.api.ActionStatus;
35 import org.openecomp.sdc.be.impl.ComponentsUtils;
36 import org.openecomp.sdc.common.log.wrappers.Logger;
37 import org.openecomp.sdc.exception.ResponseFormat;
38 import org.springframework.stereotype.Controller;
39
40 import javax.ws.rs.GET;
41 import javax.ws.rs.Path;
42 import javax.ws.rs.Produces;
43 import javax.ws.rs.core.MediaType;
44 import javax.ws.rs.core.Response;
45
46 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
47 @Path("/v1/catalog")
48 @Tags({@Tag(name = "SDC Internal APIs")})
49 @Servers({@Server(url = "/sdc2/rest")})
50 @Produces(MediaType.APPLICATION_JSON)
51 @Controller
52 public class ExceptionHandlerEndpoint {
53     private static final Logger log = Logger.getLogger(ExceptionHandlerEndpoint.class);
54     private final Gson gson = new GsonBuilder().setPrettyPrinting().create();
55     private final ComponentsUtils componentsUtils;
56
57     ExceptionHandlerEndpoint(ComponentsUtils componentsUtils) {
58         this.componentsUtils = componentsUtils;
59     }
60
61     @GET
62     @Path("/handleException")
63     @Operation(description = "Handle exception", method = "GET", responses = {
64             @ApiResponse(responseCode = "500", description = "Internal Error",
65                     content = @Content(schema = @Schema(implementation = Response.class)))})
66     public Response sendError() {
67         log.debug("Request is received");
68
69         ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR);
70         return Response.status(responseFormat.getStatus())
71                 .entity(gson.toJson(responseFormat.getRequestError()))
72                 .build();
73     }
74 }