X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=auth%2Fauth-service%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Faaf%2Fauth%2Fservice%2Fapi%2FAPI_History.java;h=341719c3100298ed57bf5f3c4c6e3333c7ee92fd;hb=1296352d8eafee57f982a4342ad79ada4aa56d28;hp=8c55e7dc7ef2b6f7f1b8997dc0d576afb47dfcf5;hpb=b126c6c5f625432722405538692184f5c74edaad;p=aaf%2Fauthz.git diff --git a/auth/auth-service/src/main/java/org/onap/aaf/auth/service/api/API_History.java b/auth/auth-service/src/main/java/org/onap/aaf/auth/service/api/API_History.java index 8c55e7dc..341719c3 100644 --- a/auth/auth-service/src/main/java/org/onap/aaf/auth/service/api/API_History.java +++ b/auth/auth-service/src/main/java/org/onap/aaf/auth/service/api/API_History.java @@ -7,9 +7,9 @@ * 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. @@ -44,196 +44,223 @@ import org.onap.aaf.auth.service.mapper.Mapper.API; /** * Pull certain types of History Info - * - * Specify yyyymm as - * single - 201504 + * + * Specify yyyymm as + * single - 201504 * commas 201503,201504 * ranges 201501-201504 * combinations 201301,201401,201501-201504 - * + * * @author Jonathan * */ public class API_History { - /** - * Normal Init level APIs - * - * @param authzAPI - * @param facade - * @throws Exception - */ - public static void init(AAF_Service authzAPI, AuthzFacade facade) throws Exception { - /** - * Get History - */ - authzAPI.route(GET,"/authz/hist/user/:user",API.HISTORY,new Code(facade,"Get History by User", true) { - @Override - public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception { - int[] years; - int descend; - try { - years = getYears(req); - descend = decending(req); - } catch(Exception e) { - context.error(trans, resp, Result.err(Status.ERR_BadData, e.getMessage())); - return; - } - - Result r = context.getHistoryByUser(trans, resp, pathParam(req,":user"),years,descend); - switch(r.status) { - case OK: - resp.setStatus(HttpStatus.OK_200); - break; - default: - context.error(trans,resp,r); - } - } - }); - - /** - * Get History by NS - */ - authzAPI.route(GET,"/authz/hist/ns/:ns",API.HISTORY,new Code(facade,"Get History by Namespace", true) { - @Override - public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception { - int[] years; - int descend; - try { - years = getYears(req); - descend = decending(req); - } catch(Exception e) { - context.error(trans, resp, Result.err(Status.ERR_BadData, e.getMessage())); - return; - } - - Result r = context.getHistoryByNS(trans, resp, pathParam(req,":ns"),years,descend); - switch(r.status) { - case OK: - resp.setStatus(HttpStatus.OK_200); - break; - default: - context.error(trans,resp,r); - } - } - }); - - /** - * Get History by Role - */ - authzAPI.route(GET,"/authz/hist/role/:role",API.HISTORY,new Code(facade,"Get History by Role", true) { - @Override - public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception { - int[] years; - int descend; - try { - years = getYears(req); - descend = decending(req); - } catch(Exception e) { - context.error(trans, resp, Result.err(Status.ERR_BadData, e.getMessage())); - return; - } - - Result r = context.getHistoryByRole(trans, resp, pathParam(req,":role"),years,descend); - switch(r.status) { - case OK: - resp.setStatus(HttpStatus.OK_200); - break; - default: - context.error(trans,resp,r); - } - } - }); - - /** - * Get History by Perm Type - */ - authzAPI.route(GET,"/authz/hist/perm/:type",API.HISTORY,new Code(facade,"Get History by Perm Type", true) { - @Override - public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception { - int[] years; - int descend; - try { - years = getYears(req); - descend = decending(req); - } catch(Exception e) { - context.error(trans, resp, Result.err(Status.ERR_BadData, e.getMessage())); - return; - } - - Result r = context.getHistoryByPerm(trans, resp, pathParam(req,":type"),years,descend); - switch(r.status) { - case OK: - resp.setStatus(HttpStatus.OK_200); - break; - default: - context.error(trans,resp,r); - } - } - }); - } - - // Check if Ascending - private static int decending(HttpServletRequest req) { - if("true".equalsIgnoreCase(req.getParameter("desc")))return -1; - if("true".equalsIgnoreCase(req.getParameter("asc")))return 1; - return 0; - } - - // Get Common "yyyymm" parameter, or none - - private static int[] getYears(HttpServletRequest req) throws NumberFormatException { - // Sonar says threading issues. - SimpleDateFormat FMT = new SimpleDateFormat("yyyyMM"); - String yyyymm = req.getParameter("yyyymm"); - ArrayList ai= new ArrayList(); - if(yyyymm==null) { - GregorianCalendar gc = new GregorianCalendar(); - // three months is the default - for(int i=0;i<3;++i) { - ai.add(Integer.parseInt(FMT.format(gc.getTime()))); - gc.add(GregorianCalendar.MONTH, -1); - } - } else { - for(String ym : yyyymm.split(",")) { - String range[] = ym.split("\\s*-\\s*"); - switch(range.length) { - case 0: - break; - case 1: - if(!ym.endsWith("-")) { - ai.add(getNum(ym)); - break; - } else { - range=new String[] {ym.substring(0, 6),FMT.format(new Date())}; - } - default: - GregorianCalendar gc = new GregorianCalendar(); - gc.set(GregorianCalendar.MONTH, Integer.parseInt(range[1].substring(4,6))-1); - gc.set(GregorianCalendar.YEAR, Integer.parseInt(range[1].substring(0,4))); - int end = getNum(FMT.format(gc.getTime())); - - gc.set(GregorianCalendar.MONTH, Integer.parseInt(range[0].substring(4,6))-1); - gc.set(GregorianCalendar.YEAR, Integer.parseInt(range[0].substring(0,4))); - for(int i=getNum(FMT.format(gc.getTime()));i<=end;gc.add(GregorianCalendar.MONTH, 1),i=getNum(FMT.format(gc.getTime()))) { - ai.add(i); - } - - } - } - } - if(ai.size()==0) { - throw new NumberFormatException(yyyymm + " is an invalid number or range"); - } - Collections.sort(ai); - int ym[] = new int[ai.size()]; - for(int i=0;i r = context.getHistoryByUser(trans, resp, pathParam(req,":user"),years,descend); + switch(r.status) { + case OK: + resp.setStatus(HttpStatus.OK_200); + break; + default: + context.error(trans,resp,r); + } + } + }); + + /** + * Get History by NS + */ + authzAPI.route(GET,"/authz/hist/ns/:ns",API.HISTORY,new Code(facade,"Get History by Namespace", true) { + @Override + public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception { + int[] years; + int descend; + try { + years = getYears(req); + descend = decending(req); + } catch (Exception e) { + context.error(trans, resp, Result.err(Status.ERR_BadData, e.getMessage())); + return; + } + + Result r = context.getHistoryByNS(trans, resp, pathParam(req,":ns"),years,descend); + switch(r.status) { + case OK: + resp.setStatus(HttpStatus.OK_200); + break; + default: + context.error(trans,resp,r); + } + } + }); + + /** + * Get History by Role + */ + authzAPI.route(GET,"/authz/hist/role/:role",API.HISTORY,new Code(facade,"Get History by Role", true) { + @Override + public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception { + int[] years; + int descend; + try { + years = getYears(req); + descend = decending(req); + } catch (Exception e) { + context.error(trans, resp, Result.err(Status.ERR_BadData, e.getMessage())); + return; + } + + Result r = context.getHistoryByRole(trans, resp, pathParam(req,":role"),years,descend); + switch(r.status) { + case OK: + resp.setStatus(HttpStatus.OK_200); + break; + default: + context.error(trans,resp,r); + } + } + }); + + /** + * Get History by Perm Type + */ + authzAPI.route(GET,"/authz/hist/perm/:type",API.HISTORY,new Code(facade,"Get History by Perm Type", true) { + @Override + public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception { + int[] years; + int descend; + try { + years = getYears(req); + descend = decending(req); + } catch (Exception e) { + context.error(trans, resp, Result.err(Status.ERR_BadData, e.getMessage())); + return; + } + + Result r = context.getHistoryByPerm(trans, resp, pathParam(req,":type"),years,descend); + switch(r.status) { + case OK: + resp.setStatus(HttpStatus.OK_200); + break; + default: + context.error(trans,resp,r); + } + } + }); + + /** + * Get History by Subject + */ + authzAPI.route(GET,"/authz/hist/subject/:type/:subject",API.HISTORY,new Code(facade,"Get History by Perm Type", true) { + @Override + public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception { + int[] years; + int descend; + try { + years = getYears(req); + descend = decending(req); + } catch (Exception e) { + context.error(trans, resp, Result.err(Status.ERR_BadData, e.getMessage())); + return; + } + + Result r = context.getHistoryBySubject(trans, resp, pathParam(req,":type"), pathParam(req,":subject"),years,descend); + switch(r.status) { + case OK: + resp.setStatus(HttpStatus.OK_200); + break; + default: + context.error(trans,resp,r); + } + } + }); + } + + // Check if Ascending + private static int decending(HttpServletRequest req) { + if ("true".equalsIgnoreCase(req.getParameter("desc")))return -1; + if ("true".equalsIgnoreCase(req.getParameter("asc")))return 1; + return 0; + } + + // Get Common "yyyymm" parameter, or none + + private static int[] getYears(HttpServletRequest req) throws NumberFormatException { + // Sonar says threading issues. + SimpleDateFormat FMT = new SimpleDateFormat("yyyyMM"); + String yyyymm = req.getParameter("yyyymm"); + ArrayList ai= new ArrayList<>(); + if (yyyymm==null) { + GregorianCalendar gc = new GregorianCalendar(); + // three months is the default + for (int i=0;i<3;++i) { + ai.add(Integer.parseInt(FMT.format(gc.getTime()))); + gc.add(GregorianCalendar.MONTH, -1); + } + } else { + for (String ym : yyyymm.split(",")) { + String range[] = ym.split("\\s*-\\s*"); + switch(range.length) { + case 0: + break; + case 1: + if (!ym.endsWith("-")) { + ai.add(getNum(ym)); + break; + } else { + range=new String[] {ym.substring(0, 6),FMT.format(new Date())}; + } + default: + GregorianCalendar gc = new GregorianCalendar(); + gc.set(GregorianCalendar.MONTH, Integer.parseInt(range[1].substring(4,6))-1); + gc.set(GregorianCalendar.YEAR, Integer.parseInt(range[1].substring(0,4))); + int end = getNum(FMT.format(gc.getTime())); + + gc.set(GregorianCalendar.MONTH, Integer.parseInt(range[0].substring(4,6))-1); + gc.set(GregorianCalendar.YEAR, Integer.parseInt(range[0].substring(0,4))); + for (int i=getNum(FMT.format(gc.getTime()));i<=end;gc.add(GregorianCalendar.MONTH, 1),i=getNum(FMT.format(gc.getTime()))) { + ai.add(i); + } + + } + } + } + if (ai.size()==0) { + throw new NumberFormatException(yyyymm + " is an invalid number or range"); + } + Collections.sort(ai); + int ym[] = new int[ai.size()]; + for (int i=0;i