use of java.io.OutputStream in project camel by apache.
the class PGPKeyAccessDataFormat method marshal.
public void marshal(Exchange exchange, Object graph, OutputStream outputStream) throws Exception {
//NOPMD
List<String> userids = determineEncryptionUserIds(exchange);
List<PGPPublicKey> keys = publicKeyAccessor.getEncryptionKeys(exchange, userids);
if (keys.isEmpty()) {
throw new IllegalArgumentException("Cannot PGP encrypt message. No public encryption key found for the User Ids " + userids + " in the public keyring. Either specify other User IDs or add correct public keys to the keyring.");
}
exchange.getOut().setHeader(NUMBER_OF_ENCRYPTION_KEYS, Integer.valueOf(keys.size()));
InputStream input = ExchangeHelper.convertToMandatoryType(exchange, InputStream.class, graph);
if (armored) {
outputStream = new ArmoredOutputStream(outputStream);
}
PGPEncryptedDataGenerator encGen = new PGPEncryptedDataGenerator(new JcePGPDataEncryptorBuilder(findAlgorithm(exchange)).setWithIntegrityPacket(integrity).setSecureRandom(new SecureRandom()).setProvider(getProvider()));
// several keys can be added
for (PGPPublicKey key : keys) {
encGen.addMethod(new JcePublicKeyKeyEncryptionMethodGenerator(key));
}
OutputStream encOut = encGen.open(outputStream, new byte[BUFFER_SIZE]);
OutputStream comOut;
if (withCompressedDataPacket) {
PGPCompressedDataGenerator comData = new PGPCompressedDataGenerator(findCompressionAlgorithm(exchange));
comOut = new BufferedOutputStream(comData.open(encOut));
} else {
comOut = encOut;
LOG.debug("No Compressed Data packet is added");
}
List<PGPSignatureGenerator> sigGens = createSignatureGenerator(exchange, comOut);
PGPLiteralDataGenerator litData = new PGPLiteralDataGenerator();
String fileName = findFileName(exchange);
OutputStream litOut = litData.open(comOut, PGPLiteralData.BINARY, fileName, new Date(), new byte[BUFFER_SIZE]);
try {
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead;
while ((bytesRead = input.read(buffer)) != -1) {
litOut.write(buffer, 0, bytesRead);
if (sigGens != null && !sigGens.isEmpty()) {
for (PGPSignatureGenerator sigGen : sigGens) {
// not nested therefore it is the same for all
// can this be improved that we only do it for one sigGen and set the result on the others?
sigGen.update(buffer, 0, bytesRead);
}
}
litOut.flush();
}
} finally {
IOHelper.close(litOut);
if (sigGens != null && !sigGens.isEmpty()) {
// reverse order
for (int i = sigGens.size() - 1; i > -1; i--) {
PGPSignatureGenerator sigGen = sigGens.get(i);
sigGen.generate().encode(comOut);
}
}
IOHelper.close(comOut, encOut, outputStream, input);
}
}
use of java.io.OutputStream in project camel by apache.
the class PGPDataFormatTest method createEncryptedNonCompressedData.
void createEncryptedNonCompressedData(ByteArrayOutputStream bos, String keyringPath) throws Exception, IOException, PGPException, UnsupportedEncodingException {
PGPEncryptedDataGenerator encGen = new PGPEncryptedDataGenerator(new JcePGPDataEncryptorBuilder(SymmetricKeyAlgorithmTags.CAST5).setSecureRandom(new SecureRandom()).setProvider(getProvider()));
encGen.addMethod(new JcePublicKeyKeyEncryptionMethodGenerator(readPublicKey(keyringPath)));
OutputStream encOut = encGen.open(bos, new byte[512]);
PGPLiteralDataGenerator litData = new PGPLiteralDataGenerator();
OutputStream litOut = litData.open(encOut, PGPLiteralData.BINARY, PGPLiteralData.CONSOLE, new Date(), new byte[512]);
try {
litOut.write("Test Message Without Compression".getBytes("UTF-8"));
litOut.flush();
} finally {
IOHelper.close(litOut);
IOHelper.close(encOut, bos);
}
}
use of java.io.OutputStream in project camel by apache.
the class CamelOutputStream method commitOutputMessage.
private void commitOutputMessage() throws IOException {
ExchangePattern pattern;
if (isOneWay) {
pattern = ExchangePattern.InOnly;
} else {
pattern = ExchangePattern.InOut;
}
LOG.debug("send the message to endpoint {}", this.targetCamelEndpointUri);
final org.apache.camel.Exchange exchange = this.producer.createExchange(pattern);
exchange.setProperty(Exchange.TO_ENDPOINT, this.targetCamelEndpointUri);
CachedOutputStream outputStream = (CachedOutputStream) outMessage.getContent(OutputStream.class);
// Send out the request message here, copy the protocolHeader back
CxfHeaderHelper.propagateCxfToCamel(this.headerFilterStrategy, outMessage, exchange.getIn(), exchange);
// TODO support different encoding
exchange.getIn().setBody(outputStream.getInputStream());
LOG.debug("template sending request: ", exchange.getIn());
if (outMessage.getExchange().isSynchronous()) {
syncInvoke(exchange);
} else {
// submit the request to the work queue
asyncInvokeFromWorkQueue(exchange);
}
}
use of java.io.OutputStream in project camel by apache.
the class CamelTransportTestSupport method sendoutMessage.
protected void sendoutMessage(Conduit conduit, Message message, Boolean isOneWay, String content) throws IOException {
Exchange cxfExchange = message.getExchange();
if (cxfExchange == null) {
cxfExchange = new ExchangeImpl();
cxfExchange.setOneWay(isOneWay);
message.setExchange(cxfExchange);
cxfExchange.setInMessage(message);
}
try {
conduit.prepare(message);
} catch (IOException ex) {
assertFalse("CamelConduit can't perpare to send out message", false);
ex.printStackTrace();
}
OutputStream os = message.getContent(OutputStream.class);
assertTrue("The OutputStream should not be null ", os != null);
os.write(content.getBytes());
os.close();
}
use of java.io.OutputStream in project camel by apache.
the class CamelConduitTest method verifyMessageContent.
public void verifyMessageContent(Message message) {
OutputStream os = message.getContent(OutputStream.class);
assertTrue("OutputStream should not be null", os != null);
}
Aggregations