Example #1
0
  private Object parseMessage() {
    // Consume SOAP message, if any
    // TODO: Add a SOAP handler in Parser.java
    if (soapAction != null) {
      try {
        MimeHeaders mime_headers = new MimeHeaders();

        for (Object k : headers.getIds()) {
          if (k instanceof String) {
            String name = (String) k;
            String value = Context.toString(ScriptableObject.getProperty(headers, name));

            mime_headers.addHeader(name, value);
          }
        }

        return MessageFactory.newInstance(SOAPConstants.DYNAMIC_SOAP_PROTOCOL)
            .createMessage(mime_headers, request.getInputStream());
      } catch (IOException ex) {
        throw new ESXXException("Unable to read SOAP message stream: " + ex.getMessage());
      } catch (SOAPException ex) {
        throw new ESXXException(400 /* Bad Request */, "Invalid SOAP message: " + ex.getMessage());
      } finally {
        try {
          request.getInputStream().close();
        } catch (Exception ignored) {
        }
      }
    } else if (contentType != null && contentLength > 0) {
      try {
        ESXX esxx = ESXX.getInstance();
        return esxx.parseStream(
            contentType,
            request.getInputStream(),
            URI.create("urn:x-esxx:incoming-request-entity"),
            null,
            new java.io.PrintWriter(request.getErrorWriter()),
            Context.getCurrentContext(),
            this);
      } catch (Exception ex) {
        throw new ESXXException(
            400 /* Bad Request */, "Unable to parse request entity: " + ex.getMessage(), ex);
      } finally {
        try {
          request.getInputStream().close();
        } catch (Exception ignored) {
        }
      }
    } else {
      // Return a dummy object
      return Context.getCurrentContext().newObject(this);
    }
  }
Example #2
0
  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;
  }
Example #3
0
  /**
   * Utility method which dynamically binds a Context to the current thread, if none already exists.
   */
  public static Object callMethod(
      ContextFactory factory,
      final Scriptable thisObj,
      final Function f,
      final Object[] args,
      final long argsToWrap) {
    if (f == null) {
      // See comments in getFunction
      return null;
    }
    if (factory == null) {
      factory = ContextFactory.getGlobal();
    }

    final Scriptable scope = f.getParentScope();
    if (argsToWrap == 0) {
      return Context.call(factory, f, scope, thisObj, args);
    }

    Context cx = Context.getCurrentContext();
    if (cx != null) {
      return doCall(cx, scope, thisObj, f, args, argsToWrap);
    } else {
      return factory.call(
          new ContextAction() {
            public Object run(Context cx) {
              return doCall(cx, scope, thisObj, f, args, argsToWrap);
            }
          });
    }
  }
Example #4
0
 private XML parse(String frag) {
   try {
     return newXML(
         XmlNode.createElement(
             options, getDefaultNamespaceURI(Context.getCurrentContext()), frag));
   } catch (SAXException e) {
     throw ScriptRuntime.typeError("Cannot parse XML: " + e.getMessage());
   }
 }
Example #5
0
 protected void addToScope(Scriptable scope) {
   Object value = exportingBundle.lookup(name);
   StringTokenizer tokenizer = new StringTokenizer(name, "."); // $NON-NLS-1$
   while (true) {
     String token = tokenizer.nextToken();
     Object current = scope.get(token, scope);
     if (!tokenizer.hasMoreTokens()) {
       if (current == Scriptable.NOT_FOUND) {
         if (value instanceof NativeObject) {
           Scriptable wrapped = Context.getCurrentContext().newObject(scope);
           wrapped.setPrototype((Scriptable) value);
           value = wrapped;
         }
         scope.put(token, scope, value);
         return;
       }
       throw new IllegalStateException(
           "Resolve error: "
               + name
               + " already exists for "
               + this.toString()); // $NON-NLS-1$//$NON-NLS-2$	
     }
     if (current == Scriptable.NOT_FOUND) {
       current = ScriptableObject.getProperty(scope, token);
       if (current == Scriptable.NOT_FOUND) current = Context.getCurrentContext().newObject(scope);
       else if (current instanceof NativeObject) {
         // we need to wrap this object from the prototype
         Scriptable wrapped = Context.getCurrentContext().newObject(scope);
         wrapped.setPrototype((Scriptable) current);
         current = wrapped;
       } else
         throw new IllegalStateException(
             "Resolve error: "
                 + name
                 + "-"
                 + token
                 + " already exists for "
                 + this.toString()); // $NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
       scope.put(token, scope, current);
     }
     scope = (Scriptable) current;
   }
 }
 /** Close the generator if it is still open. */
 @Override
 protected void finalize() throws Throwable {
   if (savedState != null) {
     // This is a little tricky since we are most likely running in
     // a different thread. We need to get a Context to run this, and
     // we must call "doTopCall" since this will likely be the outermost
     // JavaScript frame on this thread.
     Context cx = Context.getCurrentContext();
     ContextFactory factory = cx != null ? cx.getFactory() : ContextFactory.getGlobal();
     factory.call(new CloseGeneratorAction(this));
   }
 }
