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