[SO] Release so 1.13.0 image
[so.git] / mso-api-handlers / mso-api-handler-common / src / main / java / org / onap / so / apihandlerinfra / exceptions / ApiExceptionMapper.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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=========================================================
21  */
22
23 package org.onap.so.apihandlerinfra.exceptions;
24
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;
40
41 @Provider
42 public class ApiExceptionMapper implements ExceptionMapper<ApiException> {
43
44     private static Logger logger = LoggerFactory.getLogger(ApiExceptionMapper.class);
45
46
47     private final JAXBContext context;
48     private final Marshaller marshaller;
49
50     @Context
51     private HttpHeaders headers;
52
53     public ApiExceptionMapper() {
54         try {
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);
60         }
61     }
62
63     @Override
64     public Response toResponse(ApiException exception) {
65         logger.error("Error During API Call", exception);
66         return Response.status(exception.getHttpResponseCode()).entity(buildError(exception)).build();
67     }
68
69     protected RequestError buildError(ApiException exception) {
70         String errorText = exception.getMessage();
71         String messageId = exception.getMessageID();
72         List<String> variables = exception.getVariables();
73
74         if (errorText.length() > 1999) {
75             errorText = errorText.substring(0, 1999);
76         }
77         return buildServiceErrorResponse(errorText, messageId, variables);
78
79     }
80
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);
89             }
90         }
91         requestError.setServiceException(serviceException);
92         return requestError;
93     }
94
95     protected void writeErrorLog(Exception e, String errorText, ErrorLoggerInfo errorLogInfo) {
96         if (e != null)
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(),
101                     errorText);
102
103     }
104
105     public ObjectMapper createObjectMapper() {
106         return new ObjectMapper();
107     }
108
109     public Marshaller getMarshaller() {
110         return marshaller;
111     }
112
113 }