1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.model.reference.internal

File SignedMacroBlockReferenceResolver.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

2
8
1
1
92
45
4
0.5
8
1
4

Classes

Class Line # Actions
SignedMacroBlockReferenceResolver 52 8 0% 4 1
0.9090909490.9%
 

Contributing tests

This file is covered by 3 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.model.reference.internal;
21   
22    import java.io.IOException;
23   
24    import javax.inject.Inject;
25    import javax.inject.Named;
26    import javax.inject.Singleton;
27   
28    import org.xwiki.component.annotation.Component;
29    import org.xwiki.crypto.BinaryStringEncoder;
30    import org.xwiki.crypto.Digest;
31    import org.xwiki.crypto.DigestFactory;
32    import org.xwiki.model.EntityType;
33    import org.xwiki.model.reference.BlockReference;
34    import org.xwiki.model.reference.BlockReferenceResolver;
35    import org.xwiki.model.reference.EntityReference;
36    import org.xwiki.rendering.block.Block;
37    import org.xwiki.rendering.signature.internal.BlockDumper;
38   
39    /**
40    * Resolve macro block references from {@link Block} instances for the purpose of linking them to signatures.
41    *
42    * The name of the block reference is an SHA-1 digest based on the macro block content, parameters, and the
43    * metadata source if found in XDOM. The block receive no parent by default. A optional
44    * {@link EntityReference} can be provided to specify the parent entity.
45    *
46    * @version $Id: 506d680cf662e643355e3811c07f254159d8d3a9 $
47    * @since 6.1M2
48    */
49    @Component
50    @Named("signedmacro")
51    @Singleton
 
52    public class SignedMacroBlockReferenceResolver implements BlockReferenceResolver<Block>
53    {
54    @Inject
55    @Named("SHA-1")
56    private DigestFactory digestFactory;
57   
58    @Inject
59    @Named("Base64")
60    private BinaryStringEncoder encoder;
61   
62    @Inject
63    @Named("macro")
64    private BlockDumper dumper;
65   
66    /**
67    * {@inheritDoc}
68    *
69    * This implementation is specific for macro blocks. The block argument could be either
70    * a {@link org.xwiki.rendering.block.MacroBlock} or a {@link org.xwiki.rendering.block.MacroMarkerBlock}.
71    */
 
72  3 toggle @Override
73    public BlockReference resolve(Block block, Object... parameters)
74    {
75  3 EntityReference parent = null;
76   
77  3 if (parameters.length > 0 && parameters[0] instanceof EntityReference) {
78    // Try to extract the type from the passed parameter.
79  1 parent = (EntityReference) parameters[0];
80    }
81   
82  3 Digest digest = digestFactory.getInstance();
83   
84  3 try {
85  3 dumper.dump(digest.getOutputStream(), block);
86  3 return new BlockReference(new EntityReference(encoder.encode(digest.digest()), EntityType.BLOCK, parent));
87    } catch (IOException ignore) {
88    // Ignored
89    }
90  0 return null;
91    }
92    }