public JSRequest(Request request, Context cx, Scriptable scope) {
    this();

    ESXX esxx = ESXX.getInstance();

    this.request = request;

    requestURI = (JSURI) cx.newObject(scope, "URI", new Object[] {request.getRequestURI()});
    scriptURI = (JSURI) cx.newObject(scope, "URI", new Object[] {request.getScriptURI()});

    env = cx.newObject(scope);
    headers = cx.newObject(scope);
    cookies = cx.newObject(scope);
    accept = cx.newObject(scope);
    query = cx.newObject(scope);
    args = null;
    params = cx.newObject(scope);

    acceptValueOf = new FunctionObject("valueOf", acceptValueOfMethod, accept);

    for (String name : request.getProperties().stringPropertyNames()) {
      String value = request.getProperties().getProperty(name).trim();

      // Add environtment variable to esxx.env
      ScriptableObject.putProperty(env, name, value);

      // If this is an HTTP header, get the original name back
      String hdr = esxx.cgiToHTTP(name);

      if (hdr != null) {
        // Add real HTTP header to this.headers
        addHeader(hdr, value);

        // Decode cookies
        handleCookieHeader(hdr, value);

        // Decode Accept* HTTP headers
        handleAcceptHeader(hdr, value, cx, accept);

        // Decode Content-* HTTP headers
        handleContentHeader(hdr, value);

        // Handle SOAPAction
        if (hdr.equals("SOAPAction")) {
          soapAction = value;
        }
      }

      if (name.equals("QUERY_STRING")) {
        try {
          StringUtil.decodeFormVariables(value, query);
        } catch (UnsupportedEncodingException ex) {
          throw new ESXXException("Unable to parse request entity: " + ex.getMessage(), ex);
        }
      }
    }

    logger = JSESXX.newObject(cx, scope, "Logger", new Object[] {request, request.getScriptName()});
  }
Exemple #2
0
  public static Object jsConstructor(
      Context cx, java.lang.Object[] args, Function ctorObj, boolean inNewExpr)
      throws URISyntaxException {
    JSURI prop_src_uri = null;
    URI uri = null;
    String uri_string = null;
    String uri_relative = null;
    Scriptable params = null;

    if (args.length < 1 || args[0] == Context.getUndefinedValue()) {
      throw Context.reportRuntimeError("Missing argument");
    }

    // First argument is always the URI
    if (args.length >= 1 && args[0] != Context.getUndefinedValue()) {
      if (args[0] instanceof JSURI) {
        prop_src_uri = (JSURI) args[0];
        uri = prop_src_uri.uri;
      } else if (args[0] instanceof URL) {
        uri = ((URL) args[0]).toURI();
      } else if (args[0] instanceof URI) {
        uri = (URI) args[0];
      } else {
        uri_string = Context.toString(args[0]);
      }
    }

    // Third argument can only by params
    if (args.length >= 3 && args[2] != Context.getUndefinedValue()) {
      params = (Scriptable) args[2];
    }

    // Second argument can be relative URI or params
    if (args.length >= 2 && args[1] != Context.getUndefinedValue()) {
      if (args[1] instanceof Scriptable) {
        if (params != null) {
          throw Context.reportRuntimeError("Expected a String as second argument.");
        }

        params = (Scriptable) args[1];
      } else {
        // args[1] should be resolved against args[0]
        uri_relative = Context.toString(args[1]);
      }
    }

    if (params != null) {
      // Replace {...} patterns in string arguments if params was supplied
      final Scriptable final_params = params;

      StringUtil.ParamResolver resolver =
          new StringUtil.ParamResolver() {
            public String resolveParam(String param) {
              Object obj;

              try {
                obj = final_params.get(Integer.parseInt(param), final_params);
              } catch (NumberFormatException ex) {
                obj = final_params.get(param, final_params);
              }

              try {
                String value = Context.toString(obj);
                return StringUtil.encodeURI(value, false /* == encodeURIComponent() */);
              } catch (URISyntaxException ex) {
                throw new WrappedException(ex);
              }
            }
          };

      uri_string = StringUtil.format(uri_string, resolver);
      uri_relative = StringUtil.format(uri_relative, resolver);
    }

    if (uri_string != null) {
      // Resolve URI against current location, if possible
      JSESXX js_esxx = JSGlobal.getJSESXX(cx, ctorObj);

      if (js_esxx != null) {
        prop_src_uri = js_esxx.jsGet_wd();

        if (prop_src_uri != null) {
          uri = resolveURI(prop_src_uri.uri, uri_string);
        }
      }

      if (uri == null) {
        // Fall back to non-relative
        uri = new URI(uri_string);
      }
    }

    if (uri_relative != null) {
      // Resolve relative part against first URI argument
      uri = resolveURI(uri, uri_relative);
    }

    JSURI rc = new JSURI(uri);

    if (prop_src_uri != null) {
      // Copy local properties from previous JSURI object
      for (Object o : prop_src_uri.getIds()) {
        if (o instanceof String) {
          String key = (String) o;

          rc.put(key, rc, prop_src_uri.get(key, prop_src_uri));
        } else {
          int key = (Integer) o;

          rc.put(key, rc, prop_src_uri.get(key, prop_src_uri));
        }
      }
    }

    return rc;
  }