Search in sources :

Example 1 with WikiComponentRuntimeException

use of org.xwiki.component.wiki.WikiComponentRuntimeException in project xwiki-platform by xwiki.

the class DefaultWikiComponentInvocationHandler method invoke.

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Exception {
    // We look for the method in the XObjects.
    if (!this.wikiComponent.getHandledMethods().containsKey(method.getName())) {
        if (method.getDeclaringClass() == Object.class || method.getDeclaringClass() == WikiComponent.class) {
            // return ObjectMethodsProxy.invoke(proxy, method, args);
            return method.invoke(wikiComponent, args);
        } else {
            // See http://docs.oracle.com/javase/6/docs/api/java/lang/reflect/UndeclaredThrowableException.html
            throw new WikiComponentRuntimeException(String.format("You need to add an Object of type [%s] in document [%s] to implement method [%s.%s]", WikiComponentConstants.METHOD_CLASS, this.wikiComponent.getDocumentReference(), method.getDeclaringClass().getName(), method.getName()));
        }
    } else {
        WikiComponentMethodExecutor methodExecutor = componentManager.getInstance(WikiComponentMethodExecutor.class);
        Map<String, Object> methodContext = new HashMap<String, Object>();
        XDOM xdom = this.wikiComponent.getHandledMethods().get(method.getName());
        methodContext.put(METHOD_CONTEXT_COMPONENT_KEY, proxy);
        this.injectComponentDependencies(methodContext);
        return methodExecutor.execute(method, args, wikiComponent.getDocumentReference(), xdom, wikiComponent.getSyntax(), methodContext);
    }
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) HashMap(java.util.HashMap) WikiComponent(org.xwiki.component.wiki.WikiComponent) WikiComponentRuntimeException(org.xwiki.component.wiki.WikiComponentRuntimeException)

Example 2 with WikiComponentRuntimeException

use of org.xwiki.component.wiki.WikiComponentRuntimeException in project xwiki-platform by xwiki.

the class DefaultWikiComponentMethodExecutor method castRenderedContent.

/**
 * Render a XDOM and return a value converted from the rendered content. The type matches the return value of the
 * passed method.
 *
 * @param xdom The XDOM to render
 * @param method The method called
 * @return A value matching the method return type
 * @throws WikiComponentRuntimeException When the conversion fails
 */
private Object castRenderedContent(XDOM xdom, Method method) throws WikiComponentRuntimeException {
    // Since no return value has been explicitly provided, we try to convert the result of the rendering
    // into the expected return type using a Converter.
    WikiPrinter printer = new DefaultWikiPrinter();
    blockRenderer.render(xdom, printer);
    String contentResult = printer.toString();
    // Do the conversion!
    try {
        return converterManager.convert(method.getGenericReturnType(), contentResult);
    } catch (ConversionException e) {
        // Surrender!
        throw new WikiComponentRuntimeException(String.format("Failed to convert result [%s] to type [%s] for method [%s.%s]", contentResult, method.getGenericReturnType(), method.getDeclaringClass().getName(), method.getName()), e);
    }
}
Also used : ConversionException(org.xwiki.properties.converter.ConversionException) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) WikiComponentRuntimeException(org.xwiki.component.wiki.WikiComponentRuntimeException)

Example 3 with WikiComponentRuntimeException

use of org.xwiki.component.wiki.WikiComponentRuntimeException in project xwiki-platform by xwiki.

the class DefaultWikiComponentMethodExecutor method execute.

@Override
public Object execute(Method method, Object[] args, DocumentReference componentDocumentReference, XDOM xdom, Syntax syntax, Map<String, Object> methodContext) throws WikiComponentRuntimeException {
    // Prepare and put the method context in the XWiki Context
    Map<Object, Object> xwikiContext = (Map<Object, Object>) execution.getContext().getProperty("xwikicontext");
    this.prepareMethodContext(methodContext, args);
    xwikiContext.put("method", methodContext);
    // Save current context document, to put it back after the execution.
    Object contextDoc = xwikiContext.get(XWIKI_CONTEXT_DOC_KEY);
    try {
        // component document and not the context one.
        try {
            xwikiContext.put(XWIKI_CONTEXT_DOC_KEY, dab.getDocumentInstance(componentDocumentReference));
        } catch (Exception e) {
            throw new WikiComponentRuntimeException(String.format("Failed to load wiki component document [%s]", componentDocumentReference), e);
        }
        // We need to clone the xdom to avoid transforming the original and make it useless after the first
        // transformation
        XDOM transformedXDOM = xdom.clone();
        // Perform internal macro transformations
        try {
            TransformationContext transformationContext = new TransformationContext(transformedXDOM, syntax);
            transformationContext.setId(method.getClass().getName() + "#" + method.getName());
            ((MutableRenderingContext) renderingContext).transformInContext(macroTransformation, transformationContext, transformedXDOM);
        } catch (TransformationException e) {
            throw new WikiComponentRuntimeException(String.format("Error while executing wiki component macro transformation for method [%s]", method.getName()), e);
        }
        if (!method.getReturnType().getName().equals("void")) {
            if (methodContext.get(OUTPUT_KEY) != null && ((WikiMethodOutputHandler) methodContext.get(OUTPUT_KEY)).getValue() != null) {
                return method.getReturnType().cast(((WikiMethodOutputHandler) methodContext.get(OUTPUT_KEY)).getValue());
            } else {
                return this.castRenderedContent(transformedXDOM, method);
            }
        } else {
            return null;
        }
    } finally {
        if (contextDoc != null) {
            xwikiContext.put(XWIKI_CONTEXT_DOC_KEY, contextDoc);
        }
    }
}
Also used : TransformationException(org.xwiki.rendering.transformation.TransformationException) XDOM(org.xwiki.rendering.block.XDOM) HashMap(java.util.HashMap) Map(java.util.Map) TransformationContext(org.xwiki.rendering.transformation.TransformationContext) TransformationException(org.xwiki.rendering.transformation.TransformationException) WikiComponentRuntimeException(org.xwiki.component.wiki.WikiComponentRuntimeException) ConversionException(org.xwiki.properties.converter.ConversionException) WikiComponentRuntimeException(org.xwiki.component.wiki.WikiComponentRuntimeException) MutableRenderingContext(org.xwiki.rendering.internal.transformation.MutableRenderingContext)

Aggregations

WikiComponentRuntimeException (org.xwiki.component.wiki.WikiComponentRuntimeException)3 HashMap (java.util.HashMap)2 ConversionException (org.xwiki.properties.converter.ConversionException)2 XDOM (org.xwiki.rendering.block.XDOM)2 Map (java.util.Map)1 WikiComponent (org.xwiki.component.wiki.WikiComponent)1 MutableRenderingContext (org.xwiki.rendering.internal.transformation.MutableRenderingContext)1 DefaultWikiPrinter (org.xwiki.rendering.renderer.printer.DefaultWikiPrinter)1 WikiPrinter (org.xwiki.rendering.renderer.printer.WikiPrinter)1 TransformationContext (org.xwiki.rendering.transformation.TransformationContext)1 TransformationException (org.xwiki.rendering.transformation.TransformationException)1