|
1 | 1 | package orchi.HHCloud.Api;
|
2 | 2 |
|
3 |
| -import java.io.IOException; |
4 |
| - |
5 |
| -import javax.servlet.AsyncContext; |
6 |
| -import javax.servlet.Filter; |
7 |
| -import javax.servlet.FilterChain; |
8 |
| -import javax.servlet.FilterConfig; |
9 |
| -import javax.servlet.ServletException; |
10 |
| -import javax.servlet.ServletRequest; |
11 |
| -import javax.servlet.ServletResponse; |
12 |
| -import javax.servlet.http.HttpServletRequest; |
13 |
| -import javax.servlet.http.HttpServletResponse; |
14 |
| -import javax.servlet.http.HttpSession; |
15 |
| - |
| 3 | +import orchi.HHCloud.Api.ApiManager.ApiDescriptor; |
| 4 | +import orchi.HHCloud.ParseParamsMultiPart2; |
| 5 | +import orchi.HHCloud.Start; |
16 | 6 | import org.json.JSONObject;
|
17 | 7 | import org.slf4j.Logger;
|
18 | 8 | import org.slf4j.LoggerFactory;
|
19 | 9 |
|
20 |
| -import orchi.HHCloud.ParseParamsMultiPart2; |
21 |
| -import orchi.HHCloud.Start; |
22 |
| -import orchi.HHCloud.Api.ApiManager.ApiDescriptor; |
| 10 | +import javax.servlet.*; |
| 11 | +import javax.servlet.http.HttpServletRequest; |
| 12 | +import javax.servlet.http.HttpServletResponse; |
| 13 | +import javax.servlet.http.HttpSession; |
| 14 | +import java.io.IOException; |
23 | 15 |
|
24 | 16 |
|
25 | 17 | /**
|
26 | 18 | * Representa un problemas a la hora llamar las apis de manera asincrona ya q
|
27 | 19 | * antes de llamarlas hay q hacer ciertas cosas q necesitan ser ejecutadas
|
28 | 20 | * asyncContext.
|
29 |
| - * |
30 |
| - * @deprecated |
| 21 | + * |
31 | 22 | * @see ServiceTaskAPIImpl
|
| 23 | + * @deprecated |
32 | 24 | */
|
33 | 25 | public class ApiFilter implements Filter {
|
34 |
| - private static Logger log = LoggerFactory.getLogger(ApiFilter.class); |
35 |
| - private static String ACCESS_CONTROL_ALLOW_ORIGIN = Start.conf.getString("api.headers.aclo"); |
| 26 | + private static Logger log = LoggerFactory.getLogger(ApiFilter.class); |
| 27 | + private static String ACCESS_CONTROL_ALLOW_ORIGIN = Start.conf.getString("api.headers.aclo"); |
36 | 28 |
|
37 |
| - @Override |
38 |
| - public void init(FilterConfig filterConfig) throws ServletException { |
| 29 | + @Override |
| 30 | + public void init(FilterConfig filterConfig) throws ServletException { |
39 | 31 |
|
40 |
| - } |
| 32 | + } |
41 | 33 |
|
42 |
| - @Override |
43 |
| - public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) |
44 |
| - throws IOException, ServletException { |
45 |
| - HttpServletRequest r = (HttpServletRequest) request; |
46 |
| - HttpServletResponse rs = (HttpServletResponse) response; |
47 |
| - /* |
| 34 | + @Override |
| 35 | + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) |
| 36 | + throws IOException, ServletException { |
| 37 | + HttpServletRequest r = (HttpServletRequest) request; |
| 38 | + HttpServletResponse rs = (HttpServletResponse) response; |
| 39 | + /* |
48 | 40 | * rs.setHeader("Access-Control-Allow-Origin",
|
49 | 41 | * ACCESS_CONTROL_ALLOW_ORIGIN); rs.setHeader("Content-type",
|
50 | 42 | * "application/json"); rs.setHeader("Access-Control-Allow-Credentials",
|
51 | 43 | * "true");
|
52 | 44 | */
|
53 |
| - // AsyncContext ctx = r.startAsync(); |
54 |
| - HttpSession session = r.getSession(false); |
55 |
| - |
56 |
| - String path = r.getRequestURI().substring(request.getServletContext().getContextPath().length()); |
57 |
| - |
58 |
| - log.debug("PATH {}", path); |
59 |
| - |
60 |
| - ApiDescriptor apid = ApiManager.getApid(path); |
61 |
| - |
62 |
| - if (apid == null) { |
63 |
| - request.setAttribute("status", "error"); |
64 |
| - request.setAttribute("error", "api_no_found"); |
65 |
| - request.setAttribute("errorMsg", "Esta api no esta registrada."); |
66 |
| - request.getRequestDispatcher("/test").forward(request, response); |
67 |
| - ; |
68 |
| - return; |
69 |
| - } |
70 |
| - |
71 |
| - if (log.isDebugEnabled()) { |
72 |
| - log.debug(new JSONObject(apid).toString(2)); |
73 |
| - } |
74 |
| - |
75 |
| - if (apid.isIgnored()) { |
76 |
| - log.debug("{}, ignoring", path); |
77 |
| - apid.calls++; |
78 |
| - if (session == null) { |
79 |
| - if (apid.isGsr()) { |
80 |
| - apid.wrongCalls++; |
81 |
| - request.setAttribute("status", "error"); |
82 |
| - request.setAttribute("error", "session"); |
83 |
| - request.setAttribute("errorMsg", "Debe iniciar session."); |
84 |
| - request.getRequestDispatcher("/test").forward(request, response); |
85 |
| - ; |
86 |
| - return; |
87 |
| - } |
88 |
| - } |
89 |
| - chain.doFilter(request, response); |
90 |
| - } else { |
91 |
| - |
92 |
| - ParseParamsMultiPart2 params = null; |
93 |
| - try { |
94 |
| - params = new ParseParamsMultiPart2(r); |
95 |
| - } catch (Exception e) { |
96 |
| - e.printStackTrace(); |
97 |
| - } |
98 |
| - String op = params.getString("op"); |
99 |
| - String args = params.getString("args"); |
100 |
| - if (op == null || op.equalsIgnoreCase("")) { |
101 |
| - apid.wrongCalls++; |
102 |
| - request.setAttribute("status", "error"); |
103 |
| - request.setAttribute("params", params); |
104 |
| - request.setAttribute("error", "op_missing"); |
105 |
| - request.setAttribute("errorMsg", "Debe proporsionar una operacion."); |
106 |
| - request.getRequestDispatcher("/test").forward(request, response); |
107 |
| - ; |
108 |
| - return; |
109 |
| - } |
110 |
| - if (args == null || args.equalsIgnoreCase("")) { |
111 |
| - apid.wrongCalls++; |
112 |
| - request.setAttribute("status", "error"); |
113 |
| - request.setAttribute("params", params); |
114 |
| - request.setAttribute("error", "args_missing"); |
115 |
| - request.setAttribute("errorMsg", "Debe proporsionar argumentos."); |
116 |
| - request.getRequestDispatcher("/test").forward(request, response); |
117 |
| - ; |
118 |
| - return; |
119 |
| - } |
120 |
| - |
121 |
| - apid.calls++; |
122 |
| - if (!apid.hasOperation(op)) { |
123 |
| - apid.wrongCalls++; |
124 |
| - request.setAttribute("status", "error"); |
125 |
| - request.setAttribute("params", params); |
126 |
| - request.setAttribute("error", "op_wrong"); |
127 |
| - request.setAttribute("errorMsg", "Esa operacion no esta registrada."); |
128 |
| - request.getRequestDispatcher("/test").forward(request, response); |
129 |
| - ; |
130 |
| - |
131 |
| - } else { |
132 |
| - |
133 |
| - if (session == null) { |
134 |
| - if (apid.isGsr() || apid.getOperation(op).isSr()) { |
135 |
| - apid.wrongCalls++; |
136 |
| - request.setAttribute("status", "error"); |
137 |
| - request.setAttribute("params", params); |
138 |
| - request.setAttribute("error", "session"); |
139 |
| - request.setAttribute("errorMsg", "Debe iniciar session."); |
140 |
| - request.getRequestDispatcher("/test").forward(request, response); |
141 |
| - ; |
142 |
| - return; |
143 |
| - } |
144 |
| - } |
145 |
| - |
146 |
| - apid.getOperation(op).calls++; |
147 |
| - |
148 |
| - log.debug("{}, op: {}, args {}.", path, op, args); |
149 |
| - |
150 |
| - request.setAttribute("params", params); |
151 |
| - chain.doFilter(request, response); |
152 |
| - } |
153 |
| - |
154 |
| - } |
155 |
| - } |
156 |
| - |
157 |
| - @Override |
158 |
| - public void destroy() { |
159 |
| - |
160 |
| - } |
| 45 | + // AsyncContext ctx = r.startAsync(); |
| 46 | + HttpSession session = r.getSession(false); |
| 47 | + |
| 48 | + String path = r.getRequestURI().substring(request.getServletContext().getContextPath().length()); |
| 49 | + |
| 50 | + log.debug("PATH {}", path); |
| 51 | + |
| 52 | + ApiDescriptor apid = ApiManager.getApid(path); |
| 53 | + |
| 54 | + if (apid == null) { |
| 55 | + request.setAttribute("status", "error"); |
| 56 | + request.setAttribute("error", "api_no_found"); |
| 57 | + request.setAttribute("errorMsg", "Esta api no esta registrada."); |
| 58 | + request.getRequestDispatcher("/test").forward(request, response); |
| 59 | + ; |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + if (log.isDebugEnabled()) { |
| 64 | + log.debug(new JSONObject(apid).toString(2)); |
| 65 | + } |
| 66 | + |
| 67 | + if (apid.isIgnored()) { |
| 68 | + log.debug("{}, ignoring", path); |
| 69 | + apid.calls++; |
| 70 | + if (session == null) { |
| 71 | + if (apid.isGsr()) { |
| 72 | + apid.wrongCalls++; |
| 73 | + request.setAttribute("status", "error"); |
| 74 | + request.setAttribute("error", "session"); |
| 75 | + request.setAttribute("errorMsg", "Debe iniciar session."); |
| 76 | + request.getRequestDispatcher("/test").forward(request, response); |
| 77 | + ; |
| 78 | + return; |
| 79 | + } |
| 80 | + } |
| 81 | + chain.doFilter(request, response); |
| 82 | + } else { |
| 83 | + |
| 84 | + ParseParamsMultiPart2 params = null; |
| 85 | + try { |
| 86 | + params = new ParseParamsMultiPart2(r); |
| 87 | + } catch (Exception e) { |
| 88 | + e.printStackTrace(); |
| 89 | + } |
| 90 | + String op = params.getString("op"); |
| 91 | + String args = params.getString("args"); |
| 92 | + if (op == null || op.equalsIgnoreCase("")) { |
| 93 | + apid.wrongCalls++; |
| 94 | + request.setAttribute("status", "error"); |
| 95 | + request.setAttribute("params", params); |
| 96 | + request.setAttribute("error", "op_missing"); |
| 97 | + request.setAttribute("errorMsg", "Debe proporsionar una operacion."); |
| 98 | + request.getRequestDispatcher("/test").forward(request, response); |
| 99 | + ; |
| 100 | + return; |
| 101 | + } |
| 102 | + if (args == null || args.equalsIgnoreCase("")) { |
| 103 | + apid.wrongCalls++; |
| 104 | + request.setAttribute("status", "error"); |
| 105 | + request.setAttribute("params", params); |
| 106 | + request.setAttribute("error", "args_missing"); |
| 107 | + request.setAttribute("errorMsg", "Debe proporsionar argumentos."); |
| 108 | + request.getRequestDispatcher("/test").forward(request, response); |
| 109 | + ; |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + apid.calls++; |
| 114 | + if (!apid.hasOperation(op)) { |
| 115 | + apid.wrongCalls++; |
| 116 | + request.setAttribute("status", "error"); |
| 117 | + request.setAttribute("params", params); |
| 118 | + request.setAttribute("error", "op_wrong"); |
| 119 | + request.setAttribute("errorMsg", "Esa operacion no esta registrada."); |
| 120 | + request.getRequestDispatcher("/test").forward(request, response); |
| 121 | + ; |
| 122 | + |
| 123 | + } else { |
| 124 | + |
| 125 | + if (session == null) { |
| 126 | + if (apid.isGsr() || apid.getOperation(op).isSr()) { |
| 127 | + apid.wrongCalls++; |
| 128 | + request.setAttribute("status", "error"); |
| 129 | + request.setAttribute("params", params); |
| 130 | + request.setAttribute("error", "session"); |
| 131 | + request.setAttribute("errorMsg", "Debe iniciar session."); |
| 132 | + request.getRequestDispatcher("/test").forward(request, response); |
| 133 | + ; |
| 134 | + return; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + apid.getOperation(op).calls++; |
| 139 | + |
| 140 | + log.debug("{}, op: {}, args {}.", path, op, args); |
| 141 | + |
| 142 | + request.setAttribute("params", params); |
| 143 | + chain.doFilter(request, response); |
| 144 | + } |
| 145 | + |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public void destroy() { |
| 151 | + |
| 152 | + } |
161 | 153 |
|
162 | 154 | }
|
0 commit comments