Example usage for org.openqa.selenium.remote.http HttpRequest getUri

List of usage examples for org.openqa.selenium.remote.http HttpRequest getUri

Introduction

In this page you can find the example usage for org.openqa.selenium.remote.http HttpRequest getUri.

Prototype

public String getUri() 

Source Link

Usage

From source file:org.aludratest.service.gui.web.selenium.selenium2.AludraSeleniumHttpCommandExecutor.java

License:Apache License

@Override
public Response execute(Command command) throws IOException {
    HttpContext context = new BasicHttpContext();

    if (command.getSessionId() == null) {
        if (QUIT.equals(command.getName())) {
            return new Response();
        }/* www . ja  v  a2s .  c o  m*/
        if (!GET_ALL_SESSIONS.equals(command.getName()) && !NEW_SESSION.equals(command.getName())) {
            throw new SessionNotFoundException("Session ID is null. Using WebDriver after calling quit()?");
        }
    }

    HttpRequest request = commandCodec.encode(command);

    String requestUrl = remoteServer.toExternalForm().replaceAll("/$", "") + request.getUri();

    HttpUriRequest httpMethod = createHttpUriRequest(request.getMethod(), requestUrl);
    for (String name : request.getHeaderNames()) {
        // Skip content length as it is implicitly set when the message entity is set below.
        if (!"Content-Length".equalsIgnoreCase(name)) {
            for (String value : request.getHeaders(name)) {
                httpMethod.addHeader(name, value);
            }
        }
    }

    if (httpMethod instanceof HttpPost) {
        ((HttpPost) httpMethod).setEntity(new ByteArrayEntity(request.getContent()));
    }

    if (requestTimeout > 0 && (httpMethod instanceof HttpRequestBase)) {
        RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(15000)
                .setConnectTimeout(15000).setSocketTimeout(requestTimeout).build();
        ((HttpRequestBase) httpMethod).setConfig(requestConfig);
    } else if (httpMethod instanceof HttpRequestBase) {
        // ensure Selenium Standard is set
        RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(60000)
                .setConnectTimeout(60000).setSocketTimeout(THREE_HOURS).build();
        ((HttpRequestBase) httpMethod).setConfig(requestConfig);
    }

    try {
        log(LogType.PROFILER, new HttpProfilerLogEntry(command.getName(), true));
        HttpResponse response = fallBackExecute(context, httpMethod);
        log(LogType.PROFILER, new HttpProfilerLogEntry(command.getName(), false));

        lastResponse = response;
        response = followRedirects(client, context, response, /* redirect count */0);
        lastResponse = response;

        return createResponse(response, context);
    } catch (UnsupportedCommandException e) {
        if (e.getMessage() == null || "".equals(e.getMessage())) {
            throw new UnsupportedOperationException(
                    "No information from server. Command name was: " + command.getName(), e.getCause());
        }
        throw e;
    } catch (SocketTimeoutException e) {
        LoggerFactory.getLogger(AludraSeleniumHttpCommandExecutor.class)
                .warn("Timeout in HTTP Command Executor. Timeout was "
                        + ((HttpRequestBase) httpMethod).getConfig().getSocketTimeout());
        throw e;
    }
}

From source file:org.openqa.grid.web.servlet.DisplayHelpHandler.java

License:Apache License

@Override
public void execute(HttpRequest req, HttpResponse resp) throws IOException {
    String resource = req.getUri();
    if (resource.contains(HELPER_SERVLET_ASSET_PATH_PREFIX)
            && !resource.replace(HELPER_SERVLET_ASSET_PATH_PREFIX, "").contains("/")
            && !resource.replace(HELPER_SERVLET_ASSET_PATH_PREFIX, "").equals("")) {
        // request is for an asset of the help page
        resource = resource.replace(HELPER_SERVLET_ASSET_PATH_PREFIX, "");
        int index = resource.lastIndexOf('.');
        MediaType type = HTML_UTF_8;
        if (index != -1) {
            String extension = resource.substring(index);
            type = TYPES.getOrDefault(extension, HTML_UTF_8);
        }/*from ww  w  .j a v  a2s. c om*/

        resp.setHeader("Content-Type", type.toString());

        try (InputStream in = getResourceInputStream(resource)) {
            if (in == null) {
                resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
                return;
            } else {
                resp.setStatus(HttpServletResponse.SC_OK);
                resp.setContent(ByteStreams.toByteArray(in));
                return;
            }
        }
    } else {
        // request is for an unknown entity. show the help page
        try (InputStream in = getResourceInputStream(HELPER_SERVLET_TEMPLATE)) {
            if (in == null) {
                resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
            } else {
                StringBuilder jsonBuilder = new StringBuilder();
                try (JsonOutput out = json.newOutput(jsonBuilder)) {
                    out.setPrettyPrint(false).write(servletConfig);
                }

                final String json = jsonBuilder.toString();

                final String htmlTemplate;
                try (BufferedReader reader = new BufferedReader(new InputStreamReader(in, UTF_8))) {
                    htmlTemplate = reader.lines().collect(Collectors.joining("\n"));
                }
                final String updatedTemplate = htmlTemplate.replace(HELPER_SERVLET_TEMPLATE_CONFIG_JSON_VAR,
                        json);
                if (resource.equals("/")) {
                    resp.setStatus(HttpServletResponse.SC_OK);
                } else {
                    resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
                }

                resp.setHeader("Content-Type", HTML_UTF_8.toString());
                resp.setContent(updatedTemplate.getBytes(UTF_8));
            }
        }
    }
}