Search in sources :

Example 16 with AttachmentReference

use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.

the class OfficeMacroImporterTest method buildXDOM.

@Test
public void buildXDOM() throws Exception {
    DocumentAccessBridge documentAccessBridge = this.mocker.getInstance(DocumentAccessBridge.class);
    DocumentReference documentReference = new DocumentReference("wiki", "space", "page");
    XWikiDocument xwikiDocument = mock(XWikiDocument.class);
    when(documentAccessBridge.getTranslatedDocumentInstance(documentReference)).thenReturn(xwikiDocument);
    when(xwikiDocument.getSyntax()).thenReturn(Syntax.XWIKI_2_1);
    XDOM xdom = this.mocker.getComponentUnderTest().buildXDOM(new AttachmentReference("file", documentReference), true);
    assertNotNull(xdom);
    // Verify we have the proper METADATA set
    assertEquals("xwiki/2.1", ((Syntax) xdom.getMetaData().getMetaData(MetaData.SYNTAX)).toIdString());
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XDOM(org.xwiki.rendering.block.XDOM) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 17 with AttachmentReference

use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.

the class FormulaMacro method render.

/**
 * Renders the formula using the specified renderer.
 *
 * @param formula the formula text
 * @param inline is the formula supposed to be used inline or as a block-level element
 * @param fontSize the specified font size
 * @param imageType the specified resulting image type
 * @param rendererHint the hint for the renderer to use
 * @return the resulting block holding the generated image, or {@code null} in case of an error.
 * @throws MacroExecutionException if no renderer exists for the passed hint or if that rendered failed to render
 *         the formula
 * @throws IllegalArgumentException if the formula is not valid, according to the LaTeX syntax
 */
private Block render(String formula, boolean inline, FontSize fontSize, Type imageType, String rendererHint) throws MacroExecutionException, IllegalArgumentException {
    try {
        FormulaRenderer renderer = this.manager.getInstance(FormulaRenderer.class, rendererHint);
        String imageName = renderer.process(formula, inline, fontSize, imageType);
        // TODO: HACK!!
        // We're going through the getAttachmentURL() API so that when the PdfURLFactory is used, the generated
        // image is saved and then embedded in the exported PDF thanks to PDFURIResolver. In the future we need
        // to remove this hack by introduce a proper Resource for generated image (say TemporaryResource),
        // implement a TemporaryResourceSerializer<URL> and introduce a ResourceLoader interface and have it
        // implemented for TemporaryResource...
        AttachmentReference attachmentReference = new AttachmentReference(imageName, this.dab.getCurrentDocumentReference());
        String url = this.dab.getAttachmentURL(attachmentReference, false);
        // Note that we have to replace the download action by the tex action since the getAttachmentURL() API
        // will use the "download" action but when the generated URL is called by the browser it needs to point to
        // the TexAction...
        url = url.replace("/download/", "/tex/");
        // TODO: end HACK!!
        ResourceReference imageReference = new ResourceReference(url, ResourceType.URL);
        ImageBlock result = new ImageBlock(imageReference, false);
        // Set the alternative text for the image to be the original formula
        result.setParameter("alt", formula);
        return result;
    } catch (Exception e) {
        throw new MacroExecutionException(String.format("Failed to render formula using the [%s] renderer", rendererHint), e);
    }
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) ImageBlock(org.xwiki.rendering.block.ImageBlock) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) FormulaRenderer(org.xwiki.formula.FormulaRenderer) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException)

Example 18 with AttachmentReference

use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.

the class XWikiAttachmentTest method testAuthorWithoutDocument.

