17052f4a9e8fdd03a3d185c2495d17c81c296b33
[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.system.fusion.web;
21
22 import java.io.IOException;
23 import java.io.PrintWriter;
24 import java.io.StringWriter;
25 import java.io.Writer;
26 import java.lang.reflect.Method;
27
28 import javax.servlet.ServletContext;
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
31
32 import org.openecomp.portalsdk.analytics.controller.Action;
33 import org.openecomp.portalsdk.analytics.controller.ErrorHandler;
34 import org.openecomp.portalsdk.analytics.error.RaptorException;
35 import org.openecomp.portalsdk.analytics.error.RaptorRuntimeException;
36 import org.openecomp.portalsdk.analytics.model.runtime.ErrorJSONRuntime;
37 import org.openecomp.portalsdk.analytics.system.Globals;
38 import org.openecomp.portalsdk.analytics.util.AppConstants;
39 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
40 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
41 import org.springframework.stereotype.Controller;
42 import org.springframework.web.bind.annotation.RequestMapping;
43 import org.springframework.web.bind.annotation.RequestMethod;
44 import org.springframework.web.servlet.ModelAndView;
45
46 import com.fasterxml.jackson.databind.DeserializationFeature;
47 import com.fasterxml.jackson.databind.ObjectMapper;
48 import com.fasterxml.jackson.databind.SerializationFeature;
49
50 @Controller
51 @RequestMapping("/")
52 public class RaptorController extends RestrictedBaseController {
53
54         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RaptorController.class);
55
56         @RequestMapping(value = { "/report" }, method = RequestMethod.GET)
57         public ModelAndView report(HttpServletRequest request) {
58                 request.getSession().setAttribute("isEmbedded", false);
59                 return new ModelAndView("report");
60         }
61         
62         @RequestMapping(value = { "/reportDS1" }, method = RequestMethod.GET)
63         public ModelAndView reportDS1(HttpServletRequest request) {
64                 request.getSession().setAttribute("isEmbedded", false);
65                 return new ModelAndView("reportDS1");
66         }       
67
68         @RequestMapping(value = { "/report_embedded" }, method = RequestMethod.GET)
69         public ModelAndView reportEmbedded(HttpServletRequest request) {
70                 request.getSession().setAttribute("isEmbedded", true);
71                 return new ModelAndView("report_embedded");
72         }
73
74         @RequestMapping(value = { "/report_sample" }, method = RequestMethod.GET)
75         public ModelAndView reportSample(HttpServletRequest request) {
76                 return new ModelAndView("report_sample");
77         }
78
79         @RequestMapping(value = { "/report_import.htm" }, method = RequestMethod.GET)
80         public ModelAndView reportImport(HttpServletRequest request) throws IOException {
81                 String viewName = "report_import";
82                 Action action = null;
83                 String actionKey = "report.import";
84                 ServletContext servletContext = request.getSession().getServletContext();
85                 if (!Globals.isSystemInitialized()) {
86                         Globals.initializeSystem(servletContext);
87                 }
88                 try {
89                         action = Globals.getRaptorActionMapping().getAction(actionKey);
90                         if (action == null)
91                                 throw new RaptorRuntimeException("Action not found");
92                 } catch (RaptorException e) {
93                         logger.debug(EELFLoggerDelegate.debugLogger, ("[Controller.processRequest]Invalid raptor action ["
94                                         + actionKey + "]. RaptorException: " + e.getMessage()));
95                         viewName = new ErrorHandler().processFatalError(request,
96                                         new RaptorRuntimeException("[Controller.processRequest]Invalid raptor action [" + actionKey
97                                                         + "]. Exception: " + e.getMessage()));
98                 }
99                 return new ModelAndView(viewName, "model", null);
100         }
101
102         @RequestMapping(value = { "/report_wizard.htm" }, method = { RequestMethod.POST, RequestMethod.GET })
103         public ModelAndView reportWizard(HttpServletRequest request, HttpServletResponse response) throws IOException {
104                 String viewName = "";
105                 String actionKey = nvl(request.getParameter(AppConstants.RI_ACTION), request.getParameter("action"));
106                 actionKey = nvl(actionKey, "report.run");
107                 Action action = null;
108                 ServletContext servletContext = request.getSession().getServletContext();
109                 if (!Globals.isSystemInitialized()) {
110                         Globals.initializeSystem(servletContext);
111                 }
112                 try {
113                         action = Globals.getRaptorActionMapping().getAction(actionKey);
114                         if (action == null)
115                                 throw new RaptorRuntimeException("Action not found");
116                 } catch (RaptorException e) {
117                         logger.debug(EELFLoggerDelegate.debugLogger, ("[Controller.processRequest]Invalid raptor action ["
118                                         + actionKey + "]. RaptorException: " + e.getMessage()));
119                         viewName = (new ErrorHandler()).processFatalError(request,
120                                         new RaptorRuntimeException("[Controller.processRequest]Invalid raptor action [" + actionKey
121                                                         + "]. Exception: " + e.getMessage()));
122                         ErrorJSONRuntime errorJSONRuntime = new ErrorJSONRuntime();
123                         errorJSONRuntime.setErrormessage(e.toString());
124                         errorJSONRuntime.setStacktrace(getStackTrace(e));
125                         ObjectMapper mapper = new ObjectMapper();
126                         mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
127                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
128                         String jsonInString = "";
129                         try {
130                                 jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(errorJSONRuntime);
131                         } catch (Exception ex) {
132                                 ex.printStackTrace();
133                                 
134                         }
135                 }
136
137                 try {
138                         Class<?>[] paramTypes = new Class[2];
139                         paramTypes[0] = Class.forName("javax.servlet.http.HttpServletRequest");
140                         paramTypes[1] = Class.forName("java.lang.String");
141
142                         Class<?> handlerClass = Class.forName(action.getControllerClass());
143                         Object handler = handlerClass.newInstance();
144                         Method handlerMethod = handlerClass.getMethod(action.getControllerMethod(), paramTypes);
145
146                         Object[] paramValues = new Object[2];
147                         paramValues[0] = request;
148                         paramValues[1] = action.getJspName();
149                         viewName = (String) handlerMethod.invoke(handler, paramValues);
150                 } catch (Exception e) {
151                         logger.debug(EELFLoggerDelegate.debugLogger, ("[Controller.processRequest]Invalid raptor action ["
152                                         + actionKey + "]. Exception: " + e.getMessage()));
153                         viewName = (new ErrorHandler()).processFatalError(request,
154                                         new RaptorRuntimeException(
155                                                         "[Controller.processRequest] Unable to instantiate and invoke action handler. Exception: "
156                                                                         + e.getMessage()));
157                         
158                         ErrorJSONRuntime errorJSONRuntime = new ErrorJSONRuntime();
159                         errorJSONRuntime.setErrormessage(e.toString());
160                         errorJSONRuntime.setStacktrace(getStackTrace(e));
161                         ObjectMapper mapper = new ObjectMapper();
162                         mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
163                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
164                         String jsonInString = "";
165                         try {
166                                 jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(errorJSONRuntime);
167                         } catch (Exception ex) {
168                                 ex.printStackTrace();
169                                 
170                         }
171                 }
172                 return new ModelAndView(viewName, "model", null);
173         }
174
175         private String nvl(String s) {
176                 return (s == null) ? "" : s;
177         }
178
179         private String nvl(String s, String sDefault) {
180                 return nvl(s).equals("") ? sDefault : s;
181         }
182         
183           public static String getStackTrace(Throwable aThrowable) {
184                     Writer result = new StringWriter();
185                     PrintWriter printWriter = new PrintWriter(result);
186                     aThrowable.printStackTrace(printWriter);
187                     return result.toString();
188                   }
189
190 }