1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
/*******************************************************************************
* =============LICENSE_START=========================================================
*
* =================================================================================
* Copyright (c) 2017 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END=========================================================
*
* ECOMP is a trademark and service mark of AT&T Intellectual Property.
*******************************************************************************/
package org.onap.oom.dashboard.controller;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import org.onap.oom.dashboard.domain.ControllerEndpoint;
import org.onap.oom.dashboard.model.ControllerEndpointCredentials;
import org.onap.oom.dashboard.model.ControllerEndpointTransport;
import org.onap.oom.dashboard.model.RestResponseError;
import org.onap.oom.dashboard.model.RestResponseSuccess;
import org.onap.oom.dashboard.service.ControllerEndpointService;
import org.openecomp.portalsdk.core.domain.User;
import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
import org.openecomp.portalsdk.core.web.support.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* This controller maps requests for the application's landing page, which is
* an Angular single-page application.
*/
@Controller
@RequestMapping("/")
public class DashboardHomeController extends DashboardRestrictedBaseController {
private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(DashboardHomeController.class);
@Autowired
private ControllerEndpointService controllerEndpointService;
/**
* For general use in these methods
*/
private final ObjectMapper mapper;
private static final String CONTROLLERS_PATH = "controllers";
/**
* Never forget that Spring autowires fields AFTER the constructor is
* called.
*/
public DashboardHomeController() {
mapper = new ObjectMapper();
// Do not serialize null values
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
}
/**
* @return View name key, which is resolved to a file using an Apache tiles
* "definitions.xml" file.
*/
@RequestMapping(value = { "/ecd" }, method = RequestMethod.GET)
public ModelAndView dbcDefaultController() {
// a model is only useful for JSP; this app is angular.
return new ModelAndView("ecd_home_tdkey");
}
/**
* Gets the available controller endpoints.
*
* @param request
* HttpServletRequest
* @return List of ControllerEndpointTransport objects, or an error on
* failure
* @throws Exception
* if application user is not found
*/
@RequestMapping(value = { CONTROLLERS_PATH }, method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public String getControllers(HttpServletRequest request) {
logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME);
logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI());
String outboundJson = null;
// Static data
ControllerEndpointCredentials[] configured = getControllerEndpoints();
try {
User appUser = UserUtils.getUserSession(request);
if (appUser == null || appUser.getLoginId() == null || appUser.getLoginId().length() == 0)
throw new Exception("getControllers: Failed to get application user");
ControllerEndpointCredentials selectedInDb = getOrSetControllerEndpointSelection(appUser.getId());
// Built result from properties
ArrayList<ControllerEndpointTransport> list = new ArrayList<>();
for (ControllerEndpointCredentials ctrl : configured) {
// Check if this is the selected endpoint in DB
boolean selected = (selectedInDb != null && selectedInDb.getUrl() != null
&& selectedInDb.getUrl().equals(ctrl.getUrl()));
// Result has no privileged information
ControllerEndpointTransport transport = new ControllerEndpointTransport(selected, ctrl.getName(),
ctrl.getUrl());
list.add(transport);
}
outboundJson = mapper.writeValueAsString(list);
} catch (Exception ex) {
RestResponseError response = new RestResponseError("Failed to get controller endpoint list", ex);
outboundJson = response.toJson();
}
return outboundJson;
}
/**
* Sets the controller endpoint selection for the user.
*
* @param request
* HttpServletRequest
* @param endpoint
* Body with endpoint details
* @return Result indicating success or failure
* @throws Exception
* if application user is not found
*/
@RequestMapping(value = { CONTROLLERS_PATH }, method = RequestMethod.POST, produces = "application/json")
@ResponseBody
public String setControllerSelection(HttpServletRequest request, @RequestBody ControllerEndpointTransport endpoint)
throws Exception {
logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME);
logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI());
String outboundJson = null;
User appUser = UserUtils.getUserSession(request);
if (appUser == null || appUser.getLoginId() == null || appUser.getLoginId().length() == 0)
throw new Exception("setControllerSelection: Failed to get application user");
ControllerEndpoint dbEntry = new ControllerEndpoint(appUser.getId(), endpoint.getName(), endpoint.getUrl());
controllerEndpointService.updateControllerEndpointSelection(dbEntry);
RestResponseSuccess success = new RestResponseSuccess("Updated selection to " + endpoint.getName());
outboundJson = mapper.writeValueAsString(success);
return outboundJson;
}
}
|