Skip to content

Commit

Permalink
streams automatic closing
Browse files Browse the repository at this point in the history
  • Loading branch information
todvora committed Oct 7, 2016
1 parent 9a6c5c8 commit 191e56f
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>cz.tomasdvorak</groupId>
<artifactId>eet-client</artifactId>
<version>1.2</version>
<version>1.2.1</version>

<description>
Client / Connector for the #EET - registration of sales is an advanced system of online communication between entrepreneurs and Financial Authority.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import cz.tomasdvorak.eet.client.exceptions.DataSigningException;
import cz.tomasdvorak.eet.client.exceptions.InvalidKeystoreException;
import cz.tomasdvorak.eet.client.utils.IOUtils;
import cz.tomasdvorak.eet.client.utils.StringJoiner;
import org.apache.logging.log4j.Logger;
import org.apache.wss4j.common.crypto.Crypto;
Expand Down Expand Up @@ -33,6 +34,10 @@ public class ClientKey {
private final String alias;
private final ClientPasswordCallback clientPasswordCallback;

/**
* Create new ClientKey instance based on data provided in the stream together with the password
* @param inputStream will be closed automatically
*/
public ClientKey(final InputStream inputStream, final String password) throws InvalidKeystoreException {
this.password = password;
final KeyStore keystore = getKeyStore(inputStream, password);
Expand Down Expand Up @@ -73,6 +78,7 @@ private KeyStore getKeyStore(final InputStream inputStream, final String passwor
try {
final KeyStore keystore = KeyStore.getInstance("pkcs12", new BouncyCastleProvider());
keystore.load(inputStream, password.toCharArray());
inputStream.close();
return keystore;
} catch (final CertificateException e) {
throw new InvalidKeystoreException(e);
Expand All @@ -82,6 +88,8 @@ private KeyStore getKeyStore(final InputStream inputStream, final String passwor
throw new InvalidKeystoreException(e);
} catch (final IOException e) {
throw new InvalidKeystoreException(e);
} finally {
IOUtils.closeQuietly(inputStream);
}
}

Expand Down
12 changes: 10 additions & 2 deletions src/main/java/cz/tomasdvorak/eet/client/security/ServerKey.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cz.tomasdvorak.eet.client.security;

import cz.tomasdvorak.eet.client.exceptions.InvalidKeystoreException;
import cz.tomasdvorak.eet.client.utils.IOUtils;
import org.apache.logging.log4j.Logger;
import org.apache.wss4j.common.crypto.Crypto;
import org.apache.wss4j.common.crypto.Merlin;
Expand All @@ -24,9 +25,14 @@ public class ServerKey {

private final KeyStore trustStore;

public ServerKey(final InputStream caCertificate) throws InvalidKeystoreException {
/**
* Create new ServerKey instance based on data provided in the stream
* @param certificateStream will be closed automatically
*/
public ServerKey(final InputStream certificateStream) throws InvalidKeystoreException {
try {
this.trustStore = keystoreOf(caCertificate);
this.trustStore = keystoreOf(certificateStream);
certificateStream.close();
} catch (final CertificateException e) {
throw new InvalidKeystoreException(e);
} catch (final NoSuchAlgorithmException e) {
Expand All @@ -35,6 +41,8 @@ public ServerKey(final InputStream caCertificate) throws InvalidKeystoreExceptio
throw new InvalidKeystoreException(e);
} catch (final IOException e) {
throw new InvalidKeystoreException(e);
} finally {
IOUtils.closeQuietly(certificateStream);
}
}

Expand Down
15 changes: 15 additions & 0 deletions src/main/java/cz/tomasdvorak/eet/client/utils/IOUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cz.tomasdvorak.eet.client.utils;

import java.io.IOException;
import java.io.InputStream;

public class IOUtils {
public static void closeQuietly(final InputStream stream) {
try {
if (stream != null) {
stream.close();
}
} catch (final IOException ignored) {
}
}
}
1 change: 1 addition & 0 deletions src/test/java/cz/tomasdvorak/eet/client/EETClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import cz.tomasdvorak.eet.client.dto.WebserviceConfiguration;
import cz.tomasdvorak.eet.client.exceptions.CommunicationException;
import cz.tomasdvorak.eet.client.utils.StringUtils;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/cz/tomasdvorak/eet/client/utils/IOUtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cz.tomasdvorak.eet.client.utils;

import org.junit.Assert;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;

public class IOUtilsTest {

@Test
public void closeQuietly() {
final InputStream is = this.getClass().getResourceAsStream("/keys/README.md");
IOUtils.closeQuietly(is);
try {
is.read();
Assert.fail("Should throw an exception");
} catch (final IOException e) {
Assert.assertEquals("Stream closed", e.getMessage());
}
}

@Test
public void closeQuietlyWithNull() {
IOUtils.closeQuietly(null); // should not throw any exceptions!
}

}

0 comments on commit 191e56f

Please sign in to comment.