Search in sources :

Example 11 with Base64OutputStream

use of org.apache.commons.codec.binary.Base64OutputStream in project xwiki-platform by xwiki.

the class DOMXMLWriter method writeBase64.

/**
 * {@inheritDoc}
 * <p>
 * Add the element into the <code>{@link Document}</code> as a children of the element at the top of the stack of
 * opened elements, putting the whole stream content as Base64 text in the content of the
 * <code>{@link Element}</code>.
 * </p>
 *
 * @see com.xpn.xwiki.internal.xml.XMLWriter#writeBase64(org.dom4j.Element, java.io.InputStream)
 */
@Override
public void writeBase64(Element element, InputStream is) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Base64OutputStream out = new Base64OutputStream(baos, true, 0, null);
    IOUtils.copy(is, out);
    out.close();
    element.addText(baos.toString(this.format.getEncoding()));
    write(element);
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) Base64OutputStream(org.apache.commons.codec.binary.Base64OutputStream)

Example 12 with Base64OutputStream

use of org.apache.commons.codec.binary.Base64OutputStream in project xwiki-platform by xwiki.

the class XMLWriter method writeBase64.

/**
 * Writes the <code>{@link Element}</code>, including its <code>{@link
 * Attribute}</code>s, using the
 * <code>{@link InputStream}</code> encoded in Base64 for its content.
 *
 * @param element <code>{@link Element}</code> to output.
 * @param is <code>{@link InputStream}</code> that will be fully read and encoded
 * in Base64 into the element content.
 * @throws IOException a problem occurs during reading or writing.
 */
public void writeBase64(final Element element, final InputStream is) throws IOException {
    this.writeOpen(element);
    super.writePrintln();
    super.flush();
    final Base64OutputStream base64 = new Base64OutputStream(new CloseShieldOutputStream(this.out), true, BASE64_WIDTH, NEWLINE);
    IOUtils.copy(is, base64);
    base64.close();
    // The last char written was a newline, not a > so it will not indent unless it is done manually.
    super.setIndentLevel(this.parent.size() - 1);
    super.indent();
    this.writeClose(element);
}
Also used : Base64OutputStream(org.apache.commons.codec.binary.Base64OutputStream) CloseShieldOutputStream(org.apache.commons.io.output.CloseShieldOutputStream)

Example 13 with Base64OutputStream

use of org.apache.commons.codec.binary.Base64OutputStream in project candlepin by candlepin.

the class CrlFileUtil method updateCRLFile.

/**
 * Updates the specified CRL file by adding or removing entries. If both lists are either null
 * or empty, the CRL file will not be modified by this method. If the file does not exist or
 * appears to be empty, it will be initialized before processing the lists.
 *
 * @param file
 *  The CRL file to update
 *
 * @param revoke
 *  A collection of serials to revoke (add)
 *
 * @param unrevoke
 *  A collection of serials to unrevoke (remove)
 *
 * @throws IOException
 *  if an IO error occurs while updating the CRL file
 */
