Skip to content

Commit d552c47

Browse files
committed
server: Implementing factories for SeleniumBasedRequest's. It is a step toward decoupling RC from the server.
1 parent ee27db1 commit d552c47

File tree

4 files changed

+102
-26
lines changed

4 files changed

+102
-26
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.openqa.grid.web.servlet.handler;
19+
20+
import org.openqa.grid.internal.Registry;
21+
22+
import javax.servlet.http.HttpServletRequest;
23+
24+
public class LegacySeleniumRequestFactory implements SeleniumBasedRequestFactory {
25+
public SeleniumBasedRequest createFromRequest(HttpServletRequest request, Registry registry) {
26+
if (! "/selenium-server/driver".equals(request.getServletPath())) {
27+
return null;
28+
}
29+
return new LegacySeleniumRequest(request, registry);
30+
}
31+
}

java/server/src/org/openqa/grid/web/servlet/handler/SeleniumBasedRequest.java

+14-26
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
package org.openqa.grid.web.servlet.handler;
2020

2121
import com.google.common.annotations.VisibleForTesting;
22+
import com.google.common.collect.ImmutableList;
2223
import com.google.common.io.ByteStreams;
2324

2425
import org.openqa.grid.common.SeleniumProtocol;
26+
import org.openqa.grid.common.exception.GridException;
2527
import org.openqa.grid.internal.ExternalSessionKey;
2628
import org.openqa.grid.internal.Registry;
2729
import org.openqa.grid.internal.TestSession;
@@ -38,6 +40,7 @@
3840
import java.nio.charset.CharsetDecoder;
3941
import java.text.SimpleDateFormat;
4042
import java.util.Date;
43+
import java.util.List;
4144
import java.util.Map;
4245

4346
import javax.servlet.ServletInputStream;
@@ -60,27 +63,20 @@ public abstract class SeleniumBasedRequest extends HttpServletRequestWrapper {
6063
private final Map<String, Object> desiredCapability;
6164
private final long timestamp = System.currentTimeMillis();
6265

66+
private static List<SeleniumBasedRequestFactory> requestFactories =
67+
new ImmutableList.Builder<SeleniumBasedRequestFactory>()
68+
.add(new LegacySeleniumRequestFactory())
69+
.add(new WebDriverRequestFactory())
70+
.build();
6371

6472
public static SeleniumBasedRequest createFromRequest(HttpServletRequest request, Registry registry) {
65-
if (SeleniumBasedRequest.getRequestProtocol(request) == SeleniumProtocol.Selenium) {
66-
return new LegacySeleniumRequest(request, registry);
67-
} else {
68-
return new WebDriverRequest(request, registry);
69-
}
70-
}
71-
72-
73-
/**
74-
* check the request and finds out if that's a selenium legacy protocol( RC ) or a WebDriver one.
75-
* @param request
76-
* @return Either SeleniumProtocol.Selenium or SeleniumProtocol.WebDriver.
77-
*/
78-
public static SeleniumProtocol getRequestProtocol(HttpServletRequest request) {
79-
if ("/selenium-server/driver".equals(request.getServletPath())) {
80-
return SeleniumProtocol.Selenium;
81-
} else {
82-
return SeleniumProtocol.WebDriver;
73+
for (SeleniumBasedRequestFactory factory : requestFactories) {
74+
SeleniumBasedRequest sbr = factory.createFromRequest(request, registry);
75+
if (sbr != null) {
76+
return sbr;
77+
}
8378
}
79+
throw new GridException("Request path " + request.getServletPath() + " is not recognized");
8480
}
8581

8682
@VisibleForTesting
@@ -135,13 +131,10 @@ public Registry getRegistry() {
135131
*/
136132
public abstract Map<String, Object> extractDesiredCapability();
137133

138-
139134
// TODO freynaud remove the TestSession parameter.The listener can modify the
140135
// original request instead.
141136
public abstract String getNewSessionRequestedCapability(TestSession session);
142137

143-
144-
145138
public RequestType getRequestType() {
146139
return type;
147140
}
@@ -163,7 +156,6 @@ public int getContentLength() {
163156
}else {
164157
return body.length;
165158
}
166-
167159
}
168160

169161
public String getBody() {
@@ -191,7 +183,6 @@ public long getCreationTime(){
191183
return timestamp;
192184
}
193185

194-
195186
public String toString() {
196187
SimpleDateFormat format = new SimpleDateFormat("d MMM yyyy HH:mm:ss");
197188
StringBuilder builder = new StringBuilder();
@@ -231,7 +222,4 @@ public synchronized void reset() throws IOException {
231222
throw new RuntimeException("not implemented");
232223
}
233224
}
234-
235-
236-
237225
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.openqa.grid.web.servlet.handler;
19+
20+
import org.openqa.grid.internal.Registry;
21+
import javax.servlet.http.HttpServletRequest;
22+
23+
public interface SeleniumBasedRequestFactory {
24+
SeleniumBasedRequest createFromRequest(HttpServletRequest request, Registry registry);
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.openqa.grid.web.servlet.handler;
19+
20+
import org.openqa.grid.internal.Registry;
21+
22+
import javax.servlet.http.HttpServletRequest;
23+
24+
public class WebDriverRequestFactory implements SeleniumBasedRequestFactory {
25+
public SeleniumBasedRequest createFromRequest(HttpServletRequest request, Registry registry) {
26+
String path = request.getServletPath();
27+
if (! ("/grid/driver".equals(path) || "/wd/hub".equals(path))) {
28+
return null;
29+
}
30+
return new WebDriverRequest(request, registry);
31+
}
32+
}

0 commit comments

Comments
 (0)