2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * Modifications copyright (c) 2019 Nokia
20 * ================================================================================
22 package org.openecomp.sdc.be.servlets.exception;
24 import com.google.gson.Gson;
25 import com.google.gson.GsonBuilder;
26 import org.onap.sdc.security.RepresentationUtils;
27 import org.openecomp.sdc.be.components.impl.ResponseFormatManager;
28 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
29 import org.openecomp.sdc.be.impl.ComponentsUtils;
30 import org.openecomp.sdc.common.log.wrappers.Logger;
31 import org.openecomp.sdc.exception.ResponseFormat;
32 import org.springframework.stereotype.Component;
34 import javax.servlet.http.HttpServletResponse;
35 import javax.ws.rs.core.Response;
36 import javax.ws.rs.ext.ExceptionMapper;
37 import javax.ws.rs.ext.Provider;
38 import java.io.IOException;
42 public class ComponentExceptionMapper implements ExceptionMapper<ComponentException> {
44 private static final Logger log = Logger.getLogger(ComponentExceptionMapper.class);
45 private final Gson gson = new GsonBuilder().setPrettyPrinting().create();
46 private final ComponentsUtils componentsUtils;
48 public ComponentExceptionMapper(ComponentsUtils componentsUtils) {
49 this.componentsUtils = componentsUtils;
53 public Response toResponse(ComponentException exception) {
54 // TODO log this? BeEcompErrorManager.getInstance().logBeRestApiGeneralError(requestURI);
55 log.debug("#toResponse - An error occurred: ", exception);
56 ResponseFormat responseFormat = exception.getResponseFormat();
57 if (exception.getResponseFormat()==null) {
58 if (exception.getResource() == null) {
59 responseFormat = componentsUtils.getResponseFormat(exception.getActionStatus(), exception.getParams());
62 responseFormat = componentsUtils.getResponseFormatByResource(exception.getActionStatus(), exception.getResource());
66 return Response.status(responseFormat.getStatus())
67 .entity(gson.toJson(responseFormat.getRequestError()))
71 public void writeToResponse(ComponentException ce, HttpServletResponse httpResponse) throws IOException {
72 log.info("Error during request filter= {}", ce.getActionStatus());
73 ResponseFormat responseFormat = ResponseFormatManager.getInstance()
74 .getResponseFormat(ce.getActionStatus(), ce.getParams());
75 httpResponse.setStatus(responseFormat.getStatus());
76 httpResponse.setContentType("application/json");
77 httpResponse.setCharacterEncoding("UTF-8");
78 httpResponse.getWriter().write(RepresentationUtils.toRepresentation(responseFormat.getRequestError()));