api_dev/src/main/java/com/google/appengine/tools/development/testing/FakeHttpServletRequest.java [68:338]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
public class FakeHttpServletRequest implements HttpServletRequest {
  private static final String DEFAULT_HOST = "localhost";
  private static final int DEFAULT_PORT = 443;
  private static final String COOKIE_HEADER = "Cookie";
  private static final String HOST_HEADER = "Host";
  private static final String DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss zzz";
  private final Map<String, Object> attributes = Maps.newConcurrentMap();
  private final Map<String, String> headers = Maps.newHashMap();
  private final ListMultimap<String, String> parameters = LinkedListMultimap.create();
  private final Map<String, Cookie> cookies = new LinkedHashMap<>();
  private String hostName = "localhost";
  private int port = 443;
  private String contextPath = "";
  private String servletPath = "";
  private String pathInfo;
  private String method;
  protected String contentType;

  // used by POST methods
  protected byte[] bodyData = new byte[0];
  protected String characterEncoding;

  // the following two booleans ensure that either getReader() or
  // getInputStream is called, but not both, to conform to specs for the
  // HttpServletRequest class.
  protected boolean getReaderCalled = false;
  protected boolean getInputStreamCalled = false;

  static final String METHOD_POST = "POST";
  static final String METHOD_PUT = "PUT";

  public FakeHttpServletRequest() {
    this(DEFAULT_HOST, DEFAULT_PORT);
  }

  public FakeHttpServletRequest(String hostName, int port) {
    constructor(hostName, port, "", "", null);
  }

  public FakeHttpServletRequest(String urlStr) {
    URL url;
    try {
      url = new URL(urlStr);
    } catch (MalformedURLException e) {
      throw new RuntimeException(e);
    }
    String contextPath;
    String servletPath;
    String path = url.getPath();
    if (path.length() <= 1) {
      // path must be either empty string or "/"
      contextPath = path;
      servletPath = null;
    } else {
      // Look for the second slash which separates the servlet path from the
      // context path. e.g. "/foo/bar"
      int secondSlash = path.indexOf("/", 1);
      if (secondSlash < 0) {
        // No second slash
        contextPath = path;
        servletPath = null;
      } else {
        contextPath = path.substring(0, secondSlash);
        servletPath = path.substring(secondSlash);
      }
    }
    int port = url.getPort();
    // Call constructor() instead of this() because the later is only allowed
    // at the beginning of a constructor
    constructor(url.getHost(), port, contextPath, servletPath, url.getQuery());
  }

  /**
   * This method serves as the central constructor of this class. The reason it is not an actual
   * constructor is that Java doesn't allow calling another constructor at the end of a constructor.
   * e.g.
   *
   * <pre>
   *
   * public FakeHttpServletRequest(String foo) {
   *   // Do something here
   *   this(foo, bar);  // calling another constructor here is not allowed
   * }
   *
   * </pre>
   */
  protected void constructor(
      String host, int port, String contextPath, String servletPath, String queryString) {
    setHeader(HOST_HEADER, host);
    setPort(port);
    setContextPath(contextPath);
    setSerletPath(servletPath);
    setParametersFromQueryString(queryString);
  }

  @Override
  public Object getAttribute(String name) {
    return attributes.get(name);
  }

  @Override
  public Enumeration<String> getAttributeNames() {
    return Collections.enumeration(attributes.keySet());
  }

  @Override
  public String getCharacterEncoding() {
    return UTF_8.name();
  }

  @Override
  public int getContentLength() {
    return -1;
  }

  @Override
  public long getContentLengthLong() {
    return -1;
  }

  @Override
  public String getContentType() {
    return contentType;
  }

  /**
   * Get the body of the request (i.e. the body data) as a binary stream. As per Java docs, this OR
   * getReader() may be called, but not both (attempting that will result in an
   * IllegalStateException)
   */
  @Override
  public ServletInputStream getInputStream() {
    if (getReaderCalled) {
      throw new IllegalStateException("getInputStream() called after getReader()");
    }
    getInputStreamCalled = true; // so that getReader() can no longer be called

    final InputStream in = new ByteArrayInputStream(bodyData);
    return new ServletInputStream() {
      @Override
      public int read() throws IOException {
        return in.read();
      }

      @Override
      public int read(byte[] b, int off, int len) throws IOException {
        return in.read(b, off, len);
      }

      @Override
      public void close() throws IOException {
        in.close();
      }

      @Override
      public boolean isFinished() {
        return true;
      }

      @Override
      public boolean isReady() {
        return true;
      }

      @Override
      public void setReadListener(ReadListener readListener) {
        throw new UnsupportedOperationException();
      }
    };
  }

