Skip to content

Commit

Permalink
server: Implementing factories for SeleniumBasedRequest's. It is a st…
Browse files Browse the repository at this point in the history
…ep toward decoupling RC from the server.
  • Loading branch information
barancev committed Jun 5, 2015
1 parent ee27db1 commit d552c47
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.grid.web.servlet.handler;

import org.openqa.grid.internal.Registry;

import javax.servlet.http.HttpServletRequest;

public class LegacySeleniumRequestFactory implements SeleniumBasedRequestFactory {
public SeleniumBasedRequest createFromRequest(HttpServletRequest request, Registry registry) {
if (! "/selenium-server/driver".equals(request.getServletPath())) {
return null;
}
return new LegacySeleniumRequest(request, registry);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
package org.openqa.grid.web.servlet.handler;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.common.io.ByteStreams;

import org.openqa.grid.common.SeleniumProtocol;
import org.openqa.grid.common.exception.GridException;
import org.openqa.grid.internal.ExternalSessionKey;
import org.openqa.grid.internal.Registry;
import org.openqa.grid.internal.TestSession;
Expand All @@ -38,6 +40,7 @@
import java.nio.charset.CharsetDecoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

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

private static List<SeleniumBasedRequestFactory> requestFactories =
new ImmutableList.Builder<SeleniumBasedRequestFactory>()
.add(new LegacySeleniumRequestFactory())
.add(new WebDriverRequestFactory())
.build();

public static SeleniumBasedRequest createFromRequest(HttpServletRequest request, Registry registry) {
if (SeleniumBasedRequest.getRequestProtocol(request) == SeleniumProtocol.Selenium) {
return new LegacySeleniumRequest(request, registry);
} else {
return new WebDriverRequest(request, registry);
}
}


/**
* check the request and finds out if that's a selenium legacy protocol( RC ) or a WebDriver one.
* @param request
* @return Either SeleniumProtocol.Selenium or SeleniumProtocol.WebDriver.
*/
public static SeleniumProtocol getRequestProtocol(HttpServletRequest request) {
if ("/selenium-server/driver".equals(request.getServletPath())) {
return SeleniumProtocol.Selenium;
} else {
return SeleniumProtocol.WebDriver;
for (SeleniumBasedRequestFactory factory : requestFactories) {
SeleniumBasedRequest sbr = factory.createFromRequest(request, registry);
if (sbr != null) {
return sbr;
}
}
throw new GridException("Request path " + request.getServletPath() + " is not recognized");
}

@VisibleForTesting
Expand Down Expand Up @@ -135,13 +131,10 @@ public Registry getRegistry() {
*/
public abstract Map<String, Object> extractDesiredCapability();


// TODO freynaud remove the TestSession parameter.The listener can modify the
// original request instead.
public abstract String getNewSessionRequestedCapability(TestSession session);



public RequestType getRequestType() {
return type;
}
Expand All @@ -163,7 +156,6 @@ public int getContentLength() {
}else {
return body.length;
}

}

public String getBody() {
Expand Down Expand Up @@ -191,7 +183,6 @@ public long getCreationTime(){
return timestamp;
}


public String toString() {
SimpleDateFormat format = new SimpleDateFormat("d MMM yyyy HH:mm:ss");
StringBuilder builder = new StringBuilder();
Expand Down Expand Up @@ -231,7 +222,4 @@ public synchronized void reset() throws IOException {
throw new RuntimeException("not implemented");
}
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.grid.web.servlet.handler;

import org.openqa.grid.internal.Registry;
import javax.servlet.http.HttpServletRequest;

public interface SeleniumBasedRequestFactory {
SeleniumBasedRequest createFromRequest(HttpServletRequest request, Registry registry);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.grid.web.servlet.handler;

import org.openqa.grid.internal.Registry;

import javax.servlet.http.HttpServletRequest;

public class WebDriverRequestFactory implements SeleniumBasedRequestFactory {
public SeleniumBasedRequest createFromRequest(HttpServletRequest request, Registry registry) {
String path = request.getServletPath();
if (! ("/grid/driver".equals(path) || "/wd/hub".equals(path))) {
return null;
}
return new WebDriverRequest(request, registry);
}
}

0 comments on commit d552c47

Please sign in to comment.