2021-10-07 15:48:52 +02:00
|
|
|
// SPDX-FileCopyrightText: 2021 Paul Schaub <vanitasvitae@fsfe.org>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2022-09-07 13:35:58 +02:00
|
|
|
package org.pgpainless.ascii_armor;
|
2021-02-19 19:50:36 +01:00
|
|
|
|
|
|
|
import java.io.OutputStream;
|
|
|
|
|
|
|
|
import org.bouncycastle.bcpg.ArmoredOutputStream;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Factory to create configured {@link ArmoredOutputStream ArmoredOutputStreams}.
|
2021-08-15 13:41:07 +02:00
|
|
|
* The configuration entails setting custom version and comment headers.
|
2021-02-19 19:50:36 +01:00
|
|
|
*/
|
2021-08-15 15:24:19 +02:00
|
|
|
public final class ArmoredOutputStreamFactory {
|
2021-02-19 19:50:36 +01:00
|
|
|
|
2021-08-14 13:56:16 +02:00
|
|
|
public static final String PGPAINLESS = "PGPainless";
|
2021-08-15 15:35:22 +02:00
|
|
|
private static String version = PGPAINLESS;
|
|
|
|
private static String[] comment = new String[0];
|
2021-02-19 19:50:36 +01:00
|
|
|
|
2021-08-15 15:24:19 +02:00
|
|
|
private ArmoredOutputStreamFactory() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-08-15 13:41:07 +02:00
|
|
|
/**
|
|
|
|
* Wrap an {@link OutputStream} inside a preconfigured {@link ArmoredOutputStream}.
|
|
|
|
*
|
|
|
|
* @param outputStream inner stream
|
|
|
|
* @return armored output stream
|
|
|
|
*/
|
2021-02-19 19:50:36 +01:00
|
|
|
public static ArmoredOutputStream get(OutputStream outputStream) {
|
|
|
|
ArmoredOutputStream armoredOutputStream = new ArmoredOutputStream(outputStream);
|
2021-08-15 15:35:22 +02:00
|
|
|
armoredOutputStream.setHeader(ArmorUtils.HEADER_VERSION, version);
|
|
|
|
for (String comment : comment) {
|
2021-08-14 13:56:16 +02:00
|
|
|
ArmorUtils.addCommentHeader(armoredOutputStream, comment);
|
|
|
|
}
|
2021-02-19 19:50:36 +01:00
|
|
|
return armoredOutputStream;
|
|
|
|
}
|
2021-08-14 13:56:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Overwrite the version header of ASCII armors with a custom value.
|
|
|
|
* Newlines in the version info string result in multiple version header entries.
|
|
|
|
*
|
2021-08-15 15:35:22 +02:00
|
|
|
* @param versionString version string
|
2021-08-14 13:56:16 +02:00
|
|
|
*/
|
2021-08-15 15:35:22 +02:00
|
|
|
public static void setVersionInfo(String versionString) {
|
|
|
|
if (versionString == null || versionString.trim().isEmpty()) {
|
2021-08-14 13:56:16 +02:00
|
|
|
throw new IllegalArgumentException("Version Info MUST NOT be null NOR empty.");
|
|
|
|
}
|
2021-08-15 15:35:22 +02:00
|
|
|
version = versionString;
|
2021-08-14 13:56:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset the version header to its default value of {@link #PGPAINLESS}.
|
|
|
|
*/
|
|
|
|
public static void resetVersionInfo() {
|
2021-08-15 15:35:22 +02:00
|
|
|
version = PGPAINLESS;
|
2021-08-14 13:56:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a comment header value in the ASCII armor header.
|
|
|
|
* If the comment contains newlines, it will be split into multiple header entries.
|
|
|
|
*
|
2022-02-24 01:08:23 +01:00
|
|
|
* @see org.pgpainless.encryption_signing.ProducerOptions#setComment(String) for how to set comments for
|
|
|
|
* individual messages.
|
|
|
|
*
|
2021-08-15 15:35:22 +02:00
|
|
|
* @param commentString comment
|
2021-08-14 13:56:16 +02:00
|
|
|
*/
|
2021-08-15 15:35:22 +02:00
|
|
|
public static void setComment(String commentString) {
|
|
|
|
if (commentString == null) {
|
2021-08-14 13:56:16 +02:00
|
|
|
throw new IllegalArgumentException("Comment cannot be null.");
|
|
|
|
}
|
2021-08-15 15:35:22 +02:00
|
|
|
String trimmed = commentString.trim();
|
2021-08-14 13:56:16 +02:00
|
|
|
if (trimmed.isEmpty()) {
|
|
|
|
throw new IllegalArgumentException("Comment cannot be empty.");
|
|
|
|
}
|
|
|
|
|
2021-08-15 15:35:22 +02:00
|
|
|
String[] lines = commentString.split("\n");
|
|
|
|
comment = lines;
|
2021-08-14 13:56:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset to the default of no comment headers.
|
|
|
|
*/
|
|
|
|
public static void resetComment() {
|
2021-08-15 15:35:22 +02:00
|
|
|
comment = new String[0];
|
2021-08-14 13:56:16 +02:00
|
|
|
}
|
2021-02-19 19:50:36 +01:00
|
|
|
}
|