  @Override
  public String getLocalAddr() {
    return "1.2.3.4";
  }

  @Override
  public String getLocalName() {
    return "localhost";
  }

  @Override
  public int getLocalPort() {
    return port;
  }

  @Override
  public ServletContext getServletContext() {
    return null;
  }

  @Override
  public AsyncContext startAsync() {
    throw new UnsupportedOperationException();
  }

  @Override
  public AsyncContext startAsync(ServletRequest servletRequest, ServletResponse servletResponse) {
    throw new UnsupportedOperationException();
  }

  @Override
  public boolean isAsyncStarted() {
    throw new UnsupportedOperationException();
  }

  @Override
  public boolean isAsyncSupported() {
    throw new UnsupportedOperationException();
  }

  @Override
  public AsyncContext getAsyncContext() {
    throw new UnsupportedOperationException();
  }

  @Override
  public DispatcherType getDispatcherType() {
    throw new UnsupportedOperationException();
  }

  @Override
  public Locale getLocale() {
    return Locale.US;
  }

  @Override
  public Enumeration<Locale> getLocales() {
    return Collections.enumeration(Collections.singleton(Locale.US));
  }

  @Override
  public String getParameter(String name) {
    return Iterables.getFirst(parameters.get(name), null);
  }

  private static final Function<Collection<String>, String[]> STRING_COLLECTION_TO_ARRAY =
      new Function<Collection<String>, String[]>() {
        @Override
        public String[] apply(Collection<String> values) {
          return values.toArray(new String[0]);
        }
      };

  @Override
  public Map<String, String[]> getParameterMap() {
    return Collections.unmodifiableMap(
        Maps.transformValues(parameters.asMap(), STRING_COLLECTION_TO_ARRAY));
  }

  @Override
  public Enumeration<String> getParameterNames() {
    return Collections.enumeration(parameters.keySet());
  }

  @Override
  public String[] getParameterValues(String name) {
    return STRING_COLLECTION_TO_ARRAY.apply(parameters.get(name));
  }

  @Override
  public String getProtocol() {
    return "HTTP/1.1";
  }

  @Override
  public BufferedReader getReader() {
    throw new UnsupportedOperationException();
  }

  @Override
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



api_dev/src/main/java/com/google/appengine/tools/development/testing/ee10/FakeHttpServletRequest.java [69:339]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
public class FakeHttpServletRequest implements HttpServletRequest {
  private static final String DEFAULT_HOST = "localhost";
  private static final int DEFAULT_PORT = 443;
  private static final String COOKIE_HEADER = "Cookie";
  private static final String HOST_HEADER = "Host";
  private static final String DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss zzz";
  private final Map<String, Object> attributes = Maps.newConcurrentMap();
  private final Map<String, String> headers = Maps.newHashMap();
  private final ListMultimap<String, String> parameters = LinkedListMultimap.create();
  private final Map<String, Cookie> cookies = new LinkedHashMap<>();
  private String hostName = "localhost";
  private int port = 443;
  private String contextPath = "";
  private String servletPath = "";
  private String pathInfo;
  private String method;
  protected String contentType;

  // used by POST methods
  protected byte[] bodyData = new byte[0];
  protected String characterEncoding;

  // the following two booleans ensure that either getReader() or
  // getInputStream is called, but not both, to conform to specs for the
  // HttpServletRequest class.
  protected boolean getReaderCalled = false;
  protected boolean getInputStreamCalled = false;

  static final String METHOD_POST = "POST";
  static final String METHOD_PUT = "PUT";

  public FakeHttpServletRequest() {
    this(DEFAULT_HOST, DEFAULT_PORT);
  }

  public FakeHttpServletRequest(String hostName, int port) {
    constructor(hostName, port, "", "", null);
  }

  public FakeHttpServletRequest(String urlStr) {
    URL url;
    try {
      url = new URL(urlStr);
    } catch (MalformedURLException e) {
      throw new RuntimeException(e);
    }
    String contextPath;
    String servletPath;
    String path = url.getPath();
    if (path.length() <= 1) {
      // path must be either empty string or "/"
      contextPath = path;
      servletPath = null;
    } else {
      // Look for the second slash which separates the servlet path from the
      // context path. e.g. "/foo/bar"
      int secondSlash = path.indexOf("/", 1);
      if (secondSlash < 0) {
        // No second slash
        contextPath = path;
        servletPath = null;
      } else {
        contextPath = path.substring(0, secondSlash);
        servletPath = path.substring(secondSlash);
      }
    }
    int port = url.getPort();
    // Call constructor() instead of this() because the later is only allowed
    // at the beginning of a constructor
    constructor(url.getHost(), port, contextPath, servletPath, url.getQuery());
  }

