Ejemplo n.º 1
1
  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);
    }
  }
Ejemplo n.º 2
0
  private static void defineProperties(Context cx, JaggeryContext context, ScriptableObject scope) {
    WebAppContext ctx = (WebAppContext) context;

    JavaScriptProperty request = new JavaScriptProperty("request");
    request.setValue(cx.newObject(scope, "Request", new Object[] {ctx.getServletRequest()}));
    request.setAttribute(ScriptableObject.READONLY);
    RhinoEngine.defineProperty(scope, request);

    JavaScriptProperty response = new JavaScriptProperty("response");
    response.setValue(cx.newObject(scope, "Response", new Object[] {ctx.getServletResponse()}));
    response.setAttribute(ScriptableObject.READONLY);
    RhinoEngine.defineProperty(scope, response);

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

    JavaScriptProperty application = new JavaScriptProperty("application");
    application.setValue(cx.newObject(scope, "Application", new Object[] {ctx.getServletConext()}));
    application.setAttribute(ScriptableObject.READONLY);
    RhinoEngine.defineProperty(scope, application);

    if (isWebSocket(ctx.getServletRequest())) {
      JavaScriptProperty websocket = new JavaScriptProperty("websocket");
      websocket.setValue(cx.newObject(scope, "WebSocket", new Object[0]));
      websocket.setAttribute(ScriptableObject.READONLY);
      RhinoEngine.defineProperty(scope, websocket);
    }
  }
Ejemplo n.º 3
0
  @Override
  public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
    ServletContext ctx = httpSessionEvent.getSession().getServletContext();

    List<Object> jsListeners =
        (List<Object>) ctx.getAttribute(JaggeryCoreConstants.JS_DESTROYED_LISTENERS);
    JaggeryContext clonedContext = WebAppManager.clonedJaggeryContext(ctx);

    RhinoEngine engine = clonedContext.getEngine();
    Context cx = engine.enterContext();

    ScriptableObject clonedScope = clonedContext.getScope();

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

    if (jsListeners != null) {
      for (Object jsListener : jsListeners) {
        CommonManager.getCallstack(clonedContext).push((String) jsListener);
        try {
          ScriptReader sr =
              new ScriptReader(ctx.getResourceAsStream((String) jsListener)) {
                @Override
                protected void build() throws IOException {
                  try {
                    sourceReader = new StringReader(HostObjectUtil.streamToString(sourceIn));
                  } catch (ScriptException e) {
                    throw new IOException(e);
                  }
                }
              };
          engine.exec(sr, clonedScope, null);
        } catch (ScriptException e) {
          log.error(e.getMessage(), e);
        }
      }
    }
  }
Ejemplo n.º 4
0
  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);
  }