1
1
package i5 .las2peer .persistency ;
2
2
3
- import java .io .ByteArrayInputStream ;
4
- import java .io .IOException ;
5
- import java .io .ObjectInputStream ;
6
- import java .io .ObjectStreamClass ;
7
- import java .io .Serializable ;
8
- import java .io .UnsupportedEncodingException ;
9
- import java .util .Date ;
10
- import java .util .Enumeration ;
11
- import java .util .Hashtable ;
12
- import java .util .Random ;
13
-
14
- import javax .crypto .SecretKey ;
15
-
16
- import org .apache .commons .codec .binary .Base64 ;
17
-
18
3
import i5 .las2peer .execution .L2pThread ;
19
4
import i5 .las2peer .p2p .ArtifactNotFoundException ;
20
5
import i5 .las2peer .p2p .StorageException ;
32
17
import i5 .simpleXML .Parser ;
33
18
import i5 .simpleXML .XMLSyntaxException ;
34
19
20
+ import java .io .ByteArrayInputStream ;
21
+ import java .io .IOException ;
22
+ import java .io .ObjectInputStream ;
23
+ import java .io .ObjectStreamClass ;
24
+ import java .io .Serializable ;
25
+ import java .nio .charset .StandardCharsets ;
26
+ import java .util .Date ;
27
+ import java .util .Enumeration ;
28
+ import java .util .Hashtable ;
29
+ import java .util .Random ;
30
+
31
+ import javax .crypto .SecretKey ;
32
+
33
+ import org .apache .commons .codec .binary .Base64 ;
34
+
35
35
/**
36
36
* An envelope provides a secure storage for any {@link Serializable} content within the LAS2peer network.
37
37
*
@@ -211,12 +211,10 @@ private void initReaders(Agent[] readers) throws EncodingFailedException {
211
211
* @param content
212
212
* @param reader
213
213
*
214
- * @throws UnsupportedEncodingException
215
214
* @throws EncodingFailedException
216
215
* @throws DecodingFailedException
217
216
*/
218
- public Envelope (String content , Agent reader )
219
- throws UnsupportedEncodingException , EncodingFailedException , DecodingFailedException {
217
+ public Envelope (String content , Agent reader ) throws EncodingFailedException , DecodingFailedException {
220
218
this (content , new Agent [] { reader });
221
219
}
222
220
@@ -228,10 +226,9 @@ public Envelope(String content, Agent reader)
228
226
* @param content
229
227
* @param readers
230
228
*
231
- * @throws UnsupportedEncodingException
232
229
* @throws EncodingFailedException
233
230
*/
234
- public Envelope (String content , Agent [] readers ) throws UnsupportedEncodingException , EncodingFailedException {
231
+ public Envelope (String content , Agent [] readers ) throws EncodingFailedException {
235
232
this (content , readers , new Random ().nextLong ());
236
233
}
237
234
@@ -243,11 +240,9 @@ public Envelope(String content, Agent[] readers) throws UnsupportedEncodingExcep
243
240
* @param content
244
241
* @param readers
245
242
* @param id
246
- * @throws UnsupportedEncodingException
247
243
* @throws EncodingFailedException
248
244
*/
249
- private Envelope (String content , Agent [] readers , long id )
250
- throws UnsupportedEncodingException , EncodingFailedException {
245
+ private Envelope (String content , Agent [] readers , long id ) throws EncodingFailedException {
251
246
this .id = id ;
252
247
253
248
initKey ();
@@ -270,10 +265,9 @@ private Envelope(String content, Agent[] readers, long id)
270
265
* @param content
271
266
* @param readers
272
267
*
273
- * @throws UnsupportedEncodingException
274
268
* @throws EncodingFailedException
275
269
*/
276
- public Envelope (XmlAble content , Agent [] readers ) throws UnsupportedEncodingException , EncodingFailedException {
270
+ public Envelope (XmlAble content , Agent [] readers ) throws EncodingFailedException {
277
271
this (content , readers , new Random ().nextLong ());
278
272
}
279
273
@@ -288,10 +282,8 @@ public Envelope(XmlAble content, Agent[] readers) throws UnsupportedEncodingExce
288
282
* @param readers
289
283
* @param id
290
284
* @throws EncodingFailedException
291
- * @throws UnsupportedEncodingException
292
285
*/
293
- private Envelope (XmlAble content , Agent [] readers , long id )
294
- throws UnsupportedEncodingException , EncodingFailedException {
286
+ private Envelope (XmlAble content , Agent [] readers , long id ) throws EncodingFailedException {
295
287
this .id = id ;
296
288
297
289
initKey ();
@@ -315,7 +307,7 @@ private Envelope(XmlAble content, Agent[] readers, long id)
315
307
*
316
308
* @param content
317
309
* @param reader
318
- * @throws EnvelopeException
310
+ * @throws EnvelopeException
319
311
*
320
312
* @throws EncodingFailedException
321
313
* @throws SerializationException
@@ -353,8 +345,8 @@ public Envelope(Serializable content, Agent[] readers) throws EncodingFailedExce
353
345
* @throws EncodingFailedException
354
346
* @throws SerializationException
355
347
*/
356
- private Envelope (Serializable content , Agent [] readers , long id )
357
- throws EncodingFailedException , SerializationException {
348
+ private Envelope (Serializable content , Agent [] readers , long id ) throws EncodingFailedException ,
349
+ SerializationException {
358
350
this .id = id ;
359
351
360
352
initKey ();
@@ -434,7 +426,7 @@ public void open(Agent agent) throws DecodingFailedException, L2pSecurityExcepti
434
426
throw new L2pSecurityException ("agent " + agent .getId () + " has no access to this object" );
435
427
}
436
428
437
- symmetricKey = ( SecretKey ) agent .returnSecretKey (encoded );
429
+ symmetricKey = agent .returnSecretKey (encoded );
438
430
openedBy = agent ;
439
431
440
432
decryptData ();
@@ -561,6 +553,21 @@ public void removeReader(Agent agent) throws L2pSecurityException {
561
553
htEncryptedKeys .remove (agent .getId ());
562
554
}
563
555
556
+ /**
557
+ * checks if an agent is reader
558
+ *
559
+ * Attention: only direct reading access will be checked, no access gained via group memberships
560
+ *
561
+ * @param agent agent to check
562
+ * @return true if and only if the given agent is a reader
563
+ */
564
+ public boolean hasReader (Agent agent ) {
565
+ if (agent instanceof GroupAgent )
566
+ return htEncryptedGroupKeys .containsKey (agent .getId ());
567
+ else
568
+ return htEncryptedKeys .containsKey (agent .getId ());
569
+ }
570
+
564
571
/**
565
572
* add a signature for the content. only agents that signed the Evnelope have writing access. if no signature is
566
573
* given, every reader can write to the envelope.
@@ -695,17 +702,15 @@ public byte[] getContentAsBinary() throws DecodingFailedException {
695
702
* returns the contents of this envelope as string
696
703
*
697
704
* @return content as string
698
- * @throws EnvelopeException
705
+ * @throws EnvelopeException
699
706
*
700
707
* @throws DecodingFailedException
701
708
*/
702
709
public String getContentAsString () throws EnvelopeException {
703
710
byte [] content = null ;
704
711
try {
705
712
content = getContentAsBinary ();
706
- return new String (content , "UTF-8" );
707
- } catch (UnsupportedEncodingException e ) {
708
- return new String (content );
713
+ return new String (content , StandardCharsets .UTF_8 );
709
714
} catch (Exception e ) {
710
715
throw new EnvelopeException ("Coding problems with interpreting the content" , e );
711
716
}
@@ -743,6 +748,15 @@ public XmlAble getContentAsXmlAble() throws EnvelopeException {
743
748
}
744
749
}
745
750
751
+ /**
752
+ * get a list with all ids of non-group agents entitled to read this envelope
753
+ *
754
+ * @return array with all agent ids
755
+ */
756
+ public Long [] getReader () {
757
+ return htEncryptedKeys .keySet ().toArray (new Long [0 ]);
758
+ }
759
+
746
760
/**
747
761
* get a list with all ids of groups entitled to read this envelope
748
762
*
@@ -780,7 +794,8 @@ public Serializable getContentAsSerializable() throws EnvelopeException {
780
794
781
795
/**
782
796
* Get the content as deserialized object. This method uses the same class loader as the calling class.
783
- * @param <T>
797
+ *
798
+ * @param <T>
784
799
*
785
800
* @param cls
786
801
* @return the typed content of this envelope
@@ -799,7 +814,8 @@ public <T extends Serializable> T getContent(Class<T> cls) throws EnvelopeExcept
799
814
800
815
/**
801
816
* Get the content as deserialized object.
802
- * @param <T>
817
+ *
818
+ * @param <T>
803
819
*
804
820
* @param cls
805
821
* @param classLoader
@@ -837,8 +853,9 @@ protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, Clas
837
853
838
854
/**
839
855
* @return a XML (string) representation of this envelope
840
- * @throws SerializationException
856
+ * @throws SerializationException
841
857
*/
858
+ @ Override
842
859
public String toXmlString () throws SerializationException {
843
860
if (baPlainData != null && baCipherData == null ) {
844
861
try {
@@ -848,8 +865,8 @@ public String toXmlString() throws SerializationException {
848
865
}
849
866
}
850
867
851
- String encodedKeys = "\t <las2peer:keys encoding=\" base64\" encryption=\" " + CryptoTools . getAsymmetricAlgorithm ()
852
- + "\" >\n " ;
868
+ String encodedKeys = "\t <las2peer:keys encoding=\" base64\" encryption=\" "
869
+ + CryptoTools . getAsymmetricAlgorithm () + "\" >\n " ;
853
870
for (Long id : htEncryptedKeys .keySet ()) {
854
871
encodedKeys += "\t \t <las2peer:key id=\" " + id + "\" >" + Base64 .encodeBase64String (htEncryptedKeys .get (id ))
855
872
+ "</las2peer:key>\n " ;
@@ -994,11 +1011,11 @@ public static Envelope createFromXml(Element root) throws MalformedXMLException
994
1011
if (!keys .getName ().equals ("keys" ))
995
1012
throw new MalformedXMLException ("not an envelope" );
996
1013
if (!keys .getAttribute ("encoding" ).equals ("base64" ))
997
- throw new MalformedXMLException (
998
- "base 64 encoding of the content expected - got: " + keys .getAttribute ("encoding" ));
1014
+ throw new MalformedXMLException ("base 64 encoding of the content expected - got: "
1015
+ + keys .getAttribute ("encoding" ));
999
1016
if (!keys .getAttribute ("encryption" ).equals (CryptoTools .getAsymmetricAlgorithm ()))
1000
- throw new MalformedXMLException (
1001
- CryptoTools . getAsymmetricAlgorithm () + " encryption of the content expected" );
1017
+ throw new MalformedXMLException (CryptoTools . getAsymmetricAlgorithm ()
1018
+ + " encryption of the content expected" );
1002
1019
1003
1020
for (Enumeration <Element > enKeys = keys .getChildren (); enKeys .hasMoreElements ();) {
1004
1021
Element key = enKeys .nextElement ();
@@ -1019,8 +1036,8 @@ public static Envelope createFromXml(Element root) throws MalformedXMLException
1019
1036
if (!signatures .getName ().equals ("signatures" ))
1020
1037
throw new MalformedXMLException ("signatures expected" );
1021
1038
if (!signatures .getAttribute ("encoding" ).equals ("base64" ))
1022
- throw new MalformedXMLException (
1023
- "base 64 encoding of the content expected - got: " + keys .getAttribute ("encoding" ));
1039
+ throw new MalformedXMLException ("base 64 encoding of the content expected - got: "
1040
+ + keys .getAttribute ("encoding" ));
1024
1041
if (!signatures .getAttribute ("method" ).equals (CryptoTools .getSignatureMethod ()))
1025
1042
throw new MalformedXMLException (CryptoTools .getSignatureMethod () + " expected as signature method" );
1026
1043
@@ -1045,7 +1062,7 @@ public static Envelope createFromXml(Element root) throws MalformedXMLException
1045
1062
* get a locked copy of this agent
1046
1063
*
1047
1064
* @return a locked clone of this envelope
1048
- * @throws EnvelopeException
1065
+ * @throws EnvelopeException
1049
1066
* @throws EncodingFailedException
1050
1067
*/
1051
1068
public final Envelope cloneLocked () throws EnvelopeException {
@@ -1129,10 +1146,9 @@ public static long getClassEnvelopeId(String cls, String identifier) {
1129
1146
*
1130
1147
* @throws EncodingFailedException
1131
1148
* @throws SerializationException
1132
- * @throws UnsupportedEncodingException
1133
1149
*/
1134
1150
public static Envelope createClassIdEnvelope (Object content , String identifier , Agent [] readers )
1135
- throws EncodingFailedException , SerializationException , UnsupportedEncodingException {
1151
+ throws EncodingFailedException , SerializationException {
1136
1152
if (content instanceof String )
1137
1153
return new Envelope ((String ) content , readers , getClassEnvelopeId (content .getClass (), identifier ));
1138
1154
else if (content instanceof XmlAble )
@@ -1156,10 +1172,9 @@ else if (content instanceof byte[])
1156
1172
*
1157
1173
* @throws SerializationException
1158
1174
* @throws EncodingFailedException
1159
- * @throws UnsupportedEncodingException
1160
1175
*/
1161
1176
public static Envelope createClassIdEnvelope (Object content , String identifier , Agent reader )
1162
- throws UnsupportedEncodingException , EncodingFailedException , SerializationException {
1177
+ throws EncodingFailedException , SerializationException {
1163
1178
return createClassIdEnvelope (content , identifier , new Agent [] { reader });
1164
1179
}
1165
1180
@@ -1188,8 +1203,8 @@ public static Envelope fetch(long id) throws ArtifactNotFoundException, StorageE
1188
1203
* @throws ArtifactNotFoundException
1189
1204
* @throws StorageException
1190
1205
*/
1191
- public static Envelope fetchClassIdEnvelope (Class <?> cls , String identifier )
1192
- throws ArtifactNotFoundException , StorageException {
1206
+ public static Envelope fetchClassIdEnvelope (Class <?> cls , String identifier ) throws ArtifactNotFoundException ,
1207
+ StorageException {
1193
1208
return Context .getCurrent ().getStoredObject (cls , identifier );
1194
1209
}
1195
1210
@@ -1239,11 +1254,10 @@ public void updateContent(byte[] content) throws L2pSecurityException {
1239
1254
*
1240
1255
* @param content
1241
1256
*
1242
- * @throws UnsupportedEncodingException
1243
1257
* @throws L2pSecurityException
1244
1258
*/
1245
- public void updateContent (String content ) throws UnsupportedEncodingException , L2pSecurityException {
1246
- updateContent (content .getBytes ("UTF-8" ));
1259
+ public void updateContent (String content ) throws L2pSecurityException {
1260
+ updateContent (content .getBytes (StandardCharsets . UTF_8 ));
1247
1261
contentType = ContentType .String ;
1248
1262
}
1249
1263
@@ -1265,10 +1279,9 @@ public void updateContent(Serializable content) throws L2pSecurityException, Ser
1265
1279
*
1266
1280
* @param content
1267
1281
* @throws L2pSecurityException
1268
- * @throws UnsupportedEncodingException
1269
- * @throws SerializationException
1282
+ * @throws SerializationException
1270
1283
*/
1271
- public void updateContent (XmlAble content ) throws UnsupportedEncodingException , L2pSecurityException , SerializationException {
1284
+ public void updateContent (XmlAble content ) throws L2pSecurityException , SerializationException {
1272
1285
updateContent (content .toXmlString ());
1273
1286
contentType = ContentType .XmlAble ;
1274
1287
clContentClass = content .getClass ();
@@ -1297,8 +1310,8 @@ public void checkOverwrite(Envelope envelope) throws L2pSecurityException {
1297
1310
return ;
1298
1311
}
1299
1312
1300
- throw new L2pSecurityException (
1301
- "Check for Overwriting envelope " + getId () + " failed: No needed signature is provided!" );
1313
+ throw new L2pSecurityException ("Check for Overwriting envelope " + getId ()
1314
+ + " failed: No needed signature is provided!" );
1302
1315
}
1303
1316
1304
1317
/**
0 commit comments