Search in sources :

Example 1 with MacroExecutionException

use of org.xwiki.rendering.macro.MacroExecutionException in project xwiki-platform by xwiki.

the class OfficeMacro method getResourceReference.

private ResourceReference getResourceReference(MacroBlock block, OfficeMacroParameters parameters) throws MacroExecutionException {
    ResourceReference resourceReference = parameters.getReference();
    // Make sure to provide full reference for attachment
    if (resourceReference == null || resourceReference.getType().equals(ResourceType.ATTACHMENT) || !resourceReference.isTyped()) {
        AttachmentReference attachmentReference;
        if (resourceReference == null) {
            // Support former way of indicating the file to view
            String reference = parameters.getAttachment();
            if (StringUtils.isEmpty(reference)) {
                throw new MacroExecutionException("You must specify the 'reference' parameter pointing to the office file to display.");
            }
            attachmentReference = this.macroAttachmentReferenceResolver.resolve(reference, block);
        } else {
            attachmentReference = this.macroAttachmentReferenceResolver.resolve(resourceReference.getReference(), block);
        }
        resourceReference = new AttachmentResourceReference(this.serializer.serialize(attachmentReference));
    }
    return resourceReference;
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) AttachmentResourceReference(org.xwiki.rendering.listener.reference.AttachmentResourceReference) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) AttachmentResourceReference(org.xwiki.rendering.listener.reference.AttachmentResourceReference) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference)

Example 2 with MacroExecutionException

use of org.xwiki.rendering.macro.MacroExecutionException in project xwiki-platform by xwiki.

the class OfficeMacro method execute.

@Override
public List<Block> execute(OfficeMacroParameters parameters, String content, MacroTransformationContext context) throws MacroExecutionException {
    // Check if the office server is started and if not, generate an error.
    if (!this.officeServer.getState().equals(OfficeServer.ServerState.CONNECTED)) {
        throw new MacroExecutionException("The wiki needs to be connected to an office server in order to view " + "office files. Ask your administrator to configure such a server.");
    }
    ResourceReference resourceReference = getResourceReference(context.getCurrentMacroBlock(), parameters);
    Map<String, Object> viewParameters = new HashMap<String, Object>();
    viewParameters.put("filterStyles", parameters.isFilterStyles());
    viewParameters.put("ownerDocument", getOwnerDocument(context.getCurrentMacroBlock()));
    try {
        return this.officeViewer.createView(resourceReference, viewParameters).getChildren();
    } catch (Exception e) {
        throw new MacroExecutionException("Failed to view office attachment.", e);
    }
}
Also used : HashMap(java.util.HashMap) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) AttachmentResourceReference(org.xwiki.rendering.listener.reference.AttachmentResourceReference) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException)

Example 3 with MacroExecutionException

use of org.xwiki.rendering.macro.MacroExecutionException in project xwiki-platform by xwiki.

the class DashboardMacro method execute.

@Override
public List<Block> execute(DashboardMacroParameters parameters, String content, MacroTransformationContext context) throws MacroExecutionException {
    // We don't allow calling the Dashboard macro inside the Dashboard macro to prevent recursions!
    preventDashboardRecursion();
    // get the gadgets from the objects
    List<Gadget> gadgets;
    try {
        gadgets = this.gadgetSource.getGadgets(parameters.getSource(), context);
    } catch (Exception e) {
        String message = "Could not get the gadgets.";
        // log and throw further
        this.logger.error(message, e);
        throw new MacroExecutionException(message, e);
    }
    boolean isInEditMode = this.gadgetSource.isEditing();
    DashboardRenderer renderer = getDashboardRenderer(StringUtils.isEmpty(parameters.getLayout()) ? "columns" : parameters.getLayout());
    if (renderer == null) {
        String message = "Could not find dashboard renderer " + parameters.getLayout();
        // log and throw further
        this.logger.error(message);
        throw new MacroExecutionException(message);
    }
    GadgetRenderer gadgetRenderer = getGadgetRenderer(isInEditMode);
    // else, layout
    List<Block> layoutedResult;
    try {
        layoutedResult = renderer.renderGadgets(gadgets, gadgetRenderer, context);
    } catch (Exception e) {
        String message = "Could not render the gadgets for layout " + parameters.getLayout();
        // log and throw further
        this.logger.error(message, e);
        throw new MacroExecutionException(message, e);
    }
    // include the css and js for this macro. here so that it's included after any dependencies have included their
    // css, so that it cascades properly
    this.includeResources(isInEditMode);
    // put everything in a nice toplevel group for this dashboard, to be able to add classes to it
    GroupBlock topLevel = new GroupBlock();
    // mode
    if (isInEditMode) {
        topLevel.addChildren(this.gadgetSource.getDashboardSourceMetadata(parameters.getSource(), context));
    }
    topLevel.addChildren(layoutedResult);
    // add the style attribute of the dashboard macro as a class to the toplevel container
    topLevel.setParameter("class", MACRO_NAME + (StringUtils.isEmpty(parameters.getStyle()) ? "" : " " + parameters.getStyle()));
    // Reduce by 1 the recursive count so that we can have several dashboard macros rendered in the same context
    reduceDashboardRecursionCounter();
    return Collections.<Block>singletonList(topLevel);
}
Also used : Gadget(org.xwiki.rendering.macro.dashboard.Gadget) DashboardRenderer(org.xwiki.rendering.macro.dashboard.DashboardRenderer) GadgetRenderer(org.xwiki.rendering.macro.dashboard.GadgetRenderer) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) GroupBlock(org.xwiki.rendering.block.GroupBlock) Block(org.xwiki.rendering.block.Block) GroupBlock(org.xwiki.rendering.block.GroupBlock) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException)

