8eda540d942ce6f1468a11cceaef34e08d2e880b
[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  *
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     private static final String IS_EMBEDDED = "isEmbedded";
74     private static final String ACTION_NOT_FOUND = "Action not found";
75
76     @RequestMapping(value = { "/report" }, method = RequestMethod.GET)
77     public ModelAndView report(HttpServletRequest request) {
78         request.getSession().setAttribute(IS_EMBEDDED, false);
79         return new ModelAndView("report");
80     }
81
82     @RequestMapping(value = { "/reportDS1" }, method = RequestMethod.GET)
83     public ModelAndView reportDS1(HttpServletRequest request) {
84         request.getSession().setAttribute(IS_EMBEDDED, false);
85         return new ModelAndView("reportDS1");
86     }
87
88     @RequestMapping(value = { "/report_embedded" }, method = RequestMethod.GET)
89     public ModelAndView reportEmbedded(HttpServletRequest request) {
90         request.getSession().setAttribute(IS_EMBEDDED, true);
91         return new ModelAndView("report_embedded");
92     }
93
94     @RequestMapping(value = { "/report_sample" }, method = RequestMethod.GET)
95     public ModelAndView reportSample(HttpServletRequest request) {
96         return new ModelAndView("report_sample");
97     }
98
99     @RequestMapping(value = { "/report_import.htm" }, method = RequestMethod.GET)
100     public ModelAndView reportImport(HttpServletRequest request) throws IOException {
101         String viewName = "report_import";
102         Action action = null;
103         String actionKey = "report.import";
104         ServletContext servletContext = request.getSession().getServletContext();
105         if (!Globals.isSystemInitialized()) {
106             Globals.initializeSystem(servletContext);
107         }
108         try {
109             action = Globals.getRaptorActionMapping().getAction(actionKey);
110             if (action == null)
111                 throw new RaptorRuntimeException(ACTION_NOT_FOUND);
112         } catch (RaptorException e) {
113             logger.debug(EELFLoggerDelegate.debugLogger, ("[Controller.processRequest]Invalid raptor action ["
114                     + actionKey + "]. RaptorException: " + e.getMessage()));
115             viewName = new ErrorHandler().processFatalError(request,
116                     new RaptorRuntimeException("[Controller.processRequest]Invalid raptor action [" + actionKey
117                             + "]. Exception: " + e.getMessage()));
118         }
119         return new ModelAndView(viewName, "model", null);
120     }
121
122     @RequestMapping(value = { "/report_wizard.htm" }, method = { RequestMethod.POST, RequestMethod.GET })
123     public ModelAndView reportWizard(HttpServletRequest request, HttpServletResponse response) throws IOException {
124         String viewName = "";
125         String actionKey = nvl(request.getParameter(AppConstants.RI_ACTION), request.getParameter("action"));
126         actionKey = nvl(actionKey, "report.run");
127         Action action = null;
128         ServletContext servletContext = request.getSession().getServletContext();
129         if (!Globals.isSystemInitialized()) {
130             Globals.initializeSystem(servletContext);
131         }
132         try {
133             action = Globals.getRaptorActionMapping().getAction(actionKey);
134             if (action == null) {
135                 throw new RaptorRuntimeException(ACTION_NOT_FOUND);
136             }
137         } catch (RaptorException e) {
138             logger.debug(EELFLoggerDelegate.debugLogger, ("[Controller.processRequest]Invalid raptor action ["
139                     + actionKey + "]. RaptorException: " + e.getMessage()));
140             viewName = (new ErrorHandler()).processFatalError(request,
141                     new RaptorRuntimeException("[Controller.processRequest]Invalid raptor action [" + actionKey
142                             + "]. Exception: " + e.getMessage()));
143             ErrorJSONRuntime errorJSONRuntime = new ErrorJSONRuntime();
144             errorJSONRuntime.setErrormessage(e.toString());
145             errorJSONRuntime.setStacktrace(getStackTrace(e));
146             ObjectMapper mapper = new ObjectMapper();
147             mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
148             mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
149             String jsonInString = "";
150             try {
151                 jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(errorJSONRuntime);
152             } catch (Exception ex) {
153                 logger.debug(EELFLoggerDelegate.debugLogger, "Exception in JSON mapper", ex);
154             }
155         }
156
157         try {
158             Class<?>[] paramTypes = new Class[2];
159             paramTypes[0] = Class.forName("javax.servlet.http.HttpServletRequest");
160             paramTypes[1] = Class.forName("java.lang.String");
161
162             if (action != null) {
163                 Class<?> handlerClass = Class.forName(action.getControllerClass());
164                 Object handler = handlerClass.newInstance();
165                 Method handlerMethod = handlerClass.getMethod(action.getControllerMethod(), paramTypes);
166
167                 Object[] paramValues = new Object[2];
168                 paramValues[0] = request;
169                 paramValues[1] = action.getJspName();
170                 viewName = (String) handlerMethod.invoke(handler, paramValues);
171             } else {
172                 throw new RaptorRuntimeException(ACTION_NOT_FOUND);
173             }
174         } catch (Exception e) {
175             logger.debug(EELFLoggerDelegate.debugLogger, ("[Controller.processRequest]Invalid raptor action ["
176                     + actionKey + "]. Exception: " + e.getMessage()));
177             viewName = (new ErrorHandler()).processFatalError(request,
178                     new RaptorRuntimeException(
179                             "[Controller.processRequest] Unable to instantiate and invoke action handler. Exception: "
180                                     + e.getMessage()));
181
182             ErrorJSONRuntime errorJSONRuntime = new ErrorJSONRuntime();
183             errorJSONRuntime.setErrormessage(e.toString());
184             errorJSONRuntime.setStacktrace(getStackTrace(e));
185             ObjectMapper mapper = new ObjectMapper();
186             mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
187             mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
188             String jsonInString = "";
189             try {
190                 jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(errorJSONRuntime);
191             } catch (Exception ex) {
192                 logger.debug(EELFLoggerDelegate.debugLogger, "Exception in JSON mapper", ex);
193             }
194         }
195         return new ModelAndView(viewName, "model", null);
196     }
197
198     private String nvl(String s) {
199         return (s == null) ? "" : s;
200     }
201
202     private String nvl(String s, String sDefault) {
203         return nvl(s).equals("") ? sDefault : s;
204     }
205
206     public static String getStackTrace(Throwable aThrowable) {
207         Writer result = new StringWriter();
208         PrintWriter printWriter = new PrintWriter(result);
209         aThrowable.printStackTrace(printWriter);
210         return result.toString();
211     }
212
213 }