<commit_before>/*
Copyright 2007-2009 WebDriver committers
Copyright 2007-2009 Google Inc.

Licensed 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.selenium.remote;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.URI;
import org.apache.commons.httpclient.methods.DeleteMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;

import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

public class HttpCommandExecutor implements CommandExecutor {

  private final String remotePath;

  private enum HttpVerb {

    GET() {
      public HttpMethod createMethod(String url) {
        GetMethod getMethod = new GetMethod(url);
        getMethod.setFollowRedirects(true);
        return getMethod;
      }
    },
    POST() {
      public HttpMethod createMethod(String url) {
        return new PostMethod(url);
      }
    },
    DELETE() {
      public HttpMethod createMethod(String url) {
        return new DeleteMethod(url);
      }
    };

    public abstract HttpMethod createMethod(String url);
  }

  private Map<String, CommandInfo> nameToUrl = new HashMap<String, CommandInfo>();
  private HttpClient client;

  public HttpCommandExecutor(URL addressOfRemoteServer) throws Exception {
    if (addressOfRemoteServer == null) {
      String remoteServer = System.getProperty("webdriver.remote.server");
      addressOfRemoteServer = remoteServer == null ? null : new URL(remoteServer);

      if (addressOfRemoteServer == null)
        throw new IllegalArgumentException("You must specify a remote address to connect to");
    }

    this.remotePath = addressOfRemoteServer.getPath();

    URI uri = new URI(addressOfRemoteServer.toString(), false);
    client = new HttpClient();
    client.getHostConfiguration().setHost(uri);

    nameToUrl.put("newSession", new CommandInfo("/session", HttpVerb.POST));
    nameToUrl.put("quit", new CommandInfo("/session/:sessionId", HttpVerb.DELETE));
    nameToUrl.put("getCurrentWindowHandle", new CommandInfo("/session/:sessionId/:context/window_handle", HttpVerb.GET));
    nameToUrl.put("getWindowHandles", new CommandInfo("/session/:sessionId/:context/window_handles", HttpVerb.GET));
    nameToUrl.put("get", new CommandInfo("/session/:sessionId/:context/url", HttpVerb.POST));
    nameToUrl
        .put("forward", new CommandInfo("/session/:sessionId/:context/forward", HttpVerb.POST));
    nameToUrl.put("back", new CommandInfo("/session/:sessionId/:context/back", HttpVerb.POST));
    nameToUrl.put("refresh", new CommandInfo("/session/:sessionId/:context/refresh", HttpVerb.POST));

    nameToUrl.put("executeScript", new CommandInfo("/session/:sessionId/:context/execute", HttpVerb.POST));

    nameToUrl.put("currentUrl", new CommandInfo("/session/:sessionId/:context/url", HttpVerb.GET));
    nameToUrl.put("getTitle", new CommandInfo("/session/:sessionId/:context/title", HttpVerb.GET));
    nameToUrl
        .put("pageSource", new CommandInfo("/session/:sessionId/:context/source", HttpVerb.GET));
    nameToUrl
        .put("setVisible", new CommandInfo("/session/:sessionId/:context/visible", HttpVerb.POST));
    nameToUrl
        .put("getVisible", new CommandInfo("/session/:sessionId/:context/visible", HttpVerb.GET));
    nameToUrl
        .put("findElement", new CommandInfo("/session/:sessionId/:context/element", HttpVerb.POST));
    nameToUrl.put("findElements",
                  new CommandInfo("/session/:sessionId/:context/elements", HttpVerb.POST));
    nameToUrl.put("getChildrenOfType", new CommandInfo(
        "/session/:sessionId/:context/element/:id/children/:name", HttpVerb.POST));
    nameToUrl.put("getActiveElement", new CommandInfo("/session/:sessionId/:context/element/active", HttpVerb.POST));

    nameToUrl
        .put("findElementUsingElement", new CommandInfo("/session/:sessionId/:context/element/:id/element/:using", HttpVerb.POST));
    nameToUrl.put("findElementsUsingElement",
                  new CommandInfo("/session/:sessionId/:context/element/:id/elements/:using", HttpVerb.POST));
    nameToUrl.put("clickElement",
                  new CommandInfo("/session/:sessionId/:context/element/:id/click", HttpVerb.POST));
    nameToUrl.put("clearElement",
                  new CommandInfo("/session/:sessionId/:context/element/:id/clear", HttpVerb.POST));
    nameToUrl.put("submitElement", new CommandInfo(
        "/session/:sessionId/:context/element/:id/submit", HttpVerb.POST));
    nameToUrl.put("getElementText",
                  new CommandInfo("/session/:sessionId/:context/element/:id/text", HttpVerb.GET));
    nameToUrl.put("sendKeys",
                  new CommandInfo("/session/:sessionId/:context/element/:id/value", HttpVerb.POST));
    nameToUrl.put("getElementValue",
                  new CommandInfo("/session/:sessionId/:context/element/:id/value", HttpVerb.GET));
    nameToUrl.put("getElementName",
                  new CommandInfo("/session/:sessionId/:context/element/:id/name", HttpVerb.GET));
    nameToUrl.put("isElementSelected", new CommandInfo(
        "/session/:sessionId/:context/element/:id/selected", HttpVerb.GET));
    nameToUrl.put("setElementSelected", new CommandInfo(
        "/session/:sessionId/:context/element/:id/selected", HttpVerb.POST));
    nameToUrl.put("toggleElement", new CommandInfo(
        "/session/:sessionId/:context/element/:id/toggle", HttpVerb.POST));
    nameToUrl.put("isElementEnabled", new CommandInfo(
        "/session/:sessionId/:context/element/:id/enabled", HttpVerb.GET));
    nameToUrl.put("isElementDisplayed", new CommandInfo(
        "/session/:sessionId/:context/element/:id/displayed", HttpVerb.GET));
    nameToUrl.put("getElementLocation", new CommandInfo(<commit_msg>SimonStewart: Enable "getTagName" to work on the remote driver.<commit_after>/*
Copyright 2007-2009 WebDriver committers
Copyright 2007-2009 Google Inc.

Licensed 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.selenium.remote;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.URI;
import org.apache.commons.httpclient.methods.DeleteMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;

import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

public class HttpCommandExecutor implements CommandExecutor {

  private final String remotePath;

  private enum HttpVerb {

    GET() {
      public HttpMethod createMethod(String url) {
        GetMethod getMethod = new GetMethod(url);
        getMethod.setFollowRedirects(true);
        return getMethod;
      }
    },
    POST() {
      public HttpMethod createMethod(String url) {
        return new PostMethod(url);
      }
    },
    DELETE() {
      public HttpMethod createMethod(String url) {
        return new DeleteMethod(url);
      }
    };

    public abstract HttpMethod createMethod(String url);
  }

  private Map<String, CommandInfo> nameToUrl = new HashMap<String, CommandInfo>();
  private HttpClient client;

  public HttpCommandExecutor(URL addressOfRemoteServer) throws Exception {
    if (addressOfRemoteServer == null) {
      String remoteServer = System.getProperty("webdriver.remote.server");
      addressOfRemoteServer = remoteServer == null ? null : new URL(remoteServer);

      if (addressOfRemoteServer == null)
        throw new IllegalArgumentException("You must specify a remote address to connect to");
    }

    this.remotePath = addressOfRemoteServer.getPath();

    URI uri = new URI(addressOfRemoteServer.toString(), false);
    client = new HttpClient();
    client.getHostConfiguration().setHost(uri);

    nameToUrl.put("newSession", new CommandInfo("/session", HttpVerb.POST));
    nameToUrl.put("quit", new CommandInfo("/session/:sessionId", HttpVerb.DELETE));
    nameToUrl.put("getCurrentWindowHandle", new CommandInfo("/session/:sessionId/:context/window_handle", HttpVerb.GET));
    nameToUrl.put("getWindowHandles", new CommandInfo("/session/:sessionId/:context/window_handles", HttpVerb.GET));
    nameToUrl.put("get", new CommandInfo("/session/:sessionId/:context/url", HttpVerb.POST));
    nameToUrl
        .put("forward", new CommandInfo("/session/:sessionId/:context/forward", HttpVerb.POST));
    nameToUrl.put("back", new CommandInfo("/session/:sessionId/:context/back", HttpVerb.POST));
    nameToUrl.put("refresh", new CommandInfo("/session/:sessionId/:context/refresh", HttpVerb.POST));

    nameToUrl.put("executeScript", new CommandInfo("/session/:sessionId/:context/execute", HttpVerb.POST));

    nameToUrl.put("currentUrl", new CommandInfo("/session/:sessionId/:context/url", HttpVerb.GET));
    nameToUrl.put("getTitle", new CommandInfo("/session/:sessionId/:context/title", HttpVerb.GET));
    nameToUrl
        .put("pageSource", new CommandInfo("/session/:sessionId/:context/source", HttpVerb.GET));
    nameToUrl
        .put("setVisible", new CommandInfo("/session/:sessionId/:context/visible", HttpVerb.POST));
    nameToUrl
        .put("getVisible", new CommandInfo("/session/:sessionId/:context/visible", HttpVerb.GET));
    nameToUrl
        .put("findElement", new CommandInfo("/session/:sessionId/:context/element", HttpVerb.POST));
    nameToUrl.put("findElements",
                  new CommandInfo("/session/:sessionId/:context/elements", HttpVerb.POST));
    nameToUrl.put("getChildrenOfType", new CommandInfo(
        "/session/:sessionId/:context/element/:id/children/:name", HttpVerb.POST));
    nameToUrl.put("getActiveElement", new CommandInfo("/session/:sessionId/:context/element/active", HttpVerb.POST));

    nameToUrl
        .put("findElementUsingElement", new CommandInfo("/session/:sessionId/:context/element/:id/element/:using", HttpVerb.POST));
    nameToUrl.put("findElementsUsingElement",
                  new CommandInfo("/session/:sessionId/:context/element/:id/elements/:using", HttpVerb.POST));
    nameToUrl.put("clickElement",
                  new CommandInfo("/session/:sessionId/:context/element/:id/click", HttpVerb.POST));
    nameToUrl.put("clearElement",
                  new CommandInfo("/session/:sessionId/:context/element/:id/clear", HttpVerb.POST));
    nameToUrl.put("submitElement", new CommandInfo(
        "/session/:sessionId/:context/element/:id/submit", HttpVerb.POST));
    nameToUrl.put("getElementText",
                  new CommandInfo("/session/:sessionId/:context/element/:id/text", HttpVerb.GET));
    nameToUrl.put("sendKeys",
                  new CommandInfo("/session/:sessionId/:context/element/:id/value", HttpVerb.POST));
    nameToUrl.put("getElementValue",
                  new CommandInfo("/session/:sessionId/:context/element/:id/value", HttpVerb.GET));
    nameToUrl.put("getTagName",
                  new CommandInfo("/session/:sessionId/:context/element/:id/name", HttpVerb.GET));
    nameToUrl.put("isElementSelected", new CommandInfo(
        "/session/:sessionId/:context/element/:id/selected", HttpVerb.GET));
    nameToUrl.put("setElementSelected", new CommandInfo(
        "/session/:sessionId/:context/element/:id/selected", HttpVerb.POST));
    nameToUrl.put("toggleElement", new CommandInfo(
        "/session/:sessionId/:context/element/:id/toggle", HttpVerb.POST));
    nameToUrl.put("isElementEnabled", new CommandInfo(
        "/session/:sessionId/:context/element/:id/enabled", HttpVerb.GET));
    nameToUrl.put("isElementDisplayed", new CommandInfo(
        "/session/:sessionId/:context/element/:id/displayed", HttpVerb.GET));
    nameToUrl.put("getElementLocation", new CommandInfo(
{'fraction_non_alphanumeric': 0.13448857537187547, 'fraction_numerical': 0.0030670142616163166, 'mean_word_length': 4.1799046862589355, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6442204', 'n_tokens_mistral': 3636, 'n_tokens_neox': 3709, 'n_words': 719}