Search in sources :

Example 1 with OutputStream

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);
    }
}
Also used : PGPSignatureGenerator(org.bouncycastle.openpgp.PGPSignatureGenerator) InputStream(java.io.InputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) ArmoredOutputStream(org.bouncycastle.bcpg.ArmoredOutputStream) PGPCompressedDataGenerator(org.bouncycastle.openpgp.PGPCompressedDataGenerator) PGPPublicKey(org.bouncycastle.openpgp.PGPPublicKey) ArmoredOutputStream(org.bouncycastle.bcpg.ArmoredOutputStream) SecureRandom(java.security.SecureRandom) PGPLiteralDataGenerator(org.bouncycastle.openpgp.PGPLiteralDataGenerator) PGPEncryptedDataGenerator(org.bouncycastle.openpgp.PGPEncryptedDataGenerator) Date(java.util.Date) JcePublicKeyKeyEncryptionMethodGenerator(org.bouncycastle.openpgp.operator.jcajce.JcePublicKeyKeyEncryptionMethodGenerator) BufferedOutputStream(java.io.BufferedOutputStream) JcePGPDataEncryptorBuilder(org.bouncycastle.openpgp.operator.jcajce.JcePGPDataEncryptorBuilder)

Example 2 with OutputStream

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);
    }
}
Also used : BCPGOutputStream(org.bouncycastle.bcpg.BCPGOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) SecureRandom(java.security.SecureRandom) PGPLiteralDataGenerator(org.bouncycastle.openpgp.PGPLiteralDataGenerator) PGPEncryptedDataGenerator(org.bouncycastle.openpgp.PGPEncryptedDataGenerator) Date(java.util.Date) JcePGPDataEncryptorBuilder(org.bouncycastle.openpgp.operator.jcajce.JcePGPDataEncryptorBuilder) JcePublicKeyKeyEncryptionMethodGenerator(org.bouncycastle.openpgp.operator.jcajce.JcePublicKeyKeyEncryptionMethodGenerator)

Example 3 with OutputStream

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);
    }
}
Also used : ExchangePattern(org.apache.camel.ExchangePattern) OutputStream(java.io.OutputStream) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) Exchange(org.apache.camel.Exchange) CachedOutputStream(org.apache.cxf.io.CachedOutputStream)

Example 4 with OutputStream

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();
}
Also used : Exchange(org.apache.cxf.message.Exchange) OutputStream(java.io.OutputStream) IOException(java.io.IOException) ExchangeImpl(org.apache.cxf.message.ExchangeImpl)

Example 5 with OutputStream

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);
}
Also used : OutputStream(java.io.OutputStream)

Aggregations

OutputStream (java.io.OutputStream)3717 IOException (java.io.IOException)1470 FileOutputStream (java.io.FileOutputStream)1192 InputStream (java.io.InputStream)1171 File (java.io.File)808 ByteArrayOutputStream (java.io.ByteArrayOutputStream)751 Test (org.junit.Test)681 BufferedOutputStream (java.io.BufferedOutputStream)418 FileInputStream (java.io.FileInputStream)380 Socket (java.net.Socket)362 ByteArrayInputStream (java.io.ByteArrayInputStream)201 OutputStreamWriter (java.io.OutputStreamWriter)201 URL (java.net.URL)193 HttpURLConnection (java.net.HttpURLConnection)162 BufferedInputStream (java.io.BufferedInputStream)151 InputStreamReader (java.io.InputStreamReader)149 FileNotFoundException (java.io.FileNotFoundException)143 Path (org.apache.hadoop.fs.Path)143 Path (java.nio.file.Path)138 BufferedReader (java.io.BufferedReader)133