} else {
// private post
entry.setRights(Common.RIGHTS_RESERVED);
try {
StringWriter stringWriter = new StringWriter();
StreamWriter writer = Abdera.getInstance()
.getWriterFactory().newStreamWriter();
writer.setWriter(stringWriter);
writer.startEntry();
writer.writeId(entry.getId());
writer.writeUpdated(entry.getUpdated());
writer.writePublished(entry.getPublished());
if (predecessor != null) {
writer.startElement(Common.PREDECESSOR, Common.NS_URI);
writer.writeElementText(predecessor);
writer.endElement();
}
if (options.publicOptions != null) {
// these are options that will be publicly visible
if (options.publicOptions.status != null) {
writer.writeTitle(options.publicOptions.status);
} else {
writer.writeTitle(""); // empty title
}
if (options.publicOptions.body != null) {
writer.writeSummary(options.publicOptions.body);
}
if (options.publicOptions.verb != null) {
writer.startElement("verb",
"http://activitystrea.ms/spec/1.0/");
writer.writeElementText(options.publicOptions.verb);
writer.endElement();
}
if (options.publicOptions.tags != null) {
for (String s : options.publicOptions.tags) {
writer.writeCategory(s);
}
}
if (options.publicOptions.mentions != null) {
for (String s : options.publicOptions.mentions) {
writer.startElement("mention", Common.NS_URI,
"trsst");
writer.writeElementText(s);
writer.endElement();
}
}
} else {
writer.writeTitle(""); // empty title
}
writer.startContent("application/xenc+xml");
List<PublicKey> keys = new LinkedList<PublicKey>();
for (String id : options.recipientIds) {
// for each recipient
Feed recipientFeed = pull(id);
if (recipientFeed != null) {
// fetch encryption key
Element e = recipientFeed.getExtension(new QName(
Common.NS_URI, Common.ENCRYPT));
if (e == null) {
// fall back to signing key
e = recipientFeed.getExtension(new QName(
Common.NS_URI, Common.SIGN));
}
keys.add(Common.toPublicKeyFromX509(e.getText()));
}
}
// enforce the convention:
keys.remove(encryptionKeys.getPublic());
// move to end if exists;
// last encrypted key is for ourself
keys.add(encryptionKeys.getPublic());
// encrypt content key separately for each recipient
for (PublicKey recipient : keys) {
byte[] bytes = Crypto.encryptKeyWithIES(contentKey,
feed.getUpdated().getTime(), recipient,
encryptionKeys.getPrivate());
String encoded = new Base64(0, null, true)
.encodeToString(bytes);
writer.startElement("EncryptedData",
"http://www.w3.org/2001/04/xmlenc#");
writer.startElement("CipherData",
"http://www.w3.org/2001/04/xmlenc#");
writer.startElement("CipherValue",
"http://www.w3.org/2001/04/xmlenc#");
writer.writeElementText(encoded);
writer.endElement();
writer.endElement();
writer.endElement();
}
// now: encrypt the payload with content key
byte[] bytes = encryptElementAES(entry, contentKey);
String encoded = new Base64(0, null, true)
.encodeToString(bytes);
writer.startElement("EncryptedData",
"http://www.w3.org/2001/04/xmlenc#");
writer.startElement("CipherData",
"http://www.w3.org/2001/04/xmlenc#");
writer.startElement("CipherValue",
"http://www.w3.org/2001/04/xmlenc#");
writer.writeElementText(encoded);
writer.endElement();
writer.endElement();
writer.endElement();
// done with encrypted elements
writer.endContent();
writer.endEntry();
writer.flush();
// this constructed entry now replaces the encrypted
// entry
entry = (Entry) Abdera.getInstance().getParserFactory()
.getParser()
.parse(new StringReader(stringWriter.toString()))