c233b4a037b87e976fcdfa9fcb10294c8fd1f18b
[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.util.*;
23 import java.lang.reflect.*;
24 import javax.servlet.*;
25 import javax.servlet.http.*;
26
27 import org.openecomp.portalsdk.analytics.controller.*;
28 import org.openecomp.portalsdk.analytics.error.RaptorException;
29 import org.openecomp.portalsdk.analytics.error.RaptorRuntimeException;
30 import org.openecomp.portalsdk.analytics.model.runtime.ReportParamValues;
31 import org.openecomp.portalsdk.analytics.system.*;
32 import org.openecomp.portalsdk.analytics.util.*;
33 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
34
35 public class Controller extends org.openecomp.portalsdk.analytics.RaptorObject {
36         
37         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(Controller.class);
38         public Controller() {
39         }
40
41         public String processRequest(HttpServletRequest request) {
42                 String actionKey = nvl(request.getParameter(AppConstants.RI_ACTION), "report.run");
43
44                 return processRequest(actionKey, request);
45         } // processRequest
46
47         public String processRequest(String actionKey, HttpServletRequest request) {
48                 Action action = null;
49                 try {
50                         action = Globals.getRaptorActionMapping().getAction(actionKey);
51                         if (action == null)
52                                 throw new RaptorRuntimeException("Action not found");
53                 } catch (RaptorException e) {
54                         logger.debug(EELFLoggerDelegate.debugLogger, ("[Controller.processRequest]Invalid raptor action [" + actionKey
55                                                 + "]. RaptorException: " + e.getMessage()));
56 //                      if (actionKey.equals("system_upgrade")) // System override
57 //                              return att.raptor.util.upgrade.SystemUpgrade.upgradeDB(request);
58
59                         return (new ErrorHandler()).processFatalError(request, new RaptorRuntimeException(
60                                         "[Controller.processRequest]Invalid raptor action [" + actionKey
61                                                         + "]. Exception: " + e.getMessage()));
62                 }
63
64                 try {
65                         Class[] paramTypes = new Class[2];
66                         paramTypes[0] = Class.forName("javax.servlet.http.HttpServletRequest");
67                         paramTypes[1] = Class.forName("java.lang.String");
68
69             Class handlerClass = Class.forName(action.getControllerClass());
70                         Object handler = handlerClass.newInstance();
71                         Method handlerMethod = handlerClass.getMethod(action.getControllerMethod(),
72                                         paramTypes);
73
74                         Object[] paramValues = new Object[2];
75                         paramValues[0] = request;
76                         paramValues[1] = action.getJspName();
77
78                         return (String) handlerMethod.invoke(handler, paramValues);
79                 } catch (ClassNotFoundException e) {
80                         logger.debug(EELFLoggerDelegate.debugLogger, ("[Controller.processRequest]Invalid raptor action [" + actionKey
81                                         + "]. ClassNotFoundException: " + e.getMessage()));
82                         return (new ErrorHandler()).processFatalError(request, new RaptorRuntimeException(
83                                         "[Controller.processRequest] Unable to instantiate and invoke action handler. Exception: "
84                                                         + e.getMessage()));
85                 } catch (IllegalAccessException e) {
86                         logger.debug(EELFLoggerDelegate.debugLogger, ("[Controller.processRequest]Invalid raptor action [" + actionKey
87                                         + "]. IllegalAccessException: " + e.getMessage()));
88                         return (new ErrorHandler()).processFatalError(request, new RaptorRuntimeException(
89                                         "[Controller.processRequest] Unable to instantiate and invoke action handler. Exception: "
90                                                         + e.getMessage()));
91                 }catch (InstantiationException e) {
92                         logger.debug(EELFLoggerDelegate.debugLogger, ("[Controller.processRequest]Invalid raptor action [" + actionKey
93                                         + "]. InstantiationException: " + e.getMessage()));
94                         return (new ErrorHandler()).processFatalError(request, new RaptorRuntimeException(
95                                         "[Controller.processRequest] Unable to instantiate and invoke action handler. Exception: "
96                                                         + e.getMessage()));
97                 }catch (NoSuchMethodException e) {
98                         logger.debug(EELFLoggerDelegate.debugLogger, ("[Controller.processRequest]Invalid raptor action [" + actionKey
99                                         + "]. NoSuchMethodException: " + e.getMessage()));
100                         return (new ErrorHandler()).processFatalError(request, new RaptorRuntimeException(
101                                         "[Controller.processRequest] Unable to instantiate and invoke action handler. Exception: "
102                                                         + e.getMessage()));
103                 }catch (InvocationTargetException e) {
104                         logger.debug(EELFLoggerDelegate.debugLogger, ("[Controller.processRequest]Invalid raptor action [" + actionKey
105                                         + "]. InvocationTargetException: " + e.getMessage()));
106                         return (new ErrorHandler()).processFatalError(request, new RaptorRuntimeException(
107                                         "[Controller.processRequest] Unable to instantiate and invoke action handler. Exception: "
108                                                         + e.getMessage()));
109                 }
110         } // processRequest
111
112         public void handleRequest(HttpServletRequest request, HttpServletResponse response,
113                         ServletContext servletContext) throws Exception {
114                 String actionKey = nvl(request.getParameter(AppConstants.RI_ACTION), request.getParameter("action"));
115
116                 handleRequest(actionKey, request, response, servletContext);
117         } // handleRequest
118
119         public void handleRequest(String actionKey, HttpServletRequest request,
120                         HttpServletResponse response, ServletContext servletContext) throws Exception {
121                 servletContext.getRequestDispatcher("/" + processRequest(actionKey, request)).forward(
122                                 request, response);
123         } // handleRequest
124
125 } // Controller