Search in sources :

Example 76 with ComponentLookupException

use of org.xwiki.component.manager.ComponentLookupException in project xwiki-platform by xwiki.

the class XDOMOfficeDocument method renderTitle.

/**
 * Utility method for rendering a title.
 *
 * @param header header block which contains the title.
 * @return header block content rendered as a string.
 */
private String renderTitle(HeaderBlock header) {
    try {
        WikiPrinter printer = new DefaultWikiPrinter();
        BlockRenderer renderer = this.componentManager.getInstance(BlockRenderer.class, "plain/1.0");
        renderer.render(header, printer);
        return printer.toString();
    } catch (ComponentLookupException ex) {
    // Ignore.
    }
    return null;
}
Also used : DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) BlockRenderer(org.xwiki.rendering.renderer.BlockRenderer)

Example 77 with ComponentLookupException

use of org.xwiki.component.manager.ComponentLookupException in project xwiki-platform by xwiki.

the class WikiMacrosTest method testSaveWikiMacro.

@Test
public void testSaveWikiMacro() throws Exception {
    when(this.oldcore.getMockRightService().hasAccessLevel(any(String.class), any(String.class), any(String.class), any(XWikiContext.class))).thenReturn(true);
    when(this.oldcore.getMockRightService().hasWikiAdminRights(any(XWikiContext.class))).thenReturn(true);
    when(this.oldcore.getMockRightService().hasProgrammingRights(any(XWikiContext.class))).thenReturn(true);
    this.macroObject.setStringValue("visibility", "Current Wiki");
    // Save wiki macro
    this.oldcore.getSpyXWiki().saveDocument(this.macroDocument, this.oldcore.getXWikiContext());
    Macro testMacro = getWikiComponentManager().getInstance(Macro.class, "macroid");
    Assert.assertEquals("macroid", testMacro.getDescriptor().getId().getId());
    try {
        testMacro = this.oldcore.getMocker().getInstance(Macro.class, "macroid");
        Assert.fail("Found macro with wiki visibility in global componenet manager");
    } catch (ComponentLookupException expected) {
    }
}
Also used : Macro(org.xwiki.rendering.macro.Macro) XWikiContext(com.xpn.xwiki.XWikiContext) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) Test(org.junit.Test)

Example 78 with ComponentLookupException

use of org.xwiki.component.manager.ComponentLookupException in project xwiki-platform by xwiki.

the class WikiMacrosTest method testUnRegisterWikiMacroWithDifferentVisibilityKeys.

@Test
public void testUnRegisterWikiMacroWithDifferentVisibilityKeys() throws Exception {
    when(this.oldcore.getMockRightService().hasAccessLevel(any(String.class), any(String.class), any(String.class), any(XWikiContext.class))).thenReturn(true);
    this.macroObject.setStringValue("visibility", "Current User");
    DocumentReference user1 = new DocumentReference("wiki", "Wiki", "user1");
    this.macroDocument.setAuthorReference(user1);
    // Save wiki macro
    this.oldcore.getSpyXWiki().saveDocument(this.macroDocument, this.oldcore.getXWikiContext());
    // Try to lookup the macro
    this.oldcore.getXWikiContext().setUserReference(user1);
    Macro testMacro = getUserComponentManager().getInstance(Macro.class, "macroid");
    Assert.assertEquals("macroid", testMacro.getDescriptor().getId().getId());
    // register with another user
    DocumentReference user2 = new DocumentReference("wiki", "Wiki", "user2");
    this.macroDocument.setAuthorReference(user2);
    // Save wiki macro
    this.oldcore.getSpyXWiki().saveDocument(this.macroDocument, this.oldcore.getXWikiContext());
    // Try to lookup the macro
    this.oldcore.getXWikiContext().setUserReference(user2);
    testMacro = getUserComponentManager().getInstance(Macro.class, "macroid");
    Assert.assertEquals("macroid", testMacro.getDescriptor().getId().getId());
    // validate that the macro as been properly unregistered for former user
    this.oldcore.getXWikiContext().setUserReference(user1);
    try {
        testMacro = getUserComponentManager().getInstance(Macro.class, "macroid");
        Assert.fail("The macro has not been properly unregistered");
    } catch (ComponentLookupException expected) {
    }
}
Also used : Macro(org.xwiki.rendering.macro.Macro) XWikiContext(com.xpn.xwiki.XWikiContext) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 79 with ComponentLookupException

