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;
}
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);
}
}
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);
}
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());
}
}
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;
}
}
}
Aggregations