no.ks.fiks.io.asice.crypto.PipedEncryptionServiceImpl Maven / Gradle / Ivy
package no.ks.fiks.io.asice.crypto;
import com.google.common.base.Preconditions;
import no.ks.kryptering.CMSKrypteringImpl;
import no.ks.kryptering.CMSStreamKryptering;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.security.cert.X509Certificate;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ExecutorService;
public class PipedEncryptionServiceImpl implements PipedEncryptionService {
private static Logger log = LoggerFactory.getLogger(PipedEncryptionServiceImpl.class);
private final CMSStreamKryptering cmsKryptoHandler = new CMSKrypteringImpl();
private final ExecutorService executor;
public PipedEncryptionServiceImpl(ExecutorService executor) {
Preconditions.checkNotNull(executor);
this.executor = executor;
}
@Override
public PipedInputStream encrypt(PipedInputStream pipedInputStream, X509Certificate mottakerSertifikat) {
Preconditions.checkNotNull(pipedInputStream);
Preconditions.checkNotNull(mottakerSertifikat);
final Map mdc = MDC.getCopyOfContextMap();
try {
final PipedInputStream kryptertInputStream = new PipedInputStream();
final PipedOutputStream kryptertOutputStream = new PipedOutputStream(kryptertInputStream);
executor.execute(() -> {
Optional.ofNullable(mdc).ifPresent(m -> MDC.setContextMap(m));
try (OutputStream krypteringStream = cmsKryptoHandler.getKrypteringOutputStream(kryptertOutputStream, mottakerSertifikat)) {
IOUtils.copy(pipedInputStream, krypteringStream);
} catch (IOException e) {
log.error("Failed to copy stream", e);
try {
kryptertInputStream.close();
} catch (IOException ex) {
log.warn("Fikk ikke lukket strøm");
}
throw new RuntimeException(e);
} finally {
try {
kryptertOutputStream.close();
} catch (IOException e) {
log.error("Uventet feil under cleanup", e);
}
MDC.clear();
}
});
return kryptertInputStream;
} catch (IOException e) {
log.warn("Feilet under kryptering", e);
throw new RuntimeException("Kryptering feilet", e);
}
}
}