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=========================================================
21 package org.onap.so.apihandlerinfra.exceptions;
23 import java.util.List;
25 import javax.ws.rs.core.Response;
26 import javax.ws.rs.ext.ExceptionMapper;
27 import javax.ws.rs.ext.Provider;
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;
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;
44 public class ApiExceptionMapper implements ExceptionMapper<ApiException> {
46 private static MsoLogger logger = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA, ApiExceptionMapper.class);
47 private static MsoAlarmLogger alarmLogger = new MsoAlarmLogger();
49 public Response toResponse(ApiException exception) {
51 return Response.status(exception.getHttpResponseCode()).entity(buildErrorString(exception)).build();
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();
62 if (errorText.length() > 1999) {
63 errorText = errorText.substring(0, 1999);
66 writeErrorLog(errorText, errorLoggerInfo, alarmLoggerInfo);
68 return buildServiceErrorResponse(errorText,messageId,variables);
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);
82 re.setServiceException(se);
83 String requestErrorStr;
85 ObjectMapper mapper = createObjectMapper();
86 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
87 mapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT);
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);
96 return requestErrorStr;
99 protected void writeErrorLog(String errorText, ErrorLoggerInfo errorLogInfo, AlarmLoggerInfo alarmLogInfo) {
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());
108 public ObjectMapper createObjectMapper(){
109 return new ObjectMapper();