1
1
package ru .qatools .gridrouter ;
2
2
3
3
import org .apache .commons .io .IOUtils ;
4
- import org .apache .http .client .utils .URIBuilder ;
5
4
import org .eclipse .jetty .client .api .Request ;
6
5
import org .eclipse .jetty .client .util .StringContentProvider ;
7
6
import org .slf4j .Logger ;
8
7
import org .slf4j .LoggerFactory ;
9
8
import org .springframework .beans .factory .annotation .Autowired ;
10
- import ru .qatools .gridrouter .json .GridStats ;
11
9
import ru .qatools .gridrouter .json .JsonMessage ;
12
10
import ru .qatools .gridrouter .json .JsonMessageFactory ;
11
+ import ru .qatools .gridrouter .sessions .StatsCounter ;
13
12
14
13
import javax .servlet .ServletConfig ;
15
14
import javax .servlet .ServletException ;
18
17
import javax .servlet .http .HttpServletRequest ;
19
18
import javax .servlet .http .HttpServletResponse ;
20
19
import java .io .IOException ;
21
- import java .io .UnsupportedEncodingException ;
22
- import java .net .URISyntaxException ;
23
- import java .net .URLDecoder ;
24
20
25
21
import static java .nio .charset .StandardCharsets .UTF_8 ;
26
- import static org .springframework .http .HttpMethod .DELETE ;
27
22
import static org .springframework .web .context .support .SpringBeanAutowiringSupport .processInjectionBasedOnServletContext ;
23
+ import static ru .qatools .gridrouter .JsonWireUtils .*;
28
24
import static ru .qatools .gridrouter .RequestUtils .getRemoteHost ;
29
25
30
26
/**
34
30
* @author Artem Eroshenko eroshenkoam@yandex-team.ru
35
31
*/
36
32
@ WebServlet (
37
- urlPatterns = {ProxyServlet . WD_HUB_SESSION + "*" },
33
+ urlPatterns = {WD_HUB_SESSION + "*" },
38
34
asyncSupported = true ,
39
35
initParams = {
40
36
@ WebInitParam (name = "timeout" , value = "300000" ),
@@ -45,15 +41,11 @@ public class ProxyServlet extends org.eclipse.jetty.proxy.ProxyServlet {
45
41
46
42
private static final Logger LOGGER = LoggerFactory .getLogger (ProxyServlet .class );
47
43
48
- public static final String WD_HUB_SESSION = "/wd/hub/session/" ;
49
-
50
- public static final int SESSION_HASH_LENGTH = 32 ;
51
-
52
44
@ Autowired
53
- private ConfigRepository config ;
45
+ private transient ConfigRepository config ;
54
46
55
47
@ Autowired
56
- private GridStats stats ;
48
+ private transient StatsCounter statsCounter ;
57
49
58
50
@ Override
59
51
public void init (ServletConfig config ) throws ServletException {
@@ -85,7 +77,7 @@ protected String rewriteTarget(HttpServletRequest request) {
85
77
return null ;
86
78
}
87
79
88
- String route = getRoute (uri );
80
+ String route = config . getRoute (getSessionHash ( uri ) );
89
81
String command = getCommand (uri );
90
82
91
83
if (route == null ) {
@@ -95,7 +87,9 @@ protected String rewriteTarget(HttpServletRequest request) {
95
87
96
88
if (isSessionDeleteRequest (request , command )) {
97
89
LOGGER .info ("[SESSION_DELETED] [{}] [{}] [{}]" , remoteHost , route , command );
98
- stats .stopSession ();
90
+ statsCounter .deleteSession (getFullSessionId (uri ), route );
91
+ } else {
92
+ statsCounter .updateSession (getFullSessionId (uri ), route );
99
93
}
100
94
101
95
try {
@@ -132,39 +126,4 @@ private String removeSessionIdSafe(String content, String remoteHost) {
132
126
}
133
127
return content ;
134
128
}
135
-
136
- protected String redirectionUrl (String host , String command ) throws URISyntaxException {
137
- return new URIBuilder (host ).setPath (WD_HUB_SESSION + command ).build ().toString ();
138
- }
139
-
140
- protected String getRoute (String uri ) {
141
- return config .getRoutes ().get (getSessionHash (uri ));
142
- }
143
-
144
- protected String getCommand (String uri ) {
145
- String encodedCommand = uri .substring (getUriPrefixLength ());
146
- try {
147
- return URLDecoder .decode (encodedCommand , UTF_8 .name ());
148
- } catch (UnsupportedEncodingException e ) {
149
- LOGGER .error ("[UNABLE_TO_DECODE_COMMAND] - could not decode command: {}" , encodedCommand , e );
150
- return encodedCommand ;
151
- }
152
- }
153
-
154
- protected boolean isUriValid (String uri ) {
155
- return uri .length () > getUriPrefixLength ();
156
- }
157
-
158
- protected boolean isSessionDeleteRequest (HttpServletRequest request , String command ) {
159
- return DELETE .name ().equalsIgnoreCase (request .getMethod ())
160
- && !command .contains ("/" );
161
- }
162
-
163
- protected String getSessionHash (String uri ) {
164
- return uri .substring (WD_HUB_SESSION .length (), getUriPrefixLength ());
165
- }
166
-
167
- protected int getUriPrefixLength () {
168
- return WD_HUB_SESSION .length () + SESSION_HASH_LENGTH ;
169
- }
170
129
}
0 commit comments