1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * * ===========================================================================
\r
7 * * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * * you may not use this file except in compliance with the License.
\r
9 * * You may obtain a copy of the License at
\r
11 * * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * * Unless required by applicable law or agreed to in writing, software
\r
14 * * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * * See the License for the specific language governing permissions and
\r
17 * * limitations under the License.
\r
18 * * ============LICENSE_END====================================================
\r
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
23 package com.att.authz.service.api;
\r
25 import static com.att.authz.layer.Result.OK;
\r
26 import static com.att.cssa.rserv.HttpMethods.GET;
\r
28 import java.text.SimpleDateFormat;
\r
29 import java.util.ArrayList;
\r
30 import java.util.Collections;
\r
31 import java.util.Date;
\r
32 import java.util.GregorianCalendar;
\r
34 import javax.servlet.http.HttpServletRequest;
\r
35 import javax.servlet.http.HttpServletResponse;
\r
37 import com.att.aft.dme2.internal.jetty.http.HttpStatus;
\r
38 import com.att.authz.env.AuthzTrans;
\r
39 import com.att.authz.facade.AuthzFacade;
\r
40 import com.att.authz.layer.Result;
\r
41 import com.att.authz.service.AuthAPI;
\r
42 import com.att.authz.service.Code;
\r
43 import com.att.authz.service.mapper.Mapper.API;
\r
44 import com.att.dao.aaf.cass.Status;
\r
47 * Pull certain types of History Info
\r
49 * Specify yyyymm as
\r
51 * commas 201503,201504
\r
52 * ranges 201501-201504
\r
53 * combinations 201301,201401,201501-201504
\r
57 public class API_History {
\r
59 * Normal Init level APIs
\r
65 public static void init(AuthAPI authzAPI, AuthzFacade facade) throws Exception {
\r
69 authzAPI.route(GET,"/authz/hist/user/:user",API.HISTORY,new Code(facade,"Get History by User", true) {
\r
71 public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
\r
75 years = getYears(req);
\r
76 descend = decending(req);
\r
77 } catch(Exception e) {
\r
78 context.error(trans, resp, Result.err(Status.ERR_BadData, e.getMessage()));
\r
82 Result<Void> r = context.getHistoryByUser(trans, resp, pathParam(req,":user"),years,descend);
\r
85 resp.setStatus(HttpStatus.OK_200);
\r
88 context.error(trans,resp,r);
\r
96 authzAPI.route(GET,"/authz/hist/ns/:ns",API.HISTORY,new Code(facade,"Get History by Namespace", true) {
\r
98 public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
\r
102 years = getYears(req);
\r
103 descend = decending(req);
\r
104 } catch(Exception e) {
\r
105 context.error(trans, resp, Result.err(Status.ERR_BadData, e.getMessage()));
\r
109 Result<Void> r = context.getHistoryByNS(trans, resp, pathParam(req,":ns"),years,descend);
\r
112 resp.setStatus(HttpStatus.OK_200);
\r
115 context.error(trans,resp,r);
\r
121 * Get History by Role
\r
123 authzAPI.route(GET,"/authz/hist/role/:role",API.HISTORY,new Code(facade,"Get History by Role", true) {
\r
125 public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
\r
129 years = getYears(req);
\r
130 descend = decending(req);
\r
131 } catch(Exception e) {
\r
132 context.error(trans, resp, Result.err(Status.ERR_BadData, e.getMessage()));
\r
136 Result<Void> r = context.getHistoryByRole(trans, resp, pathParam(req,":role"),years,descend);
\r
139 resp.setStatus(HttpStatus.OK_200);
\r
142 context.error(trans,resp,r);
\r
148 * Get History by Perm Type
\r
150 authzAPI.route(GET,"/authz/hist/perm/:type",API.HISTORY,new Code(facade,"Get History by Perm Type", true) {
\r
152 public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
\r
156 years = getYears(req);
\r
157 descend = decending(req);
\r
158 } catch(Exception e) {
\r
159 context.error(trans, resp, Result.err(Status.ERR_BadData, e.getMessage()));
\r
163 Result<Void> r = context.getHistoryByPerm(trans, resp, pathParam(req,":type"),years,descend);
\r
166 resp.setStatus(HttpStatus.OK_200);
\r
169 context.error(trans,resp,r);
\r
175 // Check if Ascending
\r
176 private static int decending(HttpServletRequest req) {
\r
177 if("true".equalsIgnoreCase(req.getParameter("desc")))return -1;
\r
178 if("true".equalsIgnoreCase(req.getParameter("asc")))return 1;
\r
182 // Get Common "yyyymm" parameter, or none
\r
183 private static final SimpleDateFormat FMT = new SimpleDateFormat("yyyyMM");
\r
185 private static int[] getYears(HttpServletRequest req) throws NumberFormatException {
\r
186 String yyyymm = req.getParameter("yyyymm");
\r
187 ArrayList<Integer> ai= new ArrayList<Integer>();
\r
189 GregorianCalendar gc = new GregorianCalendar();
\r
190 // three months is the default
\r
191 for(int i=0;i<3;++i) {
\r
192 ai.add(Integer.parseInt(FMT.format(gc.getTime())));
\r
193 gc.add(GregorianCalendar.MONTH, -1);
\r
196 for(String ym : yyyymm.split(",")) {
\r
197 String range[] = ym.split("\\s*-\\s*");
\r
198 switch(range.length) {
\r
202 if(!ym.endsWith("-")) {
\r
203 ai.add(getNum(ym));
\r
206 range=new String[] {ym.substring(0, 6),FMT.format(new Date())};
\r
209 GregorianCalendar gc = new GregorianCalendar();
\r
210 gc.set(GregorianCalendar.MONTH, Integer.parseInt(range[1].substring(4,6))-1);
\r
211 gc.set(GregorianCalendar.YEAR, Integer.parseInt(range[1].substring(0,4)));
\r
212 int end = getNum(FMT.format(gc.getTime()));
\r
214 gc.set(GregorianCalendar.MONTH, Integer.parseInt(range[0].substring(4,6))-1);
\r
215 gc.set(GregorianCalendar.YEAR, Integer.parseInt(range[0].substring(0,4)));
\r
216 for(int i=getNum(FMT.format(gc.getTime()));i<=end;gc.add(GregorianCalendar.MONTH, 1),i=getNum(FMT.format(gc.getTime()))) {
\r
224 throw new NumberFormatException(yyyymm + " is an invalid number or range");
\r
226 Collections.sort(ai);
\r
227 int ym[] = new int[ai.size()];
\r
228 for(int i=0;i<ym.length;++i) {
\r
234 private static int getNum(String n) {
\r
235 if(n==null || n.length()!=6) throw new NumberFormatException(n + " is not in YYYYMM format");
\r
236 return Integer.parseInt(n);
\r