1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.notifications.notifiers.internal.email

File LogoAttachmentExtractor.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart4.png
80% of files have more coverage

Code metrics

14
39
5
1
158
104
14
0.36
7.8
5
2.8

Classes

Class Line # Actions
LogoAttachmentExtractor 54 39 0% 14 35
0.3965517339.7%
 

Contributing tests

No tests hitting this source file were found.

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.notifications.notifiers.internal.email;
21   
22    import java.io.InputStream;
23   
24    import javax.inject.Inject;
25    import javax.inject.Named;
26    import javax.inject.Provider;
27    import javax.inject.Singleton;
28   
29    import org.xwiki.component.annotation.Component;
30    import org.xwiki.configuration.ConfigurationSource;
31    import org.xwiki.filter.input.InputSource;
32    import org.xwiki.filter.input.InputStreamInputSource;
33    import org.xwiki.model.reference.DocumentReference;
34    import org.xwiki.model.reference.DocumentReferenceResolver;
35    import org.xwiki.notifications.NotificationException;
36    import org.xwiki.skin.Resource;
37    import org.xwiki.text.StringUtils;
38   
39    import com.xpn.xwiki.XWiki;
40    import com.xpn.xwiki.XWikiContext;
41    import com.xpn.xwiki.XWikiException;
42    import com.xpn.xwiki.api.Attachment;
43    import com.xpn.xwiki.doc.XWikiAttachment;
44    import com.xpn.xwiki.doc.XWikiAttachmentContent;
45    import com.xpn.xwiki.doc.XWikiDocument;
46    import com.xpn.xwiki.internal.skin.InternalSkinManager;
47   
48    /**
49    * @version $Id: 28907d8159cb4ffd0a3b3387931a76c27e7f78ea $
50    * @since 9.10RC1
51    */
52    @Component(roles = LogoAttachmentExtractor.class)
53    @Singleton
 
54    public class LogoAttachmentExtractor
55    {
56    private static final String LOGO = "logo";
57   
58    private static final String SVG_EXTENSION = ".svg";
59   
60    @Inject
61    private Provider<XWikiContext> xwikiContextProvider;
62   
63    @Inject
64    @Named("all")
65    private ConfigurationSource configurationSource;
66   
67    @Inject
68    private DocumentReferenceResolver<String> documentReferenceResolver;
69   
70    @Inject
71    private InternalSkinManager internalSkinManager;
72   
73    /**
74    * @return an attachment holding the logo of the wiki
75    * @throws Exception if an error happens
76    */
 
77  1 toggle public Attachment getLogo() throws Exception
78    {
79  1 XWikiContext context = xwikiContextProvider.get();
80   
81  1 String colorTheme = configurationSource.getProperty("colorTheme");
82  1 if (StringUtils.isNotBlank(colorTheme)) {
83  0 DocumentReference colorThemeRef = documentReferenceResolver.resolve(colorTheme);
84  0 XWiki xwiki = context.getWiki();
85  0 XWikiDocument doc = xwiki.getDocument(colorThemeRef, context);
86  0 String logo = doc.getStringValue(LOGO);
87  0 if (StringUtils.isBlank(logo)) {
88  0 logo = doc.getStringValue("logoImage");
89    }
90  0 if (isLogoAttachementValid(doc, logo)) {
91  0 XWikiAttachment attachment = doc.getAttachment(logo);
92   
93  0 return toLogoAttachment(attachment, context);
94    }
95    }
96   
97  1 String skin = configurationSource.getProperty("skin");
98  1 if (StringUtils.isNotBlank(skin)) {
99  0 DocumentReference skinRef = documentReferenceResolver.resolve(skin);
100  0 XWiki xwiki = context.getWiki();
101  0 XWikiDocument doc = xwiki.getDocument(skinRef, context);
102  0 String logo = doc.getStringValue(LOGO);
103  0 if (isLogoAttachementValid(doc, logo)) {
104  0 XWikiAttachment attachment = doc.getAttachment(logo);
105   
106  0 return toLogoAttachment(attachment, context);
107    }
108    }
109   
110  1 return newLogoAttachment("image/png", this.internalSkinManager.getCurrentSkin(true).getResource("logo.png"),
111    context);
112    }
113   
 
114  0 toggle private Attachment toLogoAttachment(XWikiAttachment attachment, XWikiContext xcontext) throws XWikiException
115    {
116    // Make sure attachment content is loaded
117  0 attachment.loadAttachmentContent(xcontext);
118   
119    // Make sure the attachment have the right name
120  0 if (!attachment.getFilename().equals(LOGO)) {
121  0 return newLogoAttachment(attachment.getMimeType(xcontext),
122    (XWikiAttachmentContent) attachment.getAttachment_content().clone(), xcontext);
123    } else {
124  0 return new Attachment(attachment.getDoc().newDocument(xcontext), attachment, xcontext);
125    }
126    }
127   
 
128  1 toggle private Attachment newLogoAttachment(String mimeType, XWikiAttachmentContent content, XWikiContext xcontext)
129    {
130  1 XWikiAttachment attachment = new XWikiAttachment(null, LOGO);
131  1 attachment.setAttachment_content(content);
132  1 attachment.setMimeType(mimeType);
133   
134  1 return new Attachment(null, attachment, xcontext);
135    }
136   
 
137  1 toggle private Attachment newLogoAttachment(String mimeType, Resource sourceImageIS, XWikiContext xcontext)
138    throws Exception
139    {
140  1 InputSource inputSource = sourceImageIS.getInputSource();
141   
142  1 if (inputSource instanceof InputStreamInputSource) {
143  1 try (InputStream inputStream = ((InputStreamInputSource) inputSource).getInputStream()) {
144  1 XWikiAttachmentContent content = new XWikiAttachmentContent();
145  1 content.setContent(inputStream);
146   
147  1 return newLogoAttachment(mimeType, content, xcontext);
148    }
149    }
150   
151  0 throw new NotificationException("Unsupported logo input source [" + inputSource + "]");
152    }
153   
 
154  0 toggle private boolean isLogoAttachementValid(XWikiDocument doc, String logo)
155    {
156  0 return StringUtils.isNotBlank(logo) && !logo.endsWith(SVG_EXTENSION) && doc.getAttachment(logo) != null;
157    }
158    }