@Test
public void testAuthorWithoutDocument() throws Exception {
    EntityReferenceSerializer<String> compactWikiEntityReferenceSerializer = this.oldcore.getMocker().getInstance(EntityReferenceSerializer.TYPE_STRING, "compactwiki");
    AttachmentReferenceResolver<String> currentAttachmentReferenceResolver = this.oldcore.getMocker().getInstance(AttachmentReferenceResolver.TYPE_STRING, "current");
    DocumentReferenceResolver<EntityReference> explicitDocumentReferenceResolver = this.oldcore.getMocker().registerMockComponent(DocumentReferenceResolver.TYPE_REFERENCE, "explicit");
    EntityReferenceResolver<String> xclassEntityReferenceResolver = this.oldcore.getMocker().registerMockComponent(EntityReferenceResolver.TYPE_STRING, "xclass");
    XWikiAttachment attachment = new XWikiAttachment(null, "filename");
    DocumentReference currentDocumentReference = new DocumentReference("currentWiki", "currentSpage", "currentPage");
    AttachmentReference attachmentReference = new AttachmentReference(attachment.getFilename(), currentDocumentReference);
    // getAuthor() based on getAuthorReference()
    DocumentReference userReference = new DocumentReference("userwiki", "userspace", "userpage");
    attachment.setAuthorReference(userReference);
    assertEquals(userReference, attachment.getAuthorReference());
    when(currentAttachmentReferenceResolver.resolve(attachment.getFilename())).thenReturn(attachmentReference);
    when(compactWikiEntityReferenceSerializer.serialize(userReference, attachmentReference)).thenReturn("stringUserReference");
    assertEquals("stringUserReference", attachment.getAuthor());
    // getAuthorReference() based on getAuthor()
    attachment.setAuthor("author");
    assertEquals("author", attachment.getAuthor());
    userReference = new DocumentReference("wiki", "XWiki", "author");
    EntityReference relativeUserReference = userReference.removeParent(userReference.getWikiReference());
    when(xclassEntityReferenceResolver.resolve("author", EntityType.DOCUMENT)).thenReturn(relativeUserReference);
    when(explicitDocumentReferenceResolver.resolve(relativeUserReference, attachment.getReference())).thenReturn(userReference);
    assertEquals(userReference, attachment.getAuthorReference());
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) EntityReference(org.xwiki.model.reference.EntityReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 19 with AttachmentReference

use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.

the class UserAvatarMacro method execute.

@Override
public List<Block> execute(UserAvatarMacroParameters parameters, String content, MacroTransformationContext context) throws MacroExecutionException {
    DocumentReference userReference = this.currentDocumentReferenceResolver.resolve(parameters.getUsername(), new EntityReference(USER_SPACE, EntityType.SPACE));
    // Find the avatar attachment name or null if not defined or an error happened when locating it
    String fileName = null;
    if (this.documentAccessBridge.exists(userReference)) {
        Object avatarProperty = this.documentAccessBridge.getProperty(userReference, new DocumentReference(userReference.getWikiReference().getName(), USER_SPACE, "XWikiUsers"), "avatar");
        if (avatarProperty != null) {
            fileName = avatarProperty.toString();
        }
    } else {
        throw new MacroExecutionException("User [" + this.compactWikiEntityReferenceSerializer.serialize(userReference) + "] is not registered in this wiki");
    }
    // Initialize with the default avatar.
    ResourceReference imageReference = new ResourceReference(this.skinAccessBridge.getSkinFile("icons/xwiki/noavatar.png"), ResourceType.URL);
    // Try to use the configured avatar.
    if (!StringUtils.isBlank(fileName)) {
        AttachmentReference attachmentReference = new AttachmentReference(fileName, userReference);
        // Check if the configured avatar file actually exists.
        try {
            if (documentAccessBridge.getAttachmentVersion(attachmentReference) != null) {
                // Use it.
                imageReference = new ResourceReference(this.compactWikiEntityReferenceSerializer.serialize(attachmentReference), ResourceType.ATTACHMENT);
            }
        } catch (Exception e) {
            // Log and fallback on default.
            logger.warn("Failed to get the avatar for user [{}]: [{}]. Using default.", this.compactWikiEntityReferenceSerializer.serialize(userReference), ExceptionUtils.getRootCauseMessage(e));
        }
    }
    ImageBlock imageBlock = new ImageBlock(imageReference, false);
    imageBlock.setParameter("alt", "Picture of " + userReference.getName());
    imageBlock.setParameter("title", userReference.getName());
    if (parameters.getWidth() != null) {
        imageBlock.setParameter("width", String.valueOf(parameters.getWidth()));
    }
    if (parameters.getHeight() != null) {
        imageBlock.setParameter("height", String.valueOf(parameters.getHeight()));
    }
    List<Block> result = Collections.singletonList(imageBlock);
    return context.isInline() ? result : Collections.singletonList(new GroupBlock(result));
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) ImageBlock(org.xwiki.rendering.block.ImageBlock) EntityReference(org.xwiki.model.reference.EntityReference) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) GroupBlock(org.xwiki.rendering.block.GroupBlock) Block(org.xwiki.rendering.block.Block) ImageBlock(org.xwiki.rendering.block.ImageBlock) GroupBlock(org.xwiki.rendering.block.GroupBlock) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) DocumentReference(org.xwiki.model.reference.DocumentReference) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException)