public void updateCRLFile(File file, final Collection<BigInteger> revoke, final Collection<BigInteger> unrevoke) throws IOException {
    if (!file.exists() || file.length() == 0) {
        this.initializeCRLFile(file, revoke);
        return;
    }
    File strippedFile = stripCRLFile(file);
    InputStream input = null;
    InputStream reaper = null;
    BufferedOutputStream output = null;
    OutputStream filter = null;
    OutputStream encoder = null;
    try {
        // Impl note:
        // Due to the way the X509CRLStreamWriter works (and the DER format in general), we have
        // to make two passes through the file.
        input = new Base64InputStream(new FileInputStream(strippedFile));
        reaper = new Base64InputStream(new FileInputStream(strippedFile));
        // Note: This will break if we ever stop using RSA keys
        PrivateKey key = this.certificateReader.getCaKey();
        X509CRLStreamWriter writer = new X509CRLStreamWriter(input, (RSAPrivateKey) key, this.certificateReader.getCACert());
        // Add new entries
        if (revoke != null) {
            Date now = new Date();
            for (BigInteger serial : revoke) {
                writer.add(serial, now, CRLReason.privilegeWithdrawn);
            }
        }
        // or we could miss cases where we have entries to remove, but nothing to add.
        if (unrevoke != null && !unrevoke.isEmpty()) {
            writer.preScan(reaper, new CRLEntryValidator() {

                public boolean shouldDelete(CRLEntry entry) {
                    BigInteger certSerial = entry.getUserCertificate().getValue();
                    return unrevoke.contains(certSerial);
                }
            });
        } else {
            writer.preScan(reaper);
        }
        writer.setSigningAlgorithm(PKIUtility.SIGNATURE_ALGO);
        // Verify we actually have work to do now
        if (writer.hasChangesQueued()) {
            output = new BufferedOutputStream(new FileOutputStream(file));
            filter = new FilterOutputStream(output) {

                private boolean needsLineBreak = true;

                public void write(int b) throws IOException {
                    this.needsLineBreak = (b != (byte) '\n');
                    super.write(b);
                }

                public void write(byte[] buffer) throws IOException {
                    this.needsLineBreak = (buffer[buffer.length - 1] != (byte) '\n');
                    super.write(buffer);
                }

                public void write(byte[] buffer, int off, int len) throws IOException {
                    this.needsLineBreak = (buffer[off + len - 1] != (byte) '\n');
                    super.write(buffer, off, len);
                }

                public void close() throws IOException {
                    if (this.needsLineBreak) {
                        super.write((int) '\n');
                        this.needsLineBreak = false;
                    }
                // Impl note:
                // We're intentionally not propagating the call here.
                }
            };
            encoder = new Base64OutputStream(filter, true, 76, new byte[] { (byte) '\n' });
            output.write("-----BEGIN X509 CRL-----\n".getBytes());
            writer.lock();
            writer.write(encoder);
            encoder.close();
            filter.close();
            output.write("-----END X509 CRL-----\n".getBytes());
            output.close();
        }
    } catch (GeneralSecurityException e) {
        // This should never actually happen
        log.error("Unexpected security error occurred while retrieving CA key", e);
    } catch (CryptoException e) {
        // Something went horribly wrong with the stream writer
        log.error("Unexpected error occurred while writing new CRL file", e);
    } finally {
        for (Closeable stream : Arrays.asList(encoder, output, reaper, input)) {
            if (stream != null) {
                try {
                    stream.close();
                } catch (IOException e) {
                    log.error("Unexpected exception occurred while closing stream: {}", stream, e);
                }
            }
        }
        if (!strippedFile.delete()) {
            log.error("Unable to delete temporary CRL file: {}", strippedFile);
        }
    }
}
Also used : RSAPrivateKey(java.security.interfaces.RSAPrivateKey) PrivateKey(java.security.PrivateKey) FileInputStream(java.io.FileInputStream) Base64InputStream(org.apache.commons.codec.binary.Base64InputStream) InputStream(java.io.InputStream) BufferedOutputStream(java.io.BufferedOutputStream) Base64OutputStream(org.apache.commons.codec.binary.Base64OutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FilterOutputStream(java.io.FilterOutputStream) GeneralSecurityException(java.security.GeneralSecurityException) Closeable(java.io.Closeable) CRLEntry(org.bouncycastle.asn1.x509.TBSCertList.CRLEntry) IOException(java.io.IOException) Base64OutputStream(org.apache.commons.codec.binary.Base64OutputStream) FileInputStream(java.io.FileInputStream) Date(java.util.Date) FileOutputStream(java.io.FileOutputStream) BigInteger(java.math.BigInteger) Base64InputStream(org.apache.commons.codec.binary.Base64InputStream) FilterOutputStream(java.io.FilterOutputStream) CryptoException(org.bouncycastle.crypto.CryptoException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 14 with Base64OutputStream

use of org.apache.commons.codec.binary.Base64OutputStream in project bw-calendar-engine by Bedework.

the class BwResourceContent method getEncodedContent.

/* ====================================================================
   *                   non-db methods
   * ==================================================================== */
/**
 * @return base64 encoded value
 * @throws CalFacadeException
 */
public String getEncodedContent() throws CalFacadeException {
    Base64OutputStream b64out = null;
    try {
        Blob b = getValue();
        if (b == null) {
            return null;
        }
        int len = -1;
        final int chunkSize = 1024;
        final byte[] buffer = new byte[chunkSize];
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        b64out = new Base64OutputStream(baos);
        final InputStream str = b.getBinaryStream();
        while ((len = str.read(buffer)) != -1) {
            b64out.write(buffer, 0, len);
        }
        return new String(baos.toByteArray());
    } catch (final Throwable t) {
        throw new CalFacadeException(t);
    } finally {
        try {
            b64out.close();
        } catch (Throwable t) {
        }
    }
}
Also used : Blob(java.sql.Blob) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ToString(org.bedework.util.misc.ToString) Base64OutputStream(org.apache.commons.codec.binary.Base64OutputStream) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException)

Aggregations

Base64OutputStream (org.apache.commons.codec.binary.Base64OutputStream)14 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 InputStream (java.io.InputStream)4 OutputStream (java.io.OutputStream)4 GZIPOutputStream (java.util.zip.GZIPOutputStream)4 Base64InputStream (org.apache.commons.codec.binary.Base64InputStream)4 CloseShieldOutputStream (org.apache.commons.io.output.CloseShieldOutputStream)2 LivyCluster (com.microsoft.azure.hdinsight.sdk.cluster.LivyCluster)1 HDIException (com.microsoft.azure.hdinsight.sdk.common.HDIException)1 SparkSession (com.microsoft.azure.hdinsight.sdk.common.livy.interactive.SparkSession)1 ClusterFileBase64BufferedOutputStream (com.microsoft.azure.hdinsight.sdk.io.spark.ClusterFileBase64BufferedOutputStream)1 Ack (io.socket.client.Ack)1 BufferedOutputStream (java.io.BufferedOutputStream)1 Closeable (java.io.Closeable)1 DataOutputStream (java.io.DataOutputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 FilterOutputStream (java.io.FilterOutputStream)1 IOException (java.io.IOException)1