Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
SWPAuthorityImpl |
|
| 2.3125;2.312 |
1 | ||
2 | package de.fuberlin.wiwiss.ng4j.swp.impl; | |
3 | ||
4 | import java.util.ArrayList; | |
5 | ||
6 | import com.hp.hpl.jena.datatypes.DatatypeFormatException; | |
7 | import com.hp.hpl.jena.datatypes.xsd.XSDDatatype; | |
8 | import com.hp.hpl.jena.graph.Node; | |
9 | import com.hp.hpl.jena.graph.Graph; | |
10 | import com.hp.hpl.jena.graph.Triple; | |
11 | import com.hp.hpl.jena.shared.AddDeniedException; | |
12 | import com.hp.hpl.jena.util.iterator.ExtendedIterator; | |
13 | import com.hp.hpl.jena.vocabulary.RDFS; | |
14 | ||
15 | import java.security.PublicKey; | |
16 | import java.security.cert.CertificateEncodingException; | |
17 | import java.security.cert.X509Certificate; | |
18 | ||
19 | import org.apache.commons.codec.binary.Base64; | |
20 | ||
21 | ||
22 | import de.fuberlin.wiwiss.ng4j.NamedGraph; | |
23 | import de.fuberlin.wiwiss.ng4j.swp.SWPAuthority; | |
24 | import de.fuberlin.wiwiss.ng4j.swp.vocabulary.SWP; | |
25 | import de.fuberlin.wiwiss.ng4j.swp.vocabulary.FOAF; | |
26 | ||
27 | /** | |
28 | * | |
29 | * An SWPAuthority represents information about an authority | |
30 | * like id, label, eMail, keys and certificates | |
31 | * | |
32 | * @author chris bizer | |
33 | * | |
34 | */ | |
35 | public class SWPAuthorityImpl implements SWPAuthority | |
36 | { | |
37 | ||
38 | private Node id; | |
39 | private String label; | |
40 | 30 | private String email = null; |
41 | private PublicKey publickey; | |
42 | private X509Certificate certificate; | |
43 | 30 | private NamedGraph graph = null; |
44 | ||
45 | 50 | public SWPAuthorityImpl(){} |
46 | ||
47 | /** | |
48 | * | |
49 | * @param id | |
50 | */ | |
51 | public SWPAuthorityImpl( Node id ) | |
52 | 5 | { |
53 | 5 | setID( id ); |
54 | 5 | } |
55 | ||
56 | /** | |
57 | * | |
58 | * Sets the ID of the authority. | |
59 | * Authorities can by identified using a URIref or a bNode | |
60 | * | |
61 | * @param id | |
62 | * | |
63 | */ | |
64 | public void setID( Node id ) | |
65 | { | |
66 | 28 | this.id = id ; |
67 | 28 | } |
68 | ||
69 | /** | |
70 | * | |
71 | * @return id | |
72 | */ | |
73 | public Node getID() | |
74 | { | |
75 | 77 | return this.id; |
76 | } | |
77 | ||
78 | /** | |
79 | * | |
80 | * Sets the Label / Name of the authority. | |
81 | * Will be serialized using rdfs:label | |
82 | * | |
83 | * @param label | |
84 | * | |
85 | */ | |
86 | public void setLabel( String label ) | |
87 | { | |
88 | 0 | this.label = label ; |
89 | 0 | } |
90 | ||
91 | /** | |
92 | * | |
93 | * @return label | |
94 | */ | |
95 | public String getLabel() | |
96 | { | |
97 | 0 | return this.label; |
98 | } | |
99 | ||
100 | /** | |
101 | * | |
102 | * Sets the eMail address of the authority. | |
103 | * Will be serialized using foaf:mbox | |
104 | * | |
105 | * @param email | |
106 | * | |
107 | */ | |
108 | public void setEmail( String email ) | |
109 | { | |
110 | 24 | this.email = email ; |
111 | 24 | } |
112 | ||
113 | /** | |
114 | * | |
115 | * @return email | |
116 | */ | |
117 | public String getEmail() | |
118 | { | |
119 | 0 | return this.email; |
120 | } | |
121 | ||
122 | /** | |
123 | * | |
124 | * Sets the public key of the authority. | |
125 | * Will be serialized using swp:hasKey | |
126 | * | |
127 | * @param key | |
128 | * | |
129 | */ | |
130 | public void setPublicKey( PublicKey key ) | |
131 | { | |
132 | 0 | this.publickey = key ; |
133 | 0 | } |
134 | ||
135 | /** | |
136 | * | |
137 | * @return publickey | |
138 | */ | |
139 | public PublicKey getPublicKey() | |
140 | { | |
141 | 1 | return this.publickey; |
142 | } | |
143 | ||
144 | /** | |
145 | * | |
146 | * Sets the certificate of the authority. | |
147 | * | |
148 | * @param certificate | |
149 | * | |
150 | */ | |
151 | public void setCertificate( X509Certificate certificate ) | |
152 | { | |
153 | 26 | this.certificate = certificate ; |
154 | 26 | } |
155 | ||
156 | /** | |
157 | * | |
158 | * @return certificate | |
159 | * | |
160 | */ | |
161 | public X509Certificate getCertificate() | |
162 | { | |
163 | 23 | return this.certificate; |
164 | } | |
165 | ||
166 | /** | |
167 | * | |
168 | * Adds an additional property of the authority. | |
169 | * | |
170 | * @param predicate | |
171 | * @param object | |
172 | * | |
173 | */ | |
174 | public void addProperty( Node predicate, Node object ) | |
175 | { | |
176 | 0 | if ( this.graph != null ) |
177 | { | |
178 | 0 | graph.add( new Triple( this.getID(), predicate, object ) ); |
179 | } | |
180 | else | |
181 | { | |
182 | //graph not ready! | |
183 | } | |
184 | 0 | } |
185 | ||
186 | /** | |
187 | * | |
188 | * Returns an iterator over all property values (nodes) for a given property. | |
189 | * | |
190 | * @return predicate | |
191 | * | |
192 | */ | |
193 | public ExtendedIterator getProperty( Node predicate ) | |
194 | { | |
195 | 0 | return null; |
196 | } | |
197 | ||
198 | /** | |
199 | * | |
200 | * Adds a description of the authority to a graph. | |
201 | * | |
202 | * The listOfAuthorityProperties determines which information | |
203 | * about the authority is added. | |
204 | * | |
205 | */ | |
206 | public boolean addDescriptionToGraph( NamedGraph graphP, ArrayList<Node> listOfAuthorityProperties ) | |
207 | { | |
208 | // Add swp:authority | |
209 | 19 | graphP.add( new Triple( graphP.getGraphName(), SWP.authority, this.getID() ) ); |
210 | ||
211 | ||
212 | // Check if the eMail address has to be added. | |
213 | 19 | if ( this.getID().isBlank() && this.getEmail() != null ) |
214 | { | |
215 | 0 | graphP.add( new Triple(this.getID(), FOAF.mbox.asNode(), Node.createURI( "mailto:" + this.getEmail() ) ) ); |
216 | } | |
217 | ||
218 | ||
219 | 19 | if ( listOfAuthorityProperties != null) |
220 | { | |
221 | // Add authority description | |
222 | 8 | if ( listOfAuthorityProperties.contains( FOAF.mbox.asNode() ) ) |
223 | { | |
224 | 0 | graphP.add(new Triple( this.getID(), FOAF.mbox.asNode(), Node.createURI( "mailto:" + this.getEmail() ) ) ); |
225 | } | |
226 | ||
227 | //Node rdfsLabel = Node.createURI("http://www.w3.org/2000/01/rdf-schema#label"); | |
228 | //Not fatal, so won't throw exception if missing | |
229 | 8 | if ( listOfAuthorityProperties.contains( RDFS.label.asNode() ) ) |
230 | { | |
231 | 0 | graphP.add( new Triple( this.getID(), RDFS.label.asNode(), Node.createLiteral( this.getLabel(), null, null ) ) ); |
232 | } | |
233 | ||
234 | ||
235 | 8 | if ( listOfAuthorityProperties.contains( SWP.RSAKey ) ) |
236 | { | |
237 | // We need code for publishing information about a RSA key here, using the SWP-2 and the XML-Sig vocabulary | |
238 | 1 | graphP.add( new Triple( this.getID(), |
239 | SWP.RSAKey, | |
240 | Node.createLiteral( new String( Base64.encodeBase64( this.getPublicKey().getEncoded() ) ), | |
241 | null, | |
242 | XSDDatatype.XSDbase64Binary) ) ); | |
243 | } | |
244 | ||
245 | 7 | if ( listOfAuthorityProperties.contains( SWP.X509Certificate ) ) |
246 | { | |
247 | // We need code for publishing information about a X509 certificate here, using the SWP-2 and the XML-Sig vocabulary | |
248 | try { | |
249 | 1 | graphP.add( new Triple( this.getID(), |
250 | SWP.X509Certificate, | |
251 | Node.createLiteral( new String( Base64.encodeBase64( this.getCertificate().getEncoded() ) ), | |
252 | null, | |
253 | XSDDatatype.XSDbase64Binary ) ) ); | |
254 | } | |
255 | 0 | catch ( AddDeniedException e ) |
256 | { | |
257 | 0 | return false; |
258 | } | |
259 | 0 | catch ( DatatypeFormatException e ) |
260 | { | |
261 | 0 | return false; |
262 | } | |
263 | 0 | catch ( CertificateEncodingException e ) |
264 | { | |
265 | 0 | return false; |
266 | 1 | } |
267 | } | |
268 | } | |
269 | 11 | else if ( this.getCertificate() != null) |
270 | { | |
271 | try { | |
272 | 11 | graphP.add( new Triple( this.getID(), |
273 | SWP.X509Certificate, | |
274 | Node.createLiteral( new String( Base64.encodeBase64( this.getCertificate().getEncoded() ) ), | |
275 | null, | |
276 | XSDDatatype.XSDbase64Binary ) ) ); | |
277 | 0 | } catch (AddDeniedException e) { |
278 | 0 | throw new RuntimeException(e); |
279 | 0 | } catch (DatatypeFormatException e) { |
280 | 0 | throw new RuntimeException(e); |
281 | 0 | } catch (CertificateEncodingException e) { |
282 | 0 | throw new RuntimeException(e); |
283 | 11 | } |
284 | } | |
285 | 18 | this.graph = graphP; |
286 | ||
287 | 18 | return true; |
288 | } | |
289 | ||
290 | ||
291 | /** | |
292 | * | |
293 | * Returns a graph containing all information about the authority. | |
294 | * | |
295 | * @return graph | |
296 | * | |
297 | */ | |
298 | public Graph getGraph() | |
299 | { | |
300 | 0 | return this.graph; |
301 | } | |
302 | ||
303 | } | |
304 | ||
305 | /* | |
306 | * (c) Copyright 2004 - 2010 Chris Bizer (chris@bizer.de) | |
307 | * All rights reserved. | |
308 | * | |
309 | * Redistribution and use in source and binary forms, with or without | |
310 | * modification, are permitted provided that the following conditions | |
311 | * are met: | |
312 | * 1. Redistributions of source code must retain the above copyright | |
313 | * notice, this list of conditions and the following disclaimer. | |
314 | * 2. Redistributions in binary form must reproduce the above copyright | |
315 | * notice, this list of conditions and the following disclaimer in the | |
316 | * documentation and/or other materials provided with the distribution. | |
317 | * 3. The name of the author may not be used to endorse or promote products | |
318 | * derived from this software without specific prior written permission. | |
319 | * | |
320 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
321 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
322 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
323 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |
324 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
325 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
326 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
327 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
328 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
329 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
330 | * | |
331 | */ |