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

File AbstractX509StoreQuery.java

 

Coverage histogram

../../../../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

16
31
5
1
160
81
16
0.52
6.2
5
3.2

Classes

Class Line # Actions
AbstractX509StoreQuery 40 31 0% 16 13
0.7575%
 

Contributing tests

This file is covered by 26 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.query;
21   
22    import java.io.IOException;
23    import java.util.List;
24   
25    import org.xwiki.crypto.BinaryStringEncoder;
26    import org.xwiki.crypto.store.CertificateStoreException;
27    import org.xwiki.model.EntityType;
28    import org.xwiki.model.reference.EntityReference;
29    import org.xwiki.model.reference.EntityReferenceSerializer;
30    import org.xwiki.query.Query;
31    import org.xwiki.query.QueryException;
32    import org.xwiki.query.QueryManager;
33   
34    /**
35    * Abstract class to build queries on CertificateClass in a certificate store.
36    *
37    * @version $Id: ce78e8bef4d36a314746f5bb597685e643e981df $
38    * @since 6.1M2
39    */
 
40    public abstract class AbstractX509StoreQuery
41    {
42    private static final String FROM_STATEMENT = " from Document doc";
43   
44    private static final String WHERE_STATEMENT = " where 1=1";
45   
46    private static final String STORE = "store";
47   
48    private static final String WHERE_SPACE_STATEMENT = " where doc.space=:" + STORE;
49   
50    private static final String WHERE_DOC_STATEMENT = " where doc.fullName=:" + STORE;
51   
52    /**
53    * Encoder used to encode/decode data to/from String.
54    */
55    private final BinaryStringEncoder encoder;
56   
57    /**
58    * Wrapped query.
59    */
60    private Query query;
61   
62    /**
63    * Create a query.
64    *
65    * @param store the reference of a document or a space where the certificate should be stored.
66    * @param select the select statement specific to the query.
67    * @param from the additional from statement specific to the query.
68    * @param where the additional where statement specific to the query.
69    * @param encoder a string encoder/decoder used to convert byte arrays to/from String.
70    * @param queryManager the query manager used to build queries.
71    * @param serializer the entity reference serializer to serialize the store reference for query
72    * @throws CertificateStoreException on error creating required queries.
73    */
 
74  38 toggle public AbstractX509StoreQuery(EntityReference store, String select, String from, String where,
75    BinaryStringEncoder encoder, QueryManager queryManager, EntityReferenceSerializer<String> serializer)
76    throws CertificateStoreException
77    {
78  38 EntityReference wiki = null;
79  38 EntityType storeType = null;
80   
81  38 if (store != null) {
82  38 wiki = store.extractReference(EntityType.WIKI);
83  38 storeType = getEntityType(store);
84    }
85   
86  38 if (wiki == null || storeType == null) {
87  0 throw new IllegalArgumentException("Certificate could only be queried from wiki, space, or document.");
88    }
89   
90  38 this.encoder = encoder;
91   
92  38 String statement = select + FROM_STATEMENT + from;
93   
94  38 if (storeType != EntityType.WIKI) {
95  38 if (storeType == EntityType.DOCUMENT) {
96  18 statement += WHERE_DOC_STATEMENT;
97    } else {
98  20 statement += WHERE_SPACE_STATEMENT;
99    }
100    } else {
101  0 statement += WHERE_STATEMENT;
102    }
103   
104  38 statement += where;
105   
106  38 try {
107  38 this.query = queryManager.createQuery(statement, Query.XWQL);
108    } catch (QueryException e) {
109  0 throw new CertificateStoreException(e);
110    }
111   
112  38 this.query.setWiki(wiki.getName());
113   
114  38 if (storeType != EntityType.WIKI) {
115  38 this.query.bindValue(STORE, serializer.serialize(store));
116    }
117    }
118   
 
119  38 toggle private EntityType getEntityType(EntityReference store)
120    {
121  38 if (store.getType() == EntityType.DOCUMENT && store.getParent() != null) {
122  18 return EntityType.DOCUMENT;
123    }
124  20 if (store.getType() == EntityType.SPACE) {
125  20 return EntityType.SPACE;
126    }
127  0 if (store.getType() == EntityType.WIKI) {
128  0 return EntityType.WIKI;
129    }
130  0 return null;
131    }
132   
133    /**
134    * @return the query.
135    */
 
136  26 toggle protected Query getQuery()
137    {
138  26 return this.query;
139    }
140   
141    /**
142    * @return the encoder.
143    */
 
144  28 toggle protected BinaryStringEncoder getEncoder()
145    {
146  28 return this.encoder;
147    }
148   
149    /**
150    * Execute the query.
151    *
152    * @return the query result.
153    * @throws java.io.IOException on encoding error.
154    * @throws QueryException on query error.
155    */
 
156  2 toggle protected <T> List<T> execute() throws IOException, QueryException
157    {
158  2 return getQuery().execute();
159    }
160    }