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