60c25c653b2156b5caafd6fe44bf75e8f552107d
[portal.git] / ecomp-portal-BE-common / src / main / java / org / onap / portalapp / portal / controller / AuxApiRequestMapperController.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 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 package org.onap.portalapp.portal.controller;
38
39 import java.lang.reflect.InvocationTargetException;
40 import java.lang.reflect.Method;
41 import java.util.ArrayList;
42 import java.util.Arrays;
43 import java.util.Collections;
44 import java.util.HashMap;
45 import java.util.List;
46 import java.util.Map;
47 import java.util.jar.Attributes;
48 import java.util.regex.Matcher;
49 import java.util.regex.Pattern;
50
51 import javax.servlet.http.HttpServletRequest;
52 import javax.servlet.http.HttpServletResponse;
53
54 import org.onap.portalapp.annotation.ApiVersion;
55 import org.onap.portalapp.externalsystemapproval.model.ExternalSystemUser;
56 import org.onap.portalapp.portal.domain.CentralV2RoleFunction;
57 import org.onap.portalapp.portal.domain.EPUser;
58 import org.onap.portalapp.portal.ecomp.model.PortalRestResponse;
59 import org.onap.portalapp.portal.ecomp.model.PortalRestStatusEnum;
60 import org.onap.portalapp.portal.logging.aop.EPAuditLog;
61 import org.onap.portalapp.portal.service.ManifestService;
62 import org.onap.portalapp.portal.transport.Analytics;
63 import org.onap.portalapp.portal.transport.CentralUser;
64 import org.onap.portalapp.portal.transport.CentralV2Role;
65 import org.onap.portalapp.portal.transport.EpNotificationItem;
66 import org.onap.portalapp.portal.transport.FavoritesFunctionalMenuItemJson;
67 import org.onap.portalapp.portal.transport.FunctionalMenuItem;
68 import org.onap.portalapp.portal.transport.OnboardingApp;
69 import org.onap.portalsdk.core.domain.Role;
70 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
71 import org.onap.portalsdk.core.onboarding.crossapi.PortalAPIResponse;
72 import org.onap.portalsdk.core.restful.domain.EcompRole;
73 import org.onap.portalsdk.core.restful.domain.EcompUser;
74 import org.springframework.beans.BeansException;
75 import org.springframework.beans.factory.annotation.Autowired;
76 import org.springframework.context.ApplicationContext;
77 import org.springframework.context.ApplicationContextAware;
78 import org.springframework.context.annotation.EnableAspectJAutoProxy;
79 import org.springframework.web.bind.annotation.PathVariable;
80 import org.springframework.web.bind.annotation.RequestBody;
81 import org.springframework.web.bind.annotation.RequestMapping;
82 import org.springframework.web.bind.annotation.RequestMethod;
83 import org.springframework.web.bind.annotation.RequestParam;
84 import org.springframework.web.bind.annotation.ResponseBody;
85 import org.springframework.web.bind.annotation.RestController;
86
87 import com.fasterxml.jackson.databind.ObjectMapper;
88
89 import io.swagger.annotations.ApiOperation;
90
91 @RestController
92 @RequestMapping("/auxapi")
93 @org.springframework.context.annotation.Configuration
94 @EnableAspectJAutoProxy
95 @EPAuditLog
96 public class AuxApiRequestMapperController implements ApplicationContextAware, BasicAuthenticationController {
97
98         private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(AuxApiRequestMapperController.class);
99
100         ApplicationContext context = null;
101         int minorVersion = 0;
102
103         @Autowired
104         private ManifestService manifestService;
105
106         @ApiOperation(value = "Gets user roles for an application which is upgraded to newer version.", response = String.class, responseContainer = "List")
107         @RequestMapping(value = { "/v3/user/{loginId}" }, method = RequestMethod.GET, produces = "application/json")
108         public String getUser(HttpServletRequest request, HttpServletResponse response,
109                         @PathVariable("loginId") String loginId) throws Exception {
110                 Map<String, Object> res = getMethod(request, response);
111                 String answer = null;
112                 try {
113                         answer = (String) invokeMethod(res, request, response, loginId);
114                 } catch (Exception e) {
115                         logger.error(EELFLoggerDelegate.errorLogger, "getUser failed", e);
116                 }
117                 return answer;
118         }
119
120         @SuppressWarnings("unchecked")
121         @ApiOperation(value = "Gets roles for an application which is upgraded to newer version.", response = CentralV2Role.class, responseContainer = "Json")
122         @RequestMapping(value = { "/v3/roles" }, method = RequestMethod.GET, produces = "application/json")
123         public List<CentralV2Role> getRoles(HttpServletRequest request, HttpServletResponse response) throws Exception {
124                 Map<String, Object> res = getMethod(request, response);
125                 request.getMethod();
126                 List<CentralV2Role> answer = null;
127                 try {
128                         answer = (List<CentralV2Role>) invokeMethod(res, request, response);
129                 } catch (Exception e) {
130                         logger.error(EELFLoggerDelegate.errorLogger, "getRoles failed", e);
131                 }
132                 return answer;
133         }
134
135         @SuppressWarnings("unchecked")
136         @ApiOperation(value = "Saves role for an application.", response = PortalRestResponse.class, responseContainer = "Json")
137         @RequestMapping(value = { "/v3/role" }, method = RequestMethod.POST, produces = "application/json")
138         public PortalRestResponse<String> saveRole(HttpServletRequest request, HttpServletResponse response,
139                         @RequestBody Role role) throws Exception {
140                 Map<String, Object> res = getMethod(request, response);
141                 PortalRestResponse<String> out = null;
142                 try {
143                         out = (PortalRestResponse<String>) invokeMethod(res, request, response, role);
144                 } catch (Exception e) {
145                         logger.error(EELFLoggerDelegate.errorLogger, "saveRole failed", e);
146                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, e.getMessage(), "Failed");
147                 }
148                 return out;
149         }
150
151         @ApiOperation(value = "Gets v2 role information for an application which is upgraded to newer version.", response = CentralV2Role.class, responseContainer = "Json")
152         @RequestMapping(value = { "/v3/role/{role_id}" }, method = RequestMethod.GET, produces = "application/json")
153         public CentralV2Role getRoleInfo(HttpServletRequest request, HttpServletResponse response,
154                         @PathVariable("role_id") Long roleId) throws Exception {
155                 Map<String, Object> res = getMethod(request, response);
156                 CentralV2Role role = null;
157                 try {
158                         role = (CentralV2Role) invokeMethod(res, request, response, roleId);
159                 } catch (Exception e) {
160                         logger.error(EELFLoggerDelegate.errorLogger, "getRoleInfo failed", e);
161                 }
162                 return role;
163
164         }
165
166         @SuppressWarnings("unchecked")
167         @ApiOperation(value = "Gets all active Users of application", response = String.class, responseContainer = "Json")
168         @RequestMapping(value = { "/v3/users" }, method = RequestMethod.GET, produces = "application/json")
169         public List<EcompUser> getUsersOfApplication(HttpServletRequest request, HttpServletResponse response)
170                         throws Exception {
171                 Map<String, Object> res = getMethod(request, response);
172                 List<EcompUser> users = null;
173                 try {
174                         users = (List<EcompUser>) invokeMethod(res, request, response);
175                 } catch (Exception e) {
176                         logger.error(EELFLoggerDelegate.errorLogger, "getUsersOfApplication failed", e);
177                 }
178                 return users;
179         }
180
181         @SuppressWarnings("unchecked")
182         @ApiOperation(value = "Gets all role functions for an application which is upgraded to newer version.", response = CentralV2RoleFunction.class, responseContainer = "Json")
183         @RequestMapping(value = { "/v3/functions" }, method = RequestMethod.GET, produces = "application/json")
184         public List<CentralV2RoleFunction> getRoleFunctionsList(HttpServletRequest request, HttpServletResponse response)
185                         throws Exception {
186                 Map<String, Object> res = getMethod(request, response);
187                 List<CentralV2RoleFunction> roleFunctionsList = null;
188                 try {
189                         roleFunctionsList = (List<CentralV2RoleFunction>) invokeMethod(res, request, response);
190                 } catch (Exception e) {
191                         logger.error(EELFLoggerDelegate.errorLogger, "getRoleFunctionsList failed", e);
192                 }
193                 return roleFunctionsList;
194         }
195
196         @ApiOperation(value = "Gets role information for an application provided by function code.", response = CentralV2RoleFunction.class, responseContainer = "Json")
197         @RequestMapping(value = { "/v3/function/{code}" }, method = RequestMethod.GET, produces = "application/json")
198         public CentralV2RoleFunction getRoleFunction(HttpServletRequest request, HttpServletResponse response,
199                         @PathVariable("code") String code) throws Exception {
200                 Map<String, Object> res = getMethod(request, response);
201                 CentralV2RoleFunction roleFunction = null;
202                 try {
203                         roleFunction = (CentralV2RoleFunction) invokeMethod(res, request, response, code);
204                 } catch (Exception e) {
205                         logger.error(EELFLoggerDelegate.errorLogger, "getRoleFunction failed", e);
206                 }
207                 return roleFunction;
208         }
209
210         @SuppressWarnings("unchecked")
211         @ApiOperation(value = "Saves role function for an application.", response = PortalRestResponse.class, responseContainer = "Json")
212         @RequestMapping(value = { "/v3/roleFunction" }, method = RequestMethod.POST, produces = "application/json")
213         public PortalRestResponse<String> saveRoleFunction(HttpServletRequest request, HttpServletResponse response,
214                         @RequestBody String roleFunc) throws Exception {
215                 PortalRestResponse<String> result = null;
216                 Map<String, Object> res = getMethod(request, response);
217                 try {
218                         result = (PortalRestResponse<String>) invokeMethod(res, request, response, roleFunc);
219                         return result;
220                 } catch (Exception e) {
221                         logger.error(EELFLoggerDelegate.errorLogger, "saveRoleFunction failed", e);
222                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, e.getMessage(), "Failed");
223                 }
224         }
225
226         @SuppressWarnings("unchecked")
227         @ApiOperation(value = "Deletes role function for an application.", response = PortalRestResponse.class, responseContainer = "Json")
228         @RequestMapping(value = { "/v3/roleFunction/{code}" }, method = RequestMethod.DELETE, produces = "application/json")
229         public PortalRestResponse<String> deleteRoleFunction(HttpServletRequest request, HttpServletResponse response,
230                         @PathVariable("code") String code) throws Exception {
231                 PortalRestResponse<String> result = null;
232                 Map<String, Object> res = getMethod(request, response);
233                 try {
234                         result = (PortalRestResponse<String>) invokeMethod(res, request, response, code);
235                         return result;
236                 } catch (Exception e) {
237                         logger.error(EELFLoggerDelegate.errorLogger, "deleteRoleFunction failed", e);
238                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, e.getMessage(), "Failed");
239                 }
240         }
241
242         @SuppressWarnings("unchecked")
243         @ApiOperation(value = "deletes  roles for an application.", response = PortalRestResponse.class, responseContainer = "Json")
244         @RequestMapping(value = { "/v3/deleteRole/{roleId}" }, method = RequestMethod.DELETE, produces = "application/json")
245         public PortalRestResponse<String> deleteRole(HttpServletRequest request, HttpServletResponse response,
246                         @PathVariable("roleId") Long roleId) throws Exception {
247                 PortalRestResponse<String> result = null;
248                 Map<String, Object> res = getMethod(request, response);
249                 try {
250                         result = (PortalRestResponse<String>) invokeMethod(res, request, response, roleId);
251                         return result;
252                 } catch (Exception e) {
253                         logger.error(EELFLoggerDelegate.errorLogger, "deleteRole failed", e);
254                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, e.getMessage(), "Failed");
255                 }
256         }
257
258         @SuppressWarnings("unchecked")
259         @ApiOperation(value = "Gets active roles for an application.", response = CentralV2Role.class, responseContainer = "Json")
260         @RequestMapping(value = { "/v3/activeRoles" }, method = RequestMethod.GET, produces = "application/json")
261         public List<CentralV2Role> getActiveRoles(HttpServletRequest request, HttpServletResponse response)
262                         throws Exception {
263                 List<CentralV2Role> cenRole = null;
264                 Map<String, Object> res = getMethod(request, response);
265                 try {
266                         cenRole = (List<CentralV2Role>) invokeMethod(res, request, response);
267                 } catch (Exception e) {
268                         logger.error(EELFLoggerDelegate.errorLogger, "getActiveRoles failed", e);
269                 }
270                 return cenRole;
271         }
272
273         @ApiOperation(value = "Gets ecompUser of an application.", response = CentralUser.class, responseContainer = "List")
274         @RequestMapping(value = { "/v4/user/{loginId}" }, method = RequestMethod.GET, produces = "application/json")
275         public String getEcompUser(HttpServletRequest request, HttpServletResponse response,
276                         @PathVariable("loginId") String loginId) throws Exception {
277                 Map<String, Object> res = getMethod(request, response);
278                 String answer = null;
279                 try {
280                         answer = (String) invokeMethod(res, request, response, loginId);
281                 } catch (Exception e) {
282                         logger.error(EELFLoggerDelegate.errorLogger, "getEcompUser failed", e);
283                 }
284                 return answer;
285         }
286
287         @SuppressWarnings("unchecked")
288         @ApiOperation(value = "Gets user ecomp role for an application.", response = CentralUser.class, responseContainer = "List")
289         @RequestMapping(value = { "/v4/roles" }, method = RequestMethod.GET, produces = "application/json")
290         public List<EcompRole> getEcompRolesOfApplication(HttpServletRequest request, HttpServletResponse response)
291                         throws Exception {
292                 Map<String, Object> res = getMethod(request, response);
293                 List<EcompRole> answer = null;
294                 try {
295                         answer = (List<EcompRole>) invokeMethod(res, request, response);
296                 } catch (Exception e) {
297                         logger.error(EELFLoggerDelegate.errorLogger, "getEcompRolesOfApplication failed", e);
298                 }
299                 return answer;
300         }
301
302         @ApiOperation(value = "Gets session slot-check interval, a duration in milliseconds.", response = Integer.class)
303         @RequestMapping(value = {
304                         "/v3/getSessionSlotCheckInterval" }, method = RequestMethod.GET, produces = "application/json")
305         public Integer getSessionSlotCheckInterval(HttpServletRequest request, HttpServletResponse response)
306                         throws Exception {
307                 Map<String, Object> res = getMethod(request, response);
308                 Integer ans = null;
309                 try {
310                         ans = (Integer) invokeMethod(res, request, response);
311                 } catch (Exception e) {
312                         logger.error(EELFLoggerDelegate.errorLogger, "getSessionSlotCheckInterval failed", e);
313                 }
314                 return ans;
315         }
316
317         @ApiOperation(value = "Extends session timeout values for all on-boarded applications.", response = Boolean.class)
318         @RequestMapping(value = { "/v3/extendSessionTimeOuts" }, method = RequestMethod.POST)
319         public Boolean extendSessionTimeOuts(HttpServletRequest request, HttpServletResponse response,
320                         @RequestParam String sessionMap) throws Exception {
321                 Map<String, Object> res = getMethod(request, response);
322                 Boolean ans = null;
323                 try {
324                         ans = (Boolean) invokeMethod(res, request, response, sessionMap);
325                 } catch (Exception e) {
326                         logger.error(EELFLoggerDelegate.errorLogger, "extendSessionTimeOuts failed", e);
327                 }
328                 return ans;
329         }
330
331         @ApiOperation(value = "Gets javascript with functions that support gathering and reporting web analytics.", response = String.class)
332         @RequestMapping(value = { "/v3/analytics" }, method = RequestMethod.GET, produces = "application/javascript")
333         public String getAnalyticsScript(HttpServletRequest request, HttpServletResponse response) throws Exception {
334                 Map<String, Object> res = getMethod(request, response);
335                 String ans = null;
336                 try {
337                         ans = (String) invokeMethod(res, request, response);
338                 } catch (Exception e) {
339                         logger.error(EELFLoggerDelegate.errorLogger, "getAnalyticsScript failed", e);
340                 }
341                 return ans;
342         }
343
344         @RequestMapping(value = { "/v3/storeAnalytics" }, method = RequestMethod.POST, produces = "application/json")
345         @ResponseBody
346         @ApiOperation(value = "Accepts data from partner applications with web analytics data.", response = PortalAPIResponse.class)
347         public PortalAPIResponse storeAnalyticsScript(HttpServletRequest request, HttpServletResponse response,
348                         @RequestBody Analytics analyticsMap) throws Exception {
349                 Map<String, Object> res = getMethod(request, response);
350                 PortalAPIResponse ans = new PortalAPIResponse(true, "error");
351                 try {
352                         ans = (PortalAPIResponse) invokeMethod(res, request, response, analyticsMap);
353                 } catch (Exception e) {
354                         logger.error(EELFLoggerDelegate.errorLogger, "storeAnalyticsScript failed", e);
355                 }
356                 return ans;
357
358         }
359
360         @SuppressWarnings("unchecked")
361         @ApiOperation(value = "Bulk upload functions for an application.", response = PortalRestResponse.class, responseContainer = "Json")
362         @RequestMapping(value = {
363                         "/v3/upload/portal/functions" }, method = RequestMethod.POST, produces = "application/json")
364         public PortalRestResponse<String> bulkUploadFunctions(HttpServletRequest request, HttpServletResponse response)
365                         throws Exception {
366                 PortalRestResponse<String> result = null;
367                 Map<String, Object> res = getMethod(request, response);
368                 try {
369                         result = (PortalRestResponse<String>) invokeMethod(res, request, response);
370                         return result;
371                 } catch (Exception e) {
372                         logger.error(EELFLoggerDelegate.errorLogger, "bulkUploadFunctions failed", e);
373                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, e.getMessage(), "Failed");
374                 }
375
376         }
377
378         @SuppressWarnings("unchecked")
379         @ApiOperation(value = "Bulk upload roles for an application.", response = PortalRestResponse.class, responseContainer = "Json")
380         @RequestMapping(value = { "/v3/upload/portal/roles" }, method = RequestMethod.POST, produces = "application/json")
381         public PortalRestResponse<String> bulkUploadRoles(HttpServletRequest request, HttpServletResponse response)
382                         throws Exception {
383                 PortalRestResponse<String> result = null;
384                 Map<String, Object> res = getMethod(request, response);
385                 try {
386                         result = (PortalRestResponse<String>) invokeMethod(res, request, response);
387                         return result;
388                 } catch (Exception e) {
389                         logger.error(EELFLoggerDelegate.errorLogger, "bulkUploadRoles failed", e);
390                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, e.getMessage(), "Failed");
391                 }
392         }
393
394         @SuppressWarnings("unchecked")
395         @ApiOperation(value = "Bulk upload role functions for an application.", response = PortalRestResponse.class, responseContainer = "Json")
396         @RequestMapping(value = {
397                         "/v3/upload/portal/roleFunctions" }, method = RequestMethod.POST, produces = "application/json")
398         public PortalRestResponse<String> bulkUploadRoleFunctions(HttpServletRequest request, HttpServletResponse response)
399                         throws Exception {
400                 PortalRestResponse<String> result = null;
401                 Map<String, Object> res = getMethod(request, response);
402                 try {
403                         result = (PortalRestResponse<String>) invokeMethod(res, request, response);
404                         return result;
405                 } catch (Exception e) {
406                         logger.error(EELFLoggerDelegate.errorLogger, "bulkUploadRoleFunctions failed", e);
407                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, e.getMessage(), "Failed");
408                 }
409         }
410
411         @SuppressWarnings("unchecked")
412         @ApiOperation(value = "Bulk upload user roles for an application.", response = PortalRestResponse.class, responseContainer = "Json")
413         @RequestMapping(value = {
414                         "/v3/upload/portal/userRoles" }, method = RequestMethod.POST, produces = "application/json")
415         public PortalRestResponse<String> bulkUploadUserRoles(HttpServletRequest request, HttpServletResponse response)
416                         throws Exception {
417                 PortalRestResponse<String> result = null;
418                 Map<String, Object> res = getMethod(request, response);
419                 try {
420                         result = (PortalRestResponse<String>) invokeMethod(res, request, response);
421                         return result;
422                 } catch (Exception e) {
423                         logger.error(EELFLoggerDelegate.errorLogger, "bulkUploadUserRoles failed", e);
424                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, e.getMessage(), "Failed");
425                 }
426
427         }
428
429         @SuppressWarnings("unchecked")
430         @ApiOperation(value = "Bulk upload users for renamed role of an application.", response = PortalRestResponse.class, responseContainer = "Json")
431         @RequestMapping(value = {
432                         "/v3/upload/portal/userRole/{roleId}" }, method = RequestMethod.POST, produces = "application/json")
433         public PortalRestResponse<String> bulkUploadUsersSingleRole(HttpServletRequest request,
434                         HttpServletResponse response, @PathVariable Long roleId) throws Exception {
435                 PortalRestResponse<String> result = null;
436                 Map<String, Object> res = getMethod(request, response);
437                 try {
438                         result = (PortalRestResponse<String>) invokeMethod(res, request, response, roleId);
439                         return result;
440                 } catch (Exception e) {
441                         logger.error(EELFLoggerDelegate.errorLogger, "bulkUploadUsersSingleRole failed", e);
442                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, e.getMessage(), "Failed");
443                 }
444         }
445
446         @SuppressWarnings("unchecked")
447         @ApiOperation(value = "Bulk upload functions for an partner application.", response = PortalRestResponse.class, responseContainer = "Json")
448         @RequestMapping(value = {
449                         "/v3/upload/partner/functions" }, method = RequestMethod.POST, produces = "application/json")
450         public PortalRestResponse<String> bulkUploadPartnerFunctions(HttpServletRequest request,
451                         HttpServletResponse response) throws Exception {
452                 PortalRestResponse<String> result = null;
453                 Map<String, Object> res = getMethod(request, response);
454                 try {
455                         result = (PortalRestResponse<String>) invokeMethod(res, request, response);
456                         return result;
457                 } catch (Exception e) {
458                         logger.error(EELFLoggerDelegate.errorLogger, "bulkUploadPartnerFunctions failed", e);
459                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, e.getMessage(), "Failed");
460                 }
461         }
462
463         // not using
464         @SuppressWarnings("unchecked")
465         @ApiOperation(value = "Bulk upload roles for an partner application.", response = PortalRestResponse.class, responseContainer = "Json")
466         @RequestMapping(value = { "/v3/upload/partner/roles" }, method = RequestMethod.POST, produces = "application/json")
467         public PortalRestResponse<String> bulkUploadPartnerRoles(HttpServletRequest request, HttpServletResponse response,
468                         @RequestBody List<Role> upload) throws Exception {
469                 PortalRestResponse<String> result = null;
470                 Map<String, Object> res = getMethod(request, response);
471                 try {
472                         result = (PortalRestResponse<String>) invokeMethod(res, request, response, upload);
473                         return result;
474                 } catch (Exception e) {
475                         logger.error(EELFLoggerDelegate.errorLogger, "bulkUploadPartnerRoles failed", e);
476                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, e.getMessage(), "Failed");
477                 }
478         }
479
480         @SuppressWarnings("unchecked")
481         @ApiOperation(value = "Bulk upload role functions for an partner application.", response = PortalRestResponse.class, responseContainer = "Json")
482         @RequestMapping(value = {
483                         "/v3/upload/partner/roleFunctions" }, method = RequestMethod.POST, produces = "application/json")
484         public PortalRestResponse<String> bulkUploadPartnerRoleFunctions(HttpServletRequest request,
485                         HttpServletResponse response) throws Exception {
486                 PortalRestResponse<String> result = null;
487                 Map<String, Object> res = getMethod(request, response);
488                 try {
489                         result = (PortalRestResponse<String>) invokeMethod(res, request, response);
490                         return result;
491                 } catch (Exception e) {
492                         logger.error(EELFLoggerDelegate.errorLogger, "bulkUploadPartnerRoleFunctions failed", e);
493                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, e.getMessage(), "Failed");
494                 }
495         }
496
497         @SuppressWarnings("unchecked")
498         @ApiOperation(value = "Gets all functions along with global functions", response = List.class, responseContainer = "Json")
499         @RequestMapping(value = { "/v3/menuFunctions" }, method = RequestMethod.GET, produces = "application/json")
500         public List<String> getMenuFunctions(HttpServletRequest request, HttpServletResponse response) throws Exception {
501                 List<String> functionsList = null;
502                 Map<String, Object> res = getMethod(request, response);
503                 try {
504                         functionsList = (List<String>) invokeMethod(res, request, response);
505                 } catch (Exception e) {
506                         logger.error(EELFLoggerDelegate.errorLogger, "getMenuFunctions failed", e);
507                 }
508                 return functionsList;
509         }
510
511         private String getPatchNumber() {
512                 String response = "0";
513                 try {
514                         Attributes attributes = manifestService.getWebappManifest();
515                         response = attributes.getValue("Build-Number");
516                 } catch (Exception ex) {
517                         logger.error(EELFLoggerDelegate.errorLogger, "getPatchNumber failed", ex);
518                 }
519                 return response;
520         }
521
522         @SuppressWarnings("rawtypes")
523         private List<Object> getObject(HttpServletRequest request, HttpServletResponse response) {
524                 Map<String, Object> beans = context.getBeansWithAnnotation(ApiVersion.class);
525                 @SuppressWarnings("unchecked")
526                 List<Object> beansList = new ArrayList(beans.values());
527                 return beansList;
528
529         }
530
531         private Map<String, Object> getMethod(HttpServletRequest request, HttpServletResponse response) {
532                 Method finalmethod = null;
533                 String url = request.getRequestURI();
534                 String version = "";
535                 String service = "";
536                 Object currentObject = null;
537                 Map<String, Object> res = new HashMap<String, Object>();
538                 String[] uriArray = url.split("/auxapi");
539                 List<Integer> minorversionList = new ArrayList<>();
540                 if (uriArray.length > 1) {
541                         service = uriArray[1];
542                 }
543                 int first = service.indexOf("/");
544                 int second = service.indexOf("/", first + 1);
545                 version = service.substring(first + 1, second);
546                 int min = minorVersion;
547                 if (request.getHeader("MinorVersion") != null) {
548                         min = Integer.parseInt(request.getHeader("MinorVersion"));
549                 }
550                 res.put("min", version+"."+min);
551                 res.put("service", service);
552                 List<Object> objList = getObject(request, response);
553                 String requestedApiMethodType = request.getMethod();
554                 String majorVersion = latestMajorVersionOfService(objList, service, version, requestedApiMethodType);
555                 int latestMinorVersion = latestMinorVersionOfService(objList, service, version, requestedApiMethodType);
556                 res.put("majorVersion", majorVersion);
557                 res.put("latestMinorVersion", String.valueOf(latestMinorVersion));
558                 outerloop: for (Object obj : objList) {
559                         final List<Method> allMethods = getAllMethodsOfClass(obj);
560                         for (final Method method : allMethods) {
561                                 if (method.isAnnotationPresent(ApiVersion.class)) {
562                                         ApiVersion annotInstance = method.getAnnotation(ApiVersion.class);
563                                         Pattern p = Pattern.compile(annotInstance.service(),
564                                                         Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
565                                         Matcher matcher = p.matcher(service);
566                                         boolean b = matcher.matches();
567                                         logger.debug(EELFLoggerDelegate.debugLogger, "Requested Servie is:" + service
568                                                         + "Requested MinVersion is:" + min + "Requested MajorVersion is: " + version);
569                                         if (annotInstance.max().equals(version) && b && annotInstance.min() == min
570                                                         && annotInstance.method().equals(request.getMethod())) {
571                                                 finalmethod = method;
572                                                 currentObject = obj;
573                                                 res.put("method", method);
574                                                 res.put("Obj", obj);
575                                                 break outerloop;
576                                         }
577                                 }
578                         }
579                 }
580                 return res;
581         }
582
583         private String latestMajorVersionOfService(List<Object> objList, String service, String reuqestedVersion,
584                         String requestedApiMethodType) {
585                 Integer majorVersion = 0;
586                 String serviceEndPoint = service;
587                 int firstindex = serviceEndPoint.indexOf("/");
588                 int secondindex = serviceEndPoint.indexOf("/", firstindex + 1);
589                 serviceEndPoint = serviceEndPoint.substring(secondindex + 1);
590
591                 List<Integer> latestMajorVersionList = new ArrayList<>();
592                 for (Object obj : objList) {
593                         final List<Method> allMethods = getAllMethodsOfClass(obj);
594                         for (final Method method : allMethods) {
595                                 if (method.isAnnotationPresent(ApiVersion.class)) {
596                                         ApiVersion annotInstance = method.getAnnotation(ApiVersion.class);
597                                         String endpoint = annotInstance.service();
598                                         int first = endpoint.indexOf("/");
599                                         int second = endpoint.indexOf("/", first + 1);
600                                         endpoint = endpoint.substring(second + 1);
601                                         Pattern p = Pattern.compile(endpoint,
602                                                         Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
603                                         Matcher matcher = p.matcher(serviceEndPoint);
604                                         boolean b = matcher.matches();
605                                         if (b && annotInstance.method().equals(requestedApiMethodType)) {
606                                                 int index1 = annotInstance.service().indexOf("/");
607                                                 int index2 = annotInstance.service().indexOf("/", index1 + 1);
608                                                 String majorversion = annotInstance.service().substring(index1 + 2, index2);
609                                                 latestMajorVersionList.add(Integer.parseInt(majorversion));
610                                         }
611                                 }
612                         }
613                 }
614                 majorVersion = Collections.max(latestMajorVersionList);
615                 String majorVersionWithLastestMin = "/v"+String.valueOf(majorVersion)+"/"+serviceEndPoint;
616                 int latestMinorVersion = latestMinorVersionOfService(objList, majorVersionWithLastestMin, "v"+String.valueOf(majorVersion), requestedApiMethodType);
617                 return majorVersion+"."+latestMinorVersion;
618         }
619
620         private List<Method> getAllMethodsOfClass(Object obj) {
621                 List<Method> allMethods = new ArrayList<>();
622                 Class<?> objClz = obj.getClass();
623                 if (org.springframework.aop.support.AopUtils.isAopProxy(obj)) {
624                         objClz = org.springframework.aop.support.AopUtils.getTargetClass(obj);
625                 }
626                 allMethods = new ArrayList<Method>(Arrays.asList(objClz.getMethods()));
627                 allMethods.removeIf(s -> !(s.isAnnotationPresent(ApiVersion.class)));
628                 return allMethods;
629         }
630
631         private Integer latestMinorVersionOfService(List<Object> objList, String service, String reuqestedVersion,
632                         String requestedApiMethodType) {
633                 Integer minVersion = 0;
634                 String serviceEndPoint = service;
635                 List<Integer> latestMinorVersionList = new ArrayList<>();
636                 for (Object obj : objList) {
637                         final List<Method> allMethods = getAllMethodsOfClass(obj);
638                         for (final Method method : allMethods) {
639                                 if (method.isAnnotationPresent(ApiVersion.class)) {
640                                         ApiVersion annotInstance = method.getAnnotation(ApiVersion.class);
641                                         String endpoint = annotInstance.service();
642
643                                         Pattern p = Pattern.compile(endpoint,
644                                                         Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
645                                         Matcher matcher = p.matcher(serviceEndPoint);
646                                         boolean b = matcher.matches();
647                                         if (annotInstance.max().equals(reuqestedVersion) && b
648                                                         && annotInstance.method().equals(requestedApiMethodType)) {
649                                                 int minorversion = annotInstance.min();
650                                                 latestMinorVersionList.add(minorversion);
651                                         }
652                                 }
653                         }
654                 }
655                 minVersion = Collections.max(latestMinorVersionList);
656                 return minVersion;
657         }
658
659         private HttpServletResponse setResponse(HttpServletResponse response, String requestedMinVersion,
660                         String majorVersion, String latestMinorVersion, String service) {
661                 response.setHeader("X-MinorVersion", requestedMinVersion.toUpperCase());
662                 response.setHeader("X-PatchVersion", getPatchNumber());
663                 response.setHeader("X-LatestVersion", "V"+majorVersion);
664                 return response;
665         }
666
667         /**
668          * 
669          * @param res
670          * @param args
671          *            method parameters(Maintain HttpServletRequest at 0th position
672          *            and HttpServletResponse at 1th position in args array)
673          * @return
674          * @throws Exception
675          */
676         private Object invokeMethod(Map<String, Object> res, Object... args) throws Exception {
677                 Method method = (Method) res.get("method");
678                 Object obj = res.get("Obj");
679                 Object responseObj = null;
680                 String min = res.get("min").toString();
681                 String majorVersion = res.get("majorVersion").toString();
682                 String latestMinorVersion = res.get("latestMinorVersion").toString();
683                 String service = res.get("service").toString();
684                 HttpServletRequest request = (HttpServletRequest) args[0];
685                 HttpServletResponse response = (HttpServletResponse) args[1];
686                 setResponse(response, min, majorVersion, latestMinorVersion, service);
687                 final Map<String, String> errorMap = new HashMap<>();
688                 ObjectMapper mapper = new ObjectMapper();
689                 String reason = "";
690                 try {
691                         if (method != null && obj != null) {
692                                 responseObj = method.invoke(obj, args);
693                         } else {
694                                 errorMap.put("error", "Requested api is not available");
695                                 reason = mapper.writeValueAsString(errorMap);
696                                 response.getWriter().write(reason);
697                                 logger.debug(EELFLoggerDelegate.debugLogger, "Requested api " + request.getRequestURI()
698                                                 + "is not available with minorVersion " + request.getHeader("MinorVersion"));
699                                 response.setStatus(HttpServletResponse.SC_NOT_FOUND);
700                         }
701                 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
702                         logger.error(EELFLoggerDelegate.errorLogger, "Method :" + method + "invocation failed", e);
703                 }
704                 return responseObj;
705         }
706
707         @Override
708         public void setApplicationContext(ApplicationContext context) throws BeansException {
709                 this.context = context;
710         }
711
712         @SuppressWarnings("unchecked")
713         @ApiOperation(value = "Creates an application user with the specified roles.", response = PortalRestResponse.class)
714         @RequestMapping(value = { "/v3/userProfile" }, method = RequestMethod.POST, produces = "application/json")
715         public PortalRestResponse<String> postUserProfile(HttpServletRequest request,
716                         @RequestBody ExternalSystemUser extSysUser, HttpServletResponse response) {
717                 PortalRestResponse<String> result = null;
718                 Map<String, Object> res = getMethod(request, response);
719                 try {
720                         result = (PortalRestResponse<String>) invokeMethod(res, request, response, extSysUser);
721                         return result;
722                 } catch (Exception e) {
723                         logger.error(EELFLoggerDelegate.errorLogger, "postUserProfile failed", e);
724                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, e.getMessage(), "Failed");
725                 }
726         }
727
728         @SuppressWarnings("unchecked")
729         @ApiOperation(value = "Updates an application user to have only the specified roles.", response = PortalRestResponse.class)
730         @RequestMapping(value = { "/v3/userProfile" }, method = RequestMethod.PUT, produces = "application/json")
731         public PortalRestResponse<String> putUserProfile(HttpServletRequest request,
732                         @RequestBody ExternalSystemUser extSysUser, HttpServletResponse response) {
733                 PortalRestResponse<String> result = null;
734                 Map<String, Object> res = getMethod(request, response);
735                 try {
736                         result = (PortalRestResponse<String>) invokeMethod(res, request, response, extSysUser);
737                         return result;
738                 } catch (Exception e) {
739                         logger.error(EELFLoggerDelegate.errorLogger, "putUserProfile failed", e);
740                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, e.getMessage(), "Failed");
741                 }
742         }
743
744         @SuppressWarnings("unchecked")
745         @ApiOperation(value = "Processes a request to delete one or more application roles for one      specified user who has roles.", response = PortalRestResponse.class)
746         @RequestMapping(value = { "/v3/userProfile" }, method = RequestMethod.DELETE, produces = "application/json")
747         public PortalRestResponse<String> deleteUserProfile(HttpServletRequest request,
748                         @RequestBody ExternalSystemUser extSysUser, HttpServletResponse response) {
749                 PortalRestResponse<String> result = null;
750                 Map<String, Object> res = getMethod(request, response);
751                 try {
752                         result = (PortalRestResponse<String>) invokeMethod(res, request, response, extSysUser);
753                         return result;
754                 } catch (Exception e) {
755                         logger.error(EELFLoggerDelegate.errorLogger, "deleteUserProfile failed", e);
756                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, e.getMessage(), "Failed");
757                 }
758         }
759
760         @SuppressWarnings("unchecked")
761         @ApiOperation(value = "Accepts messages from external ticketing systems and creates notifications for Portal users.", response = PortalRestResponse.class)
762         @RequestMapping(value = { "/v3/ticketevent" }, method = RequestMethod.POST)
763         public PortalRestResponse<String> handleRequest(HttpServletRequest request, HttpServletResponse response,
764                         @RequestBody String ticketEventJson) throws Exception {
765                 PortalRestResponse<String> result = null;
766                 Map<String, Object> res = getMethod(request, response);
767                 try {
768                         result = (PortalRestResponse<String>) invokeMethod(res, request, response, ticketEventJson);
769                         return result;
770                 } catch (Exception e) {
771                         logger.error(EELFLoggerDelegate.errorLogger, "handleRequest failed", e);
772                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, e.getMessage(), "Failed");
773                 }
774         }
775
776         @SuppressWarnings("unchecked")
777         @ApiOperation(value = "Creates a new user as a Portal administrator.", response = PortalRestResponse.class)
778         @RequestMapping(value = "/v3/portalAdmin", method = RequestMethod.POST, produces = "application/json")
779         @ResponseBody
780         public PortalRestResponse<String> postPortalAdmin(HttpServletRequest request, HttpServletResponse response,
781                         @RequestBody EPUser epUser) {
782                 PortalRestResponse<String> result = null;
783                 Map<String, Object> res = getMethod(request, response);
784                 try {
785                         result = (PortalRestResponse<String>) invokeMethod(res, request, response, epUser);
786                         return result;
787                 } catch (Exception e) {
788                         logger.error(EELFLoggerDelegate.errorLogger, "postPortalAdmin failed", e);
789                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, e.getMessage(), "Failed");
790                 }
791         }
792
793         @ApiOperation(value = "Gets the specified application that is on-boarded in Portal.", response = OnboardingApp.class)
794         @RequestMapping(value = { "/v3/onboardApp/{appId}" }, method = RequestMethod.GET, produces = "application/json")
795         @ResponseBody
796         public OnboardingApp getOnboardAppExternal(HttpServletRequest request, HttpServletResponse response,
797                         @PathVariable("appId") Long appId) {
798                 OnboardingApp result = new OnboardingApp();
799                 Map<String, Object> res = getMethod(request, response);
800                 try {
801                         result = (OnboardingApp) invokeMethod(res, request, response, appId);
802                 } catch (Exception e) {
803                         logger.error(EELFLoggerDelegate.errorLogger, "getOnboardAppExternal failed", e);
804                 }
805                 return result;
806         }
807
808         @SuppressWarnings("unchecked")
809         @ApiOperation(value = "Adds a new application to Portal.", response = PortalRestResponse.class)
810         @RequestMapping(value = { "/v3/onboardApp" }, method = RequestMethod.POST, produces = "application/json")
811         @ResponseBody
812         public PortalRestResponse<String> postOnboardAppExternal(HttpServletRequest request, HttpServletResponse response,
813                         @RequestBody OnboardingApp newOnboardApp) {
814                 PortalRestResponse<String> result = new PortalRestResponse<>();
815                 Map<String, Object> res = getMethod(request, response);
816                 try {
817                         result = (PortalRestResponse<String>) invokeMethod(res, request, response, newOnboardApp);
818                         return result;
819                 } catch (Exception e) {
820                         logger.error(EELFLoggerDelegate.errorLogger, "postOnboardAppExternal failed", e);
821                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, e.getMessage(), "Failed");
822                 }
823
824         }
825
826         @SuppressWarnings("unchecked")
827         @ApiOperation(value = "Updates information about an on-boarded application in Portal.", response = PortalRestResponse.class)
828         @RequestMapping(value = { "/v3/onboardApp/{appId}" }, method = RequestMethod.PUT, produces = "application/json")
829         @ResponseBody
830         public PortalRestResponse<String> putOnboardAppExternal(HttpServletRequest request, HttpServletResponse response,
831                         @PathVariable("appId") Long appId, @RequestBody OnboardingApp oldOnboardApp) {
832                 PortalRestResponse<String> result = new PortalRestResponse<>();
833                 Map<String, Object> res = getMethod(request, response);
834                 try {
835                         result = (PortalRestResponse<String>) invokeMethod(res, request, response, appId, oldOnboardApp);
836                         return result;
837                 } catch (Exception e) {
838                         logger.error(EELFLoggerDelegate.errorLogger, "putOnboardAppExternal failed", e);
839                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, e.getMessage(), "Failed");
840                 }
841         }
842
843         @ApiOperation(value = "Creates a Portal user notification for roles identified in the content from an external application.", response = PortalAPIResponse.class)
844         @RequestMapping(value = { "/v3/publishNotification" }, method = RequestMethod.POST, produces = "application/json")
845         @ResponseBody
846         public PortalAPIResponse publishNotification(HttpServletRequest request,
847                         @RequestBody EpNotificationItem notificationItem, HttpServletResponse response) throws Exception {
848                 PortalAPIResponse result = new PortalAPIResponse(true, "success");
849                 Map<String, Object> res = getMethod(request, response);
850                 try {
851                         result = (PortalAPIResponse) invokeMethod(res, request, response, notificationItem);
852                         return result;
853                 } catch (Exception e) {
854                         logger.error(EELFLoggerDelegate.errorLogger, "publishNotification failed", e);
855                         return new PortalAPIResponse(false, e.getMessage());
856                 }
857         }
858
859         @SuppressWarnings("unchecked")
860         @ApiOperation(value = "Gets favorite items within the functional menu for the current user.", response = FavoritesFunctionalMenuItemJson.class, responseContainer = "List")
861         @RequestMapping(value = { "/v3/getFavorites" }, method = RequestMethod.GET, produces = "application/json")
862         public List<FavoritesFunctionalMenuItemJson> getFavoritesForUser(HttpServletRequest request,
863                         HttpServletResponse response) throws Exception {
864                 List<FavoritesFunctionalMenuItemJson> favorites = null;
865                 Map<String, Object> res = getMethod(request, response);
866                 try {
867                         favorites = (List<FavoritesFunctionalMenuItemJson>) invokeMethod(res, request, response);
868                 } catch (Exception e) {
869                         logger.error(EELFLoggerDelegate.errorLogger, "getFavoritesForUser failed", e);
870                 }
871                 return favorites;
872         }
873
874         @SuppressWarnings("unchecked")
875         @ApiOperation(value = "Gets functional menu items appropriate for the current user.", response = FunctionalMenuItem.class, responseContainer = "List")
876         @RequestMapping(value = {
877                         "/v3/functionalMenuItemsForUser" }, method = RequestMethod.GET, produces = "application/json")
878         public List<FunctionalMenuItem> getFunctionalMenuItemsForUser(HttpServletRequest request,
879                         HttpServletResponse response) throws Exception {
880                 List<FunctionalMenuItem> fnMenuItems = null;
881                 Map<String, Object> res = getMethod(request, response);
882                 try {
883                         fnMenuItems = (List<FunctionalMenuItem>) invokeMethod(res, request, response);
884                 } catch (Exception e) {
885                         logger.error(EELFLoggerDelegate.errorLogger, "getFunctionalMenuItemsForUser failed", e);
886                 }
887                 return fnMenuItems;
888         }
889
890 }