Commit e0bc007f authored by Christian Schudt's avatar Christian Schudt

Use try-with-resources statement to properly close resources.

- Not all resources were closed in finally block (see Launcher, PluginManager, UpdateManager, FaviconServlet, ...)
- Get rid of verbose finally blocks and empty catch blocks, which makes the code more readable.
- Generally, this is the more modern approach to deal with resources.
parent 8a4fc507
......@@ -666,10 +666,8 @@ public class DbConnectionManager {
*/
public static String getLargeTextField(ResultSet rs, int columnIndex) throws SQLException {
if (isStreamTextRequired()) {
Reader bodyReader = null;
String value = null;
try {
bodyReader = rs.getCharacterStream(columnIndex);
String value;
try (Reader bodyReader = rs.getCharacterStream(columnIndex)) {
if (bodyReader == null) {
return null;
}
......@@ -686,16 +684,6 @@ public class DbConnectionManager {
Log.error(e.getMessage(), e);
throw new SQLException("Failed to load text field");
}
finally {
try {
if (bodyReader != null) {
bodyReader.close();
}
}
catch (Exception e) {
// Ignore.
}
}
return value;
}
else {
......
......@@ -235,11 +235,11 @@ public class SchemaManager {
// Resource will be like "/database/openfire_hsqldb.sql"
String resourceName = schemaKey + "_" +
DbConnectionManager.getDatabaseType() + ".sql";
InputStream resource = resourceLoader.loadResource(resourceName);
if (resource == null) {
return false;
}
try {
try (InputStream resource = resourceLoader.loadResource(resourceName)) {
if (resource == null) {
return false;
}
// For plugins, we will automatically convert jiveVersion to ofVersion
executeSQLScript(con, resource, !schemaKey.equals("openfire") && !schemaKey.equals("wildfire"));
}
......@@ -247,14 +247,6 @@ public class SchemaManager {
Log.error(e.getMessage(), e);
return false;
}
finally {
try {
resource.close();
}
catch (Exception e) {
// Ignore.
}
}
Log.info(LocaleUtils.getLocalizedString("upgrade.database.success"));
System.out.println(LocaleUtils.getLocalizedString("upgrade.database.success"));
return true;
......@@ -281,34 +273,23 @@ public class SchemaManager {
// Run all upgrade scripts until we're up to the latest schema.
for (int i = currentVersion + 1; i <= requiredVersion; i++) {
InputStream resource = getUpgradeResource(resourceLoader, i, schemaKey);
// apply the 'database-patches-done-in-java'
try {
if (i == 21 && schemaKey.equals("openfire")) {
OF33.executeFix(con);
}
} catch (Exception e) {
Log.error(e.getMessage(), e);
return false;
}
if (resource == null) {
continue;
}
try {
executeSQLScript(con, resource, !schemaKey.equals("openfire") && !schemaKey.equals("wildfire"));
}
catch (Exception e) {
Log.error(e.getMessage(), e);
return false;
}
finally {
try (InputStream resource = getUpgradeResource(resourceLoader, i, schemaKey)) {
// apply the 'database-patches-done-in-java'
try {
resource.close();
if (i == 21 && schemaKey.equals("openfire")) {
OF33.executeFix(con);
}
} catch (Exception e) {
Log.error(e.getMessage(), e);
return false;
}
catch (Exception e) {
// Ignore.
if (resource == null) {
continue;
}
executeSQLScript(con, resource, !schemaKey.equals("openfire") && !schemaKey.equals("wildfire"));
} catch (Exception e) {
Log.error(e.getMessage(), e);
return false;
}
}
Log.info(LocaleUtils.getLocalizedString("upgrade.database.success"));
......@@ -371,9 +352,7 @@ public class SchemaManager {
private static void executeSQLScript(Connection con, InputStream resource, Boolean autoreplace) throws IOException,
SQLException
{
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(resource));
try (BufferedReader in = new BufferedReader(new InputStreamReader(resource))) {
boolean done = false;
while (!done) {
StringBuilder command = new StringBuilder();
......@@ -418,16 +397,6 @@ public class SchemaManager {
}
}
}
finally {
if (in != null) {
try {
in.close();
}
catch (Exception e) {
Log.error(e.getMessage(), e);
}
}
}
}
private static abstract class ResourceLoader {
......
......@@ -474,13 +474,12 @@ public class XMPPServer {
@SuppressWarnings("unchecked")
private void loadModules() {
FileReader in = null;
try {
File modulesXml = new File(JiveGlobals.getHomeDirectory(), "conf/modules.xml");
logger.info("Loading modules from " + modulesXml.getAbsolutePath());
SAXReader xmlReader = new SAXReader();
xmlReader.setEncoding("UTF-8");
in = new FileReader(modulesXml);
File modulesXml = new File(JiveGlobals.getHomeDirectory(), "conf/modules.xml");
logger.info("Loading modules from " + modulesXml.getAbsolutePath());
SAXReader xmlReader = new SAXReader();
xmlReader.setEncoding("UTF-8");
try (FileReader in = new FileReader(modulesXml)) {
Document document = xmlReader.read(in);
Element root = document.getRootElement();
Iterator<Node> itr = root.nodeIterator();
......@@ -495,14 +494,6 @@ public class XMPPServer {
} catch (Exception e) {
e.printStackTrace();
logger.error(LocaleUtils.getLocalizedString("admin.error"), e);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
// Squash
}
}
}
// Keep a reference to the internal component manager
......@@ -773,9 +764,7 @@ public class XMPPServer {
// we have to attempt to load the value from openfire_init.xml,
// which must be in the classpath.
if (openfireHome == null) {
InputStream in = null;
try {
in = getClass().getResourceAsStream("/openfire_init.xml");
try (InputStream in = getClass().getResourceAsStream("/openfire_init.xml")) {
if (in != null) {
SAXReader reader = new SAXReader();
Document doc = reader.read(in);
......@@ -794,17 +783,6 @@ public class XMPPServer {
System.err.println("Error loading openfire_init.xml to find home.");
e.printStackTrace();
}
finally {
try {
if (in != null) {
in.close();
}
}
catch (Exception e) {
System.err.println("Could not close open connection");
e.printStackTrace();
}
}
}
if (openfireHome == null) {
......
......@@ -69,37 +69,17 @@ public class PluginIconServlet extends HttpServlet {
else {
response.setContentType("image/gif");
}
InputStream in = null;
OutputStream ost = null;
try {
in = new FileInputStream(icon);
ost = response.getOutputStream();
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) >= 0) {
ost.write(buf, 0, len);
}
ost.flush();
}
catch (IOException ioe) {
}
finally {
if (in != null) {
try {
in.close();
}
catch (Exception e) {
}
}
if (ost != null) {
try {
ost.close();
}
catch (Exception e) {
try (InputStream in = new FileInputStream(icon)) {
try (OutputStream ost = response.getOutputStream()) {
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) >= 0) {
ost.write(buf, 0, len);
}
ost.flush();
}
} catch (IOException ioe) {
throw new ServletException(ioe);
}
}
}
......
......@@ -177,12 +177,12 @@ public class PluginManager {
// Absolute path to the plugin file
String absolutePath = pluginDirectory + File.separator + pluginFilename;
// Save input stream contents to a temp file
OutputStream out = new FileOutputStream(absolutePath + ".part");
while ((len = in.read(b)) != -1) {
//write byte to file
out.write(b, 0, len);
try (OutputStream out = new FileOutputStream(absolutePath + ".part")) {
while ((len = in.read(b)) != -1) {
//write byte to file
out.write(b, 0, len);
}
}
out.close();
// Delete old .jar (if it exists)
new File(absolutePath).delete();
// Rename temp file to .jar
......@@ -1097,8 +1097,7 @@ public class PluginManager {
* @param dir the directory to extract the plugin to.
*/
private void unzipPlugin(String pluginName, File file, File dir) {
try {
ZipFile zipFile = new JarFile(file);
try (ZipFile zipFile = new JarFile(file)) {
// Ensure that this JAR is a plugin.
if (zipFile.getEntry("plugin.xml") == null) {
return;
......@@ -1116,20 +1115,18 @@ public class PluginManager {
}
if (!entry.isDirectory()) {
entryFile.getParentFile().mkdirs();
FileOutputStream out = new FileOutputStream(entryFile);
InputStream zin = zipFile.getInputStream(entry);
byte[] b = new byte[512];
int len;
while ((len = zin.read(b)) != -1) {
out.write(b, 0, len);
try (FileOutputStream out = new FileOutputStream(entryFile)) {
try (InputStream zin = zipFile.getInputStream(entry)) {
byte[] b = new byte[512];
int len;
while ((len = zin.read(b)) != -1) {
out.write(b, 0, len);
}
out.flush();
}
}
out.flush();
out.close();
zin.close();
}
}
zipFile.close();
}
catch (Exception e) {
Log.error(e.getMessage(), e);
......
......@@ -405,34 +405,18 @@ public class PluginServlet extends HttpServlet {
// response.setHeader("Content-disposition", "filename=\"" + file + "\";");
response.setContentType(contentType);
// Write out the resource to the user.
InputStream in = null;
ServletOutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(file));
out = response.getOutputStream();
try (InputStream in = new BufferedInputStream(new FileInputStream(file))) {
try (ServletOutputStream out = response.getOutputStream()) {
// Set the size of the file.
response.setContentLength((int)file.length());
// Set the size of the file.
response.setContentLength((int) file.length());
// Use a 1K buffer.
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
}
finally {
try {
in.close();
}
catch (Exception ignored) {
// Ignore.
}
try {
out.close();
}
catch (Exception ignored) {
// Ignore.
// Use a 1K buffer.
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
}
}
}
......
......@@ -125,43 +125,23 @@ public class DefaultProxyTransfer implements ProxyTransfer {
if (!isActivatable()) {
throw new IOException("Transfer missing party");
}
InputStream in = null;
OutputStream out = null;
try {
in = getInputStream();
out = new ProxyOutputStream(getOutputStream());
try (InputStream in = getInputStream()) {
try (OutputStream out = new ProxyOutputStream(getOutputStream())) {
final byte[] b = new byte[BUFFER_SIZE];
int count = 0;
amountWritten = 0;
final byte[] b = new byte[BUFFER_SIZE];
int count = 0;
amountWritten = 0;
do {
// write to the output stream
out.write(b, 0, count);
do {
// write to the output stream
out.write(b, 0, count);
amountWritten += count;
amountWritten += count;
// read more bytes from the input stream
count = in.read(b);
} while (count >= 0);
}
finally {
if (in != null) {
try {
in.close();
}
catch (Exception e) {
Log.error(e.getMessage(), e);
}
}
if (out != null) {
try {
out.close();
}
catch (Exception e) {
Log.error(e.getMessage(), e);
}
// read more bytes from the input stream
count = in.read(b);
} while (count >= 0);
}
}
}
......
......@@ -115,20 +115,17 @@ public class FlashCrossDomainServlet extends HttpServlet {
private static String getContent(File file) {
final StringBuilder content = new StringBuilder();
if (file.canRead()) {
try {
final BufferedReader in = new BufferedReader(new FileReader(
file));
String str;
while ((str = in.readLine()) != null) {
content.append(str);
content.append('\n');
}
in.close();
} catch (IOException ex) {
Log.warn("Unexpected exception while trying to read file: " + file.getName(), ex);
return null;
}
}
try (BufferedReader in = new BufferedReader(new FileReader(file))) {
String str;
while ((str = in.readLine()) != null) {
content.append(str);
content.append('\n');
}
} catch (IOException ex) {
Log.warn("Unexpected exception while trying to read file: " + file.getName(), ex);
return null;
}
}
return content.toString();
}
......
......@@ -88,8 +88,6 @@ public class ResourceServlet extends HttpServlet {
response.setHeader("Expires", "1");
compress = false;
}
OutputStream out = null;
InputStream in = null;
try {
byte[] content;
......@@ -109,24 +107,22 @@ public class ResourceServlet extends HttpServlet {
}
// Write the content out
in = new ByteArrayInputStream(content);
out = response.getOutputStream();
// Use a 128K buffer.
byte[] buf = new byte[128*1024];
int len;
while ((len=in.read(buf)) != -1) {
out.write(buf, 0, len);
try (ByteArrayInputStream in = new ByteArrayInputStream(content)) {
try (OutputStream out = response.getOutputStream()) {
// Use a 128K buffer.
byte[] buf = new byte[128 * 1024];
int len;
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
out.flush();
}
}
out.flush();
}
catch (IOException e) {
Log.error(e.getMessage(), e);
}
finally {
try { if (in != null) { in.close(); } } catch (Exception ignored) { /* ignored */ }
try { if (out != null) { out.close(); } } catch (Exception ignored) { /* ignored */ }
}
}
private static byte[] getJavaScriptContent(boolean compress) throws IOException
......@@ -137,24 +133,13 @@ public class ResourceServlet extends HttpServlet {
}
if (compress) {
ByteArrayOutputStream baos = null;
GZIPOutputStream gzos = null;
try {
baos = new ByteArrayOutputStream();
gzos = new GZIPOutputStream(baos);
gzos.write(writer.toString().getBytes());
gzos.finish();
gzos.flush();
gzos.close();
return baos.toByteArray();
}
finally {
try { if (gzos != null) { gzos.close(); } }
catch (Exception ignored) { /* ignored */ }
try { if (baos != null) { baos.close(); } }
catch (Exception ignored) { /* ignored */ }
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
try (GZIPOutputStream gzos = new GZIPOutputStream(baos)) {
gzos.write(writer.toString().getBytes());
gzos.finish();
gzos.flush();
return baos.toByteArray();
}
}
}
else {
......@@ -170,31 +155,21 @@ public class ResourceServlet extends HttpServlet {
private static String getJavaScriptFile(String path) {
StringBuilder sb = new StringBuilder();
InputStream in = null;
InputStreamReader isr = null;
BufferedReader br = null;
try {
in = getResourceAsStream(path);
try (InputStream in = getResourceAsStream(path)) {
if (in == null) {
Log.error("Unable to find javascript file: '" + path + "' in classpath");
return "";
}
isr = new InputStreamReader(in, "ISO-8859-1");
br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
sb.append(line.trim()).append('\n');
try (BufferedReader br = new BufferedReader(new InputStreamReader(in, "ISO-8859-1"))) {
String line;
while ((line = br.readLine()) != null) {
sb.append(line.trim()).append('\n');
}
}
}
catch (Exception e) {
} catch (Exception e) {
Log.error("Error loading JavaScript file: '" + path + "'.", e);
}
finally {
try { if (br != null) { br.close(); } } catch (Exception ignored) { /* ignored */ }
try { if (isr != null) { isr.close(); } } catch (Exception ignored) { /* ignored */ }
try { if (in != null) { in.close(); } } catch (Exception ignored) { /* ignored */ }
}
return sb.toString();
}
......
......@@ -427,9 +427,8 @@ public class Launcher {
@Override
public Object construct() {
if (openfired != null) {
try {
// Get the input stream and read from it
InputStream in = openfired.getInputStream();
// Get the input stream and read from it
try (InputStream in = openfired.getInputStream()) {
int c;
while ((c = in.read()) != -1) {
try {
......@@ -441,7 +440,6 @@ public class Launcher {
// Ignore.
}
}
in.close();
}
catch (IOException e) {
e.printStackTrace();
......@@ -457,9 +455,8 @@ public class Launcher {
@Override
public Object construct() {
if (openfired != null) {
try {
// Get the input stream and read from it
InputStream in = openfired.getErrorStream();
// Get the input stream and read from it
try (InputStream in = openfired.getErrorStream()) {
int c;
while ((c = in.read()) != -1) {
try {
......@@ -470,7 +467,6 @@ public class Launcher {
// Ignore.
}
}
in.close();
}
catch (IOException e) {
e.printStackTrace();
......@@ -505,11 +501,11 @@ public class Launcher {
try {
// attempt to perform a graceful shutdown by sending
// an "exit" command to the process (via stdin)
Writer out = new OutputStreamWriter(
new BufferedOutputStream(openfired.getOutputStream()));
out.write("exit\n");
out.close();
final Thread waiting = Thread.currentThread();
try (Writer out = new OutputStreamWriter(
new BufferedOutputStream(openfired.getOutputStream()))) {
out.write("exit\n");
}
final Thread waiting = Thread.currentThread();
Thread waiter = new Thread() {
public void run() {
try {
......@@ -620,30 +616,10 @@ public class Launcher {
}
private static void copy(URL src, File dst) throws IOException {
InputStream in = null;
OutputStream out = null;
try {
in = src.openStream();
out = new FileOutputStream(dst);
dst.mkdirs();
copy(in, out);
}
finally {
try {
if (in != null) {
in.close();
}
}
catch (IOException e) {
// Ignore.
}
try {
if (out != null) {
out.close();
}
}
catch (IOException e) {
// Ignore.
try (InputStream in = src.openStream()) {
try (OutputStream out = new FileOutputStream(dst)) {
dst.mkdirs();
copy(in, out);
}
}
}
......
......@@ -280,11 +280,11 @@ public class UpdateManager extends BasicModule {
int statusCode = httpClient.executeMethod(getMethod);
if (statusCode == 200) {
//get the resonse as an InputStream
InputStream in = getMethod.getResponseBodyAsStream();
String pluginFilename = url.substring(url.lastIndexOf("/") + 1);
installed = XMPPServer.getInstance().getPluginManager()
.installPlugin(in, pluginFilename);
in.close();
try (InputStream in = getMethod.getResponseBodyAsStream()) {
String pluginFilename = url.substring(url.lastIndexOf("/") + 1);
installed = XMPPServer.getInstance().getPluginManager()
.installPlugin(in, pluginFilename);
}
if (installed) {
// Remove the plugin from the list of plugins to update
for (Update update : pluginUpdates) {
......@@ -657,7 +657,6 @@ public class UpdateManager extends BasicModule {
component.addAttribute("url", serverUpdate.getURL());
}
// Write data out to conf/server-update.xml file.
Writer writer = null;
try {
// Create the conf folder if required
File file = new File(JiveGlobals.getHomeDirectory(), "conf");
......@@ -671,24 +670,15 @@ public class UpdateManager extends BasicModule {
file.delete();
}
// Create new version.xml with returned data
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
OutputFormat prettyPrinter = OutputFormat.createPrettyPrint();
XMLWriter xmlWriter = new XMLWriter(writer, prettyPrinter);
xmlWriter.write(xmlResponse);
try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"))) {
OutputFormat prettyPrinter = OutputFormat.createPrettyPrint();
XMLWriter xmlWriter = new XMLWriter(writer, prettyPrinter);
xmlWriter.write(xmlResponse);
}
}
catch (Exception e) {
Log.error(e.getMessage(), e);
}
finally {
if (writer != null) {
try {
writer.close();
}
catch (IOException e1) {
Log.error(e1.getMessage(), e1);
}
}
}
}
/**
......@@ -772,27 +762,15 @@ public class UpdateManager extends BasicModule {
Log.warn("Cannot retrieve server updates. File must be readable: " + file.getName());
return;
}
FileReader reader = null;
try {
reader = new FileReader(file);
try (FileReader reader = new FileReader(file)){
SAXReader xmlReader = new SAXReader();
xmlReader.setEncoding("UTF-8");
xmlResponse = xmlReader.read(reader);
}
catch (Exception e) {
} catch (Exception e) {
Log.error("Error reading server-update.xml", e);
return;
}
finally {
if (reader != null) {
try {
reader.close();
}
catch (Exception e) {
// Do nothing
}
}
}
// Parse info and recreate update information (if still required)
Element openfire = xmlResponse.getRootElement().element("openfire");
if (openfire != null) {
......@@ -819,27 +797,15 @@ public class UpdateManager extends BasicModule {
Log.warn("Cannot retrieve available plugins. File must be readable: " + file.getName());
return;
}
FileReader reader = null;
try {
reader = new FileReader(file);
try (FileReader reader = new FileReader(file)) {
SAXReader xmlReader = new SAXReader();
xmlReader.setEncoding("UTF-8");
xmlResponse = xmlReader.read(reader);
}
catch (Exception e) {
} catch (Exception e) {
Log.error("Error reading available-plugins.xml", e);
return;
}
finally {
if (reader != null) {
try {
reader.close();
}
catch (Exception e) {
// Do nothing
}
}
}
// Parse info and recreate available plugins
Iterator it = xmlResponse.getRootElement().elementIterator("plugin");
while (it.hasNext()) {
......
......@@ -124,11 +124,9 @@ public class FaviconServlet extends HttpServlet {
response.setContentType(CONTENT_TYPE);
// Send image
try {
ServletOutputStream sos = response.getOutputStream();
try (ServletOutputStream sos = response.getOutputStream()) {
sos.write(bytes);
sos.flush();
sos.close();
}
catch (IOException e) {
// Do nothing
......@@ -195,20 +193,20 @@ public class FaviconServlet extends HttpServlet {
urlConnection.setReadTimeout(1000);
urlConnection.connect();
DataInputStream di = new DataInputStream(urlConnection.getInputStream());
try (DataInputStream di = new DataInputStream(urlConnection.getInputStream())) {
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(byteStream);
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(byteStream);
int len;
byte[] b = new byte[1024];
while ((len = di.read(b)) != -1) {
out.write(b, 0, len);
}
di.close();
out.flush();
int len;
byte[] b = new byte[1024];
while ((len = di.read(b)) != -1) {
out.write(b, 0, len);
}
out.flush();
return byteStream.toByteArray();
return byteStream.toByteArray();
}
}
catch (IOException ioe) {
// We failed again so return null
......
......@@ -249,30 +249,11 @@ public class WebManager extends WebBean {
* Copies the contents at <CODE>src</CODE> to <CODE>dst</CODE>.
*/
public static void copy(URL src, File dst) throws IOException {
InputStream in = null;
OutputStream out = null;
try {
in = src.openStream();
out = new FileOutputStream(dst);
dst.mkdirs();
copy(in, out);
}
finally {
try {
if (in != null) {
in.close();
}
}
catch (IOException e) {
// Ignore.
}
try {
if (out != null) {
out.close();
}
}
catch (IOException e) {
// Ignore.
try (InputStream in = src.openStream()) {
try (OutputStream out = new FileOutputStream(dst)) {
dst.mkdirs();
copy(in, out);
}
}
}
......
......@@ -742,54 +742,24 @@ public class XMLProperties {
Log.error("Unable to save XML properties; no file specified");
return;
}
boolean error = false;
// Write data out to a temporary file first.
File tempFile = null;
Writer writer = null;
try {
tempFile = new File(file.getParentFile(), file.getName() + ".tmp");
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(tempFile), "UTF-8"));
File tempFile = new File(file.getParentFile(), file.getName() + ".tmp");
try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(tempFile), "UTF-8"))) {
OutputFormat prettyPrinter = OutputFormat.createPrettyPrint();
XMLWriter xmlWriter = new XMLWriter(writer, prettyPrinter);
xmlWriter.write(document);
}
catch (Exception e) {
Log.error(e.getMessage(), e);
// There were errors so abort replacing the old property file.
error = true;
}
finally {
if (writer != null) {
try {
writer.close();
}
catch (IOException e1) {
Log.error(e1.getMessage(), e1);
error = true;
}
}
}
// No errors occurred, so delete the main file.
if (!error) {
// Delete the old file so we can replace it.
if (!file.delete()) {
Log.error("Error deleting property file: " + file.getAbsolutePath());
return;
}
// Copy new contents to the file.
try {
copy(tempFile, file);
}
catch (Exception e) {
Log.error(e.getMessage(), e);
// There were errors so abort replacing the old property file.
error = true;
}
copy(tempFile, file);
// If no errors, delete the temp file.
if (!error) {
tempFile.delete();
}
tempFile.delete();
} catch (Exception e) {
Log.error(e.getMessage(), e);
}
}
......@@ -826,25 +796,9 @@ public class XMLProperties {
* @throws IOException If there was a problem making the copy
*/
private static void copy(File inFile, File outFile) throws IOException {
FileInputStream fin = null;
FileOutputStream fout = null;
try {
fin = new FileInputStream(inFile);
fout = new FileOutputStream(outFile);
copy(fin, fout);
}
finally {
try {
if (fin != null) fin.close();
}
catch (IOException e) {
// do nothing
}
try {
if (fout != null) fout.close();
}
catch (IOException e) {
// do nothing
try (FileInputStream fin = new FileInputStream(inFile)) {
try (FileOutputStream fout = new FileOutputStream(outFile)) {
copy(fin, fout);
}
}
}
......
......@@ -53,9 +53,8 @@ public class SANCertificateIdentityMapping implements CertificateIdentityMapping
Integer type = (Integer) item.get(0);
if (type == 0) {
// Type OtherName found so return the associated value
try {
try (ASN1InputStream decoder = new ASN1InputStream((byte[]) item.get(1))) {
// Value is encoded using ASN.1 so decode it to get the server's identity
ASN1InputStream decoder = new ASN1InputStream((byte[]) item.get(1));
Object object = decoder.readObject();
ASN1Sequence otherNameSeq = null;
if (object != null && object instanceof ASN1Sequence) {
......@@ -90,7 +89,6 @@ public class SANCertificateIdentityMapping implements CertificateIdentityMapping
// Add the decoded server name to the list of identities
identities.add(identity);
}
decoder.close();
} catch (IllegalArgumentException ex) {
// OF-517: othername formats are extensible. If we don't recognize the format, skip it.
Log.debug("Cannot parse altName, likely because of unknown record format.", ex);
......
......@@ -43,18 +43,18 @@ public class TestUtils {
* Returns the contents of the given file as a String.
*/
public static String getAsString(File file) throws Exception {
BufferedReader in = new BufferedReader(new FileReader(file));
StringBuffer xml = new StringBuffer();
String lineSeparator = System.getProperty("line.separator");
if (lineSeparator == null) {
lineSeparator = "\n";
try (BufferedReader in = new BufferedReader(new FileReader(file))) {
StringBuffer xml = new StringBuffer();
String lineSeparator = System.getProperty("line.separator");
if (lineSeparator == null) {
lineSeparator = "\n";
}
String line = null;
while ((line = in.readLine()) != null) {
xml.append(line).append(lineSeparator);
}
return xml.toString();
}
String line = null;
while ((line=in.readLine()) != null) {
xml.append(line).append(lineSeparator);
}
in.close();
return xml.toString();
}
public static String prepareFilename(String filename) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment