2 * ================================================================================
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
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 * ================================================================================
20 package org.openecomp.portalsdk.analytics.system.fusion.web;
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;
28 import javax.servlet.ServletContext;
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
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;
46 import com.fasterxml.jackson.databind.DeserializationFeature;
47 import com.fasterxml.jackson.databind.ObjectMapper;
48 import com.fasterxml.jackson.databind.SerializationFeature;
52 public class RaptorController extends RestrictedBaseController {
54 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RaptorController.class);
56 @RequestMapping(value = { "/report" }, method = RequestMethod.GET)
57 public ModelAndView report(HttpServletRequest request) {
58 request.getSession().setAttribute("isEmbedded", false);
59 return new ModelAndView("report");
62 @RequestMapping(value = { "/reportDS1" }, method = RequestMethod.GET)
63 public ModelAndView reportDS1(HttpServletRequest request) {
64 request.getSession().setAttribute("isEmbedded", false);
65 return new ModelAndView("reportDS1");
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");
74 @RequestMapping(value = { "/report_sample" }, method = RequestMethod.GET)
75 public ModelAndView reportSample(HttpServletRequest request) {
76 return new ModelAndView("report_sample");
79 @RequestMapping(value = { "/report_import.htm" }, method = RequestMethod.GET)
80 public ModelAndView reportImport(HttpServletRequest request) throws IOException {
81 String viewName = "report_import";
83 String actionKey = "report.import";
84 ServletContext servletContext = request.getSession().getServletContext();
85 if (!Globals.isSystemInitialized()) {
86 Globals.initializeSystem(servletContext);
89 action = Globals.getRaptorActionMapping().getAction(actionKey);
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()));
99 return new ModelAndView(viewName, "model", null);
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);
113 action = Globals.getRaptorActionMapping().getAction(actionKey);
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 = "";
130 jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(errorJSONRuntime);
131 } catch (Exception ex) {
132 ex.printStackTrace();
138 Class<?>[] paramTypes = new Class[2];
139 paramTypes[0] = Class.forName("javax.servlet.http.HttpServletRequest");
140 paramTypes[1] = Class.forName("java.lang.String");
142 Class<?> handlerClass = Class.forName(action.getControllerClass());
143 Object handler = handlerClass.newInstance();
144 Method handlerMethod = handlerClass.getMethod(action.getControllerMethod(), paramTypes);
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: "
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 = "";
166 jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(errorJSONRuntime);
167 } catch (Exception ex) {
168 ex.printStackTrace();
172 return new ModelAndView(viewName, "model", null);
175 private String nvl(String s) {
176 return (s == null) ? "" : s;
179 private String nvl(String s, String sDefault) {
180 return nvl(s).equals("") ? sDefault : s;
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();