ezvcard.property
Class Key

java.lang.Object
  extended by ezvcard.property.VCardProperty
      extended by ezvcard.property.BinaryProperty<KeyType>
          extended by ezvcard.property.Key
All Implemented Interfaces:
HasAltId, Comparable<VCardProperty>

public class Key
extends BinaryProperty<KeyType>

A public key for encryption.

Adding a key

 VCard vcard = new VCard();
 
 //URL (vCard 4.0 only; KEYs cannot have URLs in vCard 2.1 and 3.0)
 Key key = new Key("http://www.mywebsite.com/pubkey.pgp", KeyType.PGP);
 vcard.addKey(key);
 
 //binary data
 byte data[] = ...
 key = new Key(data, KeyType.PGP);
 vcard.addKey(key);
 
 //plain text value
 key = new Key();
 key.setText("...", KeyType.PGP);
 vcard.addKey(key);
 
 //if "KeyType" does not have the pre-defined constant that you need, then create a new instance
 //arg 1: the value of the 2.1/3.0 TYPE parameter
 //arg 2: the value to use for the 4.0 MEDIATYPE parameter and for 4.0 data URIs
 //arg 3: the file extension of the data type (optional)
 KeyType param = new KeyType("mykey", "application/my-key", "mkey");
 key = new Key("http://www.mywebsite.com/pubkey.enc", param);
 vcard.addKey(key);
 

Getting the keys

 VCard vcard = ...
 
 int fileCount = 0;
 for (Key key : vcard.getKeys()){
   //the key will have either a URL or a binary data
   //only 4.0 vCards are allowed to use URLs for keys
   if (key.getData() == null){
     System.out.println("Key URL: " + key.getUrl());
   } else {
     KeyType type = key.getContentType();
     
     if (type == null) {
       //the vCard may not have any content type data associated with the key
       System.out.println("Saving a key file...");
     } else {
       System.out.println("Saving a \"" + type.getMediaType() + "\" file...");
     }
     
     String folder;
     if (type == KeyType.PGP){ //it is safe to use "==" instead of "equals()"
       folder = "pgp-keys";
     } else {
       folder = "other-keys";
     }
     
     byte data[] = key.getData();
     String filename = "key" + fileCount;
     if (type != null && type.getExtension() != null){
        filename += "." + type.getExtension();
     }
     OutputStream out = new FileOutputStream(new File(folder, filename));
     out.write(data);
     out.close();
     fileCount++;
   }
 }
 

Property name: KEY

Supported versions: 2.1, 3.0, 4.0

Author:
Michael Angstadt

Field Summary
 
Fields inherited from class ezvcard.property.BinaryProperty
contentType, data, url
 
Fields inherited from class ezvcard.property.VCardProperty
group, parameters
 
Constructor Summary
Key()
          Creates an empty key property.
Key(byte[] data, KeyType type)
          Creates a key property.
Key(File file, KeyType type)
          Creates a key property.
Key(InputStream in, KeyType type)
          Creates a key property.
Key(String url, KeyType type)
          Creates a key property.
 
Method Summary
protected  void _validate(List<Warning> warnings, VCardVersion version, VCard vcard)
          Checks the property for data consistency problems or deviations from the spec.
 String getText()
          Gets the plain text representation of the key.
 void setText(String text, KeyType type)
          Sets a plain text representation of the key.
 
Methods inherited from class ezvcard.property.BinaryProperty
addPid, getAltId, getContentType, getData, getPids, getPref, getType, getUrl, removePids, setAltId, setContentType, setData, setPref, setType, setUrl
 
Methods inherited from class ezvcard.property.VCardProperty
_supportedVersions, addParameter, compareTo, getGroup, getParameter, getParameters, getParameters, getSupportedVersions, removeParameter, setGroup, setParameter, setParameters, validate
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Key

public Key()
Creates an empty key property.


Key

public Key(byte[] data,
           KeyType type)
Creates a key property.

Parameters:
data - the binary data
type - the type of key (e.g. PGP)

Key

public Key(String url,
           KeyType type)
Creates a key property.

Parameters:
url - the URL to the key (vCard 4.0 only)
type - the type of key (e.g. PGP)

Key

public Key(InputStream in,
           KeyType type)
    throws IOException
Creates a key property.

Parameters:
in - an input stream to the binary data (will be closed)
type - the content type (e.g. PGP)
Throws:
IOException - if there's a problem reading from the input stream

Key

public Key(File file,
           KeyType type)
    throws IOException
Creates a key property.

Parameters:
file - the key file
type - the content type (e.g. PGP)
Throws:
IOException - if there's a problem reading from the file
Method Detail

setText

public void setText(String text,
                    KeyType type)
Sets a plain text representation of the key.

Parameters:
text - the key in plain text
type - the key type

getText

public String getText()
Gets the plain text representation of the key.

Returns:
the key in plain text

_validate

protected void _validate(List<Warning> warnings,
                         VCardVersion version,
                         VCard vcard)
Description copied from class: VCardProperty
Checks the property for data consistency problems or deviations from the spec. Meant to be overridden by child classes that wish to provide validation logic.

Overrides:
_validate in class BinaryProperty<KeyType>
Parameters:
warnings - the list to add the warnings to
version - the version to check the property against
vcard - the vCard this property belongs to


Copyright © 2012-2014 Michael Angstadt. All Rights Reserved.