Skip to content

Commit baeda85

Browse files
author
innokenty
committed
rft: extract JsonWireUtils class from ProxyServlet
1 parent 589a440 commit baeda85

File tree

4 files changed

+67
-53
lines changed

4 files changed

+67
-53
lines changed

proxy/src/main/java/ru/qatools/gridrouter/ConfigRepository.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ public Map<String, Browsers> getQuotaMap() {
7676
return userBrowsers;
7777
}
7878

79-
public Map<String, String> getRoutes() {
80-
return routes;
79+
public String getRoute(String routeId) {
80+
return routes.get(routeId);
8181
}
8282

8383
public Version findVersion(String user, JsonCapabilities caps) {
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package ru.qatools.gridrouter;
2+
3+
import org.apache.http.client.utils.URIBuilder;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
7+
import javax.servlet.http.HttpServletRequest;
8+
import java.io.UnsupportedEncodingException;
9+
import java.net.URISyntaxException;
10+
import java.net.URLDecoder;
11+
12+
import static java.nio.charset.StandardCharsets.UTF_8;
13+
import static org.springframework.http.HttpMethod.DELETE;
14+
15+
/**
16+
* @author Alexander Andyashin [email protected]
17+
* @author Innokenty Shuvalov [email protected]
18+
* @author Dmitry Baev [email protected]
19+
* @author Artem Eroshenko [email protected]
20+
*/
21+
public final class JsonWireUtils {
22+
23+
private static final Logger LOGGER = LoggerFactory.getLogger(JsonWireUtils.class);
24+
25+
public static final String WD_HUB_SESSION = "/wd/hub/session/";
26+
27+
public static final int SESSION_HASH_LENGTH = 32;
28+
29+
private JsonWireUtils() {
30+
}
31+
32+
public static boolean isUriValid(String uri) {
33+
return uri.length() > getUriPrefixLength();
34+
}
35+
36+
public static boolean isSessionDeleteRequest(HttpServletRequest request, String command) {
37+
return DELETE.name().equalsIgnoreCase(request.getMethod()) && !command.contains("/");
38+
}
39+
40+
public static String getSessionHash(String uri) {
41+
return uri.substring(WD_HUB_SESSION.length(), getUriPrefixLength());
42+
}
43+
44+
public static int getUriPrefixLength() {
45+
return WD_HUB_SESSION.length() + SESSION_HASH_LENGTH;
46+
}
47+
48+
public static String redirectionUrl(String host, String command) throws URISyntaxException {
49+
return new URIBuilder(host).setPath(WD_HUB_SESSION + command).build().toString();
50+
}
51+
52+
public static String getCommand(String uri) {
53+
String encodedCommand = uri.substring(getUriPrefixLength());
54+
try {
55+
return URLDecoder.decode(encodedCommand, UTF_8.name());
56+
} catch (UnsupportedEncodingException e) {
57+
LOGGER.error("[UNABLE_TO_DECODE_COMMAND] - could not decode command: {}", encodedCommand, e);
58+
return encodedCommand;
59+
}
60+
}
61+
}

proxy/src/main/java/ru/qatools/gridrouter/ProxyServlet.java

Lines changed: 3 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package ru.qatools.gridrouter;
22

33
import org.apache.commons.io.IOUtils;
4-
import org.apache.http.client.utils.URIBuilder;
54
import org.eclipse.jetty.client.api.Request;
65
import org.eclipse.jetty.client.util.StringContentProvider;
76
import org.slf4j.Logger;
@@ -18,13 +17,10 @@
1817
import javax.servlet.http.HttpServletRequest;
1918
import javax.servlet.http.HttpServletResponse;
2019
import java.io.IOException;
21-
import java.io.UnsupportedEncodingException;
22-
import java.net.URISyntaxException;
23-
import java.net.URLDecoder;
2420

2521
import static java.nio.charset.StandardCharsets.UTF_8;
26-
import static org.springframework.http.HttpMethod.DELETE;
2722
import static org.springframework.web.context.support.SpringBeanAutowiringSupport.processInjectionBasedOnServletContext;
23+
import static ru.qatools.gridrouter.JsonWireUtils.*;
2824
import static ru.qatools.gridrouter.RequestUtils.getRemoteHost;
2925

3026
/**
@@ -34,7 +30,7 @@
3430
* @author Artem Eroshenko [email protected]
3531
*/
3632
@WebServlet(
37-
urlPatterns = {ProxyServlet.WD_HUB_SESSION + "*"},
33+
urlPatterns = {WD_HUB_SESSION + "*"},
3834
asyncSupported = true,
3935
initParams = {
4036
@WebInitParam(name = "timeout", value = "300000"),
@@ -45,10 +41,6 @@ public class ProxyServlet extends org.eclipse.jetty.proxy.ProxyServlet {
4541

4642
private static final Logger LOGGER = LoggerFactory.getLogger(ProxyServlet.class);
4743

48-
public static final String WD_HUB_SESSION = "/wd/hub/session/";
49-
50-
public static final int SESSION_HASH_LENGTH = 32;
51-
5244
@Autowired
5345
private ConfigRepository config;
5446

@@ -85,7 +77,7 @@ protected String rewriteTarget(HttpServletRequest request) {
8577
return null;
8678
}
8779

88-
String route = getRoute(uri);
80+
String route = config.getRoute(getSessionHash(uri));
8981
String command = getCommand(uri);
9082

9183
if (route == null) {
@@ -132,39 +124,4 @@ private String removeSessionIdSafe(String content, String remoteHost) {
132124
}
133125
return content;
134126
}
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-
}
170127
}

proxy/src/test/java/ru/qatools/gridrouter/CommandDecodingTest.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,6 @@ public static Collection<Object[]> getData() {
4141

4242
@Test
4343
public void testOutput() throws Exception {
44-
ProxyServlet proxyServlet = new ProxyServlet();
45-
String command = proxyServlet.getCommand(requestUri);
46-
assertThat(command, endsWith(elementId));
44+
assertThat(JsonWireUtils.getCommand(requestUri), endsWith(elementId));
4745
}
48-
49-
5046
}

0 commit comments

Comments
 (0)