mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-11-21 19:42:05 +01:00
Introduce CloseableUtil
This commit is contained in:
parent
1136e8a2e9
commit
a00aa726fe
19 changed files with 109 additions and 256 deletions
3
resources/smacklint.sh
Normal file
3
resources/smacklint.sh
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
find smack-integration-test -type f -path "*/src/main/java/*/smackx/*/package-info.java"
|
|
@ -41,6 +41,7 @@ import org.jivesoftware.smack.packet.Stanza;
|
||||||
import org.jivesoftware.smack.packet.StanzaError;
|
import org.jivesoftware.smack.packet.StanzaError;
|
||||||
import org.jivesoftware.smack.sasl.packet.SaslStreamElements.SASLFailure;
|
import org.jivesoftware.smack.sasl.packet.SaslStreamElements.SASLFailure;
|
||||||
import org.jivesoftware.smack.sasl.packet.SaslStreamElements.Success;
|
import org.jivesoftware.smack.sasl.packet.SaslStreamElements.Success;
|
||||||
|
import org.jivesoftware.smack.util.CloseableUtil;
|
||||||
import org.jivesoftware.smack.util.PacketParserUtils;
|
import org.jivesoftware.smack.util.PacketParserUtils;
|
||||||
|
|
||||||
import org.igniterealtime.jbosh.AbstractBody;
|
import org.igniterealtime.jbosh.AbstractBody;
|
||||||
|
@ -261,28 +262,13 @@ public class XMPPBOSHConnection extends AbstractXMPPConnection {
|
||||||
isFirstInitialization = false;
|
isFirstInitialization = false;
|
||||||
|
|
||||||
// Close down the readers and writers.
|
// Close down the readers and writers.
|
||||||
if (readerPipe != null) {
|
CloseableUtil.maybeClose(readerPipe, LOGGER);
|
||||||
try {
|
CloseableUtil.maybeClose(reader, LOGGER);
|
||||||
readerPipe.close();
|
CloseableUtil.maybeClose(writer, LOGGER);
|
||||||
}
|
|
||||||
catch (Throwable ignore) { /* ignore */ }
|
|
||||||
reader = null;
|
|
||||||
}
|
|
||||||
if (reader != null) {
|
|
||||||
try {
|
|
||||||
reader.close();
|
|
||||||
}
|
|
||||||
catch (Throwable ignore) { /* ignore */ }
|
|
||||||
reader = null;
|
|
||||||
}
|
|
||||||
if (writer != null) {
|
|
||||||
try {
|
|
||||||
writer.close();
|
|
||||||
}
|
|
||||||
catch (Throwable ignore) { /* ignore */ }
|
|
||||||
writer = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
readerPipe = null;
|
||||||
|
reader = null;
|
||||||
|
writer = null;
|
||||||
readerConsumer = null;
|
readerConsumer = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,6 @@
|
||||||
package org.jivesoftware.smack;
|
package org.jivesoftware.smack;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
@ -37,6 +36,7 @@ import org.jivesoftware.smack.sasl.core.SASLAnonymous;
|
||||||
import org.jivesoftware.smack.sasl.core.SASLXOauth2Mechanism;
|
import org.jivesoftware.smack.sasl.core.SASLXOauth2Mechanism;
|
||||||
import org.jivesoftware.smack.sasl.core.SCRAMSHA1Mechanism;
|
import org.jivesoftware.smack.sasl.core.SCRAMSHA1Mechanism;
|
||||||
import org.jivesoftware.smack.sasl.core.ScramSha1PlusMechanism;
|
import org.jivesoftware.smack.sasl.core.ScramSha1PlusMechanism;
|
||||||
|
import org.jivesoftware.smack.util.CloseableUtil;
|
||||||
import org.jivesoftware.smack.util.FileUtils;
|
import org.jivesoftware.smack.util.FileUtils;
|
||||||
import org.jivesoftware.smack.util.StringUtils;
|
import org.jivesoftware.smack.util.StringUtils;
|
||||||
|
|
||||||
|
@ -61,17 +61,15 @@ public final class SmackInitialization {
|
||||||
*/
|
*/
|
||||||
static {
|
static {
|
||||||
String smackVersion;
|
String smackVersion;
|
||||||
|
BufferedReader reader = null;
|
||||||
try {
|
try {
|
||||||
BufferedReader reader = new BufferedReader(new InputStreamReader(FileUtils.getStreamForClasspathFile("org.jivesoftware.smack/version", null), StringUtils.UTF8));
|
reader = new BufferedReader(new InputStreamReader(FileUtils.getStreamForClasspathFile("org.jivesoftware.smack/version", null), StringUtils.UTF8));
|
||||||
smackVersion = reader.readLine();
|
smackVersion = reader.readLine();
|
||||||
try {
|
|
||||||
reader.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
LOGGER.log(Level.WARNING, "IOException closing stream", e);
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
LOGGER.log(Level.SEVERE, "Could not determine Smack version", e);
|
LOGGER.log(Level.SEVERE, "Could not determine Smack version", e);
|
||||||
smackVersion = "unknown";
|
smackVersion = "unknown";
|
||||||
|
} finally {
|
||||||
|
CloseableUtil.maybeClose(reader, LOGGER);
|
||||||
}
|
}
|
||||||
SMACK_VERSION = smackVersion;
|
SMACK_VERSION = smackVersion;
|
||||||
|
|
||||||
|
@ -146,12 +144,7 @@ public final class SmackInitialization {
|
||||||
eventType = parser.next();
|
eventType = parser.next();
|
||||||
}
|
}
|
||||||
while (eventType != XmlPullParser.END_DOCUMENT);
|
while (eventType != XmlPullParser.END_DOCUMENT);
|
||||||
try {
|
CloseableUtil.maybeClose(cfgFileStream, LOGGER);
|
||||||
cfgFileStream.close();
|
|
||||||
}
|
|
||||||
catch (IOException e) {
|
|
||||||
LOGGER.log(Level.SEVERE, "Error while closing config file input stream", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void parseClassesToLoad(XmlPullParser parser, boolean optional,
|
private static void parseClassesToLoad(XmlPullParser parser, boolean optional,
|
||||||
|
|
|
@ -16,7 +16,6 @@
|
||||||
*/
|
*/
|
||||||
package org.jivesoftware.smack.initializer;
|
package org.jivesoftware.smack.initializer;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
|
@ -27,6 +26,7 @@ import java.util.logging.Logger;
|
||||||
import org.jivesoftware.smack.SmackInitialization;
|
import org.jivesoftware.smack.SmackInitialization;
|
||||||
import org.jivesoftware.smack.provider.ProviderFileLoader;
|
import org.jivesoftware.smack.provider.ProviderFileLoader;
|
||||||
import org.jivesoftware.smack.provider.ProviderManager;
|
import org.jivesoftware.smack.provider.ProviderManager;
|
||||||
|
import org.jivesoftware.smack.util.CloseableUtil;
|
||||||
import org.jivesoftware.smack.util.FileUtils;
|
import org.jivesoftware.smack.util.FileUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -86,14 +86,6 @@ public abstract class UrlInitializer implements SmackInitializer {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void maybeClose(InputStream is) {
|
private static void maybeClose(InputStream is) {
|
||||||
if (is == null) {
|
CloseableUtil.maybeClose(is, LOGGER);
|
||||||
return;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
is.close();
|
|
||||||
}
|
|
||||||
catch (IOException e) {
|
|
||||||
LOGGER.log(Level.WARNING, "Could not close input stream", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,6 +26,7 @@ import java.util.logging.Logger;
|
||||||
|
|
||||||
import org.jivesoftware.smack.packet.ExtensionElement;
|
import org.jivesoftware.smack.packet.ExtensionElement;
|
||||||
import org.jivesoftware.smack.packet.IQ;
|
import org.jivesoftware.smack.packet.IQ;
|
||||||
|
import org.jivesoftware.smack.util.CloseableUtil;
|
||||||
|
|
||||||
import org.xmlpull.v1.XmlPullParser;
|
import org.xmlpull.v1.XmlPullParser;
|
||||||
import org.xmlpull.v1.XmlPullParserFactory;
|
import org.xmlpull.v1.XmlPullParserFactory;
|
||||||
|
@ -141,12 +142,7 @@ public class ProviderFileLoader implements ProviderLoader {
|
||||||
exceptions.add(e);
|
exceptions.add(e);
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
try {
|
CloseableUtil.maybeClose(providerStream, LOGGER);
|
||||||
providerStream.close();
|
|
||||||
}
|
|
||||||
catch (Exception e) {
|
|
||||||
LOGGER.log(Level.WARNING, "Exception closing provider stream", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -21,7 +21,9 @@ import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import org.jivesoftware.smack.util.CloseableUtil;
|
||||||
import org.jivesoftware.smack.util.StringUtils;
|
import org.jivesoftware.smack.util.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -30,6 +32,8 @@ import org.jivesoftware.smack.util.StringUtils;
|
||||||
* @author Atul Aggarwal
|
* @author Atul Aggarwal
|
||||||
*/
|
*/
|
||||||
public class Socks5ProxySocketConnection implements ProxySocketConnection {
|
public class Socks5ProxySocketConnection implements ProxySocketConnection {
|
||||||
|
private static final Logger LOGGER = Logger.getLogger(Socks5ProxySocketConnection.class.getName());
|
||||||
|
|
||||||
private final ProxyInfo proxy;
|
private final ProxyInfo proxy;
|
||||||
|
|
||||||
Socks5ProxySocketConnection(ProxyInfo proxy) {
|
Socks5ProxySocketConnection(ProxyInfo proxy) {
|
||||||
|
@ -164,11 +168,7 @@ public class Socks5ProxySocketConnection implements ProxySocketConnection {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!check) {
|
if (!check) {
|
||||||
try {
|
CloseableUtil.maybeClose(socket, LOGGER);
|
||||||
socket.close();
|
|
||||||
}
|
|
||||||
catch (Exception eee) {
|
|
||||||
}
|
|
||||||
throw new ProxyException(ProxyInfo.ProxyType.SOCKS5,
|
throw new ProxyException(ProxyInfo.ProxyType.SOCKS5,
|
||||||
"fail in SOCKS5 proxy");
|
"fail in SOCKS5 proxy");
|
||||||
}
|
}
|
||||||
|
@ -253,11 +253,7 @@ public class Socks5ProxySocketConnection implements ProxySocketConnection {
|
||||||
fill(in, buf, 4);
|
fill(in, buf, 4);
|
||||||
|
|
||||||
if (buf[1] != 0) {
|
if (buf[1] != 0) {
|
||||||
try {
|
CloseableUtil.maybeClose(socket, LOGGER);
|
||||||
socket.close();
|
|
||||||
}
|
|
||||||
catch (Exception eee) {
|
|
||||||
}
|
|
||||||
throw new ProxyException(ProxyInfo.ProxyType.SOCKS5,
|
throw new ProxyException(ProxyInfo.ProxyType.SOCKS5,
|
||||||
"server returns " + buf[1]);
|
"server returns " + buf[1]);
|
||||||
}
|
}
|
||||||
|
@ -280,11 +276,7 @@ public class Socks5ProxySocketConnection implements ProxySocketConnection {
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
try {
|
CloseableUtil.maybeClose(socket, LOGGER);
|
||||||
socket.close();
|
|
||||||
}
|
|
||||||
catch (Exception eee) {
|
|
||||||
}
|
|
||||||
// TODO convert to IOException(e) when minimum Android API level is 9 or higher
|
// TODO convert to IOException(e) when minimum Android API level is 9 or higher
|
||||||
throw new IOException(e.getLocalizedMessage());
|
throw new IOException(e.getLocalizedMessage());
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,37 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Copyright 2018 Florian Schmaus
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.jivesoftware.smack.util;
|
||||||
|
|
||||||
|
import java.io.Closeable;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
public class CloseableUtil {
|
||||||
|
|
||||||
|
public static void maybeClose(Closeable closable, Logger logger) {
|
||||||
|
if (closable == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
closable.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
logger.log(Level.WARNING, "Could not close " + closable, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -29,11 +29,11 @@ import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.FutureTask;
|
import java.util.concurrent.FutureTask;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.TimeoutException;
|
import java.util.concurrent.TimeoutException;
|
||||||
import java.util.logging.Level;
|
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import org.jivesoftware.smack.SmackException;
|
import org.jivesoftware.smack.SmackException;
|
||||||
import org.jivesoftware.smack.XMPPException;
|
import org.jivesoftware.smack.XMPPException;
|
||||||
|
import org.jivesoftware.smack.util.CloseableUtil;
|
||||||
import org.jivesoftware.smack.util.StringUtils;
|
import org.jivesoftware.smack.util.StringUtils;
|
||||||
import org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.StreamHost;
|
import org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.StreamHost;
|
||||||
|
|
||||||
|
@ -97,11 +97,7 @@ public class Socks5Client {
|
||||||
}
|
}
|
||||||
catch (SmackException e) {
|
catch (SmackException e) {
|
||||||
if (!socket.isClosed()) {
|
if (!socket.isClosed()) {
|
||||||
try {
|
CloseableUtil.maybeClose(socket, LOGGER);
|
||||||
socket.close();
|
|
||||||
} catch (IOException e2) {
|
|
||||||
LOGGER.log(Level.WARNING, "Could not close SOCKS5 socket", e2);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,6 +38,7 @@ import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import org.jivesoftware.smack.SmackException;
|
import org.jivesoftware.smack.SmackException;
|
||||||
|
import org.jivesoftware.smack.util.CloseableUtil;
|
||||||
import org.jivesoftware.smack.util.StringUtils;
|
import org.jivesoftware.smack.util.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -409,14 +410,7 @@ public final class Socks5Proxy {
|
||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
try {
|
CloseableUtil.maybeClose(socket, LOGGER);
|
||||||
if (socket != null) {
|
|
||||||
socket.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (IOException e1) {
|
|
||||||
/* do nothing */
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -27,12 +27,12 @@ import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.FutureTask;
|
import java.util.concurrent.FutureTask;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.TimeoutException;
|
import java.util.concurrent.TimeoutException;
|
||||||
import java.util.logging.Level;
|
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import org.jivesoftware.smack.SmackException;
|
import org.jivesoftware.smack.SmackException;
|
||||||
import org.jivesoftware.smack.SmackException.NoResponseException;
|
import org.jivesoftware.smack.SmackException.NoResponseException;
|
||||||
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
|
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
|
||||||
|
import org.jivesoftware.smack.util.CloseableUtil;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -158,20 +158,8 @@ public class IncomingFileTransfer extends FileTransfer {
|
||||||
if (getStatus().equals(Status.in_progress)) {
|
if (getStatus().equals(Status.in_progress)) {
|
||||||
setStatus(Status.complete);
|
setStatus(Status.complete);
|
||||||
}
|
}
|
||||||
if (inputStream != null) {
|
CloseableUtil.maybeClose(inputStream, LOGGER);
|
||||||
try {
|
CloseableUtil.maybeClose(outputStream, LOGGER);
|
||||||
inputStream.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
LOGGER.log(Level.WARNING, "Closing input stream", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (outputStream != null) {
|
|
||||||
try {
|
|
||||||
outputStream.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
LOGGER.log(Level.WARNING, "Closing output stream", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}, "File Transfer " + streamID);
|
}, "File Transfer " + streamID);
|
||||||
transferThread.start();
|
transferThread.start();
|
||||||
|
|
|
@ -22,7 +22,6 @@ import java.io.FileNotFoundException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.util.logging.Level;
|
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import org.jivesoftware.smack.SmackException;
|
import org.jivesoftware.smack.SmackException;
|
||||||
|
@ -30,6 +29,7 @@ import org.jivesoftware.smack.SmackException.IllegalStateChangeException;
|
||||||
import org.jivesoftware.smack.XMPPException;
|
import org.jivesoftware.smack.XMPPException;
|
||||||
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
|
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
|
||||||
import org.jivesoftware.smack.packet.StanzaError;
|
import org.jivesoftware.smack.packet.StanzaError;
|
||||||
|
import org.jivesoftware.smack.util.CloseableUtil;
|
||||||
|
|
||||||
import org.jxmpp.jid.Jid;
|
import org.jxmpp.jid.Jid;
|
||||||
|
|
||||||
|
@ -258,19 +258,8 @@ public class OutgoingFileTransfer extends FileTransfer {
|
||||||
setStatus(FileTransfer.Status.error);
|
setStatus(FileTransfer.Status.error);
|
||||||
setException(e);
|
setException(e);
|
||||||
} finally {
|
} finally {
|
||||||
if (inputStream != null) {
|
CloseableUtil.maybeClose(inputStream, LOGGER);
|
||||||
try {
|
CloseableUtil.maybeClose(outputStream, LOGGER);
|
||||||
inputStream.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
LOGGER.log(Level.WARNING, "Closing input stream", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
outputStream.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
LOGGER.log(Level.WARNING, "Closing output stream", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
updateStatus(Status.in_progress, FileTransfer.Status.complete);
|
updateStatus(Status.in_progress, FileTransfer.Status.complete);
|
||||||
}
|
}
|
||||||
|
@ -325,16 +314,8 @@ public class OutgoingFileTransfer extends FileTransfer {
|
||||||
setStatus(FileTransfer.Status.error);
|
setStatus(FileTransfer.Status.error);
|
||||||
setException(e);
|
setException(e);
|
||||||
} finally {
|
} finally {
|
||||||
try {
|
CloseableUtil.maybeClose(in, LOGGER);
|
||||||
if (in != null) {
|
CloseableUtil.maybeClose(outputStream, LOGGER);
|
||||||
in.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
outputStream.flush();
|
|
||||||
outputStream.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
/* Do Nothing */
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
updateStatus(Status.in_progress, FileTransfer.Status.complete);
|
updateStatus(Status.in_progress, FileTransfer.Status.complete);
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,6 +29,7 @@ import java.util.logging.Logger;
|
||||||
|
|
||||||
import org.jivesoftware.smack.packet.ExtensionElement;
|
import org.jivesoftware.smack.packet.ExtensionElement;
|
||||||
import org.jivesoftware.smack.packet.Message;
|
import org.jivesoftware.smack.packet.Message;
|
||||||
|
import org.jivesoftware.smack.util.CloseableUtil;
|
||||||
import org.jivesoftware.smack.util.XmlStringBuilder;
|
import org.jivesoftware.smack.util.XmlStringBuilder;
|
||||||
import org.jivesoftware.smack.util.stringencoder.Base64;
|
import org.jivesoftware.smack.util.stringencoder.Base64;
|
||||||
|
|
||||||
|
@ -191,22 +192,8 @@ public class JivePropertiesExtension implements ExtensionElement {
|
||||||
valueStr = "Serializing error: " + e.getMessage();
|
valueStr = "Serializing error: " + e.getMessage();
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
if (out != null) {
|
CloseableUtil.maybeClose(out, LOGGER);
|
||||||
try {
|
CloseableUtil.maybeClose(byteStream, LOGGER);
|
||||||
out.close();
|
|
||||||
}
|
|
||||||
catch (Exception e) {
|
|
||||||
// Ignore.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (byteStream != null) {
|
|
||||||
try {
|
|
||||||
byteStream.close();
|
|
||||||
}
|
|
||||||
catch (Exception e) {
|
|
||||||
// Ignore.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
xml.attribute("type", type);
|
xml.attribute("type", type);
|
||||||
|
|
|
@ -35,6 +35,8 @@ import java.util.TreeSet;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import org.jivesoftware.smack.util.CloseableUtil;
|
||||||
|
|
||||||
import org.jivesoftware.smackx.omemo.exceptions.CorruptedOmemoKeyException;
|
import org.jivesoftware.smackx.omemo.exceptions.CorruptedOmemoKeyException;
|
||||||
import org.jivesoftware.smackx.omemo.internal.OmemoCachedDeviceList;
|
import org.jivesoftware.smackx.omemo.internal.OmemoCachedDeviceList;
|
||||||
import org.jivesoftware.smackx.omemo.internal.OmemoDevice;
|
import org.jivesoftware.smackx.omemo.internal.OmemoDevice;
|
||||||
|
@ -417,15 +419,8 @@ public abstract class FileBasedOmemoStore<T_IdKeyPair, T_IdKey, T_PreKey, T_SigP
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
LOGGER.log(Level.SEVERE, "Could not write longs to file.", e);
|
LOGGER.log(Level.SEVERE, "Could not write longs to file.", e);
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
if (out != null) {
|
CloseableUtil.maybeClose(out, LOGGER);
|
||||||
try {
|
|
||||||
out.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
LOGGER.log(Level.SEVERE, "Could not close OutputStream.", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -447,13 +442,7 @@ public abstract class FileBasedOmemoStore<T_IdKeyPair, T_IdKey, T_PreKey, T_SigP
|
||||||
LOGGER.log(Level.SEVERE, "Could not read long from file.", e);
|
LOGGER.log(Level.SEVERE, "Could not read long from file.", e);
|
||||||
return null;
|
return null;
|
||||||
} finally {
|
} finally {
|
||||||
if (in != null) {
|
CloseableUtil.maybeClose(in, LOGGER);
|
||||||
try {
|
|
||||||
in.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
LOGGER.log(Level.SEVERE, "Could not close InputStream.", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return l;
|
return l;
|
||||||
|
@ -481,15 +470,8 @@ public abstract class FileBasedOmemoStore<T_IdKeyPair, T_IdKey, T_PreKey, T_SigP
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
LOGGER.log(Level.SEVERE, "Could not write bytes to file.", e);
|
LOGGER.log(Level.SEVERE, "Could not write bytes to file.", e);
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
if (out != null) {
|
CloseableUtil.maybeClose(out, LOGGER);
|
||||||
try {
|
|
||||||
out.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
LOGGER.log(Level.SEVERE, "Could not close OutputStream.", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -510,15 +492,8 @@ public abstract class FileBasedOmemoStore<T_IdKeyPair, T_IdKey, T_PreKey, T_SigP
|
||||||
b = null;
|
b = null;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
LOGGER.log(Level.SEVERE, "Could not read bytes from file.", e);
|
LOGGER.log(Level.SEVERE, "Could not read bytes from file.", e);
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
if (in != null) {
|
CloseableUtil.maybeClose(in, LOGGER);
|
||||||
try {
|
|
||||||
in.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
LOGGER.log(Level.SEVERE, "Could not close InputStream.", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return b;
|
return b;
|
||||||
|
@ -540,15 +515,8 @@ public abstract class FileBasedOmemoStore<T_IdKeyPair, T_IdKey, T_PreKey, T_SigP
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
LOGGER.log(Level.SEVERE, "Could not write integers to file.", e);
|
LOGGER.log(Level.SEVERE, "Could not write integers to file.", e);
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
if (out != null) {
|
CloseableUtil.maybeClose(out, LOGGER);
|
||||||
try {
|
|
||||||
out.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
LOGGER.log(Level.SEVERE, "Could not close OutputStream.", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -577,13 +545,7 @@ public abstract class FileBasedOmemoStore<T_IdKeyPair, T_IdKey, T_PreKey, T_SigP
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
LOGGER.log(Level.SEVERE, "Could not read integers.", e);
|
LOGGER.log(Level.SEVERE, "Could not read integers.", e);
|
||||||
} finally {
|
} finally {
|
||||||
if (in != null) {
|
CloseableUtil.maybeClose(in, LOGGER);
|
||||||
try {
|
|
||||||
in.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
LOGGER.log(Level.SEVERE, "Could not close InputStream.", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return integers;
|
return integers;
|
||||||
|
|
|
@ -43,7 +43,7 @@ import org.pgpainless.util.BCUtil;
|
||||||
|
|
||||||
public abstract class AbstractOpenPgpKeyStore implements OpenPgpKeyStore {
|
public abstract class AbstractOpenPgpKeyStore implements OpenPgpKeyStore {
|
||||||
|
|
||||||
private static final Logger LOGGER = Logger.getLogger(AbstractOpenPgpKeyStore.class.getName());
|
protected static final Logger LOGGER = Logger.getLogger(AbstractOpenPgpKeyStore.class.getName());
|
||||||
|
|
||||||
protected Map<BareJid, PGPPublicKeyRingCollection> publicKeyRingCollections = new HashMap<>();
|
protected Map<BareJid, PGPPublicKeyRingCollection> publicKeyRingCollections = new HashMap<>();
|
||||||
protected Map<BareJid, PGPSecretKeyRingCollection> secretKeyRingCollections = new HashMap<>();
|
protected Map<BareJid, PGPSecretKeyRingCollection> secretKeyRingCollections = new HashMap<>();
|
||||||
|
|
|
@ -27,6 +27,7 @@ import java.io.OutputStream;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.jivesoftware.smack.util.CloseableUtil;
|
||||||
import org.jivesoftware.smack.util.Objects;
|
import org.jivesoftware.smack.util.Objects;
|
||||||
import org.jivesoftware.smackx.ox.store.abstr.AbstractOpenPgpKeyStore;
|
import org.jivesoftware.smackx.ox.store.abstr.AbstractOpenPgpKeyStore;
|
||||||
import org.jivesoftware.smackx.ox.store.definition.OpenPgpKeyStore;
|
import org.jivesoftware.smackx.ox.store.definition.OpenPgpKeyStore;
|
||||||
|
@ -82,16 +83,8 @@ public class FileBasedOpenPgpKeyStore extends AbstractOpenPgpKeyStore {
|
||||||
try {
|
try {
|
||||||
outputStream = prepareFileOutputStream(file);
|
outputStream = prepareFileOutputStream(file);
|
||||||
publicKeys.encode(outputStream);
|
publicKeys.encode(outputStream);
|
||||||
outputStream.close();
|
} finally {
|
||||||
} catch (IOException e) {
|
CloseableUtil.maybeClose(outputStream, LOGGER);
|
||||||
if (outputStream != null) {
|
|
||||||
try {
|
|
||||||
outputStream.close();
|
|
||||||
} catch (IOException ignored) {
|
|
||||||
// Don't care
|
|
||||||
}
|
|
||||||
}
|
|
||||||
throw e;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -113,16 +106,8 @@ public class FileBasedOpenPgpKeyStore extends AbstractOpenPgpKeyStore {
|
||||||
try {
|
try {
|
||||||
outputStream = prepareFileOutputStream(file);
|
outputStream = prepareFileOutputStream(file);
|
||||||
secretKeys.encode(outputStream);
|
secretKeys.encode(outputStream);
|
||||||
outputStream.close();
|
} finally {
|
||||||
} catch (IOException e) {
|
CloseableUtil.maybeClose(outputStream, LOGGER);
|
||||||
if (outputStream != null) {
|
|
||||||
try {
|
|
||||||
outputStream.close();
|
|
||||||
} catch (IOException ignored) {
|
|
||||||
// Don't care
|
|
||||||
}
|
|
||||||
}
|
|
||||||
throw e;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -31,6 +31,7 @@ import java.util.Map;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import org.jivesoftware.smack.util.CloseableUtil;
|
||||||
import org.jivesoftware.smackx.ox.store.abstr.AbstractOpenPgpMetadataStore;
|
import org.jivesoftware.smackx.ox.store.abstr.AbstractOpenPgpMetadataStore;
|
||||||
import org.jivesoftware.smackx.ox.store.definition.OpenPgpMetadataStore;
|
import org.jivesoftware.smackx.ox.store.definition.OpenPgpMetadataStore;
|
||||||
import org.jivesoftware.smackx.ox.util.FileUtils;
|
import org.jivesoftware.smackx.ox.util.FileUtils;
|
||||||
|
@ -110,18 +111,9 @@ public class FileBasedOpenPgpMetadataStore extends AbstractOpenPgpMetadataStore
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
reader.close();
|
|
||||||
return fingerprintDateMap;
|
return fingerprintDateMap;
|
||||||
|
} finally {
|
||||||
} catch (IOException e) {
|
CloseableUtil.maybeClose(reader, LOGGER);
|
||||||
if (reader != null) {
|
|
||||||
try {
|
|
||||||
reader.close();
|
|
||||||
} catch (IOException ignored) {
|
|
||||||
// Don't care
|
|
||||||
}
|
|
||||||
}
|
|
||||||
throw e;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -166,17 +158,8 @@ public class FileBasedOpenPgpMetadataStore extends AbstractOpenPgpMetadataStore
|
||||||
writer.write(line);
|
writer.write(line);
|
||||||
writer.newLine();
|
writer.newLine();
|
||||||
}
|
}
|
||||||
writer.flush();
|
} finally {
|
||||||
writer.close();
|
CloseableUtil.maybeClose(writer, LOGGER);
|
||||||
} catch (IOException e) {
|
|
||||||
if (writer != null) {
|
|
||||||
try {
|
|
||||||
writer.close();
|
|
||||||
} catch (IOException ignored) {
|
|
||||||
// Don't care
|
|
||||||
}
|
|
||||||
}
|
|
||||||
throw e;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -184,6 +167,7 @@ public class FileBasedOpenPgpMetadataStore extends AbstractOpenPgpMetadataStore
|
||||||
return new File(FileBasedOpenPgpStore.getContactsPath(basePath, contact), ANNOUNCED);
|
return new File(FileBasedOpenPgpStore.getContactsPath(basePath, contact), ANNOUNCED);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: This method appears to be unused. Remove it?
|
||||||
private File getRetrievedFingerprintsPath(BareJid contact) {
|
private File getRetrievedFingerprintsPath(BareJid contact) {
|
||||||
return new File(FileBasedOpenPgpStore.getContactsPath(basePath, contact), RETRIEVED);
|
return new File(FileBasedOpenPgpStore.getContactsPath(basePath, contact), RETRIEVED);
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,6 +28,7 @@ import java.io.OutputStreamWriter;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import org.jivesoftware.smack.util.CloseableUtil;
|
||||||
import org.jivesoftware.smackx.ox.store.abstr.AbstractOpenPgpTrustStore;
|
import org.jivesoftware.smackx.ox.store.abstr.AbstractOpenPgpTrustStore;
|
||||||
import org.jivesoftware.smackx.ox.store.definition.OpenPgpTrustStore;
|
import org.jivesoftware.smackx.ox.store.definition.OpenPgpTrustStore;
|
||||||
import org.jivesoftware.smackx.ox.util.FileUtils;
|
import org.jivesoftware.smackx.ox.util.FileUtils;
|
||||||
|
@ -82,21 +83,14 @@ public class FileBasedOpenPgpTrustStore extends AbstractOpenPgpTrustStore {
|
||||||
file.getAbsolutePath());
|
file.getAbsolutePath());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
reader.close();
|
|
||||||
return trust != null ? trust : Trust.undecided;
|
return trust != null ? trust : Trust.undecided;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
if (reader != null) {
|
|
||||||
try {
|
|
||||||
reader.close();
|
|
||||||
} catch (IOException ignored) {
|
|
||||||
// Don't care
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (e instanceof FileNotFoundException) {
|
if (e instanceof FileNotFoundException) {
|
||||||
return Trust.undecided;
|
return Trust.undecided;
|
||||||
}
|
}
|
||||||
throw e;
|
throw e;
|
||||||
|
} finally {
|
||||||
|
CloseableUtil.maybeClose(reader, LOGGER);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -134,17 +128,8 @@ public class FileBasedOpenPgpTrustStore extends AbstractOpenPgpTrustStore {
|
||||||
writer = new BufferedWriter(osw);
|
writer = new BufferedWriter(osw);
|
||||||
|
|
||||||
writer.write(trust.toString());
|
writer.write(trust.toString());
|
||||||
writer.flush();
|
} finally {
|
||||||
writer.close();
|
CloseableUtil.maybeClose(writer, LOGGER);
|
||||||
} catch (IOException e) {
|
|
||||||
if (writer != null) {
|
|
||||||
try {
|
|
||||||
writer.close();
|
|
||||||
} catch (IOException ignored) {
|
|
||||||
// Don't care
|
|
||||||
}
|
|
||||||
}
|
|
||||||
throw e;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Copyright 2015-2016 Florian Schmaus
|
* Copyright 2015-2018 Florian Schmaus
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -16,11 +16,9 @@
|
||||||
*/
|
*/
|
||||||
package org.jivesoftware.smack.util.dns.minidns;
|
package org.jivesoftware.smack.util.dns.minidns;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.security.KeyManagementException;
|
import java.security.KeyManagementException;
|
||||||
import java.security.SecureRandom;
|
import java.security.SecureRandom;
|
||||||
import java.security.cert.CertificateException;
|
import java.security.cert.CertificateException;
|
||||||
import java.util.logging.Level;
|
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import javax.net.ssl.KeyManager;
|
import javax.net.ssl.KeyManager;
|
||||||
|
@ -29,6 +27,7 @@ import javax.net.ssl.SSLSocket;
|
||||||
import javax.net.ssl.TrustManager;
|
import javax.net.ssl.TrustManager;
|
||||||
import javax.net.ssl.X509TrustManager;
|
import javax.net.ssl.X509TrustManager;
|
||||||
|
|
||||||
|
import org.jivesoftware.smack.util.CloseableUtil;
|
||||||
import org.jivesoftware.smack.util.dns.SmackDaneVerifier;
|
import org.jivesoftware.smack.util.dns.SmackDaneVerifier;
|
||||||
|
|
||||||
import org.minidns.dane.DaneVerifier;
|
import org.minidns.dane.DaneVerifier;
|
||||||
|
@ -64,11 +63,7 @@ public class MiniDnsDaneVerifier implements SmackDaneVerifier {
|
||||||
// DANE verification was successful, but according to the TLSA RR we also must perform PKIX validation.
|
// DANE verification was successful, but according to the TLSA RR we also must perform PKIX validation.
|
||||||
if (expectingTrustManager.hasException()) {
|
if (expectingTrustManager.hasException()) {
|
||||||
// PKIX validation has failed. Throw an exception but close the socket first.
|
// PKIX validation has failed. Throw an exception but close the socket first.
|
||||||
try {
|
CloseableUtil.maybeClose(sslSocket, LOGGER);
|
||||||
sslSocket.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
LOGGER.log(Level.FINER, "Closing TLS socket failed", e);
|
|
||||||
}
|
|
||||||
throw expectingTrustManager.getException();
|
throw expectingTrustManager.getException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -125,6 +125,7 @@ import org.jivesoftware.smack.sm.predicates.Predicate;
|
||||||
import org.jivesoftware.smack.sm.provider.ParseStreamManagement;
|
import org.jivesoftware.smack.sm.provider.ParseStreamManagement;
|
||||||
import org.jivesoftware.smack.util.ArrayBlockingQueueWithShutdown;
|
import org.jivesoftware.smack.util.ArrayBlockingQueueWithShutdown;
|
||||||
import org.jivesoftware.smack.util.Async;
|
import org.jivesoftware.smack.util.Async;
|
||||||
|
import org.jivesoftware.smack.util.CloseableUtil;
|
||||||
import org.jivesoftware.smack.util.DNSUtil;
|
import org.jivesoftware.smack.util.DNSUtil;
|
||||||
import org.jivesoftware.smack.util.PacketParserUtils;
|
import org.jivesoftware.smack.util.PacketParserUtils;
|
||||||
import org.jivesoftware.smack.util.StringUtils;
|
import org.jivesoftware.smack.util.StringUtils;
|
||||||
|
@ -506,11 +507,7 @@ public class XMPPTCPConnection extends AbstractXMPPConnection {
|
||||||
}
|
}
|
||||||
LOGGER.finer("PacketReader has been shut down");
|
LOGGER.finer("PacketReader has been shut down");
|
||||||
|
|
||||||
try {
|
CloseableUtil.maybeClose(socket, LOGGER);
|
||||||
socket.close();
|
|
||||||
} catch (Exception e) {
|
|
||||||
LOGGER.log(Level.WARNING, "shutdown", e);
|
|
||||||
}
|
|
||||||
|
|
||||||
setWasAuthenticated();
|
setWasAuthenticated();
|
||||||
// If we are able to resume the stream, then don't set
|
// If we are able to resume the stream, then don't set
|
||||||
|
|
Loading…
Reference in a new issue