2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (c) 2019 Samsung
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.so.apihandlerinfra.exceptions;
25 import java.util.List;
26 import javax.ws.rs.core.Context;
27 import javax.ws.rs.core.HttpHeaders;
28 import javax.ws.rs.core.Response;
29 import javax.ws.rs.ext.ExceptionMapper;
30 import javax.ws.rs.ext.Provider;
31 import javax.xml.bind.JAXBContext;
32 import javax.xml.bind.JAXBException;
33 import javax.xml.bind.Marshaller;
34 import org.onap.so.apihandlerinfra.logging.ErrorLoggerInfo;
35 import org.onap.so.serviceinstancebeans.RequestError;
36 import org.onap.so.serviceinstancebeans.ServiceException;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import com.fasterxml.jackson.databind.ObjectMapper;
42 public class ApiExceptionMapper implements ExceptionMapper<ApiException> {
44 private static Logger logger = LoggerFactory.getLogger(ApiExceptionMapper.class);
47 private final JAXBContext context;
48 private final Marshaller marshaller;
51 private HttpHeaders headers;
53 public ApiExceptionMapper() {
55 context = JAXBContext.newInstance(RequestError.class);
56 marshaller = context.createMarshaller();
57 } catch (JAXBException e) {
58 logger.debug("could not create JAXB marshaller");
59 throw new IllegalStateException(e);
64 public Response toResponse(ApiException exception) {
65 logger.error("Error During API Call", exception);
66 return Response.status(exception.getHttpResponseCode()).entity(buildError(exception)).build();
69 protected RequestError buildError(ApiException exception) {
70 String errorText = exception.getMessage();
71 String messageId = exception.getMessageID();
72 List<String> variables = exception.getVariables();
74 if (errorText.length() > 1999) {
75 errorText = errorText.substring(0, 1999);
77 return buildServiceErrorResponse(errorText, messageId, variables);
81 protected RequestError buildServiceErrorResponse(String errorText, String messageId, List<String> variables) {
82 RequestError requestError = new RequestError();
83 ServiceException serviceException = new ServiceException();
84 serviceException.setMessageId(messageId);
85 serviceException.setText(errorText);
86 if (variables != null) {
87 for (String variable : variables) {
88 serviceException.getVariables().add(variable);
91 requestError.setServiceException(serviceException);
95 protected void writeErrorLog(Exception e, String errorText, ErrorLoggerInfo errorLogInfo) {
97 logger.error("Exception occurred", e);
98 if (errorLogInfo != null)
99 logger.error(errorLogInfo.getLoggerMessageType().toString(), errorLogInfo.getErrorSource(),
100 errorLogInfo.getTargetEntity(), errorLogInfo.getTargetServiceName(), errorLogInfo.getErrorCode(),
105 public ObjectMapper createObjectMapper() {
106 return new ObjectMapper();
109 public Marshaller getMarshaller() {