1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rendering.internal.macro.useravatar

File UserAvatarMacro.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

12
25
3
1
189
107
10
0.4
8.33
3
3.33

Classes

Class Line # Actions
UserAvatarMacro 60 25 0% 10 0
1.0100%
 

Contributing tests

This file is covered by 5 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.rendering.internal.macro.useravatar;
21   
22    import java.util.Collections;
23    import java.util.List;
24   
25    import javax.inject.Inject;
26    import javax.inject.Named;
27    import javax.inject.Singleton;
28   
29    import org.apache.commons.lang3.StringUtils;
30    import org.apache.commons.lang3.exception.ExceptionUtils;
31    import org.slf4j.Logger;
32    import org.xwiki.bridge.DocumentAccessBridge;
33    import org.xwiki.bridge.SkinAccessBridge;
34    import org.xwiki.component.annotation.Component;
35    import org.xwiki.model.EntityType;
36    import org.xwiki.model.reference.AttachmentReference;
37    import org.xwiki.model.reference.DocumentReference;
38    import org.xwiki.model.reference.DocumentReferenceResolver;
39    import org.xwiki.model.reference.EntityReference;
40    import org.xwiki.model.reference.EntityReferenceSerializer;
41    import org.xwiki.model.reference.EntityReferenceValueProvider;
42    import org.xwiki.rendering.block.Block;
43    import org.xwiki.rendering.block.ImageBlock;
44    import org.xwiki.rendering.listener.reference.ResourceReference;
45    import org.xwiki.rendering.listener.reference.ResourceType;
46    import org.xwiki.rendering.macro.AbstractMacro;
47    import org.xwiki.rendering.macro.MacroExecutionException;
48    import org.xwiki.rendering.macro.useravatar.UserAvatarMacroParameters;
49    import org.xwiki.rendering.transformation.MacroTransformationContext;
50   
51    /**
52    * Allows displaying the avatar for a specific user.
53    *
54    * @version $Id: 92806525d5662309bcc3a92062fefe072509a336 $
55    * @since 1.8RC2
56    */
57    @Component
58    @Named("useravatar")
59    @Singleton
 
60    public class UserAvatarMacro extends AbstractMacro<UserAvatarMacroParameters>
61    {
62    /**
63    * The description of the macro.
64    */
65    private static final String DESCRIPTION = "Allows displaying the avatar for a specific user.";
66   
67    /**
68    * Space where XWiki user profiles are located.
69    */
70    private static final String USER_SPACE = "XWiki";
71   
72    /**
73    * Used to get the user avatar picture from his profile.
74    */
75    @Inject
76    private DocumentAccessBridge documentAccessBridge;
77   
78    /**
79    * Used to get the default avatar picture when the user doesn't exist.
80    */
81    @Inject
82    private SkinAccessBridge skinAccessBridge;
83   
84    /**
85    * Used to convert a user reference represented as a String (passed as a macro parameter by the user) to a Document
86    * Reference.
87    */
88    @Inject
89    @Named("current")
90    private DocumentReferenceResolver<String> currentDocumentReferenceResolver;
91   
92    /**
93    * Used to convert a Document Reference to string (compact form without the wiki part if it matches the current
94    * wiki).
95    */
96    @Inject
97    @Named("compactwiki")
98    private EntityReferenceSerializer<String> compactWikiEntityReferenceSerializer;
99   
100    /**
101    * Used to find out the current Wiki name.
102    */
103    @Inject
104    @Named("current")
105    private EntityReferenceValueProvider currentEntityReferenceValueProvider;
106   
107    /**
108    * Logging framework.
109    */
110    @Inject
111    private Logger logger;
112   
113    /**
114    * Create and initialize the descriptor of the macro.
115    */
 
116  6 toggle public UserAvatarMacro()
117    {
118  6 super("User Avatar", DESCRIPTION, UserAvatarMacroParameters.class);
119  6 setDefaultCategory(DEFAULT_CATEGORY_CONTENT);
120    }
121   
 
122  6 toggle @Override
123    public List<Block> execute(UserAvatarMacroParameters parameters, String content, MacroTransformationContext context)
124    throws MacroExecutionException
125    {
126  6 DocumentReference userReference =
127    this.currentDocumentReferenceResolver.resolve(parameters.getUsername(), new EntityReference(USER_SPACE,
128    EntityType.SPACE));
129   
130    // Find the avatar attachment name or null if not defined or an error happened when locating it
131  6 String fileName = null;
132  6 if (this.documentAccessBridge.exists(userReference)) {
133  5 Object avatarProperty =
134    this.documentAccessBridge.getProperty(userReference, new DocumentReference(userReference
135    .getWikiReference().getName(), USER_SPACE, "XWikiUsers"), "avatar");
136  5 if (avatarProperty != null) {
137  3 fileName = avatarProperty.toString();
138    }
139    } else {
140  1 throw new MacroExecutionException("User ["
141    + this.compactWikiEntityReferenceSerializer.serialize(userReference)
142    + "] is not registered in this wiki");
143    }
144   
145    // Initialize with the default avatar.
146  5 ResourceReference imageReference =
147    new ResourceReference(this.skinAccessBridge.getSkinFile("icons/xwiki/noavatar.png"), ResourceType.URL);
148   
149    // Try to use the configured avatar.
150  5 if (!StringUtils.isBlank(fileName)) {
151  3 AttachmentReference attachmentReference = new AttachmentReference(fileName, userReference);
152   
153    // Check if the configured avatar file actually exists.
154  3 try {
155  2 if (documentAccessBridge.getAttachmentVersion(attachmentReference) != null) {
156    // Use it.
157  1 imageReference =
158    new ResourceReference(this.compactWikiEntityReferenceSerializer.serialize(attachmentReference),
159    ResourceType.ATTACHMENT);
160    }
161    } catch (Exception e) {
162    // Log and fallback on default.
163  1 logger.warn("Failed to get the avatar for user [{}]: [{}]. Using default.",
164    this.compactWikiEntityReferenceSerializer.serialize(userReference),
165    ExceptionUtils.getRootCauseMessage(e));
166    }
167    }
168  5 ImageBlock imageBlock = new ImageBlock(imageReference, false);
169   
170  5 imageBlock.setParameter("alt", "Picture of " + userReference.getName());
171  5 imageBlock.setParameter("title", userReference.getName());
172   
173  5 if (parameters.getWidth() != null) {
174  2 imageBlock.setParameter("width", String.valueOf(parameters.getWidth()));
175    }
176   
177  5 if (parameters.getHeight() != null) {
178  1 imageBlock.setParameter("height", String.valueOf(parameters.getHeight()));
179    }
180   
181  5 return Collections.singletonList((Block) imageBlock);
182    }
183   
 
184  1 toggle @Override
185    public boolean supportsInlineMode()
186    {
187  1 return true;
188    }
189    }