org.openqa.grid.web.servlet.DriverServlet Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of selenium-server Show documentation
Show all versions of selenium-server Show documentation
Selenium automates browsers. That's it! What you do with that power is entirely up to you.
// 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;
import com.google.common.base.Throwables;
import com.google.common.io.ByteStreams;
import org.openqa.grid.internal.ExternalSessionKey;
import org.openqa.grid.internal.GridRegistry;
import org.openqa.grid.web.servlet.handler.RequestHandler;
import org.openqa.grid.web.servlet.handler.SeleniumBasedRequest;
import org.openqa.grid.web.servlet.handler.WebDriverRequest;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.remote.ErrorCodes;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* entry point for all communication request sent by the clients to the remotes managed by the grid.
*
* Runs on the socketListener threads of the servlet container
*/
public class DriverServlet extends RegistryBasedServlet {
private static final long serialVersionUID = -1693540182205547227L;
@SuppressWarnings("UnusedDeclaration")
public DriverServlet() {
this(null);
}
public DriverServlet(GridRegistry registry) {
super(registry);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
process(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException {
process(request, response);
}
@Override
protected void doDelete(HttpServletRequest request, HttpServletResponse response)
throws IOException {
process(request, response);
}
protected void process(HttpServletRequest request, HttpServletResponse response)
throws IOException {
RequestHandler req = null;
SeleniumBasedRequest r = null;
try {
r = SeleniumBasedRequest.createFromRequest(request, getRegistry());
req = new RequestHandler(r, response, getRegistry());
req.process();
} catch (Throwable e) {
if (r instanceof WebDriverRequest && !response.isCommitted()) {
// Format the exception for both the JSON wire protocol and the W3C spec compliant version.
response.reset();
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.setStatus(500);
Map resp = new TreeMap<>();
// https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol#error-handling
if (req != null) {
final ExternalSessionKey serverSession = req.getServerSession();
resp.put("sessionId", serverSession != null ? serverSession.getKey() : null);
}
resp.put("status", ErrorCodes.UNHANDLED_ERROR);
Map value = new TreeMap<>();
value.put("message", e.getMessage());
value.put("class", e.getClass().getCanonicalName());
List