/*
* This file is part of rockframework.
*
* rockframework is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* rockframework is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>;.
*/
package br.net.woodstock.rockframework.security.crypt.impl;
import java.io.IOException;
import java.io.OutputStream;
import java.security.PrivateKey;
import java.security.PublicKey;
import br.net.woodstock.rockframework.security.config.SecurityLog;
import br.net.woodstock.rockframework.security.crypt.CrypterException;
import br.net.woodstock.rockframework.security.crypt.CrypterWriter;
import br.net.woodstock.rockframework.util.Assert;
import br.net.woodstock.rockframework.xml.dom.XmlDocument;
import br.net.woodstock.rockframework.xml.dom.XmlElement;
public class AsyncCrypterWriter implements CrypterWriter<AsyncCrypter> {
public AsyncCrypterWriter() {
super();
}
@Override
public void write(final AsyncCrypter crypter, final OutputStream outputStream) {
Assert.notNull(crypter, "crypter");
Assert.notNull(outputStream, "outputStream");
try {
PrivateKey privateKey = crypter.getPrivateKey();
PublicKey publicKey = crypter.getPublicKey();
XmlDocument document = new XmlDocument(CrypterIOHelper.ASYNC_CRYPTER_ELEMENT);
XmlElement root = document.getRoot();
root.addElement(CrypterIOHelper.KEY_ALGORITHM_ELEMENT).setData(crypter.getAlgorithm());
if (privateKey != null) {
CrypterIOHelper.addKey(root, CrypterIOHelper.PRIVATE_KEY_ELEMENT, privateKey);
} else {
SecurityLog.getInstance().getLogger().info("Private key is null and not will be writed");
}
if (publicKey != null) {
CrypterIOHelper.addKey(root, CrypterIOHelper.PUBLIC_KEY_ELEMENT, publicKey);
} else {
SecurityLog.getInstance().getLogger().info("Public key is null and not will be writed");
}
document.write(outputStream);
} catch (IOException e) {
throw new CrypterException(e);
}
}
}