Package org.mozilla.javascript

Examples of org.mozilla.javascript.Callable


    static int taskId = 0;

    private static Runnable runnableInNewThread(final Scriptable scope, Object[] args, String name) {
    if(args.length == 0 || !(args[0] instanceof Callable))
      throw ScriptRuntime.constructError("TypeError", "The first parameter of " + name + " must be a function");
    final Callable function = (Callable) args[0];
    TimerTask timeoutTask;
    final int thisTaskId = taskId++;
    final Object user = UserSecurity.currentUser();
    IndividualRequest request = Client.getCurrentObjectResponse();
    final Client session = request == null ? null : Client.getCurrentObjectResponse().getConnection();

    return new Runnable(){

      public void run() {
        try{
          if(session != null)
            session.adoptThread(Thread.currentThread());
          UserSecurity.registerThisThread(user);
          Transaction.startTransaction();
          function.call(PersevereContextFactory.getContext(), scope, null, new Object[]{});
          Transaction.currentTransaction().commit();
        }catch(Throwable e){
          if(e instanceof RhinoException)
            log.warn(((RhinoException)e).details() + '\n' + ((RhinoException)e).getScriptStackTrace());
          else
View Full Code Here


        }
        // cacheable scriptable objects
        switch (name) {
          case "resolveScope":
            if (resolveScope == null) {
              resolveScope = new Callable() {
            @Override
            public Object call(org.mozilla.javascript.Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
              setParentScope(scope);
              ((JSYokeResponse) response()).setParentScope(scope);
              return Undefined.instance;
            }
          };
            }
            return resolveScope;
            case "absoluteURI":
                return absoluteURI();
            case "accepts":
                if (accepts == null) {
                    accepts = new Callable() {
                        @Override
                        public Object call(org.mozilla.javascript.Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
                            if (JSYokeRequest.this != thisObj) {
                                throw new RuntimeException("[native JSYokeFunction not bind to JSYokeRequest]");
                            }

                            if (isVararg(args, String.class)) {
                                return JSYokeRequest.this.accepts((String[]) args);
                            }

                            throw new UnsupportedOperationException();
                        }
                    };
                }
                return accepts;
            case "body":
                if (body != null) {
                    if (body instanceof Buffer) {
                        return body.toString();
                    }
                    return javaToJS(body, getParentScope());
                }
                return null;
            case "bodyHandler":
                if (bodyHandler == null) {
                    bodyHandler = new Callable() {
                        @Override
                        @SuppressWarnings({ "unchecked", "rawtypes" })
                        public Object call(final org.mozilla.javascript.Context cx, final Scriptable scope, final Scriptable thisObj, final Object[] args) {
                            if (JSYokeRequest.this != thisObj) {
                                throw new RuntimeException("[native JSYokeFunction not bind to JSYokeRequest]");
                            }

                            if (JSUtil.is(args, Handler.class)) {
                                JSYokeRequest.this.bodyHandler((Handler) args[0]);
                                return Undefined.instance;
                            }

                            if (JSUtil.is(args, Callable.class)) {
                                JSYokeRequest.this.bodyHandler(new Handler<Buffer>() {
                                    @Override
                                    public void handle(Buffer buffer) {
                                        ((Callable) args[0]).call(cx, scope, thisObj, new Object[] {
                                                buffer.toString()
                                        });
                                    }
                                });
                                return Undefined.instance;
                            }

                            throw new UnsupportedOperationException();
                        }
                    };
                }
                return bodyHandler;
            case "bodyLengthLimit":
                return bodyLengthLimit();
            case "contentLength":
                return contentLength();
            case "cookies":
                if (cookies == null) {
                  cookies = javaToJS(cookies(), parent);
                }
                return cookies;
            case "createSession":
              if (createSession == null) {
                createSession = new Callable() {
                        @Override
                        public Object call(final org.mozilla.javascript.Context cx, final Scriptable scope, final Scriptable thisObj, final Object[] args) {
                            if (JSYokeRequest.this != thisObj) {
                                throw new RuntimeException("[native JSYokeFunction not bind to JSYokeRequest]");
                            }
                            return javaToJS(createSession(), scope);
                        }
                    };
              }
                return createSession;
            case "dataHandler":
                if (dataHandler == null) {
                    dataHandler = new Callable() {
                        @Override
                        @SuppressWarnings({ "unchecked", "rawtypes" })
                        public Object call(final org.mozilla.javascript.Context cx, final Scriptable scope, final Scriptable thisObj, final Object[] args) {
                            if (JSYokeRequest.this != thisObj) {
                                throw new RuntimeException("[native JSYokeFunction not bind to JSYokeRequest]");
                            }

                            if (JSUtil.is(args, Handler.class)) {
                                JSYokeRequest.this.dataHandler((Handler) args[0]);
                                return Undefined.instance;
                            }

                            if (JSUtil.is(args, Callable.class)) {
                                JSYokeRequest.this.dataHandler(new Handler<Buffer>() {
                                    @Override
                                    public void handle(Buffer buffer) {
                                        ((Callable) args[0]).call(cx, scope, thisObj, new Object[]{
                                                buffer.toString()
                                        });
                                    }
                                });
                                return Undefined.instance;
                            }

                            throw new UnsupportedOperationException();
                        }
                    };
                }
                return dataHandler;
            case "destroySession":
              if (destroySession == null) {
                destroySession = new Callable() {
                        @Override
                        public Object call(final org.mozilla.javascript.Context cx, final Scriptable scope, final Scriptable thisObj, final Object[] args) {
                            if (JSYokeRequest.this != thisObj) {
                                throw new RuntimeException("[native JSYokeFunction not bind to JSYokeRequest]");
                            }
                            destroySession();
                            return Undefined.instance;
                        }
                    };
              }
                return destroySession;
            case "endHandler":
                if (endHandler == null) {
                    endHandler = new Callable() {
                        @Override
                        @SuppressWarnings({ "unchecked", "rawtypes" })
                        public Object call(final org.mozilla.javascript.Context cx, final Scriptable scope, final Scriptable thisObj, final Object[] args) {
                            if (JSYokeRequest.this != thisObj) {
                                throw new RuntimeException("[native JSYokeFunction not bind to JSYokeRequest]");
                            }

                            if (JSUtil.is(args, Handler.class)) {
                                JSYokeRequest.this.endHandler((Handler) args[0]);
                                return Undefined.instance;
                            }

                            if (JSUtil.is(args, Callable.class)) {
                                JSYokeRequest.this.endHandler(new Handler<Void>() {
                                    @Override
                                    public void handle(Void v) {
                                        ((Callable) args[0]).call(cx, scope, thisObj, EMPTY_OBJECT_ARRAY);
                                    }
                                });
                                return Undefined.instance;
                            }

                            throw new UnsupportedOperationException();
                        }
                    };
                }
                return endHandler;
            case "exceptionHandler":
                if (exceptionHandler == null) {
                    exceptionHandler = new Callable() {
                        @Override
                        @SuppressWarnings({ "unchecked", "rawtypes" })
                        public Object call(final org.mozilla.javascript.Context cx, final Scriptable scope, final Scriptable thisObj, final Object[] args) {
                            if (JSYokeRequest.this != thisObj) {
                                throw new RuntimeException("[native JSYokeFunction not bind to JSYokeRequest]");
                            }

                            if (JSUtil.is(args, Handler.class)) {
                                JSYokeRequest.this.exceptionHandler((Handler) args[0]);
                                return Undefined.instance;
                            }

                            if (JSUtil.is(args, Callable.class)) {
                                JSYokeRequest.this.exceptionHandler(new Handler<Throwable>() {
                                    @Override
                                    public void handle(Throwable throwable) {
                                        ((Callable) args[0]).call(cx, scope, thisObj, new Object[]{
                                                new WrappedException(throwable)});
                                    }
                                });
                                return Undefined.instance;
                            }

                            throw new UnsupportedOperationException();
                        }
                    };
                }
                return exceptionHandler;
            case "expectMultipart":
                if (expectMultipart == null) {
                    expectMultipart = new Callable() {
                        @Override
                        public Object call(org.mozilla.javascript.Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
                            if (JSYokeRequest.this != thisObj) {
                                throw new RuntimeException("[native JSYokeFunction not bind to JSYokeRequest]");
                            }

                            if (JSUtil.is(args, Boolean.class)) {
                                JSYokeRequest.this.expectMultiPart((Boolean) args[0]);
                                return Undefined.instance;
                            }

                            throw new UnsupportedOperationException();
                        }
                    };
                }
                return expectMultipart;
            case "files":
                if (files == null) {
                  files = javaToJS(files(), getParentScope());
                }
                return files;
            case "formAttributes":
                if (formAttributes == null) {
                    formAttributes = javaToJS(formAttributes(), getParentScope());
                }
                return formAttributes;
            case "getAllCookies":
                if (getAllCookies == null) {
                    getAllCookies = new Callable() {
                        @Override
                        public Object call(org.mozilla.javascript.Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
                            if (JSYokeRequest.this != thisObj) {
                                throw new RuntimeException("[native JSYokeFunction not bind to JSYokeRequest]");
                            }

                            if (JSUtil.is(args, String.class)) {
                                return javaToJS(JSYokeRequest.this.getAllCookies((String) args[0]), scope);
                            }

                            throw new UnsupportedOperationException();
                        }
                    };
                }
                return getAllCookies;
            case "getAllHeaders":
                if (getAllHeaders == null) {
                    getAllHeaders = new Callable() {
                        @Override
                        public Object call(org.mozilla.javascript.Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
                            if (JSYokeRequest.this != thisObj) {
                                throw new RuntimeException("[native JSYokeFunction not bind to JSYokeRequest]");
                            }

                            if (JSUtil.is(args, String.class)) {
                                return javaToJS(JSYokeRequest.this.getAllHeaders((String) args[0]), scope);
                            }

                            throw new UnsupportedOperationException();
                        }
                    };
                }
                return getAllHeaders;
            case "getCookie":
                if (getCookie == null) {
                    getCookie = new Callable() {
                        @Override
                        public Object call(org.mozilla.javascript.Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
                            if (JSYokeRequest.this != thisObj) {
                                throw new RuntimeException("[native JSYokeFunction not bind to JSYokeRequest]");
                            }

                            if (JSUtil.is(args, String.class)) {
                                return javaToJS(JSYokeRequest.this.getCookie((String) args[0]), scope);
                            }

                            throw new UnsupportedOperationException();
                        }
                    };
                }
                return getCookie;
            case "getFile":
                if (getFile == null) {
                    getFile = new Callable() {
                        @Override
                        public Object call(org.mozilla.javascript.Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
                            if (JSYokeRequest.this != thisObj) {
                                throw new RuntimeException("[native JSYokeFunction not bind to JSYokeRequest]");
                            }

                            if (JSUtil.is(args, String.class)) {
                                return javaToJS(JSYokeRequest.this.getFile((String) args[0]), scope);
                            }

                            throw new UnsupportedOperationException();
                        }
                    };
                }
                return getFile;
            case "getFormParameter":
                if (getFormParameter == null) {
                    getFormParameter = new Callable() {
                        @Override
                        public Object call(org.mozilla.javascript.Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
                            if (JSYokeRequest.this != thisObj) {
                                throw new RuntimeException("[native JSYokeFunction not bind to JSYokeRequest]");
                            }

                            if (JSUtil.is(args, String.class, String.class)) {
                                return JSYokeRequest.this.getFormParameter((String) args[0], (String) args[1]);
                            }

                            if (JSUtil.is(args, String.class)) {
                                return JSYokeRequest.this.getFormParameter((String) args[0]);
                            }

                            throw new UnsupportedOperationException();
                        }
                    };
                }
                return getFormParameter;
            case "getFormParameterList":
                if (getFormParameterList == null) {
                    getFormParameterList = new Callable() {
                        @Override
                        public Object call(org.mozilla.javascript.Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
                            if (JSYokeRequest.this != thisObj) {
                                throw new RuntimeException("[native JSYokeFunction not bind to JSYokeRequest]");
                            }

                            if (JSUtil.is(args, String.class)) {
                                return javaToJS(JSYokeRequest.this.getFormParameterList((String) args[0]), scope);
                            }

                            throw new UnsupportedOperationException();
                        }
                    };
                }
                return getFormParameterList;
            case "getHeader":
                if (getHeader == null) {
                    getHeader = new Callable() {
                        @Override
                        public Object call(org.mozilla.javascript.Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
                            if (JSYokeRequest.this != thisObj) {
                                throw new RuntimeException("[native JSYokeFunction not bind to JSYokeRequest]");
                            }

                            if (JSUtil.is(args, String.class, String.class)) {
                                return JSYokeRequest.this.getHeader((String) args[0], (String) args[1]);
                            }

                            if (JSUtil.is(args, String.class)) {
                                return JSYokeRequest.this.getHeader((String) args[0]);
                            }

                            throw new UnsupportedOperationException();
                        }
                    };
                }
                return getHeader;
            case "getParameter":
                if (getParameter == null) {
                    getParameter = new Callable() {
                        @Override
                        public Object call(org.mozilla.javascript.Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
                            if (JSYokeRequest.this != thisObj) {
                                throw new RuntimeException("[native JSYokeFunction not bind to JSYokeRequest]");
                            }

                            if (JSUtil.is(args, String.class, String.class)) {
                                return JSYokeRequest.this.getParameter((String) args[0], (String) args[1]);
                            }

                            if (JSUtil.is(args, String.class)) {
                                return JSYokeRequest.this.getParameter((String) args[0]);
                            }

                            throw new UnsupportedOperationException();
                        }
                    };
                }
                return getParameter;
            case "getParameterList":
                if (getParameterList == null) {
                    getParameterList = new Callable() {
                        @Override
                        public Object call(org.mozilla.javascript.Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
                            if (JSYokeRequest.this != thisObj) {
                                throw new RuntimeException("[native JSYokeFunction not bind to JSYokeRequest]");
                            }

                            if (JSUtil.is(args, String.class)) {
                                return javaToJS(JSYokeRequest.this.getParameterList((String) args[0]), scope);
                            }

                            throw new UnsupportedOperationException();
                        }
                    };
                }
                return getFormParameterList;
            case "params":
                if (params == null) {
                    params = javaToJS(params(), getParentScope());
                }
                return params;
            case "param":
              if (param == null) {
                param = new Callable() {
                        @Override
                        public Object call(org.mozilla.javascript.Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
                            if (JSYokeRequest.this != thisObj) {
                                throw new RuntimeException("[native JSYokeFunction not bind to JSYokeRequest]");
                            }

                            if (JSUtil.is(args, String.class)) {
                              String name = (String) args[0], value = null;
                                // first try to get param from params / url --> this differs from expressjs
                                value = JSYokeRequest.this.params().get(name);
                                if (value == null) {
                                  // then try to get param from body
                                  try {
                                      value = JSYokeRequest.this.formAttributes().get(name);
                                    } catch (Exception ignore) { /* maybe throw IllegalStateExcpetion */ }
                                }
                                return value != null ? value : Undefined.instance;
                            }

                            throw new UnsupportedOperationException();
                        }
                    };
              }
              return param;
            case "hasBody":
                if (hasBody == null) {
                    hasBody = new Callable() {
                        @Override
                        public Object call(org.mozilla.javascript.Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
                            if (JSYokeRequest.this != thisObj) {
                                throw new RuntimeException("[native JSYokeFunction not bind to JSYokeRequest]");
                            }
                            return JSYokeRequest.this.hasBody();
                        }
                    };
                }
                return hasBody;
            case "headers":
                if (headers == null) {
                    headers = javaToJS(headers(), getParentScope());
                }
                return headers;
            case "ip":
                return ip();
            case "is":
                if (is == null) {
                    is = new Callable() {
                        @Override
                        public Object call(org.mozilla.javascript.Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
                            if (JSYokeRequest.this != thisObj) {
                                throw new RuntimeException("[native JSYokeFunction not bind to JSYokeRequest]");
                            }

                            if (JSUtil.is(args, String.class)) {
                                return JSYokeRequest.this.is((String) args[0]);
                            }

                            throw new UnsupportedOperationException();
                        }
                    };
                }
                return is;
            case "secure":
                return isSecure();
            case "loadSession":
                if (loadSession == null) {
                    loadSession = new Callable() {
                        @Override
                        @SuppressWarnings({ "unchecked", "rawtypes" })
                        public Object call(final org.mozilla.javascript.Context cx, final Scriptable scope, final Scriptable thisObj, final Object[] args) {
                            if (JSYokeRequest.this != thisObj) {
                                throw new RuntimeException("[native JSYokeFunction not bind to JSYokeRequest]");
                            }

                            if (JSUtil.is(args, String.class, Handler.class)) {
                                JSYokeRequest.this.loadSession((String) args[0], (Handler) args[1]);
                                return Undefined.instance;
                            }

                            if (JSUtil.is(args, String.class, Callable.class)) {
                                JSYokeRequest.this.loadSession((String) args[0], new Handler<Object>() {
                                    @Override
                                    public void handle(Object error) {
                                        ((Callable) args[0]).call(cx, scope, thisObj, new Object[]{error});
                                    }
                                });
                                return Undefined.instance;
                            }

                            throw new UnsupportedOperationException();
                        }
                    };
                }
                return loadSession;
            case "localAddress":
                return localAddress().toString();
            case "locale":
                return locale().toString();
            case "method":
                return method();
            case "netSocket":
              // TODO
                return netSocket();
            case "normalizedPath":
                return normalizedPath();
            case "originalMethod":
                return originalMethod();
            case "path":
                return path();
            case "pause":
                if (pause == null) {
                    pause = new Callable() {
                        @Override
                        public Object call(org.mozilla.javascript.Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
                            if (JSYokeRequest.this != thisObj) {
                                throw new RuntimeException("[native JSYokeFunction not bind to JSYokeRequest]");
                            }

                            JSYokeRequest.this.pause();
                            return Undefined.instance;
                        }
                    };
                }
                return pause;
            case "peerCertificateChain":
              // TODO
                try {
                    return peerCertificateChain();
                } catch (SSLPeerUnverifiedException e) {
                    throw new RuntimeException(e);
                }
            case "query":
                return query();
            case "remoteAddress":
                return remoteAddress().toString();
            case "res":
            case "response":
                return response();
            case "resume":
                if (resume == null) {
                    resume = new Callable() {
                        @Override
                        public Object call(org.mozilla.javascript.Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
                            if (JSYokeRequest.this != thisObj) {
                                throw new RuntimeException("[native JSYokeFunction not bind to JSYokeRequest]");
                            }

                            JSYokeRequest.this.resume();
                            return Undefined.instance;
                        }
                    };
                }
                return resume;
            case "sortedHeader":
                if (sortedHeader == null) {
                    sortedHeader = new Callable() {
                        @Override
                        public Object call(org.mozilla.javascript.Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
                            if (JSYokeRequest.this != thisObj) {
                                throw new RuntimeException("[native JSYokeFunction not bind to JSYokeRequest]");
                            }

                            if (JSUtil.is(args, String.class)) {
                                return javaToJS(JSYokeRequest.this.sortedHeader((String) args[0]), scope);
                            }

                            throw new UnsupportedOperationException();
                        }
                    };
                }
                return sortedHeader;
            case "uploadHandler":
                if (uploadHandler == null) {
                    uploadHandler = new Callable() {
                        @Override
                        @SuppressWarnings({ "unchecked", "rawtypes" })
                        public Object call(final org.mozilla.javascript.Context cx, final Scriptable scope, final Scriptable thisObj, final Object[] args) {
                            if (JSYokeRequest.this != thisObj) {
                                throw new RuntimeException("[native JSYokeFunction not bind to JSYokeRequest]");
View Full Code Here

    @Override
    public Object get(String name, Scriptable start) {
        switch (name) {
            case "addCookie":
                if (addCookie == null) {
                    addCookie = new Callable() {
                        @Override
                        public Object call(org.mozilla.javascript.Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
                            if (JSYokeResponse.this != thisObj) {
                                throw new RuntimeException("[native JSYokeFunction not bind to JSYokeResponse]");
                            }

                            if (is(args, Cookie.class)) {
                                JSYokeResponse.this.addCookie((Cookie) args[0]);
                                return Undefined.instance;
                            }

                            throw new UnsupportedOperationException();
                        }
                    };
                }
                return addCookie;
            case "close":
                if (close == null) {
                    close = new Callable() {
                        @Override
                        public Object call(org.mozilla.javascript.Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
                            if (JSYokeResponse.this != thisObj) {
                                throw new RuntimeException("[native JSYokeFunction not bind to JSYokeResponse]");
                            }
                            JSYokeResponse.this.close();
                            return Undefined.instance;
                        }
                    };
                }
                return close;
            case "closeHandler":
                if (closeHandler == null) {
                    closeHandler = new Callable() {
                        @Override
                        @SuppressWarnings({ "unchecked", "rawtypes" })
                        public Object call(final org.mozilla.javascript.Context cx, final Scriptable scope, final Scriptable thisObj, final Object[] args) {
                            if (JSYokeResponse.this != thisObj) {
                                throw new RuntimeException("[native JSYokeFunction not bind to JSYokeResponse]");
                            }

                            if (is(args, Handler.class)) {
                                JSYokeResponse.this.closeHandler((Handler) args[0]);
                                return Undefined.instance;
                            }

                            if (is(args, Callable.class)) {
                                JSYokeResponse.this.closeHandler(new Handler<Void>() {
                                    @Override
                                    public void handle(Void event) {
                                        ((Callable) args[0]).call(cx, scope, thisObj, EMPTY_OBJECT_ARRAY);
                                    }
                                });
                                return Undefined.instance;
                            }

                            throw new UnsupportedOperationException();
                        }
                    };
                }
                return closeHandler;
            case "drainHandler":
                if (drainHandler == null) {
                    drainHandler = new Callable() {
                        @Override
                        @SuppressWarnings({ "unchecked", "rawtypes" })
                        public Object call(final org.mozilla.javascript.Context cx, final Scriptable scope, final Scriptable thisObj, final Object[] args) {
                            if (JSYokeResponse.this != thisObj) {
                                throw new RuntimeException("[native JSYokeFunction not bind to JSYokeResponse]");
                            }

                            if (is(args, Handler.class)) {
                                JSYokeResponse.this.drainHandler((Handler) args[0]);
                                return Undefined.instance;
                            }

                            if (is(args, Callable.class)) {
                                JSYokeResponse.this.drainHandler(new Handler<Void>() {
                                    @Override
                                    public void handle(Void event) {
                                        ((Callable) args[0]).call(cx, scope, thisObj, EMPTY_OBJECT_ARRAY);
                                    }
                                });
                                return Undefined.instance;
                            }

                            throw new UnsupportedOperationException();
                        }
                    };
                }
                return drainHandler;
            case "end":
                if (end == null) {
                    end = new Callable() {
                        @SuppressWarnings("rawtypes")
                        @Override
                        public Object call(org.mozilla.javascript.Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
                            if (JSYokeResponse.this != thisObj) {
                                throw new RuntimeException("[native JSYokeFunction not bind to JSYokeResponse]");
                            }

                            if (is(args, NativeObject.class) || is(args, NativeArray.class)) {
                              Object json = NativeJSON.stringify(cx, scope, args[0], null, null);
                              if (json instanceof String) {
                                JSYokeResponse.this.end((String) json);
                                    return Undefined.instance;
                              }
                                throw new UnsupportedOperationException();
                            }
                           
                            if (is(args, String.class, String.class)) {
                                JSYokeResponse.this.end((String) args[0], (String) args[1]);
                                return Undefined.instance;
                            }

                            if (is(args, JsonElement.class)) {
                                JSYokeResponse.this.end((JsonElement) args[0]);
                                return Undefined.instance;
                            }

                            if (is(args, ReadStream.class)) {
                                JSYokeResponse.this.end((ReadStream) args[0]);
                                return Undefined.instance;
                            }

                            if (is(args, String.class)) {
                                JSYokeResponse.this.end((String) args[0]);
                                return Undefined.instance;
                            }

                            if (is(args, Buffer.class)) {
                                JSYokeResponse.this.end((Buffer) args[0]);
                                return Undefined.instance;
                            }

                            if (is(args)) {
                                JSYokeResponse.this.end();
                                return Undefined.instance;
                            }

                            throw new UnsupportedOperationException();
                        }
                    };
                }
                return end;
            case "endHandler":
                if (endHandler == null) {
                    endHandler = new Callable() {
                        @Override
                        @SuppressWarnings({ "unchecked", "rawtypes" })
                        public Object call(final org.mozilla.javascript.Context cx, final Scriptable scope, final Scriptable thisObj, final Object[] args) {
                            if (JSYokeResponse.this != thisObj) {
                                throw new RuntimeException("[native JSYokeFunction not bind to JSYokeResponse]");
                            }

                            if (is(args, Handler.class)) {
                                JSYokeResponse.this.endHandler((Handler) args[0]);
                                return Undefined.instance;
                            }

                            if (is(args, Callable.class)) {
                                JSYokeResponse.this.endHandler(new Handler<Void>() {
                                    @Override
                                    public void handle(Void event) {
                                        ((Callable) args[0]).call(cx, scope, thisObj, EMPTY_OBJECT_ARRAY);
                                    }
                                });
                                return Undefined.instance;
                            }

                            throw new UnsupportedOperationException();
                        }
                    };
                }
                return endHandler;
            case "exceptionHandler":
                if (exceptionHandler == null) {
                    exceptionHandler = new Callable() {
                        @Override
                        @SuppressWarnings({ "unchecked", "rawtypes" })
                        public Object call(final org.mozilla.javascript.Context cx, final Scriptable scope, final Scriptable thisObj, final Object[] args) {
                            if (JSYokeResponse.this != thisObj) {
                                throw new RuntimeException("[native JSYokeFunction not bind to JSYokeResponse]");
                            }

                            if (is(args, Handler.class)) {
                                JSYokeResponse.this.exceptionHandler((Handler) args[0]);
                                return Undefined.instance;
                            }

                            if (is(args, Callable.class)) {
                                JSYokeResponse.this.exceptionHandler(new Handler<Throwable>() {
                                    @Override
                                    public void handle(Throwable throwable) {
                                        ((Callable) args[0]).call(cx, scope, thisObj, new Object[] {
                                                new WrappedException(throwable)});
                                    }
                                });
                                return Undefined.instance;
                            }

                            throw new UnsupportedOperationException();
                        }
                    };
                }
                return exceptionHandler;
            case "getHeader":
                if (getHeader == null) {
                    getHeader = new Callable() {
                        @Override
                        public Object call(org.mozilla.javascript.Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
                            if (JSYokeResponse.this != thisObj) {
                                throw new RuntimeException("[native JSYokeFunction not bind to JSYokeResponse]");
                            }

                            if (is(args, String.class, String.class)) {
                                return JSYokeResponse.this.getHeader((String) args[0], (String) args[1]);
                            }

                            if (is(args, String.class)) {
                                return JSYokeResponse.this.getHeader((String) args[0]);
                            }

                            throw new UnsupportedOperationException();
                        }
                    };
                }
                return getHeader;
            case "statusCode":
                return getStatusCode();
            case "statusMessage":
                return getStatusMessage();
            case "headers":
                if (headers == null) {
                    headers = javaToJS(headers(), getParentScope());
                }
                return headers;
            case "headersHandler":
                if (headersHandler == null) {
                    headersHandler = new Callable() {
                        @Override
                        @SuppressWarnings({ "unchecked", "rawtypes" })
                        public Object call(final org.mozilla.javascript.Context cx, final Scriptable scope, final Scriptable thisObj, final Object[] args) {
                            if (JSYokeResponse.this != thisObj) {
                                throw new RuntimeException("[native JSYokeFunction not bind to JSYokeResponse]");
                            }

                            if (is(args, Handler.class)) {
                                JSYokeResponse.this.headersHandler((Handler) args[0]);
                                return Undefined.instance;
                            }

                            if (is(args, Callable.class)) {
                                JSYokeResponse.this.headersHandler(new Handler<Void>() {
                                    @Override
                                    public void handle(Void event) {
                                        ((Callable) args[0]).call(cx, scope, thisObj, EMPTY_OBJECT_ARRAY);
                                    }
                                });
                                return Undefined.instance;
                            }

                            throw new UnsupportedOperationException();
                        }
                    };
                }
                return headersHandler;
            case "chunked":
                return isChunked();
            case "jsonp":
                if (jsonp == null) {
                    jsonp = new Callable() {
                        @Override
                        public Object call(org.mozilla.javascript.Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
                            if (JSYokeResponse.this != thisObj) {
                                throw new RuntimeException("[native JSYokeFunction not bind to JSYokeResponse]");
                            }

                            if (is(args, String.class, NativeObject.class) || is(args, String.class, NativeArray.class)) {
                              Object json = NativeJSON.stringify(cx, scope, args[1], null, null);
                              if (json instanceof String) {
                                JSYokeResponse.this.jsonp((String) args[0], (String) json);
                                    return Undefined.instance;
                              }
                                throw new UnsupportedOperationException();
                            }

                            if (is(args, NativeObject.class) || is(args, NativeArray.class)) {
                              Object json = NativeJSON.stringify(cx, scope, args[0], null, null);
                              if (json instanceof String) {
                                JSYokeResponse.this.jsonp((String) json);
                                    return Undefined.instance;
                              }
                                throw new UnsupportedOperationException();
                            }

                            if (is(args, String.class, JsonElement.class)) {
                                JSYokeResponse.this.jsonp((String) args[0], (JsonElement) args[1]);
                                return Undefined.instance;
                            }
                            if (is(args, String.class, String.class)) {
                                JSYokeResponse.this.jsonp((String) args[0], (String) args[1]);
                                return Undefined.instance;
                            }
                            if (is(args, JsonElement.class)) {
                                JSYokeResponse.this.jsonp((JsonElement) args[0]);
                                return Undefined.instance;
                            }
                            if (is(args, String.class)) {
                                JSYokeResponse.this.jsonp((String) args[0]);
                                return Undefined.instance;
                            }

                            throw new UnsupportedOperationException();
                        }
                    };
                }
                return jsonp;
            case "putHeader":
                if (putHeader == null) {
                    putHeader = new Callable() {
                        @SuppressWarnings("unchecked")
                        @Override
                        public Object call(org.mozilla.javascript.Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
                            if (JSYokeResponse.this != thisObj) {
                                throw new RuntimeException("[native JSYokeFunction not bind to JSYokeResponse]");
                            }

                            if (is(args, CharSequence.class, CharSequence.class)) {
                                JSYokeResponse.this.putHeader((CharSequence) args[0], (CharSequence) args[1]);
                                return Undefined.instance;
                            }

                            if (is(args, CharSequence.class, Iterable.class)) {
                                JSYokeResponse.this.putHeader((CharSequence) args[0], (Iterable<CharSequence>) args[1]);
                                return Undefined.instance;
                            }

                            if (is(args, String.class, String.class)) {
                                JSYokeResponse.this.putHeader((String) args[0], (String) args[1]);
                                return Undefined.instance;
                            }

                            if (is(args, String.class, Iterable.class)) {
                                JSYokeResponse.this.putHeader((String) args[0], (Iterable<String>) args[1]);
                                return Undefined.instance;
                            }

                            throw new UnsupportedOperationException();
                        }
                    };
                }
                return putHeader;
            case "putTrailer":
                if (putTrailer == null) {
                    putTrailer = new Callable() {
                        @SuppressWarnings("unchecked")
                        @Override
                        public Object call(org.mozilla.javascript.Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
                            if (JSYokeResponse.this != thisObj) {
                                throw new RuntimeException("[native JSYokeFunction not bind to JSYokeResponse]");
                            }

                            if (is(args, CharSequence.class, CharSequence.class)) {
                                JSYokeResponse.this.putTrailer((CharSequence) args[0], (CharSequence) args[1]);
                                return Undefined.instance;
                            }

                            if (is(args, CharSequence.class, Iterable.class)) {
                                JSYokeResponse.this.putTrailer((CharSequence) args[0], (Iterable<CharSequence>) args[1]);
                                return Undefined.instance;
                            }

                            if (is(args, String.class, String.class)) {
                                JSYokeResponse.this.putTrailer((String) args[0], (String) args[1]);
                                return Undefined.instance;
                            }

                            if (is(args, String.class, Iterable.class)) {
                                JSYokeResponse.this.putTrailer((String) args[0], (Iterable<String>) args[1]);
                                return Undefined.instance;
                            }

                            throw new UnsupportedOperationException();
                        }
                    };
                }
                return putTrailer;
            case "redirect":
                if (redirect == null) {
                    redirect = new Callable() {
                        @Override
                        public Object call(org.mozilla.javascript.Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
                            if (JSYokeResponse.this != thisObj) {
                                throw new RuntimeException("[native JSYokeFunction not bind to JSYokeResponse]");
                            }

                            if (is(args, Integer.class, String.class)) {
                                JSYokeResponse.this.redirect((Integer) args[0], (String) args[1]);
                                return Undefined.instance;
                            }

                            if (is(args, String.class)) {
                                JSYokeResponse.this.redirect((String) args[0]);
                                return Undefined.instance;
                            }

                            throw new UnsupportedOperationException();
                        }
                    };
                }
                return redirect;
            case "render":
                if (render == null) {
                    render = new Callable() {
                        @Override
                        @SuppressWarnings({ "unchecked", "rawtypes" })
                        public Object call(final org.mozilla.javascript.Context cx, final Scriptable scope, final Scriptable thisObj, final Object[] args) {
                            if (JSYokeResponse.this != thisObj) {
                                throw new RuntimeException("[native JSYokeFunction not bind to JSYokeResponse]");
                            }

                            if (is(args, String.class, Handler.class)) {
                              JSYokeResponse.this.render((String) args[0], (Handler) args[1]);
                                return Undefined.instance;
                            }
                           
                            if (is(args, String.class, Callable.class)) {
                             
                                JSYokeResponse.this.render((String) args[0], new Handler<Object>() {
                                    @Override
                                    public void handle(Object error) {
                                        ((Callable) args[1]).call(cx, scope, thisObj, new Object[]{error});
                                    }
                                });                                                                                        
                                return Undefined.instance;
                            }
                           
                            if (is(args, String.class)) {
                                JSYokeResponse.this.render((String) args[0]);
                                return Undefined.instance;
                            }
                           
                            throw new UnsupportedOperationException();
                        }
                    };
                }
                return render;
            case "sendFile":
                if (sendFile == null) {
                    sendFile = new Callable() {
                        @Override
                        @SuppressWarnings({ "unchecked", "rawtypes" })
                        public Object call(final org.mozilla.javascript.Context cx, final Scriptable scope, final Scriptable thisObj, final Object[] args) {
                            if (JSYokeResponse.this != thisObj) {
                                throw new RuntimeException("[native JSYokeFunction not bind to JSYokeResponse]");
                            }

                            if (is(args, String.class, String.class, Handler.class)) {
                                JSYokeResponse.this.sendFile((String) args[0], (String) args[1], (Handler) args[2]);
                                return Undefined.instance;
                            }

                            if (is(args, String.class, String.class, Callable.class)) {
                                JSYokeResponse.this.sendFile((String) args[0], (String) args[1], new Handler<AsyncResult<Void>>() {
                                    @Override
                                    public void handle(AsyncResult<Void> result) {
                                        ((Callable) args[2]).call(cx, scope, thisObj, new Object[]{result.cause(), result.result()});
                                    }
                                });
                                return Undefined.instance;
                            }

                            if (is(args, String.class, String.class)) {
                                JSYokeResponse.this.sendFile((String) args[0], (String) args[1]);
                                return Undefined.instance;
                            }

                            if (is(args, String.class, Handler.class)) {
                                JSYokeResponse.this.sendFile((String) args[0], (Handler) args[1]);
                                return Undefined.instance;
                            }

                            if (is(args, String.class, Callable.class)) {
                                JSYokeResponse.this.sendFile((String) args[0], new Handler<AsyncResult<Void>>() {
                                    @Override
                                    public void handle(AsyncResult<Void> result) {
                                        ((Callable) args[1]).call(cx, scope, thisObj, new Object[]{result.cause(), result.result()});
                                    }
                                });
                                return Undefined.instance;
                            }

                            if (is(args, String.class)) {
                                JSYokeResponse.this.sendFile((String) args[0]);
                                return Undefined.instance;
                            }

                            throw new UnsupportedOperationException();
                        }
                    };
                }
                return sendFile;
            case "setContentType":
                if (setContentType == null) {
                    setContentType = new Callable() {
                        @Override
                        public Object call(org.mozilla.javascript.Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
                            if (JSYokeResponse.this != thisObj) {
                                throw new RuntimeException("[native JSYokeFunction not bind to JSYokeResponse]");
                            }

                            if (is(args, String.class, String.class)) {
                                JSYokeResponse.this.setContentType((String) args[0], (String) args[1]);
                                return Undefined.instance;
                            }

                            if (is(args, String.class)) {
                                JSYokeResponse.this.setContentType((String) args[0]);
                                return Undefined.instance;
                            }

                            throw new UnsupportedOperationException();
                        }
                    };
                }
                return setContentType;
            case "setWriteQueueMaxSize":
                if (setWriteQueueMaxSize == null) {
                    setWriteQueueMaxSize = new Callable() {
                        @Override
                        public Object call(org.mozilla.javascript.Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
                            if (JSYokeResponse.this != thisObj) {
                                throw new RuntimeException("[native JSYokeFunction not bind to JSYokeResponse]");
                            }

                            if (is(args, Integer.class)) {
                                JSYokeResponse.this.setWriteQueueMaxSize((Integer) args[0]);
                                return Undefined.instance;
                            }

                            throw new UnsupportedOperationException();
                        }
                    };
                }
                return setWriteQueueMaxSize;
            case "trailers":
                if (trailers == null) {
                    trailers = javaToJS(trailers(), getParentScope());
                }
                return trailers;
            case "write":
                if (write == null) {
                    write = new Callable() {
                        @Override
                        public Object call(org.mozilla.javascript.Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
                            if (JSYokeResponse.this != thisObj) {
                                throw new RuntimeException("[native JSYokeFunction not bind to JSYokeResponse]");
                            }
View Full Code Here

    // call it on the object and again use its return value
    // as a hashtable if it is a NativeObject.
    Class classObject = getClassObject();
    int modifiers = classObject.getModifiers();
    NativeObject properties = null;
    Callable initialize = null;
    if (args.length > 0
        && !Modifier.isInterface(modifiers)
        && !Modifier.isAbstract(modifiers)) {
      // Look at the last argument to find out if we need to do something
      // special. Possibilities: a object literal that defines fields to
      // be set, or a function that is executed on the object and of which
      // the result can be fields to be set.
      Object last = args[args.length - 1];
      // Match callables for initialize functions but filter out java constructors
      // which might be arguments to methods...
      if (last instanceof Callable && !(last instanceof NativeJavaClass))
        initialize = (Callable) last;
      else if (last instanceof NativeObject) {
        // Now see if the constructor takes a Map as the last argument.
        // If so, the NativeObject will be converted to it thought
        // RhinoWrapFactory. Otherwise, the NativeObject is used
        // as the properties to be set on the instance after creation.
        MemberBox ctor = findConstructor(cx, args);
        if (ctor != null) {
          Class[] types = ctor.ctor().getParameterTypes();
          Class lastType = types[types.length - 1];
          // Only set the property object if the constructor does
          // not expect an ArgumentReader or a Map object, both
          // of which NativeObject's can be converted to.
          if (!ArgumentReader.class.isAssignableFrom(lastType)
              && !Map.class.isAssignableFrom(lastType)) {
            properties = (NativeObject) last;
            if (ScriptableObject.hasProperty(properties, "unwrap"))
              properties = null;
          }
        } else {
          // There is no constructor that has to be checked, so it
          // can only be a properties list.
          properties = (NativeObject) last;
        }
        if (properties != null) {
          // Support initialize in the passed object literal too.
          Object obj = ScriptableObject.getProperty(properties, "initialize");
          if (obj instanceof Callable)
            initialize = (Callable) obj;
        }
      }
      // Remove the last argument from the list, so the right constructor
      // will be found:
      if (initialize != null || properties != null) {
        Object[] newArgs = new Object[args.length - 1];
        for (int i = 0; i < newArgs.length; i++)
          newArgs[i] = args[i];
        args = newArgs;
      }
    }
    Scriptable obj = super.construct(cx, scope, args);
    // If properties are to be added, do it now. Add the ones from the
    // object literal first, then call initialize and after add the
    // properties returned by initialize.
    if (properties != null)
      setProperties(obj, properties);
    // If an initialize function was passed as the last argument, execute
    // it now. The fields of the result of the function are then injected
    // into the object after, if it is a NativeObject.
    if (initialize != null) {
      Object res = initialize.call(cx, scope, obj, args);
      if (res instanceof NativeObject)
        setProperties(obj, (NativeObject) res);
    }
    return obj;
  }
View Full Code Here

  public LatteServlet() throws Exception {
    ScriptLoader loader = new ScriptLoader();
    this.parent = loader.getRoot();

    loader.register("httpserver", new Callable() {
      public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] params) {
        fn = (Callable)params[1]
        return null;
      }
    });
View Full Code Here

  public ScriptLoader() throws Exception {
    Context cx = ContextFactory.getGlobal().enterContext();
    this.parent = new ImporterTopLevel(cx);
    cx.initStandardObjects(parent);
   
    register("require", new Callable() {
      public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] params) {
        try {
          if(params != null && params.length == 1 && params[0] instanceof String) return ((Javascript)get((String)params[0])).eval(cx, scope, null);
          else throw new IllegalArgumentException("expecting string");
        } catch(Exception e) {
          throw new IllegalArgumentException(e);
        }
      }
    });
    register("register", new Callable() {
      public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] params) {
        if(params != null && params.length == 2 && params[0] instanceof String) {
          parent.put((String) params[0], parent, params[1]);
          return null;
        } else throw new IllegalArgumentException("expecting string, obj");
      }
    });
    register("render", new Callable() {
      public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] params) {
        try {
          if(params != null && params.length == 2) return ((JHTML)get((String)params[0])).render(cx, scope, params[1]);
          else throw new IllegalArgumentException("expecting 2 args");
        } catch(Exception e) {
View Full Code Here

      script.eval(
        cx,
        scope,
        new Tuple[] {
          new Tuple("params", params),
          new Tuple("echo", new Callable() {
            public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] params) {
              try {
                if(params != null && params.length == 1) buffer.append(params[0].toString());
                else throw new IllegalArgumentException("expecting 1 arg");
             
View Full Code Here

TOP

Related Classes of org.mozilla.javascript.Callable

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.