Search in sources :

Example 91 with ScriptContext

use of javax.script.ScriptContext in project xwiki-platform by xwiki.

the class DBTreeListClass method displayTree.

private String displayTree(String name, String prefix, List<String> selectlist, String mode, XWikiContext context) {
    ScriptContextManager scriptManager = Utils.getComponent(ScriptContextManager.class);
    ScriptContext scontext = scriptManager.getCurrentScriptContext();
    Map<String, ListItem> map = getMap(context);
    Map<String, List<ListItem>> treemap = getTreeMap(context);
    scontext.setAttribute("selectlist", selectlist, ScriptContext.ENGINE_SCOPE);
    scontext.setAttribute("fieldname", prefix + name, ScriptContext.ENGINE_SCOPE);
    scontext.setAttribute("tree", map, ScriptContext.ENGINE_SCOPE);
    scontext.setAttribute("treelist", getTreeList(treemap, map, context), ScriptContext.ENGINE_SCOPE);
    scontext.setAttribute("treemap", treemap, ScriptContext.ENGINE_SCOPE);
    scontext.setAttribute("mode", mode, ScriptContext.ENGINE_SCOPE);
    return context.getWiki().parseTemplate("treeview.vm", context);
}
Also used : ScriptContext(javax.script.ScriptContext) ArrayList(java.util.ArrayList) List(java.util.List) ScriptContextManager(org.xwiki.script.ScriptContextManager)

Example 92 with ScriptContext

use of javax.script.ScriptContext in project xwiki-platform by xwiki.

the class ComputedFieldClass method displayView.

