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

File AbstractSignableMacro.java

 

Coverage histogram

../../../../img/srcFileCovDistChart7.png
64% of files have more coverage

Code metrics

10
39
13
1
246
138
27
0.69
3
13
2.08

Classes

Class Line # Actions
AbstractSignableMacro 50 39 0% 27 21
0.6612903566.1%
 

Contributing tests

This file is covered by 56 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.macro;
21   
22    import java.io.IOException;
23    import java.security.GeneralSecurityException;
24   
25    import javax.inject.Inject;
26   
27    import org.xwiki.component.manager.ComponentLookupException;
28    import org.xwiki.component.manager.ComponentManager;
29    import org.xwiki.component.util.DefaultParameterizedType;
30    import org.xwiki.crypto.pkix.CertificateProvider;
31    import org.xwiki.crypto.signer.param.CMSSignedDataGeneratorParameters;
32    import org.xwiki.crypto.signer.param.CMSSignedDataVerified;
33    import org.xwiki.crypto.store.SignatureStore;
34    import org.xwiki.crypto.store.SignatureStoreException;
35    import org.xwiki.model.reference.BlockReferenceResolver;
36    import org.xwiki.model.reference.EntityReference;
37    import org.xwiki.rendering.block.Block;
38    import org.xwiki.rendering.macro.descriptor.ContentDescriptor;
39    import org.xwiki.rendering.signature.BlockSignatureGenerator;
40    import org.xwiki.rendering.signature.BlockSignatureVerifier;
41   
42    /**
43    * Helper to implement signable Macro, supplementing the default implementation provided
44    * by {@link org.xwiki.rendering.macro.AbstractMacro} to ease the support of signatures.
45    *
46    * @param <P> the type of the macro parameters bean
47    * @version $Id: 11774a40e438014158da212b347f7ea167023733 $
48    * @since 6.1M2
49    */
 