use of org.xwiki.component.manager.ComponentLookupException in project xwiki-platform by xwiki.

the class DefaultWikiMacro method execute.

@Override
public List<Block> execute(WikiMacroParameters parameters, String macroContent, MacroTransformationContext context) throws MacroExecutionException {
    validate(parameters, macroContent);
    // Parse the wiki macro content.
    XDOM xdom = prepareWikiMacroContent(context);
    // Prepare macro context.
    Map<String, Object> macroBinding = new HashMap<>();
    macroBinding.put(MACRO_PARAMS_KEY, parameters);
    macroBinding.put(MACRO_CONTENT_KEY, macroContent);
    macroBinding.put(MACRO_DESCRIPTOR_KEY, getDescriptor());
    macroBinding.put(MACRO_CONTEXT_KEY, context);
    macroBinding.put(MACRO_RESULT_KEY, null);
    // Extension point to add more wiki macro bindings
    try {
        List<WikiMacroBindingInitializer> bindingInitializers = this.componentManager.getInstanceList(WikiMacroBindingInitializer.class);
        for (WikiMacroBindingInitializer bindingInitializer : bindingInitializers) {
            bindingInitializer.initialize(this.macroDocumentReference, parameters, macroContent, context, macroBinding);
        }
    } catch (ComponentLookupException e) {
    // TODO: we should probably log something but that should never happen
    }
    // Execute the macro
    ObservationManager observation = null;
    try {
        observation = this.componentManager.getInstance(ObservationManager.class);
    } catch (ComponentLookupException e) {
    // TODO: maybe log something
    }
    // Get XWiki context
    Map<String, Object> xwikiContext = null;
    try {
        Execution execution = this.componentManager.getInstance(Execution.class);
        ExecutionContext econtext = execution.getContext();
        if (econtext != null) {
            xwikiContext = (Map<String, Object>) execution.getContext().getProperty("xwikicontext");
        }
    } catch (ComponentLookupException e) {
    // TODO: maybe log something
    }
    try {
        Transformation macroTransformation = this.componentManager.getInstance(Transformation.class, MACRO_HINT);
        if (xwikiContext != null) {
            // Place macro context inside xwiki context ($xcontext.macro).
            xwikiContext.put(MACRO_KEY, macroBinding);
        }
        MacroBlock wikiMacroBlock = context.getCurrentMacroBlock();
        MacroMarkerBlock wikiMacroMarker = new MacroMarkerBlock(wikiMacroBlock.getId(), wikiMacroBlock.getParameters(), wikiMacroBlock.getContent(), xdom.getChildren(), wikiMacroBlock.isInline());
        // Make sure to use provided metadatas
        MetaDataBlock metaDataBlock = new MetaDataBlock(Collections.<Block>singletonList(wikiMacroMarker), xdom.getMetaData());
        // Make sure the context XDOM contains the wiki macro content
        wikiMacroBlock.getParent().replaceChild(metaDataBlock, wikiMacroBlock);
        // "Emulate" the fact that wiki macro block is still part of the XDOM (what is in the XDOM is a
        // MacroMarkerBlock and MacroTransformationContext current macro block only support MacroBlock so we can't
        // switch it without breaking some APIs)
        wikiMacroBlock.setParent(metaDataBlock.getParent());
        wikiMacroBlock.setNextSiblingBlock(metaDataBlock.getNextSibling());
        wikiMacroBlock.setPreviousSiblingBlock(metaDataBlock.getPreviousSibling());
        try {
            if (observation != null) {
                observation.notify(STARTEXECUTION_EVENT, this, macroBinding);
            }
            // Perform internal macro transformations.
            TransformationContext txContext = new TransformationContext(context.getXDOM(), this.syntax);
            txContext.setId(context.getId());
            RenderingContext renderingContext = componentManager.getInstance(RenderingContext.class);
            ((MutableRenderingContext) renderingContext).transformInContext(macroTransformation, txContext, wikiMacroMarker);
        } finally {
            // Restore context XDOM to its previous state
            metaDataBlock.getParent().replaceChild(wikiMacroBlock, metaDataBlock);
        }
        return extractResult(wikiMacroMarker.getChildren(), macroBinding, context);
    } catch (Exception ex) {
        throw new MacroExecutionException("Error while performing internal macro transformations", ex);
    } finally {
        if (xwikiContext != null) {
            xwikiContext.remove(MACRO_KEY);
        }
        if (observation != null) {
            observation.notify(ENDEXECUTION_EVENT, this);
        }
    }
}
Also used : RenderingContext(org.xwiki.rendering.transformation.RenderingContext) MutableRenderingContext(org.xwiki.rendering.internal.transformation.MutableRenderingContext) Transformation(org.xwiki.rendering.transformation.Transformation) XDOM(org.xwiki.rendering.block.XDOM) HashMap(java.util.HashMap) MacroMarkerBlock(org.xwiki.rendering.block.MacroMarkerBlock) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) ObservationManager(org.xwiki.observation.ObservationManager) TransformationContext(org.xwiki.rendering.transformation.TransformationContext) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) MacroParameterException(org.xwiki.rendering.macro.parameter.MacroParameterException) Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) MutableRenderingContext(org.xwiki.rendering.internal.transformation.MutableRenderingContext) MacroBlock(org.xwiki.rendering.block.MacroBlock) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock)

