Examples of ScriptException


Examples of javax.script.ScriptException

          "Method argument scriptContext must not be null.");
    }
        Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
        SlingScriptHelper helper = (SlingScriptHelper) bindings.get(SlingBindings.SLING);
        if (helper == null) {
            throw new ScriptException("SlingScriptHelper missing from bindings");
        }

        String scriptName = helper.getScript().getScriptResource().getPath();

        final ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
       
        try {
          Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
         
          // initialize the Velocity context
          final VelocityContext c = new VelocityContext();
          for (Object entryObj : bindings.entrySet()) {
              Map.Entry<?, ?> entry = (Map.Entry<?, ?>) entryObj;
              c.put((String) entry.getKey(), entry.getValue());
          }
 
          // let Velocity evaluate the script, and send the output to the browser
          final String logTag = getClass().getSimpleName();
          Writer w = scriptContext.getWriter();
          try {
              velocity.evaluate(c, w, logTag, script);
          } catch (Throwable t) {
              throw new ScriptException("Failure running script " + scriptName
                  + ": " + t + ", stack trace: " + t.getStackTrace() );
          }
      } finally {
        Thread.currentThread().setContextClassLoader(oldClassLoader);
      }
View Full Code Here

Examples of javax.script.ScriptException

            parser.setFeature("http://xml.org/sax/features/namespaces", false);
            parser.setProperty("http://cyberneko.org/html/properties/names/elems", "lower");
            parser.setProperty("http://cyberneko.org/html/properties/names/attrs", "lower");
            parser.parse(new InputSource(scriptStream));
        } catch(Exception e) {
            final ScriptException se = new ScriptException("Error parsing script " + scriptPath);
            se.initCause(e);
            throw se;
        }
        final Document template = parser.getDocument();

        // compute default rendering
        final StringWriter defaultRendering = new StringWriter();
        if(n!=null) {
            final PrintWriter pw = new PrintWriter(defaultRendering);
            htmlRenderer.render(pw, r, n, pageTitle);
            pw.flush();
        }

        // compute currentNode values in JSON format
        final StringWriter jsonData = new StringWriter();
        if(n != null) {
            final PrintWriter pw = new PrintWriter(jsonData);
            final JsonItemWriter j = new JsonItemWriter(null);
            final int maxRecursionLevels = 1;
            pw.print("var currentNode=");
            j.dump(n, pw, maxRecursionLevels);
            pw.print(";");
            pw.flush();
        }

        // run XSLT transform on script, passing parameter
        // for our computed values
        final String xslt = "/xslt/script-transform.xsl";
        InputStream xslTransform = getClass().getResourceAsStream(xslt);
        if(xslTransform == null) {
            throw new ScriptException("XSLT transform " + xslt + " not found");
        }
        try {
            final TransformerFactory tf = TransformerFactory.newInstance();
            final Transformer t = tf.newTransformer(new StreamSource(xslTransform));
            t.setParameter("pageTitle", pageTitle);
            t.setParameter("slingScriptPath", fullPath(request,SLING_JS_PATH));
            t.setParameter("jstScriptPath", fullPath(request, scriptPath + ".jst.js"));
            t.setParameter("defaultRendering", defaultRendering.toString());
            t.setParameter("jsonData", jsonData.toString());
            final Result result = new StreamResult(output);
            final DOMSource source = new DOMSource(template);
            t.transform(source, result);

        } catch (Exception e) {
            final ScriptException se = new ScriptException("Error in XSLT transform for " + scriptPath);
            se.initCause(e);
            throw se;

        } finally {
            xslTransform.close();
        }
View Full Code Here