  /**
   * This method serves as the central constructor of this class. The reason it is not an actual
   * constructor is that Java doesn't allow calling another constructor at the end of a constructor.
   * e.g.
   *
   * <pre>
   *
   * public FakeHttpServletRequest(String foo) {
   *   // Do something here
   *   this(foo, bar);  // calling another constructor here is not allowed
   * }
   *
   * </pre>
   */
  protected void constructor(
      String host, int port, String contextPath, String servletPath, String queryString) {
    setHeader(HOST_HEADER, host);
    setPort(port);
    setContextPath(contextPath);
    setSerletPath(servletPath);
    setParametersFromQueryString(queryString);
  }

  @Override
  public Object getAttribute(String name) {
    return attributes.get(name);
  }

  @Override
  public Enumeration<String> getAttributeNames() {
    return Collections.enumeration(attributes.keySet());
  }

  @Override
  public String getCharacterEncoding() {
    return UTF_8.name();
  }

  @Override
  public int getContentLength() {
    return -1;
  }

  @Override
  public long getContentLengthLong() {
    return -1;
  }

  @Override
  public String getContentType() {
    return contentType;
  }

  /**
   * Get the body of the request (i.e. the body data) as a binary stream. As per Java docs, this OR
   * getReader() may be called, but not both (attempting that will result in an
   * IllegalStateException)
   */
  @Override
  public ServletInputStream getInputStream() {
    if (getReaderCalled) {
      throw new IllegalStateException("getInputStream() called after getReader()");
    }
    getInputStreamCalled = true; // so that getReader() can no longer be called

    final InputStream in = new ByteArrayInputStream(bodyData);
    return new ServletInputStream() {
      @Override
      public int read() throws IOException {
        return in.read();
      }

      @Override
      public int read(byte[] b, int off, int len) throws IOException {
        return in.read(b, off, len);
      }

      @Override
      public void close() throws IOException {
        in.close();
      }

      @Override
      public boolean isFinished() {
        return true;
      }

      @Override
      public boolean isReady() {
        return true;
      }

      @Override
      public void setReadListener(ReadListener readListener) {
        throw new UnsupportedOperationException();
      }
    };
  }

  @Override
  public String getLocalAddr() {
    return "1.2.3.4";
  }

  @Override
  public String getLocalName() {
    return "localhost";
  }

  @Override
  public int getLocalPort() {
    return port;
  }

  @Override
  public ServletContext getServletContext() {
    return null;
  }

  @Override
  public AsyncContext startAsync() {
    throw new UnsupportedOperationException();
  }

  @Override
  public AsyncContext startAsync(ServletRequest servletRequest, ServletResponse servletResponse) {
    throw new UnsupportedOperationException();
  }

  @Override
  public boolean isAsyncStarted() {
    throw new UnsupportedOperationException();
  }

  @Override
  public boolean isAsyncSupported() {
    throw new UnsupportedOperationException();
  }

  @Override
  public AsyncContext getAsyncContext() {
    throw new UnsupportedOperationException();
  }

  @Override
  public DispatcherType getDispatcherType() {
    throw new UnsupportedOperationException();
  }

  @Override
  public Locale getLocale() {
    return Locale.US;
  }

  @Override
  public Enumeration<Locale> getLocales() {
    return Collections.enumeration(Collections.singleton(Locale.US));
  }

  @Override
  public String getParameter(String name) {
    return Iterables.getFirst(parameters.get(name), null);
  }

  private static final Function<Collection<String>, String[]> STRING_COLLECTION_TO_ARRAY =
      new Function<Collection<String>, String[]>() {
        @Override
        public String[] apply(Collection<String> values) {
          return values.toArray(new String[0]);
        }
      };

  @Override
  public Map<String, String[]> getParameterMap() {
    return Collections.unmodifiableMap(
        Maps.transformValues(parameters.asMap(), STRING_COLLECTION_TO_ARRAY));
  }

  @Override
  public Enumeration<String> getParameterNames() {
    return Collections.enumeration(parameters.keySet());
  }

  @Override
  public String[] getParameterValues(String name) {
    return STRING_COLLECTION_TO_ARRAY.apply(parameters.get(name));
  }

  @Override
  public String getProtocol() {
    return "HTTP/1.1";
  }

  @Override
  public BufferedReader getReader() {
    throw new UnsupportedOperationException();
  }

  @Override
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



