75d88d0f06f2e9d7c1f171a5957cb61b717c0da1
[portal/sdk.git] /
1 /*-
2  * ================================================================================
3  * eCOMP Portal SDK
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
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  * ================================================================================
19  */
20 package org.openecomp.portalsdk.analytics.controller;
21
22 import java.io.PrintWriter;
23 import java.io.StringWriter;
24 import java.io.Writer;
25 import java.util.*;
26 import javax.servlet.http.*;
27
28 import org.openecomp.portalsdk.analytics.error.*;
29 import org.openecomp.portalsdk.analytics.model.definition.ReportDefinition;
30 import org.openecomp.portalsdk.analytics.model.runtime.ErrorJSONRuntime;
31 import org.openecomp.portalsdk.analytics.model.runtime.ReportRuntime;
32 import org.openecomp.portalsdk.analytics.system.*;
33 import org.openecomp.portalsdk.analytics.util.*;
34 import org.openecomp.portalsdk.core.logging.format.AlarmSeverityEnum;
35 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
36
37 import com.fasterxml.jackson.databind.DeserializationFeature;
38 import com.fasterxml.jackson.databind.ObjectMapper;
39 import com.fasterxml.jackson.databind.SerializationFeature;
40
41 public class ErrorHandler extends org.openecomp.portalsdk.analytics.RaptorObject {
42
43
44         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ErrorHandler.class);
45         
46         public ErrorHandler() {
47         }
48
49         public void processError(HttpServletRequest request, String errorMsg) {
50                 //Log.write(errorMsg, 2);
51                 logger.error(EELFLoggerDelegate.debugLogger, (errorMsg));
52                 ArrayList error_list = (ArrayList) request.getAttribute(AppConstants.RI_ERROR_LIST);
53                 if (error_list == null)
54                         error_list = new ArrayList(1);
55                 error_list.add(errorMsg);
56                 request.setAttribute(AppConstants.RI_ERROR_LIST, error_list);
57         } // processError
58
59         public void processError(HttpServletRequest request, RaptorException e) {
60                 processError(request, "Exception: " + e.getMessage());
61         } // processError
62
63     private String getSessionLog(HttpServletRequest request) {
64                 String[] sessionVariablesToLog = Globals.getLogVariablesInSession().split(",");
65                 StringBuffer sessionLogStrBuf = new StringBuffer("\n");
66                 sessionLogStrBuf.append("***** ADDITIONAL INFORMATION ******");
67                 HttpSession session = request.getSession();
68                 ReportRuntime rr = (ReportRuntime) session.getAttribute(AppConstants.SI_REPORT_RUNTIME);
69                 ReportDefinition rdef = (ReportDefinition) session.getAttribute(AppConstants.SI_REPORT_DEFINITION);             
70                 if(rr!=null) {
71                         sessionLogStrBuf.append("\nWHILE RUNNING");
72                         sessionLogStrBuf.append("\nReport Id="+rr.getReportID()+";\t");
73                         sessionLogStrBuf.append("Report Name="+rr.getReportName()+";\t\n");
74                 } else if (rdef != null) {
75                         sessionLogStrBuf.append("\nWHILE CREATING/UPDATING");
76                         sessionLogStrBuf.append("\nReport Id="+rdef.getReportID()+";\t");
77                         sessionLogStrBuf.append("Report Name="+rdef.getReportName()+";\t\n");
78                 }
79         for (int i = 0; i < sessionVariablesToLog.length; i++) {
80                 if(session.getAttribute(sessionVariablesToLog[i])!=null)
81                 sessionLogStrBuf.append(sessionVariablesToLog[i]+"="+(String)session.getAttribute(sessionVariablesToLog[i])+";\t");
82         }
83                 sessionLogStrBuf.append("\n***********************************");
84                 sessionLogStrBuf.append("\n");
85                 return sessionLogStrBuf.toString();
86     }
87         public String processFatalError(HttpServletRequest request, RaptorException e) {
88                 //Log.write("Fatal error [" + e.getClass().getName() + "]: " + nvl(e.getMessage()), 1);
89                 logger.error(EELFLoggerDelegate.debugLogger, ("[EXCEPTION ENCOUNTERED IN RAPTOR] Fatal error [" + e.getClass().getName() + "]: " + nvl(e.getMessage())+" "+ getSessionLog(request) + e.getMessage()),AlarmSeverityEnum.MAJOR);
90                 if (e instanceof ReportSQLException) {
91                         String errorSQL = ((ReportSQLException) e).getReportSQL();
92                         if (nvl(errorSQL).length() > 0)
93                                 request.setAttribute("c_error_sql", errorSQL);
94                 } // if
95                 AppUtils.processErrorNotification(request, e);
96
97                 request.setAttribute(AppConstants.RI_EXCEPTION, e);
98                 return AppUtils.getErrorPage();
99         } // processFatalError
100         
101         public String processFatalErrorJSON(HttpServletRequest request, RaptorException e) {
102                 //Log.write("Fatal error [" + e.getClass().getName() + "]: " + nvl(e.getMessage()), 1);
103                 logger.error(EELFLoggerDelegate.debugLogger, ("[EXCEPTION ENCOUNTERED IN RAPTOR] Fatal error [" + e.getClass().getName() + "]: " + nvl(e.getMessage())+" "+ getSessionLog(request) + e.getMessage()),AlarmSeverityEnum.MAJOR);
104                 if (e instanceof ReportSQLException) {
105                         String errorSQL = ((ReportSQLException) e).getReportSQL();
106                         if (nvl(errorSQL).length() > 0)
107                                 request.setAttribute("c_error_sql", errorSQL);
108                 } // if
109                 //AppUtils.processErrorNotification(request, e);
110
111                 request.setAttribute(AppConstants.RI_EXCEPTION, e);
112                 ErrorJSONRuntime errorJSONRuntime = new ErrorJSONRuntime();
113                 errorJSONRuntime.setErrormessage(e.toString());
114                 errorJSONRuntime.setStacktrace(getStackTrace(e));
115                 ObjectMapper mapper = new ObjectMapper();
116                 //mapper.setVisibility(JsonMethod.FIELD, Visibility.ANY);
117                 //mapper.setVisibilityChecker(mapper.getVisibilityChecker().with(JsonAutoDetect.Visibility.NONE));
118                 mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
119                 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
120                 String jsonInString = "";
121                 try {
122                         jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(errorJSONRuntime);
123                 } catch (Exception ex) {
124                         ex.printStackTrace();
125                         
126                 }
127                 return jsonInString;
128         } // processFatalError
129         
130           public static String getStackTrace(Throwable aThrowable) {
131                     Writer result = new StringWriter();
132                     PrintWriter printWriter = new PrintWriter(result);
133                     aThrowable.printStackTrace(printWriter);
134                     return result.toString();
135                   }     
136         public String processFatalErrorWMenu(HttpServletRequest request, RaptorException e) {
137                 //Log.write("Fatal error [" + e.getClass().getName() + "]: " + nvl(e.getMessage()), 1);
138                 logger.error(EELFLoggerDelegate.debugLogger, ("[EXCEPTION ENCOUNTERED IN RAPTOR] Fatal error [" + e.getClass().getName() + "]: " + nvl(e.getMessage())+" "+ getSessionLog(request) + e.getMessage()),AlarmSeverityEnum.MAJOR);
139                 if (e instanceof ReportSQLException) {
140                         String errorSQL = ((ReportSQLException) e).getReportSQL();
141                         if (nvl(errorSQL).length() > 0)
142                                 request.setAttribute("c_error_sql", errorSQL);
143                 } // if
144                 AppUtils.processErrorNotification(request, e);
145
146                 request.setAttribute(AppConstants.RI_EXCEPTION, e);
147                 return AppUtils.getErrorPageWMenu();
148         } // processFatalError
149         
150 } // ErrorHandler
151