1 /*******************************************************************************
2 * =============LICENSE_START=========================================================
4 * =================================================================================
5 * Copyright (c) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21 *******************************************************************************/
22 package org.onap.ccsdk.dashboard.controller;
24 import java.text.DateFormat;
25 import java.text.SimpleDateFormat;
26 import java.util.List;
28 import javax.servlet.http.HttpServletRequest;
30 import org.onap.ccsdk.dashboard.service.ControllerEndpointService;
31 import org.onap.ccsdk.dashboard.util.DashboardProperties;
32 import org.onap.portalsdk.core.controller.RestrictedBaseController;
33 import org.springframework.beans.factory.annotation.Autowired;
35 import com.fasterxml.jackson.annotation.JsonInclude;
36 import com.fasterxml.jackson.databind.ObjectMapper;
37 import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
40 * This base class provides utility methods to child controllers.
42 public class DashboardRestrictedBaseController extends RestrictedBaseController {
47 protected static final String APP_NAME = "ecd-app";
50 * EELF-approved format
52 protected static final DateFormat logDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
55 * Query parameter for desired page number
57 protected static final String PAGE_NUM_QUERY_PARAM = "pageNum";
60 * Query parameter for desired items per page
62 protected static final String PAGE_SIZE_QUERY_PARAM = "viewPerPage";
65 * For general use in these methods and subclasses
67 protected final ObjectMapper objectMapper = new ObjectMapper();
70 * Application properties - NOT available to constructor.
73 protected DashboardProperties appProperties;
76 * For getting selected controller
79 private ControllerEndpointService controllerEndpointService;
82 * Hello Spring, here's your no-arg constructor.
84 public DashboardRestrictedBaseController() {
85 // Do not serialize null values
86 objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
87 // Register Jdk8Module() for Stream and Optional types
88 objectMapper.registerModule(new Jdk8Module());
92 * Access method for subclasses.
94 * @return DbcappProperties object that was autowired by Spring.
96 protected DashboardProperties getAppProperties() {
101 * Gets the requested page number from a query parameter in the
102 * HttpServletRequest. Defaults to 1, which is useful to allow manual testing of
103 * endpoints without supplying those pesky parameters.
105 * @param request HttpServletRequest
106 * @return Value of query parameter {@link #PAGE_NUM_QUERY_PARAM}; 1 if not
109 protected int getRequestPageNumber(HttpServletRequest request) {
111 String param = request.getParameter(PAGE_NUM_QUERY_PARAM);
113 pageNum = Integer.parseInt(param);
118 * Gets the requested page size from a query parameter in the
119 * HttpServletRequest. Defaults to 50, which is useful to allow manual testing
120 * of endpoints without supplying those pesky parameters.
122 * @param request HttpServletRequest
123 * @return Value of query parameter {@link #PAGE_SIZE_QUERY_PARAM}; 50 if not
126 protected int getRequestPageSize(HttpServletRequest request) {
128 String param = request.getParameter(PAGE_SIZE_QUERY_PARAM);
130 pageSize = Integer.parseInt(param);
135 * Gets the items for the specified page from the specified list.
137 * @param pageNum Page number requested by user, indexed from 1
138 * @param pageSize Number of items per page
139 * @param itemList List of items to adjust
140 * @return List of items; empty list if from==to
142 @SuppressWarnings("rawtypes")
143 protected static List getPageOfList(final int pageNum, final int pageSize, final List itemList) {
144 int firstIndexOnThisPage = pageSize * (pageNum - 1);
145 int firstIndexOnNextPage = pageSize * pageNum;
146 int fromIndex = firstIndexOnThisPage < itemList.size() ? firstIndexOnThisPage : itemList.size();
147 int toIndex = firstIndexOnNextPage < itemList.size() ? firstIndexOnNextPage : itemList.size();
148 return itemList.subList(fromIndex, toIndex);