1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.crypto.store.wiki.internal

File DefaultSignatureStore.java

 

Coverage histogram

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

Code metrics

8
33
3
1
162
91
13
0.39
11
3
4.33

Classes

Class Line # Actions
DefaultSignatureStore 52 33 0% 13 8
0.818181881.8%
 

Contributing tests

This file is covered by 4 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.crypto.store.wiki.internal;
21   
22    import java.io.IOException;
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.crypto.BinaryStringEncoder;
31    import org.xwiki.crypto.store.SignatureStore;
32    import org.xwiki.crypto.store.SignatureStoreException;
33    import org.xwiki.model.EntityType;
34    import org.xwiki.model.reference.DocumentReference;
35    import org.xwiki.model.reference.DocumentReferenceResolver;
36    import org.xwiki.model.reference.EntityReference;
37    import org.xwiki.model.reference.LocalDocumentReference;
38   
39    import com.xpn.xwiki.XWikiContext;
40    import com.xpn.xwiki.XWikiException;
41    import com.xpn.xwiki.doc.XWikiDocument;
42    import com.xpn.xwiki.objects.BaseObject;
43   
44    /**
45    * Default implementation of {@link SignatureStore}.
46    *
47    * @version $Id: 1fa62b24a270d054ccbbd89728cbf4502891ac0f $
48    * @since 6.1M2
49    */
50    @Component
51    @Singleton
 
52    public class DefaultSignatureStore implements SignatureStore
53    {
54    /**
55    * Reference to the signature class.
56    */
57    public static final LocalDocumentReference SIGNATURECLASS = new LocalDocumentReference("Crypto", "SignatureClass");
58   
59    /**
60    * Name of the reference field.
61    */
62    public static final String SIGNATURECLASS_PROP_REFERENCE = "reference";
63   
64    /**
65    * Name of the signature field.
66    */
67    public static final String SIGNATURECLASS_PROP_SIGNATURE = "signature";
68   
69    /**
70    * Context provider used to manipulate XWikiDocuments and XObjects.
71    */
72    @Inject
73    private Provider<XWikiContext> contextProvider;
74   
75    /**
76    * Document reference resolver used to resolved provided entity reference to document reference.
77    */
78    @Inject
79    @Named("current")
80    private DocumentReferenceResolver<EntityReference> resolver;
81   
82    /**
83    * Encoder/decoder used to convert byte arrays to/from String.
84    */
85    @Inject
86    @Named("Base64")
87    private BinaryStringEncoder base64;
88   
 
89  2 toggle @Override
90    public void store(EntityReference entity, byte[] signature) throws SignatureStoreException
91    {
92  2 checkArguments(entity);
93   
94    // TODO: Support object property as parent
95  2 DocumentReference docRef = this.resolver.resolve(entity.getParent());
96  2 XWikiContext context = this.contextProvider.get();
97   
98  2 try {
99  2 XWikiDocument document = context.getWiki().getDocument(docRef, context);
100   
101  2 DocumentReference signatureClass = new DocumentReference(SIGNATURECLASS, docRef.getWikiReference());
102    // TODO: Support any kind of entity by serializing the reference and its type properly
103  2 String entityReference = entity.getName();
104   
105  2 BaseObject object = document.getXObject(signatureClass, SIGNATURECLASS_PROP_REFERENCE, entityReference);
106  2 if (object == null) {
107  1 object = document.newXObject(SIGNATURECLASS, context);
108  1 object.setStringValue(SIGNATURECLASS_PROP_REFERENCE, entityReference);
109    }
110   
111  2 object.setLargeStringValue(SIGNATURECLASS_PROP_SIGNATURE, this.base64.encode(signature, 64));
112   
113  2 context.getWiki().saveDocument(document, context);
114    } catch (XWikiException e) {
115  0 throw new SignatureStoreException("Error while storing signature for entity [" + entity + "]", e);
116    } catch (IOException e) {
117  0 throw new SignatureStoreException("Error while encoding signature for entity [" + entity + "]", e);
118    }
119    }
120   
 
121  2 toggle @Override
122    public byte[] retrieve(EntityReference entity) throws SignatureStoreException
123    {
124  2 checkArguments(entity);
125   
126  2 DocumentReference docRef = this.resolver.resolve(entity);
127  2 XWikiContext context = this.contextProvider.get();
128   
129  2 try {
130  2 XWikiDocument document = context.getWiki().getDocument(docRef, context);
131  2 DocumentReference signatureClass = new DocumentReference(SIGNATURECLASS, docRef.getWikiReference());
132    // TODO: Support any kind of entity by serializing the reference and its type properly
133  2 String entityReference = entity.getName();
134   
135  2 BaseObject object = document.getXObject(signatureClass, SIGNATURECLASS_PROP_REFERENCE, entityReference);
136  2 if (object == null) {
137  1 return null;
138    }
139   
140  1 String signature = object.getLargeStringValue(SIGNATURECLASS_PROP_SIGNATURE);
141  1 if (signature == null) {
142  0 return null;
143    }
144   
145  1 return this.base64.decode(signature);
146    } catch (XWikiException e) {
147  0 throw new SignatureStoreException("Error while retrieving signature for entity [" + entity + "]", e);
148    } catch (IOException e) {
149  0 throw new SignatureStoreException("Error while decoding signature for entity [" + entity + "]", e);
150    }
151    }
152   
 
153  4 toggle private void checkArguments(EntityReference entity)
154    {
155    // TODO: Support any kind of entity
156  4 if (entity.getType() != EntityType.BLOCK || entity.getParent() == null
157    || entity.getParent().getType() != EntityType.DOCUMENT) {
158  0 throw new IllegalArgumentException("Unsupported reference type [" + entity.getType() + "] or parent ["
159    + "]. Only blocks of documents are supported.");
160    }
161    }
162    }