Example #7
0
  public static void finishInit(
      Scriptable scope, FunctionObject constructor, Scriptable prototype) {
    // Create and make these properties in the prototype visible
    Context cx = Context.getCurrentContext();

    Scriptable jars = cx.newArray(prototype, 0);
    jars.put(0, jars, cx.newArray(jars, 0));

    defineProperty(prototype, "params", cx.newArray(prototype, 0), ScriptableObject.PERMANENT);
    defineProperty(prototype, "auth", cx.newArray(prototype, 0), ScriptableObject.PERMANENT);
    defineProperty(prototype, "jars", jars, ScriptableObject.PERMANENT);
    defineProperty(prototype, "headers", cx.newArray(prototype, 0), ScriptableObject.PERMANENT);
  }
Example #8
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);
  }
Example #9
0
  Namespace getDefaultNamespace(Context cx) {
    if (cx == null) {
      cx = Context.getCurrentContext();
      if (cx == null) {
        return namespacePrototype;
      }
    }

    Object ns = ScriptRuntime.searchDefaultNamespace(cx);
    if (ns == null) {
      return namespacePrototype;
    } else {
      if (ns instanceof Namespace) {
        return (Namespace) ns;
      } else {
        //    TODO    Clarify or remove the following comment
        // Should not happen but for now it could
        // due to bad searchDefaultNamespace implementation.
        return namespacePrototype;
      }
    }
  }
Example #10
0
  // Needed by NativeJavaObject de-serializer
  public static Object readAdapterObject(Scriptable self, ObjectInputStream in)
      throws IOException, ClassNotFoundException {
    ContextFactory factory;
    Context cx = Context.getCurrentContext();
    if (cx != null) {
      factory = cx.getFactory();
    } else {
      factory = null;
    }

    Class<?> superClass = Class.forName((String) in.readObject());

    String[] interfaceNames = (String[]) in.readObject();
    Class<?>[] interfaces = new Class[interfaceNames.length];

    for (int i = 0; i < interfaceNames.length; i++)
      interfaces[i] = Class.forName(interfaceNames[i]);

    Scriptable delegee = (Scriptable) in.readObject();

    Class<?> adapterClass = getAdapterClass(self, superClass, interfaces, delegee);

    Class<?>[] ctorParms = {
      ScriptRuntime.ContextFactoryClass,
      ScriptRuntime.ScriptableClass,
      ScriptRuntime.ScriptableClass
    };
    Object[] ctorArgs = {factory, delegee, self};
    try {
      return adapterClass.getConstructor(ctorParms).newInstance(ctorArgs);
    } catch (InstantiationException e) {
    } catch (IllegalAccessException e) {
    } catch (InvocationTargetException e) {
    } catch (NoSuchMethodException e) {
    }

    throw new ClassNotFoundException("adapter");
  }
  public static String getMessage(String messageId, Object[] args) {
    Context cx = Context.getCurrentContext();
    Locale locale = cx == null ? Locale.getDefault() : cx.getLocale();

    // ResourceBundle does caching.
    ResourceBundle rb =
        ResourceBundle.getBundle(
            "net.sourceforge.htmlunit.corejs.javascript.tools.resources.Messages", locale);

    String formatString;
    try {
      formatString = rb.getString(messageId);
    } catch (java.util.MissingResourceException mre) {
      throw new RuntimeException("no message resource found for message property " + messageId);
    }

    if (args == null) {
      return formatString;
    } else {
      MessageFormat formatter = new MessageFormat(formatString);
      return formatter.format(args);
    }
  }
 /** @deprecated NativeJavaPackage is an internal class, do not use it directly. */
 public NativeJavaPackage(String packageName) {
   this(false, packageName, Context.getCurrentContext().getApplicationClassLoader());
 }