Example 4 with MacroExecutionException

use of org.xwiki.rendering.macro.MacroExecutionException in project xwiki-platform by xwiki.

the class DisplayMacroTest method testDisplayMacroWithNoDocumentSpecified.

@Test
public void testDisplayMacroWithNoDocumentSpecified() throws Exception {
    DisplayMacroParameters parameters = new DisplayMacroParameters();
    try {
        this.displayMacro.execute(parameters, null, createMacroTransformationContext("whatever", false));
        Assert.fail("An exception should have been thrown");
    } catch (MacroExecutionException expected) {
        Assert.assertEquals("You must specify a 'reference' parameter pointing to the entity to display.", expected.getMessage());
    }
}
Also used : MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) DisplayMacroParameters(org.xwiki.rendering.macro.display.DisplayMacroParameters) Test(org.junit.Test)

Example 5 with MacroExecutionException

use of org.xwiki.rendering.macro.MacroExecutionException in project xwiki-platform by xwiki.

the class DisplayMacroTest method testDisplayMacroWithRecursiveDisplay.

@Test
public void testDisplayMacroWithRecursiveDisplay() throws Exception {
    final DocumentDisplayer mockDocumentDisplayer = getMockery().mock(DocumentDisplayer.class);
    this.displayMacro.setDocumentAccessBridge(mockSetup.bridge);
    FieldUtils.writeField(this.displayMacro, "documentDisplayer", mockDocumentDisplayer, true);
    final MacroTransformationContext macroContext = createMacroTransformationContext("wiki:space.page", false);
    final DisplayMacroParameters parameters = new DisplayMacroParameters();
    parameters.setReference("wiki:space.page");
    getMockery().checking(new Expectations() {

        {
            allowing(mockDocumentReferenceResolver).resolve("wiki:space.page", macroContext.getCurrentMacroBlock());
            will(returnValue(new DocumentReference("wiki", "space", "page")));
            allowing(mockSetup.bridge).isDocumentViewable(with(any(DocumentReference.class)));
            will(returnValue(true));
            allowing(mockSetup.bridge).getDocumentInstance(with(any(DocumentReference.class)));
            will(returnValue(null));
            allowing(mockDocumentDisplayer).display(with(same((DocumentModelBridge) null)), with(any(DocumentDisplayerParameters.class)));
            will(new CustomAction("recursively call the include macro again") {

                @Override
                public Object invoke(Invocation invocation) throws Throwable {
                    try {
                        displayMacro.execute(parameters, null, macroContext);
                    } catch (Exception expected) {
                        if (expected.getMessage().contains("Found recursive display")) {
                            throw new ExpectedRecursiveInclusionException();
                        }
                    }
                    return true;
                }
            });
        }
    });
    try {
        this.displayMacro.execute(parameters, null, macroContext);
        Assert.fail("The display macro hasn't checked the recursive display");
    } catch (MacroExecutionException expected) {
        if (!(expected.getCause() instanceof ExpectedRecursiveInclusionException)) {
            throw expected;
        }
    }
}
Also used : Expectations(org.jmock.Expectations) DocumentDisplayerParameters(org.xwiki.display.internal.DocumentDisplayerParameters) DocumentDisplayer(org.xwiki.display.internal.DocumentDisplayer) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) Invocation(org.jmock.api.Invocation) CustomAction(org.jmock.lib.action.CustomAction) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) DisplayMacroParameters(org.xwiki.rendering.macro.display.DisplayMacroParameters) DocumentReference(org.xwiki.model.reference.DocumentReference) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) Test(org.junit.Test)

Aggregations

MacroExecutionException (org.xwiki.rendering.macro.MacroExecutionException)48 Test (org.junit.Test)12 Block (org.xwiki.rendering.block.Block)12 MacroTransformationContext (org.xwiki.rendering.transformation.MacroTransformationContext)10 DocumentReference (org.xwiki.model.reference.DocumentReference)9 DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)7 MacroBlock (org.xwiki.rendering.block.MacroBlock)7 XDOM (org.xwiki.rendering.block.XDOM)7 MetaDataBlock (org.xwiki.rendering.block.MetaDataBlock)6 Expectations (org.jmock.Expectations)5 DocumentDisplayerParameters (org.xwiki.display.internal.DocumentDisplayerParameters)5 StringReader (java.io.StringReader)4 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)4 ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)4 IncludeMacroParameters (org.xwiki.rendering.macro.include.IncludeMacroParameters)4 HashMap (java.util.HashMap)3 AttachmentReference (org.xwiki.model.reference.AttachmentReference)3 GroupBlock (org.xwiki.rendering.block.GroupBlock)3 MacroMarkerBlock (org.xwiki.rendering.block.MacroMarkerBlock)3 TableBlock (org.xwiki.rendering.block.TableBlock)3