Examples of javax.script.ScriptException

            htmlGenerator.generateHtml(helper.getRequest(),
                    helper.getScript().getScriptResource().getPath(), scriptStream,
                    helper.getResponse().getWriter());
                   
        } catch (IOException ioe) {
            throw new ScriptException(ioe);
           
        } catch(RepositoryException re) {
            throw new ScriptException(re);
           
        } catch(JSONException je) {
            throw new ScriptException(je);
           
        } finally {
            if(scriptStream != null) {
                try {
                    scriptStream.close();
View Full Code Here

Examples of javax.script.ScriptException

            if (session == null) {
                session = repository.loginAdministrative(null);
            }
            Node node = deepCreateNode(outDir, session, "sling:Folder");
            if (node == null) {
                throw new ScriptException("Unable to create node " + outDir);
            }
            return JcrFS.create(node);
        }
        catch (RepositoryException e) {
            throw (ScriptException) new ScriptException("Unable to create node " + outDir).initCause(e);
        }
    }
View Full Code Here

Examples of javax.script.ScriptException

        .getBindings(ScriptContext.ENGINE_SCOPE);

    SlingScriptHelper helper = (SlingScriptHelper) bindings
        .get(SlingBindings.SLING);
    if (helper == null) {
      throw new ScriptException("SlingScriptHelper missing from bindings");
    }

    // ensure GET request
    if (helper.getRequest() != null && !"GET".equals(helper.getRequest().getMethod())) {
      throw new ScriptException(
          "Python scripting only supports GET requests");
    }

    final ClassLoader oldClassLoader = Thread.currentThread()
        .getContextClassLoader();
    try {
      Thread.currentThread().setContextClassLoader(
          getClass().getClassLoader());
      StringBuffer scriptString = new StringBuffer();
      BufferedReader bufferedScript = new BufferedReader(script);
      String nextLine = bufferedScript.readLine();
      String newLine = System.getProperty("line.separator");
      while (nextLine != null) {
        scriptString.append(nextLine);
        scriptString.append(newLine);
        nextLine = bufferedScript.readLine();
      }

      // set writer
      interp.setOut(scriptContext.getWriter());
      interp.setErr(scriptContext.getErrorWriter());
     
      // set all bindings
      for (Object entryObj : bindings.entrySet()) {
        Map.Entry<?, ?> entry = (Map.Entry<?, ?>) entryObj;
        interp.set((String) entry.getKey(), entry.getValue());
      }

      // execute Python code
      interp.exec(scriptString.toString());

    } catch (Throwable t) {
      final ScriptException ex = new ScriptException(
          "Failure running Python script:" + t);
      ex.initCause(t);
      throw ex;
    } finally {
      Thread.currentThread().setContextClassLoader(oldClassLoader);
    }
    return null;
View Full Code Here

Examples of javax.script.ScriptException

    private static SlingScriptHelper getScriptHelper(ScriptContext context) throws ScriptException {
        SlingBindings bindings = new SlingBindings();
        bindings.putAll(context.getBindings(ScriptContext.ENGINE_SCOPE));
        SlingScriptHelper scriptHelper = bindings.getSling();
        if (scriptHelper == null) {
            throw new ScriptException("Error retrieving Sling script helper from script context");
        }
        return scriptHelper;
    }
View Full Code Here

Examples of javax.script.ScriptException

    public Object eval(Reader reader, ScriptContext ctx) throws ScriptException {
        Template template = null;
        try {
            template = templateEngine.createTemplate(reader);
        } catch (IOException e) {
            throw new ScriptException("Unable to compile GSP script: " + e.getMessage());
        } catch (ClassNotFoundException e) {
            throw new ScriptException("Unable to compile GSP script: " + e.getMessage());
        }

        Bindings bindings = ctx.getBindings(ScriptContext.ENGINE_SCOPE);

        Writable result = template.make(bindings);

        try {
            result.writeTo(ctx.getWriter());
        } catch (IOException e) {
            throw new ScriptException("Unable to write result of script execution: " + e.getMessage());
        }

        return null;
    }
View Full Code Here

Examples of javax.script.ScriptException

  public Object eval(Reader reader, ScriptContext scriptContext) throws ScriptException {
    Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
    SlingScriptHelper helper = (SlingScriptHelper) bindings.get(SlingBindings.SLING);
    if (helper == null) {
      throw new ScriptException("SlingScriptHelper missing from bindings");
    }
   
    String scriptName = helper.getScript().getScriptResource().getPath();
   
    try {
      XplBuilder xplBuilder = new XplBuilder();
      Pipeline xpl = (Pipeline) xplBuilder.build(reader);
      xpl.getEnv().setSling(helper);
      xpl.eval();
    } catch (Throwable t) {
      log.error("Failure running XProc script.", t);
      final ScriptException se = new ScriptException("Failure running XProc script " + scriptName);
      se.initCause(t);
      throw se;
    }
   
    return null;
  }
View Full Code Here

Examples of javax.script.ScriptException

    public Object eval(Reader reader, ScriptContext scriptContext)
            throws ScriptException {
        Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
        SlingScriptHelper helper = (SlingScriptHelper) bindings.get(SlingBindings.SLING);
        if (helper == null) {
            throw new ScriptException("SlingScriptHelper missing from bindings");
        }

        // ensure GET request
        if (!"GET".equals(helper.getRequest().getMethod())) {
            throw new ScriptException(
                "FreeMarker templates only support GET requests");
        }

        String scriptName = helper.getScript().getScriptResource().getPath();

        try {
            Template tmpl = new Template(scriptName, reader, configuration);
            bindings.put("currentNode", new NodeModel((Node) bindings.get("currentNode")));
            tmpl.process(bindings, scriptContext.getWriter());
        } catch (Throwable t) {
            log.error("Failure running Freemarker script.", t);
            throw new ScriptException("Failure running FreeMarker script "
                + scriptName);
        }

        return null;
    }
View Full Code Here

Examples of javax.script.ScriptException

    public Object eval(final Reader reader, final ScriptContext scriptContext) throws ScriptException {
        final Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
        final SlingScriptHelper helper = (SlingScriptHelper) bindings.get(SlingBindings.SLING);

        if (helper == null) {
            throw new ScriptException("SlingScriptHelper missing from bindings");
        }

        final SlingHttpServletRequest request = helper.getRequest();
        final SlingHttpServletResponse response = helper.getResponse();
        final ServletContext servletContext = null; // only used by Thymeleaf's ServletContextResourceResolver

        final Locale locale = helper.getResponse().getLocale();
        final String scriptName = helper.getScript().getScriptResource().getPath();

        try {
            final IContext context = new SlingWebContext(request, response, servletContext, locale, bindings);
            thymeleafScriptEngineFactory.getTemplateEngine().process(scriptName, context, scriptContext.getWriter());
        } catch (Exception e) {
            logger.error("Failure rendering Thymeleaf template '{}': {}", scriptName, e.getMessage());
            throw new ScriptException(e);
        }

        return null;
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.