diff --git a/java/src/main/java/com/genexus/filters/APIObjectFilter.java b/java/src/main/java/com/genexus/filters/APIObjectFilter.java index 441711ac4..2d83bbe8d 100644 --- a/java/src/main/java/com/genexus/filters/APIObjectFilter.java +++ b/java/src/main/java/com/genexus/filters/APIObjectFilter.java @@ -19,82 +19,82 @@ public class APIObjectFilter extends Filter { private ArrayList appPath = new ArrayList(); - + static final String PRIVATE_DIR="private"; + static final String WEB_INFO="WEB-INF"; public static final Logger logger = LogManager.getLogger(APIObjectFilter.class); - public void doFilter(IServletRequest request, IServletResponse response, IFilterChain chain) throws Exception { - if (request.isHttpServletRequest() && response.isHttpServletResponse()) { - IHttpServletRequest httpRequest = request.getHttpServletRequest(); + public void doFilter(IServletRequest request, IServletResponse response, IFilterChain chain) throws Exception { + if (request.isHttpServletRequest() && response.isHttpServletResponse()) { + IHttpServletRequest httpRequest = request.getHttpServletRequest(); String path = httpRequest.getRequestURI().substring(httpRequest.getContextPath().length()).substring(1); String urlString = path.toLowerCase(); boolean isPath = false; - for(String appBasePath : appPath) - { - if (urlString.startsWith(appBasePath)) - { + for (String appBasePath : this.appPath) { + if (urlString.startsWith(appBasePath)) { isPath = true; break; } } - if(isPath) - { + if(isPath) { String fwdURI = "/rest/" + path; + logger.info("Forwarding from " + path +" to: " + fwdURI) ; httpRequest.getRequestDispatcher(fwdURI).forward(request,response); } - else - { + else { chain.doFilter(request, response); } } - else - { + else { chain.doFilter(request, response); } } public void init(Map headers, String path, String sessionCookieName) throws ServletException { try { - String paramValue = headers.get("BasePath"); - if (paramValue != null && !paramValue.isEmpty()) - { - if (paramValue.equals("*")) - { - if (path != null && !path.isEmpty()) - { - paramValue = path + "private"; - Path privateFolder = Paths.get(paramValue); - if (!Files.exists(privateFolder)){ - paramValue = path + "WEB-INF" + File.separator + "private"; - } - } - } - logger.info("API metadata path: " + paramValue) ; - Stream walk = Files.walk(Paths.get(paramValue + File.separator)); - List result = walk.map(x -> x.toString()).filter(f -> f.endsWith(".grp.json")).collect(Collectors.toList()); - for (String temp : result) - { - try{ - String read = String.join( "", Files.readAllLines(Paths.get(temp))); - JSONObject jo = new JSONObject(read); - String apipath = jo.getString("BasePath"); - appPath.add(apipath.toLowerCase()); - } - catch(IOException e) - { - logger.error("Exception in API Filter: ", e); - } - } - } - else - { - logger.info("API base path invalid."); + String paramValue = headers.get("BasePath"); + if (paramValue != null && !paramValue.isEmpty()) { + Path privateFolder = null; + if (paramValue.equals("*")) { + if (path != null && !path.isEmpty()) { + privateFolder = Paths.get(path, PRIVATE_DIR); + if (!Files.exists(privateFolder)) { + privateFolder = Paths.get(path, WEB_INFO, PRIVATE_DIR); + } + } + } + else { + privateFolder = Paths.get(paramValue); + } + if (privateFolder != null) { + logger.info("API metadata folder: [" + privateFolder.toString() + "]") ; + Stream walk = Files.walk(privateFolder); + List result = walk.map(x -> x.toString()).filter(f -> f.endsWith(".grp.json")).collect(Collectors.toList()); + for (String temp : result) { + try { + String read = String.join("", Files.readAllLines(Paths.get(temp))); + JSONObject jo = new JSONObject(read); + String apiPath = jo.getString("BasePath"); + appPath.add(apiPath.toLowerCase()); + } + catch (IOException e) { + logger.error("Exception API Filter Metadata: ", e); + } + } + } + else { + logger.info("API path invalid"); + } + } + else { + logger.info("API base path is empty."); } } catch (Exception e) { - logger.error("Exception in API Filter: ", e); + logger.error("Exception in API Filter initilization: ", e); } } public void destroy() { } + }