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 org.onap.aaf.authz.service.api;
\r
25 import static org.onap.aaf.authz.layer.Result.OK;
\r
26 import static org.onap.aaf.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 org.onap.aaf.authz.env.AuthzTrans;
\r
38 import org.onap.aaf.authz.facade.AuthzFacade;
\r
39 import org.onap.aaf.authz.layer.Result;
\r
40 import org.onap.aaf.authz.service.AuthAPI;
\r
41 import org.onap.aaf.authz.service.Code;
\r
42 import org.onap.aaf.authz.service.mapper.Mapper.API;
\r
43 import org.onap.aaf.dao.aaf.cass.Status;
\r
45 import com.att.aft.dme2.internal.jetty.http.HttpStatus;
\r
48 * Pull certain types of History Info
\r
50 * Specify yyyymm as
\r
52 * commas 201503,201504
\r
53 * ranges 201501-201504
\r
54 * combinations 201301,201401,201501-201504
\r
58 public class API_History {
\r
60 * Normal Init level APIs
\r
66 public static void init(AuthAPI authzAPI, AuthzFacade facade) throws Exception {
\r
70 authzAPI.route(GET,"/authz/hist/user/:user",API.HISTORY,new Code(facade,"Get History by User", true) {
\r
72 public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
\r
76 years = getYears(req);
\r
77 descend = decending(req);
\r
78 } catch(Exception e) {
\r
79 context.error(trans, resp, Result.err(Status.ERR_BadData, e.getMessage()));
\r
83 Result<Void> r = context.getHistoryByUser(trans, resp, pathParam(req,":user"),years,descend);
\r
86 resp.setStatus(HttpStatus.OK_200);
\r
89 context.error(trans,resp,r);
\r
97 authzAPI.route(GET,"/authz/hist/ns/:ns",API.HISTORY,new Code(facade,"Get History by Namespace", true) {
\r
99 public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
\r
103 years = getYears(req);
\r
104 descend = decending(req);
\r
105 } catch(Exception e) {
\r
106 context.error(trans, resp, Result.err(Status.ERR_BadData, e.getMessage()));
\r
110 Result<Void> r = context.getHistoryByNS(trans, resp, pathParam(req,":ns"),years,descend);
\r
113 resp.setStatus(HttpStatus.OK_200);
\r
116 context.error(trans,resp,r);
\r
122 * Get History by Role
\r
124 authzAPI.route(GET,"/authz/hist/role/:role",API.HISTORY,new Code(facade,"Get History by Role", true) {
\r
126 public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
\r
130 years = getYears(req);
\r
131 descend = decending(req);
\r
132 } catch(Exception e) {
\r
133 context.error(trans, resp, Result.err(Status.ERR_BadData, e.getMessage()));
\r
137 Result<Void> r = context.getHistoryByRole(trans, resp, pathParam(req,":role"),years,descend);
\r
140 resp.setStatus(HttpStatus.OK_200);
\r
143 context.error(trans,resp,r);
\r
149 * Get History by Perm Type
\r
151 authzAPI.route(GET,"/authz/hist/perm/:type",API.HISTORY,new Code(facade,"Get History by Perm Type", true) {
\r
153 public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
\r
157 years = getYears(req);
\r
158 descend = decending(req);
\r
159 } catch(Exception e) {
\r
160 context.error(trans, resp, Result.err(Status.ERR_BadData, e.getMessage()));
\r
164 Result<Void> r = context.getHistoryByPerm(trans, resp, pathParam(req,":type"),years,descend);
\r
167 resp.setStatus(HttpStatus.OK_200);
\r
170 context.error(trans,resp,r);
\r
176 // Check if Ascending
\r
177 private static int decending(HttpServletRequest req) {
\r
178 if("true".equalsIgnoreCase(req.getParameter("desc")))return -1;
\r
179 if("true".equalsIgnoreCase(req.getParameter("asc")))return 1;
\r
183 // Get Common "yyyymm" parameter, or none
\r
184 private static final SimpleDateFormat FMT = new SimpleDateFormat("yyyyMM");
\r
186 private static int[] getYears(HttpServletRequest req) throws NumberFormatException {
\r
187 String yyyymm = req.getParameter("yyyymm");
\r
188 ArrayList<Integer> ai= new ArrayList<Integer>();
\r
190 GregorianCalendar gc = new GregorianCalendar();
\r
191 // three months is the default
\r
192 for(int i=0;i<3;++i) {
\r
193 ai.add(Integer.parseInt(FMT.format(gc.getTime())));
\r
194 gc.add(GregorianCalendar.MONTH, -1);
\r
197 for(String ym : yyyymm.split(",")) {
\r
198 String range[] = ym.split("\\s*-\\s*");
\r
199 switch(range.length) {
\r
203 if(!ym.endsWith("-")) {
\r
204 ai.add(getNum(ym));
\r
207 range=new String[] {ym.substring(0, 6),FMT.format(new Date())};
\r
210 GregorianCalendar gc = new GregorianCalendar();
\r
211 gc.set(GregorianCalendar.MONTH, Integer.parseInt(range[1].substring(4,6))-1);
\r
212 gc.set(GregorianCalendar.YEAR, Integer.parseInt(range[1].substring(0,4)));
\r
213 int end = getNum(FMT.format(gc.getTime()));
\r
215 gc.set(GregorianCalendar.MONTH, Integer.parseInt(range[0].substring(4,6))-1);
\r
216 gc.set(GregorianCalendar.YEAR, Integer.parseInt(range[0].substring(0,4)));
\r
217 for(int i=getNum(FMT.format(gc.getTime()));i<=end;gc.add(GregorianCalendar.MONTH, 1),i=getNum(FMT.format(gc.getTime()))) {
\r
225 throw new NumberFormatException(yyyymm + " is an invalid number or range");
\r
227 Collections.sort(ai);
\r
228 int ym[] = new int[ai.size()];
\r
229 for(int i=0;i<ym.length;++i) {
\r
235 private static int getNum(String n) {
\r
236 if(n==null || n.length()!=6) throw new NumberFormatException(n + " is not in YYYYMM format");
\r
237 return Integer.parseInt(n);
\r