コード例 #1
1
ファイル: WebAppManager.java プロジェクト: kasunbg/jaggery
  private static void defineProperties(Context cx, JaggeryContext context, ScriptableObject scope) {

    JavaScriptProperty request = new JavaScriptProperty("request");
    HttpServletRequest servletRequest = (HttpServletRequest) context.getProperty(SERVLET_REQUEST);
    request.setValue(cx.newObject(scope, "Request", new Object[] {servletRequest}));
    request.setAttribute(ScriptableObject.READONLY);
    RhinoEngine.defineProperty(scope, request);

    JavaScriptProperty response = new JavaScriptProperty("response");
    HttpServletResponse servletResponse =
        (HttpServletResponse) context.getProperty(SERVLET_RESPONSE);
    response.setValue(cx.newObject(scope, "Response", new Object[] {servletResponse}));
    response.setAttribute(ScriptableObject.READONLY);
    RhinoEngine.defineProperty(scope, response);

    JavaScriptProperty session = new JavaScriptProperty("session");
    session.setValue(cx.newObject(scope, "Session", new Object[] {servletRequest.getSession()}));
    session.setAttribute(ScriptableObject.READONLY);
    RhinoEngine.defineProperty(scope, session);

    JavaScriptProperty application = new JavaScriptProperty("application");
    ServletContext servletConext = (ServletContext) context.getProperty(Constants.SERVLET_CONTEXT);
    application.setValue(cx.newObject(scope, "Application", new Object[] {servletConext}));
    application.setAttribute(ScriptableObject.READONLY);
    RhinoEngine.defineProperty(scope, application);

    if (isWebSocket(servletRequest)) {
      JavaScriptProperty websocket = new JavaScriptProperty("webSocket");
      websocket.setValue(cx.newObject(scope, "WebSocket", new Object[0]));
      websocket.setAttribute(ScriptableObject.READONLY);
      RhinoEngine.defineProperty(scope, websocket);
    }
  }
コード例 #2
0
ファイル: WebAppManager.java プロジェクト: kasunbg/jaggery
  public static JaggeryContext clonedJaggeryContext(ServletContext context) {
    JaggeryContext shared = sharedJaggeryContext(context);
    RhinoEngine engine = shared.getEngine();
    Scriptable sharedScope = shared.getScope();
    Context cx = Context.getCurrentContext();
    ScriptableObject instanceScope = (ScriptableObject) cx.newObject(sharedScope);
    instanceScope.setPrototype(sharedScope);
    instanceScope.setParentScope(null);

    JaggeryContext clone = new JaggeryContext();
    clone.setEngine(engine);
    clone.setTenantId(shared.getTenantId());
    clone.setScope(instanceScope);

    clone.addProperty(Constants.SERVLET_CONTEXT, shared.getProperty(Constants.SERVLET_CONTEXT));
    clone.addProperty(LogHostObject.LOG_LEVEL, shared.getProperty(LogHostObject.LOG_LEVEL));
    clone.addProperty(
        FileHostObject.JAVASCRIPT_FILE_MANAGER,
        shared.getProperty(FileHostObject.JAVASCRIPT_FILE_MANAGER));
    clone.addProperty(
        Constants.JAGGERY_CORE_MANAGER, shared.getProperty(Constants.JAGGERY_CORE_MANAGER));
    clone.addProperty(Constants.JAGGERY_INCLUDED_SCRIPTS, new HashMap<String, Boolean>());
    clone.addProperty(Constants.JAGGERY_INCLUDES_CALLSTACK, new Stack<String>());
    clone.addProperty(Constants.JAGGERY_REQUIRED_MODULES, new HashMap<String, ScriptableObject>());

    CommonManager.setJaggeryContext(clone);

    return clone;
  }
コード例 #3
0
  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()});
  }
コード例 #4
0
ファイル: WebAppManager.java プロジェクト: kasunbg/jaggery
  public static ScriptableObject require(
      Context cx, Scriptable thisObj, Object[] args, Function funObj)
      throws ScriptException, IOException {
    String functionName = "require";
    int argsCount = args.length;
    if (argsCount != 1) {
      HostObjectUtil.invalidNumberOfArgs(
          CommonManager.HOST_OBJECT_NAME, functionName, argsCount, false);
    }
    if (!(args[0] instanceof String)) {
      HostObjectUtil.invalidArgsError(
          CommonManager.HOST_OBJECT_NAME, functionName, "1", "string", args[0], false);
    }

    String moduleId = (String) args[0];
    int dotIndex = moduleId.lastIndexOf(".");
    if (moduleId.length() == dotIndex + 1) {
      String msg = "Invalid file path for require method : " + moduleId;
      log.error(msg);
      throw new ScriptException(msg);
    }

    JaggeryContext jaggeryContext = CommonManager.getJaggeryContext();
    Map<String, ScriptableObject> requiredModules =
        (Map<String, ScriptableObject>)
            jaggeryContext.getProperty(Constants.JAGGERY_REQUIRED_MODULES);
    ScriptableObject object = requiredModules.get(moduleId);
    if (object != null) {
      return object;
    }

    if (dotIndex == -1) {
      object = CommonManager.require(cx, thisObj, args, funObj);
      initModule(cx, jaggeryContext, moduleId, object);
    } else {
      object = (ScriptableObject) cx.newObject(thisObj);
      object.setPrototype(thisObj);
      object.setParentScope(null);
      String ext = moduleId.substring(dotIndex + 1);
      if (ext.equalsIgnoreCase("json")) {
        object = executeScript(jaggeryContext, object, moduleId, true, true, false);
      } else if (ext.equalsIgnoreCase("js")) {
        object = executeScript(jaggeryContext, object, moduleId, false, true, false);
      } else if (ext.equalsIgnoreCase("jag")) {
        object = executeScript(jaggeryContext, object, moduleId, false, false, false);
      } else {
        String msg = "Unsupported file type for require() method : ." + ext;
        log.error(msg);
        throw new ScriptException(msg);
      }
    }
    requiredModules.put(moduleId, object);
    return object;
  }