Example 20 with AttachmentReference

use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.

the class IntegrationTests method initialize.

@RenderingTestSuite.Initialized
public void initialize(ComponentManager componentManager) throws Exception {
    Mockery mockery = new JUnit4Mockery();
    // Skin Access Bridge Mock
    final SkinAccessBridge mockSkinAccessBridge = registerMockComponent(componentManager, mockery, SkinAccessBridge.class);
    mockery.checking(new Expectations() {

        {
            allowing(mockSkinAccessBridge).getSkinFile("icons/xwiki/noavatar.png");
            will(returnValue("/xwiki/resources/icons/xwiki/noavatar.png"));
        }
    });
    // Document Access Bridge Mock
    final DocumentReference adminUserReference = new DocumentReference("wiki", "XWiki", "Admin");
    final DocumentReference userWithoutAvatarReference = new DocumentReference("wiki", "XWiki", "ExistingUserWithoutAvatar");
    final DocumentReference userNotExistingReference = new DocumentReference("wiki", "XWiki", "UserNotExisting");
    final DocumentReference userWithNonExistingAvatarFileReference = new DocumentReference("wiki", "XWiki", "UserWithNonExistingAvatarFile");
    final DocumentReference userWithExceptionRetrievingAvatarFileReference = new DocumentReference("wiki", "XWiki", "UserWithExceptionRetrievingAvatarFile");
    final DocumentReference userClassReference = new DocumentReference("wiki", "XWiki", "XWikiUsers");
    final DocumentAccessBridge mockDocumentAccessBridge = registerMockComponent(componentManager, mockery, DocumentAccessBridge.class);
    mockery.checking(new Expectations() {

        {
            allowing(mockDocumentAccessBridge).exists(adminUserReference);
            will(returnValue(true));
            allowing(mockDocumentAccessBridge).exists(userWithoutAvatarReference);
            will(returnValue(true));
            allowing(mockDocumentAccessBridge).exists(with(any(String.class)));
            will(returnValue(false));
            allowing(mockDocumentAccessBridge).exists(userNotExistingReference);
            will(returnValue(false));
            allowing(mockDocumentAccessBridge).exists(userWithNonExistingAvatarFileReference);
            will(returnValue(true));
            allowing(mockDocumentAccessBridge).exists(userWithExceptionRetrievingAvatarFileReference);
            will(returnValue(true));
            allowing(mockDocumentAccessBridge).getProperty(adminUserReference, userClassReference, "avatar");
            will(returnValue("mockAvatar.png"));
            allowing(mockDocumentAccessBridge).getProperty(userWithoutAvatarReference, userClassReference, "avatar");
            will(returnValue(null));
            allowing(mockDocumentAccessBridge).getProperty(userWithNonExistingAvatarFileReference, userClassReference, "avatar");
            will(returnValue("mockAvatar.png"));
            allowing(mockDocumentAccessBridge).getProperty(userWithExceptionRetrievingAvatarFileReference, userClassReference, "avatar");
            will(returnValue("mockAvatar.png"));
            allowing(mockDocumentAccessBridge).getAttachmentVersion(new AttachmentReference("mockAvatar.png", adminUserReference));
            will(returnValue("1.1"));
            allowing(mockDocumentAccessBridge).getAttachmentVersion(new AttachmentReference("mockAvatar.png", userWithNonExistingAvatarFileReference));
            will(returnValue(null));
            allowing(mockDocumentAccessBridge).getAttachmentVersion(new AttachmentReference("mockAvatar.png", userWithExceptionRetrievingAvatarFileReference));
            will(throwException(new Exception("Sum Ting Wong")));
        }
    });
    // Document Resolver Mock
    final DocumentReferenceResolver<String> mockDocumentReferenceResolver = registerMockComponent(componentManager, mockery, DocumentReferenceResolver.TYPE_STRING, "current");
    mockery.checking(new Expectations() {

        {
            allowing(mockDocumentReferenceResolver).resolve("XWiki.Admin", new EntityReference("XWiki", EntityType.SPACE));
            will(returnValue(adminUserReference));
            allowing(mockDocumentReferenceResolver).resolve("XWiki.ExistingUserWithoutAvatar", new EntityReference("XWiki", EntityType.SPACE));
            will(returnValue(userWithoutAvatarReference));
            allowing(mockDocumentReferenceResolver).resolve("XWiki.UserNotExisting", new EntityReference("XWiki", EntityType.SPACE));
            will(returnValue(userNotExistingReference));
            allowing(mockDocumentReferenceResolver).resolve("XWiki.UserWithNonExistingAvatarFile", new EntityReference("XWiki", EntityType.SPACE));
            will(returnValue(userWithNonExistingAvatarFileReference));
            allowing(mockDocumentReferenceResolver).resolve("XWiki.UserWithExceptionRetrievingAvatarFile", new EntityReference("XWiki", EntityType.SPACE));
            will(returnValue(userWithExceptionRetrievingAvatarFileReference));
        }
    });
    // Entity Reference Serializer Mock
    final EntityReferenceSerializer<String> mockEntityReferenceSerializer = registerMockComponent(componentManager, mockery, EntityReferenceSerializer.TYPE_STRING, "compactwiki");
    mockery.checking(new Expectations() {

        {
            allowing(mockEntityReferenceSerializer).serialize(new AttachmentReference("mockAvatar.png", adminUserReference));
            will(returnValue("XWiki.Admin@mockAvatar.png"));
            allowing(mockEntityReferenceSerializer).serialize(userNotExistingReference);
            will(returnValue("XWiki.UserNotExisting"));
            allowing(mockEntityReferenceSerializer).serialize(new AttachmentReference("mockAvatar.png", userWithNonExistingAvatarFileReference));
            will(returnValue("XWiki.UserWithNonExistingAvatarFile@mockAvatar.png"));
            allowing(mockEntityReferenceSerializer).serialize(new AttachmentReference("mockAvatar.png", userWithExceptionRetrievingAvatarFileReference));
            will(returnValue("XWiki.UserWithExceptionRetrievingAvatarFile@mockAvatar.png"));
            allowing(mockEntityReferenceSerializer).serialize(userWithExceptionRetrievingAvatarFileReference);
            will(returnValue("XWiki.UserWithExceptionRetrievingAvatarFile"));
        }
    });
    // Entity Reference Serializer Mock
    final EntityReferenceValueProvider mockEntityReferenceValueProvider = registerMockComponent(componentManager, mockery, EntityReferenceValueProvider.class, "current");
    mockery.checking(new Expectations() {

        {
            allowing(mockEntityReferenceValueProvider).getDefaultValue(EntityType.WIKI);
            will(returnValue("wiki"));
        }
    });
}
Also used : Expectations(org.jmock.Expectations) AttachmentReference(org.xwiki.model.reference.AttachmentReference) EntityReferenceValueProvider(org.xwiki.model.reference.EntityReferenceValueProvider) SkinAccessBridge(org.xwiki.bridge.SkinAccessBridge) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) Mockery(org.jmock.Mockery) JUnit4Mockery(org.jmock.integration.junit4.JUnit4Mockery) JUnit4Mockery(org.jmock.integration.junit4.JUnit4Mockery) EntityReference(org.xwiki.model.reference.EntityReference) DocumentReference(org.xwiki.model.reference.DocumentReference)

Aggregations

AttachmentReference (org.xwiki.model.reference.AttachmentReference)64 DocumentReference (org.xwiki.model.reference.DocumentReference)44 Test (org.junit.Test)31 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)17 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)14 ByteArrayInputStream (java.io.ByteArrayInputStream)10 ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)9 XWikiContext (com.xpn.xwiki.XWikiContext)8 EntityReference (org.xwiki.model.reference.EntityReference)7 Expectations (org.jmock.Expectations)6 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)4 XWikiException (com.xpn.xwiki.XWikiException)3 InputStream (java.io.InputStream)3 URL (java.net.URL)3 Mockery (org.jmock.Mockery)3 JUnit4Mockery (org.jmock.integration.junit4.JUnit4Mockery)3 XDOMOfficeDocument (org.xwiki.officeimporter.document.XDOMOfficeDocument)3 ImageBlock (org.xwiki.rendering.block.ImageBlock)3