Containerization feature of SO
[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  * 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.onap.so.apihandlerinfra.exceptions;
22
23 import java.util.List;
24
25 import javax.ws.rs.core.Response;
26 import javax.ws.rs.ext.ExceptionMapper;
27 import javax.ws.rs.ext.Provider;
28
29 import org.onap.so.apihandlerinfra.logging.AlarmLoggerInfo;
30 import org.onap.so.apihandlerinfra.logging.ErrorLoggerInfo;
31 import org.onap.so.logger.MessageEnum;
32 import org.onap.so.logger.MsoAlarmLogger;
33 import org.onap.so.logger.MsoLogger;
34 import org.onap.so.serviceinstancebeans.RequestError;
35 import org.onap.so.serviceinstancebeans.ServiceException;
36
37
38 import com.fasterxml.jackson.annotation.JsonInclude;
39 import com.fasterxml.jackson.core.JsonProcessingException;
40 import com.fasterxml.jackson.databind.ObjectMapper;
41 import com.fasterxml.jackson.databind.SerializationFeature;
42
43 @Provider
44 public class ApiExceptionMapper implements ExceptionMapper<ApiException> {
45
46     private static MsoLogger logger = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA, ApiExceptionMapper.class);
47     private static MsoAlarmLogger alarmLogger = new MsoAlarmLogger();
48     @Override
49     public Response toResponse(ApiException exception) {
50
51         return Response.status(exception.getHttpResponseCode()).entity(buildErrorString(exception)).build();
52     }
53
54     protected String buildErrorString(ApiException exception) {
55         String errorText = exception.getMessage();
56         String messageId = exception.getMessageID();
57         List<String> variables = exception.getVariables();
58         ErrorLoggerInfo errorLoggerInfo = exception.getErrorLoggerInfo();
59         AlarmLoggerInfo alarmLoggerInfo = exception.getAlarmLoggerInfo();
60
61
62         if (errorText.length() > 1999) {
63             errorText = errorText.substring(0, 1999);
64         }
65
66         writeErrorLog(errorText, errorLoggerInfo, alarmLoggerInfo);
67
68         return buildServiceErrorResponse(errorText,messageId,variables);
69
70     }
71
72     protected String buildServiceErrorResponse(String errorText, String messageId, List<String> variables){
73         RequestError re = new RequestError();
74         ServiceException se = new ServiceException();
75         se.setMessageId(messageId);
76         se.setText(errorText);
77         if (variables != null) {
78             for (String variable : variables) {
79                 se.getVariables().add(variable);
80             }
81         }
82         re.setServiceException(se);
83         String requestErrorStr;
84
85         ObjectMapper mapper = createObjectMapper();
86         mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
87         mapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT);
88         try {
89             requestErrorStr = mapper.writeValueAsString(re);
90         } catch (JsonProcessingException e) {
91             String errorMsg = "Exception in buildServiceErrorResponse writing exceptionType to string " + e.getMessage();
92             logger.error(MessageEnum.GENERAL_EXCEPTION, "BuildServiceErrorResponse", "", "", MsoLogger.ErrorCode.DataError, errorMsg, e);
93             return errorMsg;
94         }
95
96         return requestErrorStr;
97     }
98
99     protected void writeErrorLog(String errorText, ErrorLoggerInfo errorLogInfo, AlarmLoggerInfo alarmLogInfo) {
100
101         if(errorLogInfo != null)
102             logger.error(errorLogInfo.getLoggerMessageType().toString(), errorLogInfo.getErrorSource(), errorLogInfo.getTargetEntity(), errorLogInfo.getTargetServiceName(), errorLogInfo.getErrorCode(), errorText);
103         if(alarmLogInfo != null){
104             alarmLogger.sendAlarm(alarmLogInfo.getAlarm(),alarmLogInfo.getState(),alarmLogInfo.getDetail());
105         }
106     }
107
108     public ObjectMapper createObjectMapper(){
109         return new ObjectMapper();
110     }
111 }