668431d5caa1fb2b2b7a978187c54f5ee1bd8d30
[portal/sdk.git] /
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal SDK
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the “License”);
10  * you may not use this software 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  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the “License”);
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.onap.portalsdk.analytics.system.fusion.web;
39
40 import java.io.IOException;
41 import java.io.PrintWriter;
42 import java.io.StringWriter;
43 import java.io.Writer;
44 import java.lang.reflect.Method;
45
46 import javax.servlet.ServletContext;
47 import javax.servlet.http.HttpServletRequest;
48 import javax.servlet.http.HttpServletResponse;
49
50 import org.onap.portalsdk.analytics.controller.Action;
51 import org.onap.portalsdk.analytics.controller.ErrorHandler;
52 import org.onap.portalsdk.analytics.error.RaptorException;
53 import org.onap.portalsdk.analytics.error.RaptorRuntimeException;
54 import org.onap.portalsdk.analytics.model.runtime.ErrorJSONRuntime;
55 import org.onap.portalsdk.analytics.system.Globals;
56 import org.onap.portalsdk.analytics.util.AppConstants;
57 import org.onap.portalsdk.core.controller.RestrictedBaseController;
58 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
59 import org.springframework.stereotype.Controller;
60 import org.springframework.web.bind.annotation.RequestMapping;
61 import org.springframework.web.bind.annotation.RequestMethod;
62 import org.springframework.web.servlet.ModelAndView;
63
64 import com.fasterxml.jackson.databind.DeserializationFeature;
65 import com.fasterxml.jackson.databind.ObjectMapper;
66 import com.fasterxml.jackson.databind.SerializationFeature;
67
68 @Controller
69 @RequestMapping("/")
70 public class RaptorController extends RestrictedBaseController {
71
72         private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RaptorController.class);
73
74         @RequestMapping(value = { "/report" }, method = RequestMethod.GET)
75         public ModelAndView report(HttpServletRequest request) {
76                 request.getSession().setAttribute("isEmbedded", false);
77                 return new ModelAndView("report");
78         }
79         
80         @RequestMapping(value = { "/reportDS1" }, method = RequestMethod.GET)
81         public ModelAndView reportDS1(HttpServletRequest request) {
82                 request.getSession().setAttribute("isEmbedded", false);
83                 return new ModelAndView("reportDS1");
84         }       
85
86         @RequestMapping(value = { "/report_embedded" }, method = RequestMethod.GET)
87         public ModelAndView reportEmbedded(HttpServletRequest request) {
88                 request.getSession().setAttribute("isEmbedded", true);
89                 return new ModelAndView("report_embedded");
90         }
91
92         @RequestMapping(value = { "/report_sample" }, method = RequestMethod.GET)
93         public ModelAndView reportSample(HttpServletRequest request) {
94                 return new ModelAndView("report_sample");
95         }
96
97         @RequestMapping(value = { "/report_import.htm" }, method = RequestMethod.GET)
98         public ModelAndView reportImport(HttpServletRequest request) throws IOException {
99                 String viewName = "report_import";
100                 Action action = null;
101                 String actionKey = "report.import";
102                 ServletContext servletContext = request.getSession().getServletContext();
103                 if (!Globals.isSystemInitialized()) {
104                         Globals.initializeSystem(servletContext);
105                 }
106                 try {
107                         action = Globals.getRaptorActionMapping().getAction(actionKey);
108                         if (action == null)
109                                 throw new RaptorRuntimeException("Action not found");
110                 } catch (RaptorException e) {
111                         logger.debug(EELFLoggerDelegate.debugLogger, ("[Controller.processRequest]Invalid raptor action ["
112                                         + actionKey + "]. RaptorException: " + e.getMessage()));
113                         viewName = new ErrorHandler().processFatalError(request,
114                                         new RaptorRuntimeException("[Controller.processRequest]Invalid raptor action [" + actionKey
115                                                         + "]. Exception: " + e.getMessage()));
116                 }
117                 return new ModelAndView(viewName, "model", null);
118         }
119
120         @RequestMapping(value = { "/report_wizard.htm" }, method = { RequestMethod.POST, RequestMethod.GET })
121         public ModelAndView reportWizard(HttpServletRequest request, HttpServletResponse response) throws IOException {
122                 String viewName = "";
123                 String actionKey = nvl(request.getParameter(AppConstants.RI_ACTION), request.getParameter("action"));
124                 actionKey = nvl(actionKey, "report.run");
125                 Action action = null;
126                 ServletContext servletContext = request.getSession().getServletContext();
127                 if (!Globals.isSystemInitialized()) {
128                         Globals.initializeSystem(servletContext);
129                 }
130                 try {
131                         action = Globals.getRaptorActionMapping().getAction(actionKey);
132                         if (action == null)
133                                 throw new RaptorRuntimeException("Action not found");
134                 } catch (RaptorException e) {
135                         logger.debug(EELFLoggerDelegate.debugLogger, ("[Controller.processRequest]Invalid raptor action ["
136                                         + actionKey + "]. RaptorException: " + e.getMessage()));
137                         viewName = (new ErrorHandler()).processFatalError(request,
138                                         new RaptorRuntimeException("[Controller.processRequest]Invalid raptor action [" + actionKey
139                                                         + "]. Exception: " + e.getMessage()));
140                         ErrorJSONRuntime errorJSONRuntime = new ErrorJSONRuntime();
141                         errorJSONRuntime.setErrormessage(e.toString());
142                         errorJSONRuntime.setStacktrace(getStackTrace(e));
143                         ObjectMapper mapper = new ObjectMapper();
144                         mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
145                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
146                         String jsonInString = "";
147                         try {
148                                 jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(errorJSONRuntime);
149                         } catch (Exception ex) {
150                                 ex.printStackTrace();
151                                 
152                         }
153                 }
154
155                 try {
156                         Class<?>[] paramTypes = new Class[2];
157                         paramTypes[0] = Class.forName("javax.servlet.http.HttpServletRequest");
158                         paramTypes[1] = Class.forName("java.lang.String");
159
160                         Class<?> handlerClass = Class.forName(action.getControllerClass());
161                         Object handler = handlerClass.newInstance();
162                         Method handlerMethod = handlerClass.getMethod(action.getControllerMethod(), paramTypes);
163
164                         Object[] paramValues = new Object[2];
165                         paramValues[0] = request;
166                         paramValues[1] = action.getJspName();
167                         viewName = (String) handlerMethod.invoke(handler, paramValues);
168                 } catch (Exception e) {
169                         logger.debug(EELFLoggerDelegate.debugLogger, ("[Controller.processRequest]Invalid raptor action ["
170                                         + actionKey + "]. Exception: " + e.getMessage()));
171                         viewName = (new ErrorHandler()).processFatalError(request,
172                                         new RaptorRuntimeException(
173                                                         "[Controller.processRequest] Unable to instantiate and invoke action handler. Exception: "
174                                                                         + e.getMessage()));
175                         
176                         ErrorJSONRuntime errorJSONRuntime = new ErrorJSONRuntime();
177                         errorJSONRuntime.setErrormessage(e.toString());
178                         errorJSONRuntime.setStacktrace(getStackTrace(e));
179                         ObjectMapper mapper = new ObjectMapper();
180                         mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
181                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
182                         String jsonInString = "";
183                         try {
184                                 jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(errorJSONRuntime);
185                         } catch (Exception ex) {
186                                 ex.printStackTrace();
187                                 
188                         }
189                 }
190                 return new ModelAndView(viewName, "model", null);
191         }
192
193         private String nvl(String s) {
194                 return (s == null) ? "" : s;
195         }
196
197         private String nvl(String s, String sDefault) {
198                 return nvl(s).equals("") ? sDefault : s;
199         }
200         
201           public static String getStackTrace(Throwable aThrowable) {
202                     Writer result = new StringWriter();
203                     PrintWriter printWriter = new PrintWriter(result);
204                     aThrowable.printStackTrace(printWriter);
205                     return result.toString();
206                   }
207
208 }