コード例 #5
0
ファイル: WebAppManager.java プロジェクト: kasunbg/jaggery
  public static void deploy(org.apache.catalina.Context context) throws ScriptException {
    ServletContext ctx = context.getServletContext();
    JaggeryContext sharedContext = new JaggeryContext();
    Context cx = Context.getCurrentContext();
    CommonManager.initContext(sharedContext);

    sharedContext.addProperty(Constants.SERVLET_CONTEXT, ctx);
    sharedContext.addProperty(FileHostObject.JAVASCRIPT_FILE_MANAGER, new WebAppFileManager(ctx));
    sharedContext.addProperty(
        Constants.JAGGERY_REQUIRED_MODULES, new HashMap<String, ScriptableObject>());
    String logLevel = (String) ctx.getAttribute(LogHostObject.LOG_LEVEL);
    if (logLevel != null) {
      sharedContext.addProperty(LogHostObject.LOG_LEVEL, logLevel);
    }
    ScriptableObject sharedScope = sharedContext.getScope();
    JavaScriptProperty application = new JavaScriptProperty("application");
    application.setValue(cx.newObject(sharedScope, "Application", new Object[] {ctx}));
    application.setAttribute(ScriptableObject.READONLY);
    RhinoEngine.defineProperty(sharedScope, application);
    ctx.setAttribute(SHARED_JAGGERY_CONTEXT, sharedContext);
  }
コード例 #6
0
ファイル: JSURI.java プロジェクト: BackupTheBerlios/esxx-svn
  public Scriptable getAuth(Context cx, URI req_uri, String realm, String mechanism) {
    if (uri.getRawUserInfo() != null) {
      String[] unp = uri.getRawUserInfo().split(":", 2);

      // If the URI already carries authorization information, use it
      if (unp.length == 2) {
        try {
          Scriptable res = cx.newObject(this);

          ScriptableObject.putProperty(res, "username", StringUtil.decodeURI(unp[0], false));
          ScriptableObject.putProperty(res, "password", StringUtil.decodeURI(unp[1], false));

          return res;
        } catch (URISyntaxException ignored) {
        }
      }
    }

    // Else, search the 'auth' property for matching entries
    return getBestProperty(cx, "auth", req_uri, realm, mechanism);
  }
コード例 #7
0
  private void handleAcceptHeader(String hdr, String value, Context cx, Scriptable accept) {
    String subname;

    if (hdr.equals("Accept")) {
      subname = "media";
    } else if (hdr.startsWith("Accept-")) {
      subname = hdr.substring(7).toLowerCase();
    } else {
      // Do nothing
      return;
    }

    Map<Double, List<Scriptable>> objects = new TreeMap<Double, List<Scriptable>>();

    String[] values = value.split(",");

    for (String v : values) {
      double q = 1.0;
      double w = 0.0;
      String[] parts = v.split(";");

      Scriptable object = cx.newObject(accept);
      object.put("valueOf", object, acceptValueOf);
      object.put("value", object, parts[0].trim());

      // Add all attributes
      for (int i = 1; i < parts.length; ++i) {
        String[] attr = parts[i].split("=", 2);

        if (attr.length == 2) {
          // Parse Q factor
          if (attr[0].trim().equals("q")) {
            q = Double.parseDouble(attr[1].trim());
          } else {
            object.put(attr[0].trim(), object, attr[1].trim());
          }
        }
      }

      object.put("q", object, "" + q);

      // Calculate implicit weight
      if (parts[0].trim().equals("*/*")) {
        w = 0.0000;
      } else if (parts[0].trim().endsWith("/*")) {
        w = 0.0001;
      } else {
        w = 0.0002;
      }

      // Attributes give extra points
      w += parts.length * 0.00001;

      // Add to tree multi-map, inverse order
      double key = -(q + w);

      List<Scriptable> l = objects.get(key);

      if (l == null) {
        l = new ArrayList<Scriptable>();
        objects.put(key, l);
      }

      l.add(object);
    }

    Scriptable object = cx.newArray(accept, objects.size());
    accept.put(subname, accept, object);

    int i = 0;
    for (List<Scriptable> l : objects.values()) {
      for (Scriptable s : l) {
        object.put(i++, object, s);
      }
    }
  }
コード例 #8
0
ファイル: JSURI.java プロジェクト: BackupTheBerlios/esxx-svn
 public static JSURI newJSURI(Context cx, Application app, URI uri) {
   return (JSURI) cx.newObject(app.getJSGlobal(), "URI", new Object[] {uri});
 }