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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
/*******************************************************************************
* =============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.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.onap.oom.dashboard.domain.ControllerEndpoint;
import org.onap.oom.dashboard.exception.OOMDashboardException;
import org.onap.oom.dashboard.model.ControllerEndpointCredentials;
import org.onap.oom.dashboard.rest.ControllerRestClientImpl;
import org.onap.oom.dashboard.rest.ControllerRestClientMockImpl;
import org.onap.oom.dashboard.rest.IControllerRestClient;
import org.onap.oom.dashboard.service.ControllerEndpointService;
import org.onap.oom.dashboard.util.DashboardProperties;
import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
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 com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* This base class provides utility methods to child controllers.
*/
public class DashboardRestrictedBaseController extends RestrictedBaseController {
/**
* Logger that conforms with ECOMP guidelines
*/
private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(DashboardRestrictedBaseController.class);
/**
* Application name
*/
protected static final String APP_NAME = "ecd-app";
/**
* EELF-approved format
*/
protected static final DateFormat logDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
/**
* Query parameter for desired page number
*/
protected static final String PAGE_NUM_QUERY_PARAM = "pageNum";
/**
* Query parameter for desired items per page
*/
protected static final String PAGE_SIZE_QUERY_PARAM = "viewPerPage";
/**
* For general use in these methods and subclasses
*/
protected final ObjectMapper objectMapper = new ObjectMapper();
/**
* Application properties - NOT available to constructor.
*/
@Autowired
private DashboardProperties appProperties;
/**
* For getting selected controller
*/
@Autowired
private ControllerEndpointService controllerEndpointService;
/**
* Hello Spring, here's your no-arg constructor.
*/
public DashboardRestrictedBaseController() {
// Do not serialize null values
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
}
/**
* Access method for subclasses.
*
* @return DbcappProperties object that was autowired by Spring.
*/
protected DashboardProperties getAppProperties() {
return appProperties;
}
/**
* Gets the requested page number from a query parameter in the
* HttpServletRequest. Defaults to 1, which is useful to allow manual
* testing of endpoints without supplying those pesky parameters.
*
* @param request
* HttpServletRequest
* @return Value of query parameter {@link #PAGE_NUM_QUERY_PARAM}; 1 if not
* found.
*/
protected int getRequestPageNumber(HttpServletRequest request) {
int pageNum = 1;
String param = request.getParameter(PAGE_NUM_QUERY_PARAM);
if (param != null)
pageNum = Integer.parseInt(param);
return pageNum;
}
/**
* Gets the requested page size from a query parameter in the
* HttpServletRequest. Defaults to 50, which is useful to allow manual
* testing of endpoints without supplying those pesky parameters.
*
* @param request
* HttpServletRequest
* @return Value of query parameter {@link #PAGE_SIZE_QUERY_PARAM}; 50 if
* not found.
*/
protected int getRequestPageSize(HttpServletRequest request) {
int pageSize = 50;
String param = request.getParameter(PAGE_SIZE_QUERY_PARAM);
if (param != null)
pageSize = Integer.parseInt(param);
return pageSize;
}
/**
* Gets the items for the specified page from the specified list.
*
* @param pageNum
* Page number requested by user, indexed from 1
* @param pageSize
* Number of items per page
* @param itemList
* List of items to adjust
* @return List of items; empty list if from==to
*/
@SuppressWarnings("rawtypes")
protected static List getPageOfList(final int pageNum, final int pageSize, final List itemList) {
int firstIndexOnThisPage = pageSize * (pageNum - 1);
int firstIndexOnNextPage = pageSize * pageNum;
int fromIndex = firstIndexOnThisPage < itemList.size() ? firstIndexOnThisPage : itemList.size();
int toIndex = firstIndexOnNextPage < itemList.size() ? firstIndexOnNextPage : itemList.size();
return itemList.subList(fromIndex, toIndex);
}
/**
* Gets all configured controllers from properties.
*
* @return Array of ControllerEndpointRestricted objects
* @throws IllegalStateException
* if a required property is not found
*/
protected ControllerEndpointCredentials[] getControllerEndpoints() {
final String[] controllerKeys = appProperties.getCsvListProperty(DashboardProperties.CONTROLLER_KEY_LIST);
ControllerEndpointCredentials[] controllers = new ControllerEndpointCredentials[controllerKeys.length];
for (int i = 0; i < controllerKeys.length; ++i) {
String key = controllerKeys[i];
final String name = appProperties.getControllerProperty(key, DashboardProperties.CONTROLLER_SUBKEY_NAME);
final String url = appProperties.getControllerProperty(key, DashboardProperties.CONTROLLER_SUBKEY_URL);
final String user = appProperties.getControllerProperty(key,
DashboardProperties.CONTROLLER_SUBKEY_USERNAME);
final String pass = appProperties.getControllerProperty(key,
DashboardProperties.CONTROLLER_SUBKEY_PASSWORD);
final boolean encr = new Boolean(
appProperties.getControllerProperty(key, DashboardProperties.CONTROLLER_SUBKEY_ENCRYPTED));
logger.debug(EELFLoggerDelegate.debugLogger, "getConfiguredControllers: key {} yields url {}", key, url);
controllers[i] = new ControllerEndpointCredentials(false, name, url, user, pass, encr);
}
return controllers;
}
/**
* Gets the controller endpoint for the specified user ID. Chooses the first
* one from properties if the user has not selected one previously.
*
* @param userId
* Database User ID
* @return ControllerEndpointCredentials for the specified user
*/
protected ControllerEndpointCredentials getOrSetControllerEndpointSelection(long userId) {
// Always need the complete list from properties
ControllerEndpointCredentials[] configured = getControllerEndpoints();
// See if the database has an entry for this user
ControllerEndpoint dbEntry = controllerEndpointService.getControllerEndpointSelection(userId);
// If no row found DAO returns an object with null entries.
if (dbEntry == null || dbEntry.getName() == null) {
// Arbitrarily choose the first one
ControllerEndpointCredentials first = configured[0];
dbEntry = new ControllerEndpoint(userId, first.getName(), first.getUrl());
controllerEndpointService.updateControllerEndpointSelection(dbEntry);
}
// Fetch complete details for the selected item
ControllerEndpointCredentials selected = null;
for (ControllerEndpointCredentials cec : configured) {
if (dbEntry.getUrl().equals(cec.getUrl())) {
selected = cec;
break;
}
}
// Defend against a stale database entry.
if (selected == null) {
selected = configured[0];
dbEntry = new ControllerEndpoint(userId, selected.getName(), selected.getUrl());
controllerEndpointService.updateControllerEndpointSelection(dbEntry);
}
return selected;
}
/**
* Convenience method that gets the user ID from the session and fetches the
* REST client. Factors code out of subclass methods.
*
* @param request
* HttpServletRequest
* @return REST client appropriate for the user
*/
protected IControllerRestClient getControllerRestClient(HttpServletRequest request) throws Exception {
User appUser = UserUtils.getUserSession(request);
if (appUser == null || appUser.getLoginId() == null || appUser.getLoginId().length() == 0)
throw new OOMDashboardException("getControllerRestClient: Failed to get application user");
return getControllerRestClient(appUser.getId());
}
/**
* Gets a REST client; either a mock client (returns canned data), or a real
* client with appropriate credentials from properties.
*
* @return REST client.
*/
protected IControllerRestClient getControllerRestClient(long userId) throws Exception {
IControllerRestClient result = null;
// Be robust to missing development-only property
boolean mock = false;
if (appProperties.containsProperty(DashboardProperties.CONTROLLER_MOCK_DATA))
mock = appProperties.getBooleanProperty(DashboardProperties.CONTROLLER_MOCK_DATA);
if (mock) {
result = new ControllerRestClientMockImpl();
} else {
ControllerEndpointCredentials details = getOrSetControllerEndpointSelection(userId);
final String clearText = details.getEncryptedPassword() ? details.decryptPassword() : details.getPassword();
result = new ControllerRestClientImpl(details.getUrl(), details.getUsername(), clearText);
}
return result;
}
}
|