|
19 | 19 | public class APIObjectFilter extends Filter { |
20 | 20 |
|
21 | 21 | private ArrayList<String> appPath = new ArrayList<String>(); |
22 | | - |
| 22 | + static final String PRIVATE_DIR="private"; |
| 23 | + static final String WEB_INFO="WEB-INF"; |
23 | 24 | public static final Logger logger = LogManager.getLogger(APIObjectFilter.class); |
24 | 25 |
|
25 | | - public void doFilter(IServletRequest request, IServletResponse response, IFilterChain chain) throws Exception { |
26 | | - if (request.isHttpServletRequest() && response.isHttpServletResponse()) { |
27 | | - IHttpServletRequest httpRequest = request.getHttpServletRequest(); |
| 26 | + public void doFilter(IServletRequest request, IServletResponse response, IFilterChain chain) throws Exception { |
| 27 | + if (request.isHttpServletRequest() && response.isHttpServletResponse()) { |
| 28 | + IHttpServletRequest httpRequest = request.getHttpServletRequest(); |
28 | 29 | String path = httpRequest.getRequestURI().substring(httpRequest.getContextPath().length()).substring(1); |
29 | 30 | String urlString = path.toLowerCase(); |
30 | 31 | boolean isPath = false; |
31 | | - for(String appBasePath : appPath) |
32 | | - { |
33 | | - if (urlString.startsWith(appBasePath)) |
34 | | - { |
| 32 | + for (String appBasePath : this.appPath) { |
| 33 | + if (urlString.startsWith(appBasePath)) { |
35 | 34 | isPath = true; |
36 | 35 | break; |
37 | 36 | } |
38 | 37 | } |
39 | | - if(isPath) |
40 | | - { |
| 38 | + if(isPath) { |
41 | 39 | String fwdURI = "/rest/" + path; |
| 40 | + logger.info("Forwarding from " + path +" to: " + fwdURI) ; |
42 | 41 | httpRequest.getRequestDispatcher(fwdURI).forward(request,response); |
43 | 42 | } |
44 | | - else |
45 | | - { |
| 43 | + else { |
46 | 44 | chain.doFilter(request, response); |
47 | 45 | } |
48 | 46 | } |
49 | | - else |
50 | | - { |
| 47 | + else { |
51 | 48 | chain.doFilter(request, response); |
52 | 49 | } |
53 | 50 | } |
54 | 51 |
|
55 | 52 | public void init(Map<String, String> headers, String path, String sessionCookieName) throws ServletException { |
56 | 53 | try { |
57 | | - String paramValue = headers.get("BasePath"); |
58 | | - if (paramValue != null && !paramValue.isEmpty()) |
59 | | - { |
60 | | - if (paramValue.equals("*")) |
61 | | - { |
62 | | - if (path != null && !path.isEmpty()) |
63 | | - { |
64 | | - paramValue = path + "private"; |
65 | | - Path privateFolder = Paths.get(paramValue); |
66 | | - if (!Files.exists(privateFolder)){ |
67 | | - paramValue = path + "WEB-INF" + File.separator + "private"; |
68 | | - } |
69 | | - } |
70 | | - } |
71 | | - logger.info("API metadata path: " + paramValue) ; |
72 | | - Stream<Path> walk = Files.walk(Paths.get(paramValue + File.separator)); |
73 | | - List<String> result = walk.map(x -> x.toString()).filter(f -> f.endsWith(".grp.json")).collect(Collectors.toList()); |
74 | | - for (String temp : result) |
75 | | - { |
76 | | - try{ |
77 | | - String read = String.join( "", Files.readAllLines(Paths.get(temp))); |
78 | | - JSONObject jo = new JSONObject(read); |
79 | | - String apipath = jo.getString("BasePath"); |
80 | | - appPath.add(apipath.toLowerCase()); |
81 | | - } |
82 | | - catch(IOException e) |
83 | | - { |
84 | | - logger.error("Exception in API Filter: ", e); |
85 | | - } |
86 | | - } |
87 | | - } |
88 | | - else |
89 | | - { |
90 | | - logger.info("API base path invalid."); |
| 54 | + String paramValue = headers.get("BasePath"); |
| 55 | + if (paramValue != null && !paramValue.isEmpty()) { |
| 56 | + Path privateFolder = null; |
| 57 | + if (paramValue.equals("*")) { |
| 58 | + if (path != null && !path.isEmpty()) { |
| 59 | + privateFolder = Paths.get(path, PRIVATE_DIR); |
| 60 | + if (!Files.exists(privateFolder)) { |
| 61 | + privateFolder = Paths.get(path, WEB_INFO, PRIVATE_DIR); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + else { |
| 66 | + privateFolder = Paths.get(paramValue); |
| 67 | + } |
| 68 | + if (privateFolder != null) { |
| 69 | + logger.info("API metadata folder: [" + privateFolder.toString() + "]") ; |
| 70 | + Stream<Path> walk = Files.walk(privateFolder); |
| 71 | + List<String> result = walk.map(x -> x.toString()).filter(f -> f.endsWith(".grp.json")).collect(Collectors.toList()); |
| 72 | + for (String temp : result) { |
| 73 | + try { |
| 74 | + String read = String.join("", Files.readAllLines(Paths.get(temp))); |
| 75 | + JSONObject jo = new JSONObject(read); |
| 76 | + String apiPath = jo.getString("BasePath"); |
| 77 | + appPath.add(apiPath.toLowerCase()); |
| 78 | + } |
| 79 | + catch (IOException e) { |
| 80 | + logger.error("Exception API Filter Metadata: ", e); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + else { |
| 85 | + logger.info("API path invalid"); |
| 86 | + } |
| 87 | + } |
| 88 | + else { |
| 89 | + logger.info("API base path is empty."); |
91 | 90 | } |
92 | 91 | } |
93 | 92 | catch (Exception e) { |
94 | | - logger.error("Exception in API Filter: ", e); |
| 93 | + logger.error("Exception in API Filter initilization: ", e); |
95 | 94 | } |
96 | 95 | } |
97 | 96 |
|
98 | 97 | public void destroy() { |
99 | 98 | } |
| 99 | + |
100 | 100 | } |
0 commit comments