Close resources in tests and in case of Exceptions and use try-with-resources. Close the socket-connection during encrpyting/decrypting as soon as it is not needed any more.
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1828178 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
48c8b9af81
commit
f440933277
@ -124,48 +124,53 @@ public class TSPTimeStampService implements TimeStampService {
|
|||||||
int port = proxyUrl.getPort();
|
int port = proxyUrl.getPort();
|
||||||
proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(InetAddress.getByName(host), (port == -1 ? 80 : port)));
|
proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(InetAddress.getByName(host), (port == -1 ? 80 : port)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ByteArrayOutputStream bos;
|
||||||
|
String contentType;
|
||||||
HttpURLConnection huc = (HttpURLConnection)new URL(signatureConfig.getTspUrl()).openConnection(proxy);
|
HttpURLConnection huc = (HttpURLConnection)new URL(signatureConfig.getTspUrl()).openConnection(proxy);
|
||||||
|
try {
|
||||||
if (signatureConfig.getTspUser() != null) {
|
if (signatureConfig.getTspUser() != null) {
|
||||||
String userPassword = signatureConfig.getTspUser() + ":" + signatureConfig.getTspPass();
|
String userPassword = signatureConfig.getTspUser() + ":" + signatureConfig.getTspPass();
|
||||||
String encoding = DatatypeConverter.printBase64Binary(userPassword.getBytes(StandardCharsets.ISO_8859_1));
|
String encoding = DatatypeConverter.printBase64Binary(userPassword.getBytes(StandardCharsets.ISO_8859_1));
|
||||||
huc.setRequestProperty("Authorization", "Basic " + encoding);
|
huc.setRequestProperty("Authorization", "Basic " + encoding);
|
||||||
|
}
|
||||||
|
|
||||||
|
huc.setRequestMethod("POST");
|
||||||
|
huc.setConnectTimeout(20000);
|
||||||
|
huc.setReadTimeout(20000);
|
||||||
|
huc.setDoOutput(true); // also sets method to POST.
|
||||||
|
huc.setRequestProperty("User-Agent", signatureConfig.getUserAgent());
|
||||||
|
huc.setRequestProperty("Content-Type", signatureConfig.isTspOldProtocol()
|
||||||
|
? "application/timestamp-request"
|
||||||
|
: "application/timestamp-query"); // "; charset=ISO-8859-1");
|
||||||
|
|
||||||
|
OutputStream hucOut = huc.getOutputStream();
|
||||||
|
hucOut.write(encodedRequest);
|
||||||
|
|
||||||
|
// invoke TSP service
|
||||||
|
huc.connect();
|
||||||
|
|
||||||
|
int statusCode = huc.getResponseCode();
|
||||||
|
if (statusCode != 200) {
|
||||||
|
LOG.log(POILogger.ERROR, "Error contacting TSP server ", signatureConfig.getTspUrl() +
|
||||||
|
", had status code " + statusCode + "/" + huc.getResponseMessage());
|
||||||
|
throw new IOException("Error contacting TSP server " + signatureConfig.getTspUrl() +
|
||||||
|
", had status code " + statusCode + "/" + huc.getResponseMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
// HTTP input validation
|
||||||
|
contentType = huc.getHeaderField("Content-Type");
|
||||||
|
if (null == contentType) {
|
||||||
|
throw new RuntimeException("missing Content-Type header");
|
||||||
|
}
|
||||||
|
|
||||||
|
bos = new ByteArrayOutputStream();
|
||||||
|
IOUtils.copy(huc.getInputStream(), bos);
|
||||||
|
LOG.log(POILogger.DEBUG, "response content: ", HexDump.dump(bos.toByteArray(), 0, 0));
|
||||||
|
} finally {
|
||||||
|
huc.disconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
huc.setRequestMethod("POST");
|
|
||||||
huc.setConnectTimeout(20000);
|
|
||||||
huc.setReadTimeout(20000);
|
|
||||||
huc.setDoOutput(true); // also sets method to POST.
|
|
||||||
huc.setRequestProperty("User-Agent", signatureConfig.getUserAgent());
|
|
||||||
huc.setRequestProperty("Content-Type", signatureConfig.isTspOldProtocol()
|
|
||||||
? "application/timestamp-request"
|
|
||||||
: "application/timestamp-query"); // "; charset=ISO-8859-1");
|
|
||||||
|
|
||||||
OutputStream hucOut = huc.getOutputStream();
|
|
||||||
hucOut.write(encodedRequest);
|
|
||||||
|
|
||||||
// invoke TSP service
|
|
||||||
huc.connect();
|
|
||||||
|
|
||||||
int statusCode = huc.getResponseCode();
|
|
||||||
if (statusCode != 200) {
|
|
||||||
LOG.log(POILogger.ERROR, "Error contacting TSP server ", signatureConfig.getTspUrl() +
|
|
||||||
", had status code " + statusCode + "/" + huc.getResponseMessage());
|
|
||||||
throw new IOException("Error contacting TSP server " + signatureConfig.getTspUrl() +
|
|
||||||
", had status code " + statusCode + "/" + huc.getResponseMessage());
|
|
||||||
}
|
|
||||||
|
|
||||||
// HTTP input validation
|
|
||||||
String contentType = huc.getHeaderField("Content-Type");
|
|
||||||
if (null == contentType) {
|
|
||||||
throw new RuntimeException("missing Content-Type header");
|
|
||||||
}
|
|
||||||
|
|
||||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
||||||
IOUtils.copy(huc.getInputStream(), bos);
|
|
||||||
LOG.log(POILogger.DEBUG, "response content: ", HexDump.dump(bos.toByteArray(), 0, 0));
|
|
||||||
|
|
||||||
if (!contentType.startsWith(signatureConfig.isTspOldProtocol()
|
if (!contentType.startsWith(signatureConfig.isTspOldProtocol()
|
||||||
? "application/timestamp-response"
|
? "application/timestamp-response"
|
||||||
: "application/timestamp-reply"
|
: "application/timestamp-reply"
|
||||||
|
@ -806,6 +806,7 @@ public class TestExtractorFactory {
|
|||||||
ExtractorFactory.createExtractor(xlsEmb);
|
ExtractorFactory.createExtractor(xlsEmb);
|
||||||
embeds = ExtractorFactory.getEmbededDocsTextExtractors(ext);
|
embeds = ExtractorFactory.getEmbededDocsTextExtractors(ext);
|
||||||
assertNotNull(embeds);
|
assertNotNull(embeds);
|
||||||
|
ext.close();
|
||||||
|
|
||||||
// Excel
|
// Excel
|
||||||
ext = (POIOLE2TextExtractor)
|
ext = (POIOLE2TextExtractor)
|
||||||
|
@ -33,16 +33,16 @@ public class TestZipSecureFile {
|
|||||||
// ClassCastException in ZipFile now
|
// ClassCastException in ZipFile now
|
||||||
// The relevant change in the JDK is http://hg.openjdk.java.net/jdk/jdk10/rev/85ea7e83af30#l5.66
|
// The relevant change in the JDK is http://hg.openjdk.java.net/jdk/jdk10/rev/85ea7e83af30#l5.66
|
||||||
|
|
||||||
ZipFile thresholdInputStream = new ZipFile(XSSFTestDataSamples.getSampleFile("template.xlsx"));
|
try (ZipFile thresholdInputStream = new ZipFile(XSSFTestDataSamples.getSampleFile("template.xlsx"))) {
|
||||||
|
try (ZipSecureFile secureFile = new ZipSecureFile(XSSFTestDataSamples.getSampleFile("template.xlsx"))) {
|
||||||
|
Enumeration<? extends ZipEntry> entries = thresholdInputStream.entries();
|
||||||
|
while (entries.hasMoreElements()) {
|
||||||
|
ZipEntry entry = entries.nextElement();
|
||||||
|
|
||||||
ZipSecureFile secureFile = new ZipSecureFile(XSSFTestDataSamples.getSampleFile("template.xlsx"));
|
InputStream inputStream = secureFile.getInputStream(entry);
|
||||||
|
assertTrue(inputStream.available() > 0);
|
||||||
Enumeration<? extends ZipEntry> entries = thresholdInputStream.entries();
|
}
|
||||||
while (entries.hasMoreElements()) {
|
}
|
||||||
ZipEntry entry = entries.nextElement();
|
|
||||||
|
|
||||||
InputStream inputStream = secureFile.getInputStream(entry);
|
|
||||||
assertTrue(inputStream.available() > 0);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -113,7 +113,7 @@ public class TestSignatureInfo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void initBouncy() throws IOException {
|
public static void initBouncy() {
|
||||||
CryptoFunctions.registerBouncyCastle();
|
CryptoFunctions.registerBouncyCastle();
|
||||||
|
|
||||||
// Set cal to now ... only set to fixed date for debugging ...
|
// Set cal to now ... only set to fixed date for debugging ...
|
||||||
@ -164,7 +164,7 @@ public class TestSignatureInfo {
|
|||||||
Calendar cal = LocaleUtil.getLocaleCalendar(LocaleUtil.TIMEZONE_UTC);
|
Calendar cal = LocaleUtil.getLocaleCalendar(LocaleUtil.TIMEZONE_UTC);
|
||||||
cal.clear();
|
cal.clear();
|
||||||
cal.setTimeZone(LocaleUtil.TIMEZONE_UTC);
|
cal.setTimeZone(LocaleUtil.TIMEZONE_UTC);
|
||||||
cal.set(2017, 6, 1);
|
cal.set(2017, Calendar.JULY, 1);
|
||||||
|
|
||||||
SignatureConfig signatureConfig = prepareConfig("test", "CN=Test", pfxInput);
|
SignatureConfig signatureConfig = prepareConfig("test", "CN=Test", pfxInput);
|
||||||
signatureConfig.setExecutionTime(cal.getTime());
|
signatureConfig.setExecutionTime(cal.getTime());
|
||||||
@ -440,7 +440,7 @@ public class TestSignatureInfo {
|
|||||||
if (mockTsp) {
|
if (mockTsp) {
|
||||||
TimeStampService tspService = new TimeStampService(){
|
TimeStampService tspService = new TimeStampService(){
|
||||||
@Override
|
@Override
|
||||||
public byte[] timeStamp(byte[] data, RevocationData revocationData) throws Exception {
|
public byte[] timeStamp(byte[] data, RevocationData revocationData) {
|
||||||
revocationData.addCRL(crl);
|
revocationData.addCRL(crl);
|
||||||
return "time-stamp-token".getBytes(LocaleUtil.CHARSET_1252);
|
return "time-stamp-token".getBytes(LocaleUtil.CHARSET_1252);
|
||||||
}
|
}
|
||||||
@ -451,14 +451,10 @@ public class TestSignatureInfo {
|
|||||||
};
|
};
|
||||||
signatureConfig.setTspService(tspService);
|
signatureConfig.setTspService(tspService);
|
||||||
} else {
|
} else {
|
||||||
TimeStampServiceValidator tspValidator = new TimeStampServiceValidator() {
|
TimeStampServiceValidator tspValidator = (validateChain, revocationData) -> {
|
||||||
@Override
|
for (X509Certificate certificate : validateChain) {
|
||||||
public void validate(List<X509Certificate> validateChain,
|
LOG.log(POILogger.DEBUG, "certificate: " + certificate.getSubjectX500Principal());
|
||||||
RevocationData revocationData) throws Exception {
|
LOG.log(POILogger.DEBUG, "validity: " + certificate.getNotBefore() + " - " + certificate.getNotAfter());
|
||||||
for (X509Certificate certificate : validateChain) {
|
|
||||||
LOG.log(POILogger.DEBUG, "certificate: " + certificate.getSubjectX500Principal());
|
|
||||||
LOG.log(POILogger.DEBUG, "validity: " + certificate.getNotBefore() + " - " + certificate.getNotAfter());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
signatureConfig.setTspValidator(tspValidator);
|
signatureConfig.setTspValidator(tspValidator);
|
||||||
@ -471,12 +467,7 @@ public class TestSignatureInfo {
|
|||||||
x509, x509, keyPair.getPrivate(), "SHA1withRSA", cal.getTimeInMillis());
|
x509, x509, keyPair.getPrivate(), "SHA1withRSA", cal.getTimeInMillis());
|
||||||
revocationData.addOCSP(ocspResp.getEncoded());
|
revocationData.addOCSP(ocspResp.getEncoded());
|
||||||
|
|
||||||
RevocationDataService revocationDataService = new RevocationDataService(){
|
RevocationDataService revocationDataService = revocationChain -> revocationData;
|
||||||
@Override
|
|
||||||
public RevocationData getRevocationData(List<X509Certificate> revocationChain) {
|
|
||||||
return revocationData;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
signatureConfig.setRevocationDataService(revocationDataService);
|
signatureConfig.setRevocationDataService(revocationDataService);
|
||||||
|
|
||||||
// operate
|
// operate
|
||||||
|
@ -113,8 +113,8 @@ public final class TestSXSSFBugs extends BaseTestBugzillaIssues {
|
|||||||
writeWorkbook(new XSSFWorkbook(), XSSFITestDataProvider.instance);
|
writeWorkbook(new XSSFWorkbook(), XSSFITestDataProvider.instance);
|
||||||
|
|
||||||
// does not work
|
// does not work
|
||||||
try {
|
try (SXSSFWorkbook wb = new SXSSFWorkbook()) {
|
||||||
writeWorkbook(new SXSSFWorkbook(), SXSSFITestDataProvider.instance);
|
writeWorkbook(wb, SXSSFITestDataProvider.instance);
|
||||||
fail("Should catch exception here");
|
fail("Should catch exception here");
|
||||||
} catch (RuntimeException e) {
|
} catch (RuntimeException e) {
|
||||||
// this is not implemented yet
|
// this is not implemented yet
|
||||||
|
@ -37,7 +37,12 @@ public class HWPFTestDataSamples {
|
|||||||
public static HWPFDocument openSampleFile(String sampleFileName) {
|
public static HWPFDocument openSampleFile(String sampleFileName) {
|
||||||
try {
|
try {
|
||||||
InputStream is = POIDataSamples.getDocumentInstance().openResourceAsStream(sampleFileName);
|
InputStream is = POIDataSamples.getDocumentInstance().openResourceAsStream(sampleFileName);
|
||||||
return new HWPFDocument(is);
|
try {
|
||||||
|
return new HWPFDocument(is);
|
||||||
|
} catch (Throwable e) {
|
||||||
|
is.close();
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
@ -48,42 +53,33 @@ public class HWPFTestDataSamples {
|
|||||||
final long start = System.currentTimeMillis();
|
final long start = System.currentTimeMillis();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
ZipInputStream is = new ZipInputStream( POIDataSamples
|
try (ZipInputStream is = new ZipInputStream(POIDataSamples
|
||||||
.getDocumentInstance()
|
.getDocumentInstance()
|
||||||
.openResourceAsStream( sampleFileName ) );
|
.openResourceAsStream(sampleFileName))) {
|
||||||
try
|
|
||||||
{
|
|
||||||
is.getNextEntry();
|
is.getNextEntry();
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
try
|
try {
|
||||||
{
|
IOUtils.copy(is, baos);
|
||||||
IOUtils.copy( is, baos );
|
} finally {
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
baos.close();
|
baos.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
final long endUnzip = System.currentTimeMillis();
|
final long endUnzip = System.currentTimeMillis();
|
||||||
byte[] byteArray = baos.toByteArray();
|
byte[] byteArray = baos.toByteArray();
|
||||||
|
|
||||||
logger.log( POILogger.DEBUG, "Unzipped in ",
|
logger.log(POILogger.DEBUG, "Unzipped in ",
|
||||||
Long.valueOf( endUnzip - start ), " ms -- ",
|
Long.valueOf(endUnzip - start), " ms -- ",
|
||||||
Long.valueOf( byteArray.length ), " byte(s)" );
|
Long.valueOf(byteArray.length), " byte(s)");
|
||||||
|
|
||||||
ByteArrayInputStream bais = new ByteArrayInputStream( byteArray );
|
ByteArrayInputStream bais = new ByteArrayInputStream(byteArray);
|
||||||
HWPFDocument doc = new HWPFDocument( bais );
|
HWPFDocument doc = new HWPFDocument(bais);
|
||||||
final long endParse = System.currentTimeMillis();
|
final long endParse = System.currentTimeMillis();
|
||||||
|
|
||||||
logger.log( POILogger.DEBUG, "Parsed in ",
|
logger.log(POILogger.DEBUG, "Parsed in ",
|
||||||
Long.valueOf( endParse - start ), " ms" );
|
Long.valueOf(endParse - start), " ms");
|
||||||
|
|
||||||
return doc;
|
return doc;
|
||||||
}
|
}
|
||||||
finally
|
|
||||||
{
|
|
||||||
is.close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch ( IOException e )
|
catch ( IOException e )
|
||||||
{
|
{
|
||||||
@ -105,39 +101,30 @@ public class HWPFTestDataSamples {
|
|||||||
{
|
{
|
||||||
logger.log(POILogger.DEBUG, "Downloading ", sampleFileUrl, " ...");
|
logger.log(POILogger.DEBUG, "Downloading ", sampleFileUrl, " ...");
|
||||||
|
|
||||||
InputStream is = new URL( sampleFileUrl ).openStream();
|
try (InputStream is = new URL(sampleFileUrl).openStream()) {
|
||||||
try
|
|
||||||
{
|
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
try
|
try {
|
||||||
{
|
IOUtils.copy(is, baos);
|
||||||
IOUtils.copy( is, baos );
|
} finally {
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
baos.close();
|
baos.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
final long endDownload = System.currentTimeMillis();
|
final long endDownload = System.currentTimeMillis();
|
||||||
byte[] byteArray = baos.toByteArray();
|
byte[] byteArray = baos.toByteArray();
|
||||||
|
|
||||||
logger.log( POILogger.DEBUG, "Downloaded in ",
|
logger.log(POILogger.DEBUG, "Downloaded in ",
|
||||||
Long.valueOf( endDownload - start ), " ms -- ",
|
Long.valueOf(endDownload - start), " ms -- ",
|
||||||
Long.valueOf( byteArray.length ), " byte(s)" );
|
Long.valueOf(byteArray.length), " byte(s)");
|
||||||
|
|
||||||
ByteArrayInputStream bais = new ByteArrayInputStream( byteArray );
|
ByteArrayInputStream bais = new ByteArrayInputStream(byteArray);
|
||||||
HWPFDocument doc = new HWPFDocument( bais );
|
HWPFDocument doc = new HWPFDocument(bais);
|
||||||
final long endParse = System.currentTimeMillis();
|
final long endParse = System.currentTimeMillis();
|
||||||
|
|
||||||
logger.log( POILogger.DEBUG, "Parsed in ",
|
logger.log(POILogger.DEBUG, "Parsed in ",
|
||||||
Long.valueOf( endParse - start ), " ms" );
|
Long.valueOf(endParse - start), " ms");
|
||||||
|
|
||||||
return doc;
|
return doc;
|
||||||
}
|
}
|
||||||
finally
|
|
||||||
{
|
|
||||||
is.close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch ( IOException e )
|
catch ( IOException e )
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user