@Override
public void displayView(StringBuffer buffer, String name, String prefix, BaseCollection object, XWikiContext context) {
    String script = getScript();
    try {
        ScriptContext scontext = Utils.getComponent(ScriptContextManager.class).getCurrentScriptContext();
        scontext.setAttribute("name", name, ScriptContext.ENGINE_SCOPE);
        scontext.setAttribute("prefix", prefix, ScriptContext.ENGINE_SCOPE);
        scontext.setAttribute("object", new com.xpn.xwiki.api.Object((BaseObject) object, context), ScriptContext.ENGINE_SCOPE);
        XWikiDocument classDocument = object.getXClass(context).getOwnerDocument();
        String result = renderContentInContext(script, classDocument.getSyntax().toIdString(), classDocument.getAuthorReference(), context);
        buffer.append(result);
    } catch (Exception e) {
        // TODO: append a rendering style complete error instead
        buffer.append(e.getMessage());
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ScriptContext(javax.script.ScriptContext) ScriptContextManager(org.xwiki.script.ScriptContextManager) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 93 with ScriptContext

use of javax.script.ScriptContext in project xwiki-platform by xwiki.

the class XWiki method include.

public String include(String topic, boolean isForm, XWikiContext context) throws XWikiException {
    String database = null, incdatabase = null;
    String prefixedTopic, localTopic;
    // Save current documents in script context
    Document currentAPIdoc = null, currentAPIcdoc = null, currentAPItdoc = null;
    ScriptContextManager scritContextManager = Utils.getComponent(ScriptContextManager.class);
    ScriptContext scontext = scritContextManager.getScriptContext();
    String currentDocName = context.getWikiId() + ":" + context.getDoc().getFullName();
    if (scontext != null) {
        currentAPIdoc = (Document) scontext.getAttribute("doc");
        currentAPIcdoc = (Document) scontext.getAttribute("cdoc");
        currentAPItdoc = (Document) scontext.getAttribute("tdoc");
    }
    try {
        int i0 = topic.indexOf(':');
        if (i0 != -1) {
            incdatabase = topic.substring(0, i0);
            database = context.getWikiId();
            context.setWikiId(incdatabase);
            prefixedTopic = topic;
            localTopic = topic.substring(i0 + 1);
        } else {
            prefixedTopic = context.getWikiId() + ":" + topic;
            localTopic = topic;
        }
        XWikiDocument doc = null;
        try {
            LOGGER.debug("Including Topic " + topic);
            try {
                @SuppressWarnings("unchecked") Set<String> includedDocs = (Set<String>) context.get("included_docs");
                if (includedDocs == null) {
                    includedDocs = new HashSet<String>();
                    context.put("included_docs", includedDocs);
                }
                if (includedDocs.contains(prefixedTopic) || currentDocName.equals(prefixedTopic)) {
                    LOGGER.warn("Error on too many recursive includes for topic " + topic);
                    return "Cannot make recursive include";
                }
                includedDocs.add(prefixedTopic);
            } catch (Exception e) {
            }
            // Get document to include
            DocumentReference targetDocumentReference = getCurrentMixedDocumentReferenceResolver().resolve(localTopic);
            doc = getDocument(targetDocumentReference, context);
            if (checkAccess("view", doc, context) == false) {
                throw new XWikiException(XWikiException.MODULE_XWIKI_ACCESS, XWikiException.ERROR_XWIKI_ACCESS_DENIED, "Access to this document is denied: " + doc);
            }
        } catch (XWikiException e) {
            LOGGER.warn("Exception Including Topic " + topic, e);
            return "Topic " + topic + " does not exist";
        }
        XWikiDocument contentdoc = doc.getTranslatedDocument(context);
        String result;
        if (isForm) {
            // We do everything in the context of the including document
            if (database != null) {
                context.setWikiId(database);
            }
            // Note: the Script macro in the new rendering checks for programming rights for the document in
            // the xwiki context.
            result = getRenderedContent(contentdoc, (XWikiDocument) context.get("doc"), context);
        } else {
            // We stay in the included document context
            // Since the Script macro checks for programming rights in the current document, we need to
            // temporarily set the contentdoc as the current doc before rendering it.
            XWikiDocument originalDoc = null;
            try {
                originalDoc = context.getDoc();
                context.put("doc", doc);
                result = getRenderedContent(contentdoc, doc, context);
            } finally {
                context.put("doc", originalDoc);
            }
        }
        try {
            @SuppressWarnings("unchecked") Set<String> includedDocs = (Set<String>) context.get("included_docs");
            if (includedDocs != null) {
                includedDocs.remove(prefixedTopic);
            }
        } catch (Exception e) {
        }
        return result;
    } finally {
        if (database != null) {
            context.setWikiId(database);
        }
        if (currentAPIdoc != null) {
            if (scontext != null) {
                scontext.setAttribute("doc", currentAPIdoc, ScriptContext.ENGINE_SCOPE);
            }
        }
        if (currentAPIcdoc != null) {
            if (scontext != null) {
                scontext.setAttribute("cdoc", currentAPIcdoc, ScriptContext.ENGINE_SCOPE);
            }
        }
        if (currentAPItdoc != null) {
            if (scontext != null) {
                scontext.setAttribute("tdoc", currentAPItdoc, ScriptContext.ENGINE_SCOPE);
            }
        }
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) ScriptContext(javax.script.ScriptContext) ParseGroovyFromString(com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString) IncludeServletAsString(com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString) XWikiDeletedDocument(com.xpn.xwiki.doc.XWikiDeletedDocument) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) Document(com.xpn.xwiki.api.Document) ScriptContextManager(org.xwiki.script.ScriptContextManager) WikiManagerException(org.xwiki.wiki.manager.WikiManagerException) IOException(java.io.IOException) JobException(org.xwiki.job.JobException) ParseException(org.xwiki.rendering.parser.ParseException) QueryException(org.xwiki.query.QueryException) URIException(org.apache.commons.httpclient.URIException) InvocationTargetException(java.lang.reflect.InvocationTargetException) HibernateException(org.hibernate.HibernateException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) NamingException(javax.naming.NamingException) FileNotFoundException(java.io.FileNotFoundException) MalformedURLException(java.net.MalformedURLException) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DocumentReference(org.xwiki.model.reference.DocumentReference) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference)

Example 94 with ScriptContext

use of javax.script.ScriptContext in project xwiki-platform by xwiki.

the class CreateAction method render.

@Override
public String render(XWikiContext context) throws XWikiException {
    CreateActionRequestHandler handler = new CreateActionRequestHandler(context);
    // Read the request and extract the passed information.
    handler.processRequest();
    // Save the determined values so we have them available in the action template.
    ScriptContext scontext = getCurrentScriptContext();
    scontext.setAttribute(SPACE_REFERENCE, handler.getSpaceReference(), ScriptContext.ENGINE_SCOPE);
    scontext.setAttribute(NAME, handler.getName(), ScriptContext.ENGINE_SCOPE);
    scontext.setAttribute(IS_SPACE, handler.isSpace(), ScriptContext.ENGINE_SCOPE);
    // put the available templates on the context, for the .vm to not compute them again
    scontext.setAttribute("availableTemplateProviders", handler.getAvailableTemplateProviders(), ScriptContext.ENGINE_SCOPE);
    scontext.setAttribute("recommendedTemplateProviders", handler.getRecommendedTemplateProviders(), ScriptContext.ENGINE_SCOPE);
    DocumentReference newDocumentReference = handler.getNewDocumentReference();
    if (newDocumentReference == null) {
        // There is information still missing, go back to the template and fill it.
        return CREATE_TEMPLATE;
    }
    // Check if the creation in the spaceReference is allowed.
    if (!handler.isTemplateProviderAllowedToCreateInCurrentSpace()) {
        // something else.
        return CREATE_TEMPLATE;
    }
    // Checking the rights to create the new document.
    // Note: Note checking the logical spaceReference, but the space of the final actual document reference, since
    // that is where we are creating the new document.
    checkRights(newDocumentReference.getLastSpaceReference(), context);
    // Check if the document to create already exists.
    XWikiDocument newDocument = context.getWiki().getDocument(newDocumentReference, context);
    if (handler.isDocumentAlreadyExisting(newDocument)) {
        return CREATE_TEMPLATE;
    }
    // TODO: handle this type in doCreate() that we call above (see: https://jira.xwiki.org/browse/XWIKI-12585).
    if (StringUtils.isBlank(handler.getType()) && !handler.hasTemplate()) {
        return CREATE_TEMPLATE;
    }
    // create is finally valid, can be executed
    doCreate(context, newDocument, handler.isSpace(), handler.getTemplateProvider());
    return null;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ScriptContext(javax.script.ScriptContext) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 95 with ScriptContext

use of javax.script.ScriptContext in project xwiki-platform by xwiki.

the class XWikiValidationStatus method addError.

public void addError(String className, String propName, String propPrettyName, String validationMessage, XWikiContext context) {
    getErrorObjects().add(className);
    getPropertyErrors().add(propName);
    if ((validationMessage != null) && (!validationMessage.trim().equals(""))) {
        getErrors().add(validationMessage);
    } else {
        ScriptContextManager scriptManager = Utils.getComponent(ScriptContextManager.class);
        ScriptContext scontext = scriptManager.getCurrentScriptContext();
        if (scontext == null) {
            getErrors().add("Validation error for property " + propPrettyName + " in class " + className);
        } else {
            scontext.setAttribute("className", className, ScriptContext.ENGINE_SCOPE);
            scontext.setAttribute("propName", propPrettyName, ScriptContext.ENGINE_SCOPE);
            String message = context.getMessageTool().get("validationerror", Collections.singletonList(propName));
            getErrors().add(message);
        }
    }
}
Also used : ScriptContext(javax.script.ScriptContext) ScriptContextManager(org.xwiki.script.ScriptContextManager)

Aggregations

ScriptContext (javax.script.ScriptContext)126 SimpleScriptContext (javax.script.SimpleScriptContext)83 Bindings (javax.script.Bindings)33 Test (org.junit.Test)30 SimpleBindings (javax.script.SimpleBindings)28 Test (org.junit.jupiter.api.Test)19 ScriptEngine (javax.script.ScriptEngine)18 ScriptException (javax.script.ScriptException)18 HashMap (java.util.HashMap)13 CompiledScript (javax.script.CompiledScript)11 IOException (java.io.IOException)9 Map (java.util.Map)8 ScriptEngineManager (javax.script.ScriptEngineManager)8 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)7 Test (org.testng.annotations.Test)7 StringWriter (java.io.StringWriter)6 NashornScriptEngine (jdk.nashorn.api.scripting.NashornScriptEngine)6 XWikiException (com.xpn.xwiki.XWikiException)5 Reader (java.io.Reader)5 StringReader (java.io.StringReader)5