Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / gnu / javax / crypto / keyring / EnvelopeEntry.java
blob25b1dc2a04d99816a212e2fe9770d6b59fdb97ad
1 /* EnvelopeEntry.java --
2 Copyright (C) 2003, 2006 Free Software Foundation, Inc.
4 This file is a part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or (at
9 your option) any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
19 USA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.javax.crypto.keyring;
41 import java.io.ByteArrayOutputStream;
42 import java.io.DataInputStream;
43 import java.io.DataOutputStream;
44 import java.io.IOException;
46 import java.util.ArrayList;
47 import java.util.Date;
48 import java.util.Iterator;
49 import java.util.LinkedList;
50 import java.util.List;
51 import java.util.StringTokenizer;
53 /**
54 * An envelope entry is a generic container for some number of primitive
55 * and other envelope entries.
57 public abstract class EnvelopeEntry extends Entry
60 // Fields.
61 // ------------------------------------------------------------------------
63 /** The envelope that contains this one (if any). */
64 protected EnvelopeEntry containingEnvelope;
66 /** The contained entries. */
67 protected List entries;
69 // Constructor.
70 // ------------------------------------------------------------------------
72 public EnvelopeEntry(int type, Properties properties)
74 super(type, properties);
75 entries = new LinkedList();
76 if (this.properties.get("alias-list") != null)
78 this.properties.remove("alias-list");
82 protected EnvelopeEntry(int type)
84 super(type);
85 entries = new LinkedList();
88 // Instance methods.
89 // ------------------------------------------------------------------------
91 /**
92 * Adds an entry to this envelope.
94 * @param entry The entry to add.
96 public void add(Entry entry)
98 if (!containsEntry(entry))
100 if (entry instanceof EnvelopeEntry)
102 ((EnvelopeEntry) entry).setContainingEnvelope(this);
104 entries.add(entry);
105 payload = null;
106 makeAliasList();
111 * Tests if this envelope contains a primitive entry with the
112 * given alias.
114 * @param alias The alias to test.
115 * @return True if this envelope (or one of the contained envelopes)
116 * contains a primitive entry with the given alias.
118 public boolean containsAlias(String alias)
120 String aliases = getAliasList();
121 if (aliases == null)
123 return false;
125 StringTokenizer tok = new StringTokenizer(aliases, ";");
126 while (tok.hasMoreTokens())
128 if (tok.nextToken().equals(alias))
130 return true;
133 return false;
137 * Tests if this envelope contains the given entry.
139 * @param entry The entry to test.
140 * @return True if this envelope contains the given entry.
142 public boolean containsEntry(Entry entry)
144 if (entry instanceof EnvelopeEntry)
146 return entries.contains(entry);
148 else if (entry instanceof PrimitiveEntry)
150 for (Iterator it = entries.iterator(); it.hasNext();)
152 Entry e = (Entry) it.next();
153 if (e.equals(entry))
154 return true;
155 if ((e instanceof EnvelopeEntry)
156 && ((EnvelopeEntry) e).containsEntry(entry))
157 return true;
160 return false;
164 * Returns a copy of all entries this envelope contains.
166 * @return All contained entries.
168 public List getEntries()
170 return new ArrayList(entries);
174 * Gets all primitive entries that have the given alias. If there
175 * are any masked entries that contain the given alias, they will
176 * be returned as well.
178 * @param alias The alias of the entries to get.
179 * @return A list of all primitive entries that have the given alias.
181 public List get(String alias)
183 List result = new LinkedList();
184 for (Iterator it = entries.iterator(); it.hasNext();)
186 Entry e = (Entry) it.next();
187 if (e instanceof EnvelopeEntry)
189 if (!((EnvelopeEntry) e).containsAlias(alias))
191 continue;
193 if (e instanceof MaskableEnvelopeEntry)
195 if (((MaskableEnvelopeEntry) e).isMasked())
197 result.add(e);
198 continue;
201 result.addAll(((EnvelopeEntry) e).get(alias));
203 else if (e instanceof PrimitiveEntry)
205 if (((PrimitiveEntry) e).getAlias().equals(alias))
207 result.add(e);
211 return result;
215 * Returns the list of all aliases contained by this envelope,
216 * separated by a semicolon (';').
218 * @return The list of aliases.
220 public String getAliasList()
222 String list = properties.get("alias-list");
223 if (list == null)
225 return "";
227 else
229 return list;
234 * Removes the specified entry.
236 * @param entry The entry.
237 * @return True if an entry was removed.
239 public boolean remove(Entry entry)
241 boolean ret = false;
242 for (Iterator it = entries.iterator(); it.hasNext();)
244 Entry e = (Entry) it.next();
245 if (e instanceof EnvelopeEntry)
247 if (e == entry)
249 it.remove();
250 ret = true;
251 break;
253 if (((EnvelopeEntry) e).remove(entry))
255 ret = true;
256 break;
259 else if (e instanceof PrimitiveEntry)
261 if (((PrimitiveEntry) e).equals(entry))
263 it.remove();
264 ret = true;
265 break;
269 if (ret)
271 payload = null;
272 makeAliasList();
274 return ret;
278 * Removes all primitive entries that have the specified alias.
280 * @param alias The alias of the entries to remove.
282 public void remove(String alias)
284 for (Iterator it = entries.iterator(); it.hasNext();)
286 Entry e = (Entry) it.next();
287 if (e instanceof EnvelopeEntry)
289 ((EnvelopeEntry) e).remove(alias);
291 else if (e instanceof PrimitiveEntry)
293 if (((PrimitiveEntry) e).getAlias().equals(alias))
295 it.remove();
299 payload = null;
300 makeAliasList();
303 // Protected methods.
304 // ------------------------------------------------------------------------
306 protected void encodePayload() throws IOException
308 ByteArrayOutputStream bout = new ByteArrayOutputStream(1024);
309 DataOutputStream out = new DataOutputStream(bout);
310 for (Iterator it = entries.iterator(); it.hasNext();)
312 ((Entry) it.next()).encode(out);
316 protected void setContainingEnvelope(EnvelopeEntry e)
318 if (containingEnvelope != null)
320 throw new IllegalArgumentException("envelopes may not be shared");
322 containingEnvelope = e;
325 protected void decodeEnvelope(DataInputStream in) throws IOException
327 while (true)
329 int type = in.read();
330 switch (type)
332 case EncryptedEntry.TYPE:
333 add(EncryptedEntry.decode(in));
334 break;
335 case PasswordEncryptedEntry.TYPE:
336 add(PasswordEncryptedEntry.decode(in));
337 break;
338 case PasswordAuthenticatedEntry.TYPE:
339 add(PasswordAuthenticatedEntry.decode(in));
340 break;
341 case AuthenticatedEntry.TYPE:
342 add(AuthenticatedEntry.decode(in));
343 break;
344 case CompressedEntry.TYPE:
345 add(CompressedEntry.decode(in));
346 break;
347 case CertificateEntry.TYPE:
348 add(CertificateEntry.decode(in));
349 break;
350 case PublicKeyEntry.TYPE:
351 add(PublicKeyEntry.decode(in));
352 break;
353 case PrivateKeyEntry.TYPE:
354 add(PrivateKeyEntry.decode(in));
355 break;
356 case CertPathEntry.TYPE:
357 add(CertPathEntry.decode(in));
358 break;
359 case BinaryDataEntry.TYPE:
360 add(BinaryDataEntry.decode(in));
361 break;
362 case -1:
363 return;
364 default:
365 throw new MalformedKeyringException("unknown type " + type);
370 // Own methods.
371 // ------------------------------------------------------------------------
373 private void makeAliasList()
375 if (entries.isEmpty())
376 return;
377 StringBuffer buf = new StringBuffer();
378 for (Iterator it = entries.iterator(); it.hasNext();)
380 Entry entry = (Entry) it.next();
381 if (entry instanceof EnvelopeEntry)
383 buf.append(((EnvelopeEntry) entry).getAliasList());
385 else if (entry instanceof PrimitiveEntry)
387 buf.append(((PrimitiveEntry) entry).getAlias());
389 if (it.hasNext())
390 buf.append(';');
392 properties.put("alias-list", buf.toString());
393 if (containingEnvelope != null)
395 containingEnvelope.makeAliasList();