50    public abstract class AbstractSignableMacro<P> extends AbstractMacro<P> implements SignableMacro
51    {
52    private static final String HINT = "macro";
53   
54    /**
55    * Used to lazily load of other components, avoiding normal macro that never get signed to need these components.
56    */
57    @Inject
58    private ComponentManager componentManager;
59   
60    // Lazily loaded components.
61    private SignatureStore signatureStore;
62    private BlockSignatureGenerator signer;
63    private BlockSignatureVerifier verifier;
64    private BlockReferenceResolver<Block> blockResolver;
65   
66    /**
67    * Creates a new {@link Macro} instance.
68    *5005
69    * @param name the name of the macro (eg "Table Of Contents" for the TOC macro)
70    */
 
71  2 toggle public AbstractSignableMacro(String name)
72    {
73  2 super(name);
74    }
75   
76    /**
77    * Creates a new {@link Macro} instance.
78    *
79    * @param name the name of the macro (eg "Table Of Contents" for the TOC macro)
80    * @param description a string describing this macro.
81    */
 
82  0 toggle public AbstractSignableMacro(String name, String description)
83    {
84  0 super(name, description);
85    }
86   
87    /**
88    * Creates a new {@link Macro} instance.
89    *
90    * @param name the name of the macro (eg "Table Of Contents" for the TOC macro)
91    * @param description a string describing this macro.
92    * @param contentDescriptor {@link ContentDescriptor} for this macro.
93    */
 
94  0 toggle public AbstractSignableMacro(String name, String description,
95    ContentDescriptor contentDescriptor)
96    {
97  0 super(name, description, contentDescriptor);
98    }
99   
100    /**
101    * Creates a new {@link Macro} instance.
102    *
103    * @param name the name of the macro (eg "Table Of Contents" for the TOC macro)
104    * @param description a string describing this macro.
105    * @param parametersBeanClass class of the parameters bean of this macro.
106    */
 
107  0 toggle public AbstractSignableMacro(String name, String description, Class<?> parametersBeanClass)
108    {
109  0 super(name, description, parametersBeanClass);
110    }
111   
112    /**
113    * Creates a new {@link Macro} instance.
114    *
115    * @param name the name of the macro (eg "Table Of Contents" for the TOC macro)
116    * @param description string describing this macro.
117    * @param contentDescriptor the {@link ContentDescriptor} describing the content of this macro.
118    * @param parametersBeanClass class of the parameters bean.
119    */
 
120  93 toggle public AbstractSignableMacro(String name, String description,
121    ContentDescriptor contentDescriptor, Class<?> parametersBeanClass)
122    {
123  93 super(name, description, contentDescriptor, parametersBeanClass);
124    }
125   
126    /**
127    * @return the component manager
128    * @since 2.0M1 (moved from AbstractScriptMacro)
129    */
 
130  8636 toggle protected ComponentManager getComponentManager()
131    {
132  8636 return this.componentManager;
133    }
134   
135    /**
136    * @return the default signature store. Lazy get to avoid strong dependency.
137    * @throws ComponentLookupException if no instance has been found.
138    */
 
139  2 toggle private SignatureStore getSignatureStore() throws ComponentLookupException
140    {
141  2 if (signatureStore == null) {
142  2 signatureStore = getComponentManager().getInstance(SignatureStore.class);
143    }
144  2 return signatureStore;
145    }
146   
147    /**
148    * @return the macro block signer. Lazy get to avoid strong dependency.
149    * @throws ComponentLookupException if no instance has been found.
150    */
 
151  1 toggle private BlockSignatureGenerator getSigner() throws ComponentLookupException
152    {
153  1 if (signer == null) {
154  1 signer = getComponentManager().getInstance(BlockSignatureGenerator.class, HINT);
155    }
156  1 return signer;
157    }
158   
159    /**
160    * @return the macro block verifier. Lazy get to avoid strong dependency.
161    * @throws ComponentLookupException if no instance has been found.
162    */
 
163  1 toggle private BlockSignatureVerifier getVerifier() throws ComponentLookupException
164    {
165  1 if (verifier == null) {
166  1 verifier = getComponentManager().getInstance(BlockSignatureVerifier.class, HINT);
167    }
168  1 return verifier;
169    }
170   
171    /**
172    * @return the current signed macro block reference resolver. Lazy get to avoid strong dependency.
173    * @throws ComponentLookupException if no instance has been found.
174    */
 
175  2 toggle private BlockReferenceResolver<Block> getBlockResolver() throws ComponentLookupException
176    {
177  2 if (blockResolver == null) {
178  2 blockResolver = getComponentManager().getInstance(
179    new DefaultParameterizedType(null, BlockReferenceResolver.class, Block.class),
180    "currentsignedmacro");
181    }
182  2 return blockResolver;
183    }
184   
 
185  1 toggle @Override
186    public void sign(Block block, CMSSignedDataGeneratorParameters parameters) throws MacroSignatureException
187    {
188  1 EntityReference blockRef = getBlockReference(block);
189   
190  1 try {
191  1 getSignatureStore().store(blockRef, getSigner().generate(block, parameters));
192    } catch (SignatureStoreException e) {
193  0 throw new MacroSignatureException(
194    String.format("Unable to store the signature of macro block [%s].", blockRef), e);
195    } catch (GeneralSecurityException e) {
196  0 throw new MacroSignatureException(
197    String.format("Unable to compute signature of macro block [%s].", blockRef), e);
198    } catch (IOException e) {
199  0 throw new MacroSignatureException(
200    String.format("Unable to encode signature of macro block [%s].", blockRef), e);
201    } catch (ComponentLookupException e) {
202  0 throw new MacroSignatureException(
203    String.format("Missing components to sign macro block [%s].", blockRef), e);
204    }
205    }
206   
 
207  1 toggle @Override
208    public CMSSignedDataVerified verify(Block block, CertificateProvider certificateProvider)
209    throws MacroSignatureException
210    {
211  1 EntityReference blockRef = getBlockReference(block);
212   
213  1 try {
214  1 return getVerifier().verify(getSignatureStore().retrieve(blockRef), block, certificateProvider);
215    } catch (SignatureStoreException e) {
216  0 throw new MacroSignatureException(
217    String.format("Unable to retrieve the signature of macro block [%s].", blockRef), e);
218    } catch (GeneralSecurityException e) {
219  0 throw new MacroSignatureException(
220    String.format("Unable to verify signature of macro block [%s].", blockRef), e);
221    } catch (IOException e) {
222  0 throw new MacroSignatureException(
223    String.format("Unable to decode signature of macro block [%s].", blockRef), e);
224    } catch (ComponentLookupException e) {
225  0 throw new MacroSignatureException(
226    String.format("Missing components to verify macro block [%s].", blockRef), e);
227    }
228    }
229   
 
230  2 toggle private EntityReference getBlockReference(Block block) throws MacroSignatureException
231    {
232  2 EntityReference blockRef;
233   
234  2 try {
235  2 blockRef = getBlockResolver().resolve(block);
236    } catch (ComponentLookupException e) {
237  0 throw new MacroSignatureException("Missing component to resolve macro block reference.", e);
238    }
239   
240  2 if (blockRef == null) {
241  0 throw new MacroSignatureException("Unable to determine the block reference of the macro block.");
242    }
243   
244  2 return blockRef;
245    }
246    }