Example 80 with ComponentLookupException

use of org.xwiki.component.manager.ComponentLookupException in project xwiki-platform by xwiki.

the class CascadingVfsPermissionChecker method checkPermission.

@Override
public void checkPermission(VfsResourceReference resourceReference) throws VfsException {
    // Prevent using a VFS scheme that correspond to the hint of this component
    String scheme = resourceReference.getURI().getScheme();
    if (HINT.equals(scheme)) {
        throw new VfsException(String.format("[%s] is a reserved VFS URI scheme and cannot be used.", HINT));
    }
    // Look for a scheme-specific permission checker
    VfsPermissionChecker resolvedChecker;
    ComponentManager componentManager = this.componentManagerProvider.get();
    try {
        resolvedChecker = componentManager.getInstance(VfsPermissionChecker.class, scheme);
    } catch (ComponentLookupException e) {
        // Use the Generic permission checker
        try {
            resolvedChecker = componentManager.getInstance(VfsPermissionChecker.class);
        } catch (ComponentLookupException ee) {
            throw new VfsException(String.format("No VFS Permission Checked has been found in the system. " + "Refusing access to VFS URI scheme [%s]", scheme), ee);
        }
    }
    resolvedChecker.checkPermission(resourceReference);
}
Also used : VfsException(org.xwiki.vfs.VfsException) VfsPermissionChecker(org.xwiki.vfs.VfsPermissionChecker) ComponentManager(org.xwiki.component.manager.ComponentManager) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException)

Aggregations

ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)104 ComponentManager (org.xwiki.component.manager.ComponentManager)24 Test (org.junit.Test)15 DocumentReference (org.xwiki.model.reference.DocumentReference)12 XWikiContext (com.xpn.xwiki.XWikiContext)10 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)10 InitializationException (org.xwiki.component.phase.InitializationException)8 ArrayList (java.util.ArrayList)7 XWikiException (com.xpn.xwiki.XWikiException)5 HashMap (java.util.HashMap)5 DefaultComponentDescriptor (org.xwiki.component.descriptor.DefaultComponentDescriptor)5 NamespacedComponentManager (org.xwiki.component.manager.NamespacedComponentManager)5 FilterException (org.xwiki.filter.FilterException)5 ExtendedURL (org.xwiki.url.ExtendedURL)5 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)4 Type (java.lang.reflect.Type)4 HashSet (java.util.HashSet)4 WikiComponentException (org.xwiki.component.wiki.WikiComponentException)4 ExecutionContext (org.xwiki.context.ExecutionContext)4 WikiReference (org.xwiki.model.reference.WikiReference)4