f3b614701e536a3dfab31de362c152c30881038e
[ccsdk/dashboard.git] /
1 /*******************************************************************************
2  * =============LICENSE_START=========================================================
3  *
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
10  *  
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *  
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=========================================================
19  *
20  *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  *******************************************************************************/
22 package org.onap.ccsdk.dashboard.controller;
23
24 import java.text.DateFormat;
25 import java.text.SimpleDateFormat;
26 import java.util.List;
27
28 import javax.servlet.http.HttpServletRequest;
29
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;
34
35 import com.fasterxml.jackson.annotation.JsonInclude;
36 import com.fasterxml.jackson.databind.ObjectMapper;
37 import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
38
39 /**
40  * This base class provides utility methods to child controllers.
41  */
42 public class DashboardRestrictedBaseController extends RestrictedBaseController {
43
44     /**
45      * Application name
46      */
47     protected static final String APP_NAME = "ecd-app";
48
49     /**
50      * EELF-approved format
51      */
52     protected static final DateFormat logDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
53
54     /**
55      * Query parameter for desired page number
56      */
57     protected static final String PAGE_NUM_QUERY_PARAM = "pageNum";
58
59     /**
60      * Query parameter for desired items per page
61      */
62     protected static final String PAGE_SIZE_QUERY_PARAM = "viewPerPage";
63
64     /**
65      * For general use in these methods and subclasses
66      */
67     protected final ObjectMapper objectMapper = new ObjectMapper();
68
69     /**
70      * Application properties - NOT available to constructor.
71      */
72     @Autowired
73     protected DashboardProperties appProperties;
74
75     /**
76      * For getting selected controller
77      */
78     @Autowired
79     private ControllerEndpointService controllerEndpointService;
80
81     /**
82      * Hello Spring, here's your no-arg constructor.
83      */
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());
89     }
90
91     /**
92      * Access method for subclasses.
93      * 
94      * @return DbcappProperties object that was autowired by Spring.
95      */
96     protected DashboardProperties getAppProperties() {
97         return appProperties;
98     }
99
100     /**
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.
104      * 
105      * @param request HttpServletRequest
106      * @return Value of query parameter {@link #PAGE_NUM_QUERY_PARAM}; 1 if not
107      *         found.
108      */
109     protected int getRequestPageNumber(HttpServletRequest request) {
110         int pageNum = 1;
111         String param = request.getParameter(PAGE_NUM_QUERY_PARAM);
112         if (param != null)
113             pageNum = Integer.parseInt(param);
114         return pageNum;
115     }
116
117     /**
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.
121      * 
122      * @param request HttpServletRequest
123      * @return Value of query parameter {@link #PAGE_SIZE_QUERY_PARAM}; 50 if not
124      *         found.
125      */
126     protected int getRequestPageSize(HttpServletRequest request) {
127         int pageSize = 50;
128         String param = request.getParameter(PAGE_SIZE_QUERY_PARAM);
129         if (param != null)
130             pageSize = Integer.parseInt(param);
131         return pageSize;
132     }
133
134     /**
135      * Gets the items for the specified page from the specified list.
136      * 
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
141      */
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);
149     }
150 }